summaryrefslogtreecommitdiff
path: root/src/test/check_grammar.c
blob: 98a07cde5f57c60740b1c80b1d8bf4fc322268c9 (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
#include <stdio.h>
#include <stdlib.h>
#include "../grammar.h"

int
main(U_ATTR int argc, U_ATTR char **argv)
{
  /* check new_tnt and print it */
  TNT *tnt = new_tnt(1, 12);

  printf("Print a TNT value of type NT: ");
  print_tnt(tnt);
  printf("\n");

  free(tnt);

  /* check new_tnt_string */

  List *tnt_string = new_tnt_string("tntnt", 5,
                                    (T) 1, (NT) 2, (T) 3, (NT) 4, (T) 15);

  if (!tnt_string) {
    eprintf("error!\n");
    return 1;
  }

  /* check new_rule, print_rule, and destroy_rule. */

  Rule *rule = new_rule(1, tnt_string);

  print_rule(rule);

  destroy_rule(rule, 1);

  /* check build_grammar, print_grammar, and destroy_grammar */

  List *rules = new_list();
  List *names = new_list();

  for (int i = 0; i < 4; i++) {
    tnt_string = new_tnt_string("ntn", 3,
                                (NT) 3*(i+1),
                                (T) 3*(i+1)*(i+1),
                                (NT) 3*(i+1)*(i+1)-2);

    if (!tnt_string) {
      eprintf("i = %d, cannot create tnt string\n", i);

      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(i%3, tnt_string);
    add_to_list(rules, rule);

    char *name = MYALLOC(char, 7);
    snprintf(name, 7, "Rule %d", i);

    utf8* uname = new_utf8(name, 6);

    str_info info = get_info((str *)uname, 0);

    NUM *varray = MYALLOC(NUM, 6);
    NUM vindex = 0;

    for (NUM index = 0;
         info.value >= 0 && index < str_length((str *) uname);
         index += info.step, vindex++) {
      info = get_info((str *)uname, index);

      *(varray+vindex) = info.value;
    }

    destroy_str((str *) uname, 1);

    cpa *cpap = MYALLOC(cpa, 1);
    cpap->array = varray;
    cpap->size = vindex;

    add_to_list(names, cpap);
  }

  Grammar *g = new_grammar();

  build_grammar(g, rules, names);

  print_grammar(g);

  destroy_grammar(g, 1);

  destroy_list(rules, 1);

  return 0;
}