END_TEST START_TEST(test_table_encode) { TreeNode *t = huffman_build_tree("books/holmes.txt");//holmes ck_assert_int_eq(tree_size(t), 161); EncodeTable *etab = table_build(t); ck_assert_msg(etab != NULL, "Encode table should not be NULL."); char* e_encode = table_bit_encode(etab, 'e'); ck_assert_msg(e_encode != NULL, "Problem finding the encoding for 'e'."); ck_assert_int_eq(e_encode[0], 0); ck_assert_int_eq(e_encode[1], 0); ck_assert_int_eq(e_encode[2], 0); ck_assert_int_eq(e_encode[3], 0); ck_assert_int_eq(e_encode[4], -1); free(e_encode); char* space_encode = table_bit_encode(etab, ' '); ck_assert_msg(space_encode != NULL, "Problem finding the encoding for ' '."); ck_assert_int_eq(space_encode[0], 1); ck_assert_int_eq(space_encode[1], 0); ck_assert_int_eq(space_encode[2], -1); free(space_encode); free(t); free(etab); }
int main (int argc, char *argv[]) { if (argc != 2) { usage(); exit(1); } char *infile = argv[1]; TreeNode *tree = huffman_build_tree(infile); if (tree == NULL) { printf("Could not build the tree!"); usage(); exit(1); } EncodeTable *etab = table_build(tree); if (etab == NULL) { printf("Could not build the table!"); usage(); exit(1); } table_print(etab); table_free(etab); tree_free(tree); return 0; }
END_TEST START_TEST(test_table_free) { TreeNode *t = huffman_build_tree("books/holmes.txt");//holmes ck_assert_int_eq(tree_size(t), 161); EncodeTable *etab = table_build(t); ck_assert_msg(etab != NULL, "Encode table should not be NULL."); free(t); free(etab); }
END_TEST START_TEST(test_huffman_find) { TreeNode *t = huffman_build_tree("books/holmes.txt");//holmes ck_assert_int_eq(tree_size(t), 161); EncodeTable *etab = table_build(t); ck_assert_msg(etab != NULL, "Encode table should not be NULL."); char* e_encode = table_bit_encode(etab, 'e'); ck_assert_msg(e_encode != NULL, "Problem finding the encoding for 'e'."); char c = huffman_find(t, e_encode); ck_assert_int_eq(c, 'e'); char* b_encode = table_bit_encode(etab, 'b'); c = huffman_find(t, b_encode); ck_assert_int_eq(c, 'b'); }
void main(int argc, char **argv) { if ( argc != 3 ) { printf("Usage: %s (filename) (number of entries)\n", argv[0]); exit(1); } FILE *fp; char buf[12]; table t; mpz_t u; ulint i, count, invalid, found; unsigned long long phonenum; unsigned long long *numlist; struct timeval starttime, endtime; ulint starttime_us, endtime_us; if ( (fp = fopen(argv[1], "r")) == NULL ) { printf("Could not open file: %s\n", argv[1]); } sscanf(argv[2], "%ld", &count); numlist = (unsigned long long *)calloc( count, sizeof(unsigned long long)); mpz_init(u); mpz_set_ui(u, 1000 * 1000); mpz_mul_ui(u, u, 10000); table_init(&t, count, u); srand(time(NULL)); i = 0; invalid = 0; while ( fgets(buf, 12, fp) != NULL ) { sscanf(buf, "%lld\n", &phonenum); numlist[i] = phonenum; if (rand() <= PROB_INVALID * RAND_MAX) { numlist[i] = numlist[i] / 4; invalid++; } table_insert(&t, phonenum); i++; } fclose(fp); printf("Building table...\n"); table_build(&t); printf("Done inserting %ld numbers (%ld invalids)\n", i, invalid); i = 0; found = 0; invalid = 0; gettimeofday(&starttime, NULL); while ( i < count ) { if ( table_find(&t, numlist[i]) ) { found++; } else { invalid++; } i++; } gettimeofday(&endtime, NULL); starttime_us = starttime.tv_sec*1000000 + starttime.tv_usec; endtime_us = endtime.tv_sec*1000000 + endtime.tv_usec; printf("Found %ld numbers, rejected %ld\n", found, invalid); printf("Total find time: %ld us\n", endtime_us-starttime_us); free(numlist); table_destroy(&t); mpz_clear(u); }