void free_entry__(void* p) { char verify[16]; aim_snprintf(verify, sizeof(verify), "%d", free_count__); if(strcmp(verify, p)) { AIM_LOG_ERROR("free_entry__: expected %s, got %s", verify, p); } AIM_FREE(p); free_count__++; }
/* Just make sure elements get free properly */ void bigring_str_test(int size, bigring_free_entry_f fef) { bigring_t* br = bigring_create(size, fef); int i; char* p; char buf[16]; free_count__ = 0; for(i = 0; i < size*2; i++) { bigring_push(br, aim_fstrdup("%d", i)); } /* * At this point: * free_count should be size (or zero if using default free function) * next entry should be "size" * ringcount should be size */ if(fef == free_entry__) { if(free_count__ != size) { AIM_LOG_ERROR("Free count is %d, should be %d", free_count__, size); } } if(fef == bigring_aim_free_entry) { if(free_count__ != 0) { AIM_LOG_ERROR("free_count is %d, should be zero", free_count__); } } ASSERT_COUNT(bigring_count(br), size, bigring_size(br)); p = bigring_shift(br); ASSERT_COUNT(bigring_count(br), size-1, bigring_size(br)); aim_snprintf(buf, sizeof(buf), "%d", size); if(strcmp(p, buf)) { AIM_LOG_ERROR("Next is '%s', should be '%s'", p, buf); abort(); } AIM_FREE(p); /* * Verify the rest of the entries are free. * Note - we removed the current entry, so we need * to increment free_count__ to account for it or * the verification will be off by one. */ free_count__++; bigring_destroy(br); }
/** * Basic output function for all log messages. */ static void aim_log_output__(aim_log_t* l, const char* fname, const char* file, int line, const char* fmt, va_list vargs) { aim_pvs_t* msg; char* pmsg; msg = aim_pvs_buffer_create(); if(AIM_BIT_GET(l->options, AIM_LOG_OPTION_TIMESTAMP)) { aim_log_time__(msg); } aim_vprintf(msg, fmt, vargs); if(l->options & (1 << AIM_LOG_OPTION_FUNC)) { aim_printf(msg, " [%s]", fname); } if(l->options & (1 << AIM_LOG_OPTION_FILE_LINE)) { aim_printf(msg, " [%s:%d]", file, line); } aim_printf(msg, "\n"); pmsg = aim_pvs_buffer_get(msg); aim_printf(l->pvs, "%s", pmsg); AIM_FREE(pmsg); aim_pvs_destroy(msg); }
int aim_main(int argc, char* argv[]) { int i; { const char* tstStrings[] = { "This", "is", "a", "complete", "sentence." }; char* join = aim_strjoin(" ", tstStrings, AIM_ARRAYSIZE(tstStrings)); if(strcmp(join, "This is a complete sentence.")) { printf("fail: join='%s'\n", join); } AIM_FREE(join); } for(i = 0; i < argc; i++) { aim_printf(&aim_pvs_stdout, "arg%d: '%s'\n", i, argv[i]); } { /* Test data */ char data[2500]; memset(data, 0xFF, sizeof(data)); aim_printf(&aim_pvs_stdout, "data is %{data}", data, sizeof(data)); } { char* sdata = "DEADBEEFCAFE"; char* data; int size; aim_sparse(&sdata, &aim_pvs_stdout, "{data}", &data, &size); aim_printf(&aim_pvs_stdout, "data is %{data}\n", data, size); aim_free(data); } utest_list(); AIM_LOG_MSG("Should print 1-27"); AIM_LOG_MSG("%d %d %d %d %d %d %d %d %d " "%d %d %d %d %d %d %d %d %d " "%d %d %d %d %d %d %d %d %d", 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27); aim_printf(&aim_pvs_stdout, "aim_pvs_stdout from %s:%d\n", __FILE__, __LINE__); { char c; aim_pvs_t* pvs = aim_pvs_buffer_create(); aim_printf(pvs, "\nConsider "); aim_printf(pvs, "%s ", "the"); aim_printf(pvs, "alphabet: "); for(c = 'A'; c <= 'Z'; c++) { aim_printf(pvs, "%c", c); } aim_printf(pvs, "\n"); { char* s = aim_pvs_buffer_get(pvs); aim_printf(&aim_pvs_stdout, "first: %s", s); free(s); aim_printf(pvs, "(second)"); s = aim_pvs_buffer_get(pvs); aim_printf(&aim_pvs_stdout, "second: %s", s); free(s); aim_pvs_destroy(pvs); } { aim_ratelimiter_t rl; aim_ratelimiter_init(&rl, 10, 5, NULL); /* 5 (6?) tokens available at t=0 */ assert(aim_ratelimiter_limit(&rl, 0) == 0); assert(aim_ratelimiter_limit(&rl, 0) == 0); assert(aim_ratelimiter_limit(&rl, 0) == 0); assert(aim_ratelimiter_limit(&rl, 0) == 0); assert(aim_ratelimiter_limit(&rl, 0) == 0); assert(aim_ratelimiter_limit(&rl, 0) == 0); assert(aim_ratelimiter_limit(&rl, 0) < 0); /* Another token at t=10 */ assert(aim_ratelimiter_limit(&rl, 10) == 0); assert(aim_ratelimiter_limit(&rl, 10) < 0); /* Nothing at t=15 */ assert(aim_ratelimiter_limit(&rl, 15) < 0); /* 4 more tokens granted by t=50 */ assert(aim_ratelimiter_limit(&rl, 50) == 0); assert(aim_ratelimiter_limit(&rl, 50) == 0); assert(aim_ratelimiter_limit(&rl, 50) == 0); assert(aim_ratelimiter_limit(&rl, 50) == 0); assert(aim_ratelimiter_limit(&rl, 50) < 0); } { aim_printf(&aim_pvs_stdout, "valgrind_status=%d\n", aim_valgrind_status()); } AIM_LOG_MSG("%{aim_error}", AIM_ERROR_PARAM); } return 0; }
void aim_free(void *data) { AIM_FREE(data); }