summaryrefslogtreecommitdiff
path: root/src/test.c
blob: f9ab43bc27fbe03223d4a6ec423e788a2bf252b4 (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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <limits.h>
#include <string.h>
#include <emacs-module.h>

#include "big_endian.h"
#include "helper.h"

int
main(int argc, char **argv)
{
  unsigned char error_vec_len[8] = { 0 };
  unsigned char error_vec_cap[8] = { 0 };

  struct SignedVec error_vec = { 0 };

  error_vec.len = error_vec_len;
  error_vec.capacity = error_vec_cap;

  char *grammar_string = "document     =  1*( item )\n"
"\n"
"item         =  header [ price ] *1( note )\n"
"\n"
"item         =/ header [ note ] *1( price )\n"
"\n"
"header       =  star \"SP\" title %xA *( \"SP\" / %xA )\n"
"\n"
"title        =  1*\"TEXT\"\n"
"\n"
"star         =  %x2A\n"
"\n"
"note         =  \"note:\" \"SP\" note-content %xA *( \"SP\" / %xA )\n"
"\n"
"note-content =  1*\"TEXT\"\n"
"\n"
"price        =  \"price:\" \"SP\" 1*\"DIGIT\" %xA *( \"SP\" / %xA )\n";

  struct parser *parser = new_parser(grammar_string, &error_vec);
  /* struct parser *parser = new_parser("start  = \"a\"\"b\"\n", &error_vec); */

  uint64_t error_length = from_big_endian(error_vec.len);

  if (error_length) {
    printf("error: ");

    char *error_str = error_vec.data;

    for (int i = 0; i < error_length; i++) {
      printf("%c", *(error_str+i));
    }

    printf("\n");

    clean_signed(&error_vec, 4);

    return 1;
  }

  /* int input_unenc[] = { 3, 0, 2, 1, 5, 0, 6, 1 }; */
  
  /* int input_unenc[] = { 3, 0, 2, 2, 2, 2, 1, 5, 0,
   *                       6, 6, 6, 1, 3, 0, 2, 2, 2,
   *                       2, 1, 5, 0, 6, 6, 6, 1, 3,
   *                       0, 2, 2, 2, 2, 1, 5, 0, 6,
   *                       6, 6, 1, 4, 2, 2, 2, 2, 2,
   *                       2, 2, 2, 2, 2, 2, 2, 2, 2,
   *                       2, 2, 1 }; */
  
  int input_unenc[] = { 3, 0, 2, 1,
                        5, 0, 6, 1,
                        3, 0, 2, 1,
                        5, 0, 6, 1,
                        3, 0, 2, 1,
                        5, 0, 6, 1,
                        4, 0, 2, 1 };
  
  int input_unenc_len = (int) (sizeof (input_unenc) /
                               sizeof(input_unenc[0]));

  printf("input length = %d\n", input_unenc_len);

  unsigned char *input = malloc (sizeof(unsigned char) *
                                 input_unenc_len * 8);

  if (input == NULL) {
    clean_parser(parser);
    clean_signed(&error_vec, 4);
    printf("malloc fails at %s:%d\n", __FILE__, __LINE__);

    return EXIT_FAILURE;
  }
  
  for (int i = 0; i < input_unenc_len; i++)
    to_big_endian(input_unenc[i], input + (8 * i));

  unsigned char input_len[8] = { 0 };
  /* input_len[7] = 8*10; */

  to_big_endian((uint64_t) (8 * input_unenc_len), input_len);

  struct UnsignedVec input_vec =
    (struct UnsignedVec) { input_len, NULL, input };

  int result = parser_recognize
    (parser,
     &input_vec,
     &error_vec,
     (unsigned char) 0);

  error_length = from_big_endian(error_vec.len);

  if (error_length) {
    clean_parser((void *) parser);
    free(input);

    printf("error: ");

    char *error_str = error_vec.data;

    for (uint64_t i = 0; i < error_length; i++) {
      printf("%c", *(error_str + (unsigned long) i));
    }

    printf("\n");

    clean_signed(&error_vec, 4);

    return 1;
  }

  if (result)
    printf("result = true\n");
  else
    printf("result = false\n");

  for (int i = 0; i < 8; i++) {
    error_vec.len[i] = 0;
    error_vec.capacity[i] = 0;
  }

  struct UnsignedVec *forest_ptr = parser_parse
    (parser,
     &input_vec,
     &error_vec,
     (unsigned char) 1);

  error_length = from_big_endian(error_vec.len);

  if (error_length) {
    clean_parser((void *) parser);
    free(input);

    printf("error: ");

    char *error_str = error_vec.data;

    for (uint64_t i = 0; i < error_length; i++) {
      printf("%c", *(error_str + (unsigned long) i));
    }

    printf("\n");

    clean_signed(&error_vec, 4);

    return 1;
  }

  free(input);
  clean_parser((void *) parser);

  if (forest_ptr) {
    uint64_t forest_len = from_big_endian(forest_ptr->len);

    /* forest_len++; */

    printf("a non-empty forest of length %llu\n", forest_len);

    unsigned char *forest = forest_ptr->data;

    uint64_t forest_real_len = from_big_endian(forest);

    if (forest_real_len != forest_len) {
      fprintf(stderr, "wrong length: %llu\n", forest_real_len);

      clean_unsigned(forest_ptr, 15);

      return 1;
    }

    if (forest_len < 27) {
      fprintf(stderr, "length too small: %llu\n", forest_len);

      clean_unsigned(forest_ptr, 15);

      return 1;
    }

    printf("the lengths match\n");

    if (*(forest+8) != 114 || *(forest+9) != 101 || *(forest+10) != 112) {
      fprintf(stderr, "the forest does not begin with the special mark\n");

      fprintf(stderr, "the first bytes are: ");
      fprintf(stderr, "%c", *(forest+8));
      fprintf(stderr, "%c", *(forest+9));
      fprintf(stderr, "%c\n", *(forest+10));

      clean_unsigned(forest_ptr, 15);

      return 1;
    }

    printf("the special mark is correct.\n");

    uint64_t nodes_len = from_big_endian(forest+11);

    printf("forest has %llu nodes\n", nodes_len);

    uint64_t labels_offset = from_big_endian(forest+19);

    printf("the offset of labels is %llu\n", labels_offset);

    if (forest_len < labels_offset ||
        forest_len < (27 + 16 * nodes_len)) {
      fprintf(stderr, "length too small: %llu\n", forest_len);

      clean_unsigned(forest_ptr, 15);

      return 1;
    }

    uint64_t total_degree = 0;

    for (uint64_t i = 0; i < nodes_len; i++) {
      uint64_t offset = 27 + 16 * i;

      uint64_t degree = from_big_endian(forest+offset);
      uint64_t node_offset = from_big_endian(forest+offset+8);

      total_degree += degree;

      printf("the %llu-th node has degree %llu and offset %llu\n",
             i, degree, node_offset);

      printf("label: ");
      print_label(read_label(forest + labels_offset + 34 * i));

      for (uint64_t j = 0; j < degree; j++) {
        uint64_t child = from_big_endian(forest+node_offset+8*j);

        printf("the %llu-th child is %llu\n", j, child);
      }

      printf("\n");
    }

    uint64_t correct = 27 + 50 * nodes_len + 8 * total_degree;

    if (forest_len != correct) {
      fprintf(stderr, "length does not conform to the format: %llu\n", correct);

      clean_unsigned(forest_ptr, 15);

      return 1;
    }

    printf("the forest has the correct length according to the format\n");

    printf("saving to the file \"forest.rep\"\n");

    char *open_mode = "a";

    if (fopen("forest.rep", "r") != NULL) {
      printf("The file \"forest.rep\" already exists.\n"
             "Do you want to overwrite it? [y/n] ");

      fflush(stdout);

      #define UNANSWERED 0
      #define YES 1
      #define NO 2

      int yes = UNANSWERED;

      while (yes == UNANSWERED) {
        char input = getchar();

        switch (input) {
        case 'y':
          yes = YES;
          break;
        case 'n':
          yes = NO;
          break;
        default:
          printf("Please answer 'y' or 'n'.\n");
          yes = UNANSWERED;
          break;
        }
      }

      if (yes == NO) {
        printf("Exiting without saving...\n");

        clean_unsigned(forest_ptr, 15);

        return 0;
      }

      open_mode = "w";

      #undef UNANSWERED
      #undef YES
      #undef NO
    }

    FILE *forest_file = fopen("forest.rep", open_mode);

    if (forest_file == NULL) {
      fprintf(stderr, "Cannot open file \"forest.rep\" to write\n");

      clean_unsigned(forest_ptr, 15);

      return 1;
    }

    fwrite(forest, 1, forest_len, forest_file);

    fclose(forest_file);

    printf("Successfully saved the forest to the file \"forest.rep\"\n");

    clean_unsigned(forest_ptr, 15);
  } else {
    printf("no forest\n");
  }

  return 0;
}