summaryrefslogtreecommitdiff
path: root/src/test/check_pred.c
blob: 2767c4556d1201f8ca1458f02246b8b08da1f7a7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#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)
{
  /* 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;
}