static int test_search (u_test_case_t *tc) { enum { NELEMS = 1000000 }; u_bst_t *bst = NULL; u_bst_node_t *node; /* Prepare a BST with 'NELEMS' nodes. */ u_test_err_if ((bst = prepare_bst(tc, NELEMS)) == NULL); /* Push a needle into the haystack. */ u_test_err_if (u_bst_push(bst, "needle", NULL)); /* Search for it. */ u_test_err_if ((node = u_bst_search(bst, "needle")) == NULL); u_test_case_printf(tc, "\'%s\' found !", (const char *) u_bst_node_key(node)); u_bst_free(bst); return U_TEST_SUCCESS; err: if (bst) u_bst_free(bst); return U_TEST_FAILURE; }
static void cmp_last_string (u_bst_node_t *node, void *p) { u_test_case_t *tc = (u_test_case_t *) p; const char *key = (const char *) u_bst_node_key(node); static char last[KEY_SZ] = { '\0' }; /* Compare against last saved key. */ if (strcmp(last, key) > 0) { /* FIXME: on failure should fire an abort() or set some global * to explicitly invalidate the test. */ u_test_case_printf(tc, "SORT FAILED on key %s", key); } /* Save current node's key to 'last'. */ (void) u_strlcpy(last, key, sizeof last); return ; }
static int test_top10 (u_test_case_t *tc) { enum { EMAX = 10 }; size_t i; double key, keymax = DBL_MAX; u_pq_t *pq = NULL; srand(time(NULL)); u_test_err_if (u_pq_create(EMAX, &pq)); /* fill the pqueue */ for (i = 0; i < EMAX; i++) u_test_err_if (u_pq_push(pq, (double) rand(), NULL)); /* del-push cycle */ for (i = EMAX; i < 10000000; i++) { (void) u_pq_peekmax(pq, &keymax); if (keymax > (key = (double) rand())) { (void) u_pq_delmax(pq, NULL); u_test_err_if (u_pq_push(pq, key, NULL)); } } /* print results */ for (i = 0; !u_pq_empty(pq); i++) { (void) u_pq_delmax(pq, &key); u_test_case_printf(tc, "%zu: %.0lf", EMAX - i, key); } u_pq_free(pq); return U_TEST_SUCCESS; err: u_pq_free(pq); return U_TEST_FAILURE; }
static int test_sort (u_test_case_t *tc) { enum { NELEMS = 1000000 }; u_bst_t *bst = NULL; /* Prepare a BST with 'NELEMS' nodes. */ u_test_err_if ((bst = prepare_bst(tc, NELEMS)) == NULL); u_test_case_printf(tc, "BST sorting %u elements", u_bst_count(bst)); /* Check for monotonically increasing key values. */ (void) u_bst_foreach(bst, cmp_last_string, tc); u_bst_free(bst); return U_TEST_SUCCESS; err: if (bst) u_bst_free(bst); return U_TEST_FAILURE; }
static int example_easy_basic (u_test_case_t *tc) { u_hmap_opts_t opts; u_hmap_t *hmap = NULL; u_hmap_opts_init(&opts); dbg_err_if (u_hmap_opts_set_val_type(&opts, U_HMAP_OPTS_DATATYPE_STRING)); dbg_err_if (u_hmap_easy_new(&opts, &hmap)); dbg_err_if (u_hmap_easy_put(hmap, "jack", (const void *) ":S")); dbg_err_if (u_hmap_easy_put(hmap, "jill", (const void *) ":)))")); u_test_case_printf(tc, "jack is %s and jill is %s", (const char *) u_hmap_easy_get(hmap, "jack"), (const char *) u_hmap_easy_get(hmap, "jill")); u_hmap_easy_free(hmap); return U_TEST_SUCCESS; err: return U_TEST_FAILURE; }