summaryrefslogtreecommitdiff
path: root/src/test
diff options
context:
space:
mode:
authorJSDurand <mmemmew@gmail.com>2022-01-28 11:16:16 +0800
committerJSDurand <mmemmew@gmail.com>2022-01-28 11:16:16 +0800
commite8e1c91b40c9c82a0fd8373746a7b8cfb130f467 (patch)
treed815ae94866fccc3dea037cea36bd020770a49a1 /src/test
parentbad2b1934da66021cbc7f0d715686706bd444449 (diff)
BSR
A prototype of BSR is roughly finished.
Diffstat (limited to 'src/test')
-rw-r--r--src/test/check_bsr.c151
-rw-r--r--src/test/check_grammar.c1
-rw-r--r--src/test/check_ht.c187
3 files changed, 336 insertions, 3 deletions
diff --git a/src/test/check_bsr.c b/src/test/check_bsr.c
new file mode 100644
index 0000000..4a6ba4e
--- /dev/null
+++ b/src/test/check_bsr.c
@@ -0,0 +1,151 @@
+#include <string.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include "util.h"
+#include "str.h"
+#include "utf8.h"
+#include "grammar.h"
+#include "list.h"
+#include "../bsr.h"
+
+#define READ_INTO_CPA(N, U, I, VA, VI, CP) do { \
+ U = new_utf8(N, 1); \
+ I = get_info((str *)U, 0); \
+ VA = MYALLOC(NUM, 1); \
+ VI = 0; \
+ for (NUM index = 0; \
+ I.value >= 0 && index < str_length((str *) U); \
+ index += I.step, VI++) { \
+ I = get_info((str *)U, index); \
+ *(VA+VI) = I.value; \
+ } \
+ CP = MYALLOC(cpa, 1); \
+ CP->array = VA; \
+ CP->size = VI; \
+ add_to_list(names, CP); \
+ destroy_str((str *)U, 1); \
+ } while (0)
+
+#define READ_TNT_STRING(LEFT, FORMAT, LEN, ...) do { \
+ tnt_string = new_tnt_string(FORMAT, LEN, __VA_ARGS__); \
+ if (!tnt_string) { \
+ fleprintf("left = %d, f = %s, l = %d, " \
+ "cannot create tnt string\n", \
+ LEFT, FORMAT, LEN); \
+ map_list(rules, destroy_rule_and_free_all); \
+ destroy_list(rules, 0); \
+ map_list(names, destroy_cpa_and_free_all); \
+ destroy_list(names, 0); \
+ return 1; \
+ } \
+ rule = new_rule(LEFT, tnt_string); \
+ add_to_list(rules, rule); \
+ } while (0)
+
+
+int
+main(int UNUSED argc, char ** UNUSED argv)
+{
+ /* have a grammar for testing */
+ List *tnt_string = NULL;
+ Rule *rule = NULL;
+ List *rules = new_list();
+ List *names = new_list();
+
+ char *name = MYALLOC(char, 2);
+ *(name+1) = 0;
+ *name = 'E';
+
+ utf8* uname = NULL;
+
+ str_info info = EMPTY_STR_INFO;
+
+ NUM *varray = NULL;
+ NUM vindex = 0;
+ cpa *cpap = NULL;
+
+ READ_INTO_CPA(name, uname, info, varray, vindex, cpap);
+
+ name = MYALLOC(char, 2);
+ *(name+1) = 0;
+ *name = 'T';
+
+ READ_INTO_CPA(name, uname, info, varray, vindex, cpap);
+
+ name = MYALLOC(char, 2);
+ *(name+1) = 0;
+ *name = 'F';
+
+ READ_INTO_CPA(name, uname, info, varray, vindex, cpap);
+
+ name = MYALLOC(char, 2);
+ *(name+1) = 0;
+ *name = 'D';
+
+ READ_INTO_CPA(name, uname, info, varray, vindex, cpap);
+
+ READ_TNT_STRING(0, "n", 1, (NT) 1);
+ READ_TNT_STRING(0, "ntn", 3, (NT) 1, (T) 43, (NT) 1);
+
+ READ_TNT_STRING(1, "n", 1, (NT) 2);
+ READ_TNT_STRING(1, "ntn", 3, (NT) 2, (T) 42, (NT) 2);
+
+ READ_TNT_STRING(2, "n", 1, (NT) 3);
+ READ_TNT_STRING(2, "tnt", 3, (T) 40, (NT) 0, (T) 41);
+
+ READ_TNT_STRING(3, "tn", 2, (T) 48, (NT) 3);
+ READ_TNT_STRING(3, "tn", 2, (T) 49, (NT) 3);
+ READ_TNT_STRING(3, "tn", 2, (T) 50, (NT) 3);
+ READ_TNT_STRING(3, "tn", 2, (T) 51, (NT) 3);
+ READ_TNT_STRING(3, "tn", 2, (T) 52, (NT) 3);
+ READ_TNT_STRING(3, "tn", 2, (T) 53, (NT) 3);
+ READ_TNT_STRING(3, "tn", 2, (T) 54, (NT) 3);
+ READ_TNT_STRING(3, "tn", 2, (T) 55, (NT) 3);
+ READ_TNT_STRING(3, "tn", 2, (T) 56, (NT) 3);
+ READ_TNT_STRING(3, "tn", 2, (T) 57, (NT) 3);
+ rule = new_rule(3, new_list());
+ add_to_list(rules, rule);
+
+ Grammar *g = new_grammar();
+
+ build_grammar(g, rules, names);
+
+ print_grammar(g);
+
+
+ BOOL error = 0;
+ bsr *bsrp = new_bsr(&error, g);
+
+ if (error) goto cleanup;
+
+ bsr_add(g, bsrp, 0, 0, 1, 0, 1, 2);
+ bsr_add(g, bsrp, 0, 1, 1, 0, 1, 2);
+ bsr_add(g, bsrp, 0, 1, 2, 0, 1, 2);
+
+ if (bsr_find(bsrp, g, &error,
+ 0, 0, 1, 0, 1, 2)) {
+ printf("Successfully found 0, 0, 1, 0, 1, 2\n");
+ } else {
+ fleprintf0("Fail to find 0, 0, 1, 0, 1, 2\n");
+ }
+
+ if (bsr_find(bsrp, g, &error,
+ 0, 0, 1, 0, 1, 3)) {
+ fleprintf0("Accidentally found 0, 0, 1, 0, 1, 3\n");
+ } else {
+ printf("Indeed cannot find 0, 0, 1, 0, 1, 3\n");
+ }
+
+ bsr_print(bsrp, g, 1);
+
+ cleanup:
+
+ destroy_grammar(g, 1);
+
+ destroy_list(rules, 1);
+
+ destroy_bsr(bsrp);
+
+ return 0;
+
+}
diff --git a/src/test/check_grammar.c b/src/test/check_grammar.c
index 2b6c8f2..7e55681 100644
--- a/src/test/check_grammar.c
+++ b/src/test/check_grammar.c
@@ -147,7 +147,6 @@ main(UNUSED int argc, UNUSED char **argv)
printf("\n\n");
- /* TODO: Check first and follow sets */
NUM left_len = grammar_left_len(g);
BOOL *epsilon_array = NULL;
ht *terminal_hts = NULL, *predicate_hts = NULL;
diff --git a/src/test/check_ht.c b/src/test/check_ht.c
index 0298087..b572549 100644
--- a/src/test/check_ht.c
+++ b/src/test/check_ht.c
@@ -23,9 +23,9 @@ int main(int UNUSED argc, char ** UNUSED argv)
fleprintf("We found no value for key %ld\n", key);
NUM size = ht_size(htp);
-
+
fleprintf("The size of the hash table is %ld\n", size);
-
+
for (NUM i = 0; i < size; i++)
fleprintf("The %ld-th element has key %ld and value %ld\n",
i,
@@ -50,5 +50,188 @@ int main(int UNUSED argc, char ** UNUSED argv)
}
destroy_ht(htp, DESTROY_VALUE_SELF);
+
+ /* Testing hash tables of pairs of integers. */
+
+ htp = new_ht2(10);
+
+ pair2 *key2 = NULL;
+
+ for (int i = 0; i < 10; i++) {
+ if (i>=2 && i % 2) {
+ NEW_P2(key2, i<<1, i,
+ destroy_ht(htp, DESTROY_KEY_SELF);
+ return 1;);
+ } else {
+ NEW_P2(key2, i, i<<1,
+ destroy_ht(htp, DESTROY_KEY_SELF);
+ return 1;);
+ }
+ if (ht_insert(htp, key2, (void*)1)) {
+ fleprintf0("Fail to insert\n");
+ destroy_ht(htp, DESTROY_KEY_SELF);
+ return 1;
+ }
+ }
+
+ printf("Success!\n");
+
+ pair2 testk2 = { .x = 0, .y = 0 };
+
+ if (ht_find(htp, &testk2) == NULL) {
+ fleprintf0("Fail to find zero zero pair\n");
+ destroy_ht(htp, DESTROY_KEY_SELF);
+ return 1;
+ } else {
+ printf("We find value %p for zero zero pair\n",
+ ht_find(htp, &testk2));
+ }
+
+ testk2.x = 1;
+ testk2.y = 1;
+
+ if (ht_find(htp, &testk2)) {
+ fleprintf0("Accidentally find one one pair\n");
+ destroy_ht(htp, DESTROY_KEY_SELF);
+ return 1;
+ } else {
+ printf("We find value %p for one one pair\n",
+ ht_find(htp, &testk2));
+ }
+
+ testk2.x = 1;
+ testk2.y = 2;
+
+ if (ht_find(htp, &testk2) == NULL) {
+ fleprintf0("Fail to find one two pair\n");
+ destroy_ht(htp, DESTROY_KEY_SELF);
+ return 1;
+ } else {
+ printf("We find value %p for one two pair\n",
+ ht_find(htp, &testk2));
+ }
+
+ destroy_ht(htp, DESTROY_KEY_SELF);
+
+ /* testing hash tables with pair3 keys */
+
+ htp = new_ht3(10);
+
+ pair3 *key3 = NULL;
+
+ for (int i = 0; i < 10; i++) {
+ NEW_P3(key3, i, i << 1, -i,
+ destroy_ht(htp, DESTROY_KEY_SELF);
+ return 1;);
+
+ if (ht_insert(htp, key3, (void*)1)) {
+ fleprintf0("Fail to insert\n");
+ destroy_ht(htp, DESTROY_KEY_SELF);
+ return 1;
+ }
+ }
+
+ printf("Success!\n");
+
+ pair3 testk3 = { .x = 0, .y = 0, .z = 0 };
+
+ if (ht_find(htp, &testk3) == NULL) {
+ fleprintf0("Fail to find zero pair\n");
+ destroy_ht(htp, DESTROY_KEY_SELF);
+ return 1;
+ } else {
+ printf("We find value %p for zero pair\n",
+ ht_find(htp, &testk3));
+ }
+
+ testk3.x = 1;
+ testk3.y = 2;
+ testk3.z = -1;
+
+ if (ht_find(htp, &testk3) == NULL) {
+ fleprintf0("Fail to find one two minus one pair\n");
+ destroy_ht(htp, DESTROY_KEY_SELF);
+ return 1;
+ } else {
+ printf("We find value %p for one two minus one pair\n",
+ ht_find(htp, &testk3));
+ }
+
+ testk3.x = 1;
+ testk3.y = 2;
+ testk3.z = 1;
+
+ if (ht_find(htp, &testk3)) {
+ fleprintf0("Accidentally find one two one pair\n");
+ destroy_ht(htp, DESTROY_KEY_SELF);
+ return 1;
+ } else {
+ printf("We find value %p for one two one pair\n",
+ ht_find(htp, &testk3));
+ }
+
+ destroy_ht(htp, DESTROY_KEY_SELF);
+
+ /* testing hash tables with pair4 keys */
+
+ htp = new_ht4(10);
+
+ pair4 *key4 = NULL;
+
+ for (int i = 0; i < 10; i++) {
+ NEW_P4(key4, i, i << 1, -i, i+1,
+ destroy_ht(htp, DESTROY_KEY_SELF);
+ return 1;);
+
+ if (ht_insert(htp, key4, (void*)1)) {
+ fleprintf0("Fail to insert\n");
+ destroy_ht(htp, DESTROY_KEY_SELF);
+ return 1;
+ }
+ }
+
+ printf("Success!\n");
+
+ pair4 testk4 = { .x = 0, .y = 0, .z = 0, .w = 1 };
+
+ if (ht_find(htp, &testk4) == NULL) {
+ fleprintf0("Fail to find zero pair\n");
+ destroy_ht(htp, DESTROY_KEY_SELF);
+ return 1;
+ } else {
+ printf("We find value %p for zero pair\n",
+ ht_find(htp, &testk4));
+ }
+
+ testk4.x = 1;
+ testk4.y = 2;
+ testk4.z = -1;
+ testk4.w = 2;
+
+ if (ht_find(htp, &testk4) == NULL) {
+ fleprintf0("Fail to find one two minus one pair\n");
+ destroy_ht(htp, DESTROY_KEY_SELF);
+ return 1;
+ } else {
+ printf("We find value %p for one two minus one two pair\n",
+ ht_find(htp, &testk4));
+ }
+
+ testk4.x = 1;
+ testk4.y = 2;
+ testk4.z = 1;
+ testk4.w = 2;
+
+ if (ht_find(htp, &testk4)) {
+ fleprintf0("Accidentally find one two one two pair\n");
+ destroy_ht(htp, DESTROY_KEY_SELF);
+ return 1;
+ } else {
+ printf("We find value %p for one two one two pair\n",
+ ht_find(htp, &testk4));
+ }
+
+ destroy_ht(htp, DESTROY_KEY_SELF);
+
return 0;
}