test/bst.c

00001 #include <u/libu.h>
00002 
00003 #define KEY_SZ  128
00004 
00005 int test_suite_bst_register (u_test_t *t);
00006 
00007 static u_bst_t *prepare_bst (u_test_case_t *tc, size_t nelems);
00008 static void cmp_last_string (u_bst_node_t *node, void *dummy);
00009 
00010 static int test_sort (u_test_case_t *tc);
00011 static int test_search (u_test_case_t *tc);
00012 
00013 static int test_sort (u_test_case_t *tc)
00014 {
00015     enum { NELEMS = 1000000 };
00016     u_bst_t *bst = NULL;
00017 
00018     /* Prepare a BST with 'NELEMS' nodes. */
00019     u_test_err_if ((bst = prepare_bst(tc, NELEMS)) == NULL);
00020 
00021     u_test_case_printf(tc, "BST sorting %u elements", u_bst_count(bst));
00022 
00023     /* Check for monotonically increasing key values. */
00024     (void) u_bst_foreach(bst, cmp_last_string, tc);
00025 
00026     u_bst_free(bst);
00027 
00028     return U_TEST_SUCCESS;
00029 err:
00030     if (bst)
00031         u_bst_free(bst);
00032 
00033     return U_TEST_FAILURE;
00034 }
00035 
00036 static int test_search (u_test_case_t *tc)
00037 {
00038     enum { NELEMS = 1000000 };
00039     u_bst_t *bst = NULL;
00040     u_bst_node_t *node;
00041 
00042     /* Prepare a BST with 'NELEMS' nodes. */
00043     u_test_err_if ((bst = prepare_bst(tc, NELEMS)) == NULL);
00044 
00045     /* Push a needle into the haystack. */
00046     u_test_err_if (u_bst_push(bst, "needle", NULL));
00047 
00048     /* Search for it. */
00049     u_test_err_if ((node = u_bst_search(bst, "needle")) == NULL);
00050 
00051     u_test_case_printf(tc, "\'%s\' found !", 
00052             (const char *) u_bst_node_key(node));
00053 
00054     u_bst_free(bst);
00055 
00056     return U_TEST_SUCCESS;
00057 err:
00058     if (bst)
00059         u_bst_free(bst);
00060 
00061     return U_TEST_FAILURE;
00062 }
00063 
00064 static u_bst_t *prepare_bst (u_test_case_t *tc, size_t nelems)
00065 {
00066     size_t i;
00067     char key[KEY_SZ];
00068     u_bst_t *bst = NULL;
00069 
00070     /* Seed the PRNG. */
00071     srand((unsigned) getpid());
00072 
00073     u_test_err_if (u_bst_new(U_BST_OPT_NONE, &bst));
00074     
00075     /* Push 'nelem' random nodes with string keys. */
00076     for (i = 0; i < nelems; i++)
00077     {
00078         (void) u_snprintf(key, sizeof key, "%12.12d", rand());
00079         u_test_err_if (u_bst_push(bst, key, NULL));
00080     }
00081 
00082     return bst;
00083 err:
00084     if (bst)
00085         u_bst_free(bst);
00086     return NULL;
00087 }
00088 
00089 static void cmp_last_string (u_bst_node_t *node, void *p)
00090 {
00091     u_test_case_t *tc = (u_test_case_t *) p;
00092     const char *key = (const char *) u_bst_node_key(node);
00093     static char last[KEY_SZ] = { '\0' };
00094 
00095     /* Compare against last saved key. */
00096     if (strcmp(last, key) > 0)
00097     {
00098         /* FIXME: on failure should fire an abort() or set some global 
00099          *        to explicitly invalidate the test. */
00100         u_test_case_printf(tc, "SORT FAILED on key %s", key);
00101     }
00102 
00103     /* Save current node's key to 'last'. */
00104     (void) u_strlcpy(last, key, sizeof last);
00105 
00106     return ;
00107 }
00108 
00109 int test_suite_bst_register (u_test_t *t)
00110 {
00111     u_test_suite_t *ts = NULL;
00112 
00113     con_err_if (u_test_suite_new("Binary Search Tree", &ts));
00114 
00115     con_err_if (u_test_case_register("Sort", test_sort, ts));
00116     con_err_if (u_test_case_register("Search", test_search, ts));
00117 
00118     return u_test_suite_add(ts, t);
00119 err:
00120     u_test_suite_free(ts);
00121     return ~0;
00122 }

←Products
© 2005-2012 - KoanLogic S.r.l. - All rights reserved