From 510b10b96b546fcc6c6b6be85050305ddd192a41 Mon Sep 17 00:00:00 2001 From: JSDurand Date: Sat, 5 Feb 2022 17:30:11 +0800 Subject: replace some hash table usage by tuples Previously I used hash tables, which consume too much memory. Now the critical parts are replaced by a new hand-written library called "tuple.h". Now we can easily parse larger inputs. I haven't tested its limits, though. --- src/test/check_tuple.c | 244 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 244 insertions(+) create mode 100644 src/test/check_tuple.c (limited to 'src/test/check_tuple.c') diff --git a/src/test/check_tuple.c b/src/test/check_tuple.c new file mode 100644 index 0000000..74a60b2 --- /dev/null +++ b/src/test/check_tuple.c @@ -0,0 +1,244 @@ +#include +#include "../util.h" +#include "../tuple.h" + +void print_label3(pair3 label) +{ + printf("(%ld, %ld, %ld)\n", label.x, label.y, label.z); +} + +void print_label5(pair5 label) +{ + printf("(%ld, %ld, %ld, %ld, %ld)\n", + label.x, label.y, label.z, label.u, label.v); +} + +void print_label6(pair6 label) +{ + printf("(%ld, %ld, %ld, %ld, %ld, %ld)\n", + label.x, label.y, label.z, label.u, label.v, label.w); +} + +int +main(int UNUSED argc, char ** UNUSED argv) +{ + NUM *lengths = NULL; + + SAFE_MALLOC(NUM, lengths, 3, return 1;); + + *lengths = 2; + *(lengths+1) = 2; + *(lengths+2) = 6; + + luple3 *l3 = new_luple3(lengths); + + printf("Successfully created 3 tuple!\n"); + + if (add_to_luple3(l3, (pair3) { 0 }, 1)) { + fleprintf0("Fail to add to L3\n"); + destroy_luple3(l3); + goto cleanup; + } + + if (add_to_luple3(l3, (pair3) { 0, 0, 1 }, 1)) { + fleprintf0("Fail to add to L3\n"); + destroy_luple3(l3); + goto cleanup; + } + + if (add_to_luple3(l3, (pair3) { 0, 0, 5 }, 1)) { + fleprintf0("Fail to add to L3\n"); + destroy_luple3(l3); + goto cleanup; + } + + printf("Successfully added to L3\n"); + + NUM *result = luple3_find(l3, (pair3) { 0 }); + + if (result) { + printf("Successfully found value %ld for (0, 0, 0)\n", + *result); + } else { + fleprintf0("fail to find value for (0, 0, 0)\n"); + destroy_luple3(l3); + goto cleanup; + } + + /* the first is one and the rest are all zero */ + result = luple3_find(l3, (pair3) { 1 }); + + if (result) { + fleprintf("Accidentally found value %ld for (1, 0, 0)\n", + *result); + destroy_luple3(l3); + goto cleanup; + } else { + printf("Fail to find value for (1, 0, 0), as expected\n"); + } + + printf("Printing values starting from (0, 0)...\n"); + luple3_map_2(l3, (pair2) { 0 }, print_label3); + + destroy_luple3(l3); + + SAFE_MALLOC(NUM, lengths, 5, goto cleanup;); + + for (int i = 0; i < 4;) *(lengths+i++) = 2; + *(lengths+4) = 10; + + luple5 *l5 = new_luple5(lengths); + + printf("Successfully created 5 tuple!\n"); + + pair5 p5 = (pair5) { 1 }; + + if (add_to_luple5(l5, p5, 1)) { + fleprintf0("Fail to add to L5\n"); + destroy_luple5(l5); + goto cleanup; + } + + p5.v = 2; + + if (add_to_luple5(l5, p5, 1)) { + fleprintf0("Fail to add to L5\n"); + destroy_luple5(l5); + goto cleanup; + } + + p5.v = 9; + + if (add_to_luple5(l5, p5, 1)) { + fleprintf0("Fail to add to L5\n"); + destroy_luple5(l5); + goto cleanup; + } + + printf("Successfully added to L5\n"); + + printf("Printing values starting from (1, 0, 0)...\n"); + luple5_map_3(l5, (pair3) { 1 }, print_label5); + + destroy_luple5(l5); + + SAFE_MALLOC(NUM, lengths, 6, return 1;); + + for (int i = 0; i < 5;) *(lengths+i++) = 2; + *(lengths+5) = 10; + + luple6 *l6 = new_luple6(lengths); + + printf("Successfully created 6 tuple!\n"); + + pair6 p6 = (pair6) { 1, 0, 1 }; + + if (add_to_luple6(l6, p6, 1)) { + fleprintf0("Fail to add to L6\n"); + destroy_luple6(l6); + goto cleanup; + } + + p6.w = 2; + + if (add_to_luple6(l6, p6, 1)) { + fleprintf0("Fail to add to L6\n"); + destroy_luple6(l6); + goto cleanup; + } + + p6.w = 9; + + if (add_to_luple6(l6, p6, 1)) { + fleprintf0("Fail to add to L6\n"); + destroy_luple6(l6); + goto cleanup; + } + + p6.w = 7; + + if (add_to_luple6(l6, p6, 1)) { + fleprintf0("Fail to add to L6\n"); + destroy_luple6(l6); + goto cleanup; + } + + printf("Successfully added to L6\n"); + + if (add_to_luple6_pt_2(l6, (pair2) { 0 })) { + fleprintf0("Fail to add partially to L6\n"); + destroy_luple6(l6); + goto cleanup; + } + + printf("Successfully added partially to L6\n"); + + result = luple6_find(l6, p6); + + if (result) { + printf("Successfully found value %ld for " + "(1, 0, 1, 0, 0, 0)\n", + *result); + } else { + fleprintf0("fail to find value for " + "(1, 0, 1, 0, 0, 0)\n"); + destroy_luple6(l6); + goto cleanup; + } + + result = luple6_find(l6, (pair6) { 1, 4 }); + + if (result) { + fleprintf("Accidentally found value %ld for " + "(1, 4, 0, 0, 0, 0)\n", + *result); + destroy_luple6(l6); + goto cleanup; + } else { + printf("Fail to find value for " + "(1, 4, 0, 0, 0, 0), as expected\n"); + } + + if (luple6_pf_2(l6, (pair2) { 1 })) { + printf("Successfully detected existence of (1, 0)\n"); + } else { + fleprintf0("fail to detect existence of (1, 0)\n"); + destroy_luple6(l6); + goto cleanup; + } + + if (luple6_pf_2(l6, (pair2) { 1, 1 })) { + fleprintf0("Accidentally detected existence of (1, 1)\n"); + destroy_luple6(l6); + goto cleanup; + } else { + printf("Fail to detect existence of (1, 0), as expected\n"); + } + + if (luple6_pf_2(l6, (pair2) { 0 })) { + printf("Successfully detected partial existence of (0, 0)\n"); + } else { + fleprintf0("fail to detect partial existence of (0, 0)\n"); + destroy_luple6(l6); + goto cleanup; + } + + printf("Printing values starting from (1, 0) under 7...\n"); + luple6_map_2(l6, (pair2) { 1 }, 7, print_label6); + + printf("Printing values starting from (1, 0) under 2...\n"); + luple6_map_2(l6, (pair2) { 1 }, 2, print_label6); + + printf("Printing values starting from (1, 0) under 9...\n"); + luple6_map_2(l6, (pair2) { 1 }, 9, print_label6); + + printf("Printing values starting from (1, 0, 1, 0, 0)...\n"); + luple6_map_5(l6, (pair5) { 1, 0, 1 }, print_label6); + + destroy_luple6(l6); + + return 0; + + cleanup: + return 1; +} -- cgit v1.2.3-18-g5258