summaryrefslogtreecommitdiff
path: root/src/test
diff options
context:
space:
mode:
Diffstat (limited to 'src/test')
-rw-r--r--src/test/check_cnp.c9
-rw-r--r--src/test/check_pred.c165
2 files changed, 169 insertions, 5 deletions
diff --git a/src/test/check_cnp.c b/src/test/check_cnp.c
index a21d74e..f01a68c 100644
--- a/src/test/check_cnp.c
+++ b/src/test/check_cnp.c
@@ -9,8 +9,9 @@ print_label6(pair6 label)
label.x, label.y, label.z, label.u, label.v, label.w);
}
-#define TIC struct timespec tic, toc; \
- do { clock_gettime(CLOCK_MONOTONIC_RAW, &tic); } while (0)
+#define TITO struct timespec tic, toc
+
+#define TIC do { clock_gettime(CLOCK_MONOTONIC_RAW, &tic); } while (0)
#define TOC do { \
clock_gettime(CLOCK_MONOTONIC_RAW, &toc); \
@@ -53,7 +54,7 @@ print_label6(pair6 label)
add_to_list(rules, rule); \
} while (0)
-#define GRAMMAR 2
+#define GRAMMAR 4
int
main(int UNUSED argc, char ** UNUSED argv)
@@ -197,6 +198,8 @@ main(int UNUSED argc, char ** UNUSED argv)
printf("\nPrinting the input...\n%s\n", get_data((str *) string));
+ TITO;
+
TIC;
Environment *env = cnp_parse(g, (str *) string);
diff --git a/src/test/check_pred.c b/src/test/check_pred.c
index b0b95b3..2767c45 100644
--- a/src/test/check_pred.c
+++ b/src/test/check_pred.c
@@ -1,7 +1,168 @@
-#include "../grammar.h"
+#include "time.h"
+#include "../cnp.h"
+
+#define TITO struct timespec tic, toc
+
+#define TIC do { clock_gettime(CLOCK_MONOTONIC_RAW, &tic); } while (0)
+
+#define TOC do { \
+ clock_gettime(CLOCK_MONOTONIC_RAW, &toc); \
+ printf("\nTotal time = %f seconds\n", \
+ (toc.tv_nsec - tic.tv_nsec) / 1000000000.0 + \
+ toc.tv_sec - tic.tv_sec); \
+ } while (0)
+
+#define READ_INTO_CPA(N, U, L, I, VA, VI, CP) do { \
+ U = new_utf8(N, L); \
+ I = get_info((str *)U, 0); \
+ VA = MYALLOC(NUM, 2); \
+ 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)
{
- return 77;
+ /* have a grammar for testing */
+ List *tnt_string = NULL;
+ Rule *rule = NULL;
+ List *rules = new_list();
+ List *names = new_list();
+ List *preds = new_list();
+
+ char *user_name = NULL;
+ char *raw_name = NULL;
+
+ str *user_name_s = NULL, *raw_name_s = NULL;
+ NUM *pred_bytes = NULL, pred_bytes_len = 0;
+
+ char *name = MYALLOC(char, 2);
+ utf8* uname = NULL;
+
+ str_info info = EMPTY_STR_INFO;
+
+ NUM *varray = NULL;
+ NUM vindex = 0;
+ cpa *cpap = NULL;
+
+ *(name+1) = 0;
+ *name = 'S';
+
+ READ_INTO_CPA(name, uname, 1, info, varray, vindex, cpap);
+
+ READ_TNT_STRING(0, "pn", 2, (PT) 0, (NT) 0);
+ rule = new_rule(0, new_list());
+ add_to_list(rules, rule);
+
+ SAFE_MALLOC(char, user_name, 6, return 1;);
+ *(user_name) = 'a';
+ *(user_name+1) = 's';
+ *(user_name+2) = 'c';
+ *(user_name+3) = 'i';
+ *(user_name+4) = 'i';
+ *(user_name+5) = 0;
+
+ user_name_s = new_utf8(user_name, 5);
+
+ SAFE_MALLOC(char, raw_name, 7, return 1;);
+
+ *(raw_name+0) = 'a';
+ *(raw_name+1) = '-';
+ *(raw_name+2) = 'z';
+ *(raw_name+3) = 'A';
+ *(raw_name+4) = '-';
+ *(raw_name+5) = 'Z';
+ *(raw_name+6) = 0;
+
+ raw_name_s = new_utf8(raw_name, 6);
+
+ SAFE_MALLOC(NUM, pred_bytes, 52, return 1;);
+
+ for (int i = 0; i < 26; i++)
+ *(pred_bytes+pred_bytes_len++) = 'a' + i;
+
+ for (int i = 0; i < 26; i++)
+ *(pred_bytes+pred_bytes_len++) = 'A' + i;
+
+ if (add_to_list(preds, new_ptd(user_name_s, raw_name_s,
+ dfa_from_bytes
+ (pred_bytes_len, pred_bytes)))) {
+ fleprintf0("Fail to add a predicate\n");
+ return 1;
+ }
+
+ free(pred_bytes);
+
+ Grammar *g = new_grammar();
+
+ build_grammar(g, rules, names, preds);
+
+ print_grammar(g);
+
+ utf8 *string = new_utf8("awdfsdjbfsjdhxy", 15);
+
+ printf("\nPrinting the input...\n%s\n", get_data((str *) string));
+
+ TITO;
+
+ TIC;
+
+ Environment *env = cnp_parse(g, (str *) string);
+
+ TOC;
+
+ if (env) {
+ if (!(env_error_p(env))) {
+ BOOL result = bsr_lookup
+ (env_bsrp(env), 0, 0, str_length((str *) string));
+
+ if (result) {
+ printf("\nSuccessfully parsed the input!\n");
+ } else {
+ printf("\nThe input does not parse!\n");
+ }
+
+ printf("\nAll BSRs follow:\n\n");
+ if (argc == 1)
+ bsr_print(env_bsrp(env), env_grammar(env), 1);
+ } else {
+ printf("There are errors!\n");
+ }
+
+ destroy_env(env);
+ }
+
+ destroy_grammar(g, 1);
+
+ destroy_list(rules, 1);
+
+ free(string);
+
+ return 0;
}