summaryrefslogtreecommitdiff
path: root/src/test.c
blob: d309fb7602b48045b1fb0fd69e4e11bcc9d2be01 (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
#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;

  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;
  }

  unsigned char *input = malloc (sizeof(unsigned char) * 16);

  for (int i = 0; i < 16; i++) *(input+i) = 0;

  *(input+15) = 1;

  unsigned char input_len[8] = { 0 };
  input_len[7] = 16;

  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");

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

  return 0;
}