summaryrefslogtreecommitdiff
path: root/src/test/check_crf.c
blob: 72931f80512910d0dc87852fbca397ad040e8e27 (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
#include <stdio.h>
#include "../util.h"
#include "../crf.h"

#define READ_INTO_CPA(N, U, I, VA, VI, CP) do {         \
    U = new_utf8(N, 1);                                 \
    I = get_info((str *)U, 0);                          \
    VA = MYALLOC(NUM, 1);                               \
    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)

static void
print_label3(pair3 label)
{
  printf("(%ld, %ld, %ld)\n", label.x, label.y, label.z);
}

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

  char *name = MYALLOC(char, 2);
  *(name+1) = 0;
  *name = 'E';

  utf8* uname = NULL;

  str_info info = EMPTY_STR_INFO;

  NUM *varray = NULL;
  NUM vindex = 0;
  cpa *cpap = NULL;

  READ_INTO_CPA(name, uname, info, varray, vindex, cpap);

  name = MYALLOC(char, 2);
  *(name+1) = 0;
  *name = 'T';

  READ_INTO_CPA(name, uname, info, varray, vindex, cpap);

  name = MYALLOC(char, 2);
  *(name+1) = 0;
  *name = 'F';

  READ_INTO_CPA(name, uname, info, varray, vindex, cpap);

  name = MYALLOC(char, 2);
  *(name+1) = 0;
  *name = 'D';

  READ_INTO_CPA(name, uname, info, varray, vindex, cpap);

  READ_TNT_STRING(0, "n", 1, (NT) 1);
  READ_TNT_STRING(0, "ntn", 3, (NT) 1, (T) 43, (NT) 1);

  READ_TNT_STRING(1, "n", 1, (NT) 2);
  READ_TNT_STRING(1, "ntn", 3, (NT) 2, (T) 42, (NT) 2);

  READ_TNT_STRING(2, "n", 1, (NT) 3);
  READ_TNT_STRING(2, "tnt", 3, (T) 40, (NT) 0, (T) 41);

  READ_TNT_STRING(3, "tn", 2, (T) 48, (NT) 3);
  READ_TNT_STRING(3, "tn", 2, (T) 49, (NT) 3);
  READ_TNT_STRING(3, "tn", 2, (T) 50, (NT) 3);
  READ_TNT_STRING(3, "tn", 2, (T) 51, (NT) 3);
  READ_TNT_STRING(3, "tn", 2, (T) 52, (NT) 3);
  READ_TNT_STRING(3, "tn", 2, (T) 53, (NT) 3);
  READ_TNT_STRING(3, "tn", 2, (T) 54, (NT) 3);
  READ_TNT_STRING(3, "tn", 2, (T) 55, (NT) 3);
  READ_TNT_STRING(3, "tn", 2, (T) 56, (NT) 3);
  READ_TNT_STRING(3, "tn", 2, (T) 57, (NT) 3);
  rule = new_rule(3, new_list());
  add_to_list(rules, rule);

  Grammar *g = new_grammar();

  build_grammar(g, rules, names, NULL);

  print_grammar(g);

  procr *pr = NULL;
  procu *pu = NULL;

  spa *spap = NULL;

  prodecor *prod = NULL;

  NUM input_len = 10;

  /* check crf */

  crf *crfp = new_crf(g, input_len);
  if (crfp == NULL) {
    fleprintf0("Fail to create new CRF\n");
    goto cleanup;
  }

  pair2 p = { 0 };
  p.x = 0;
  p.y = 1;

  if (!(crf_find_node(crfp, p))) {
    if (crf_add_node(crfp, p)) {
      fleprintf0("fail to add node\n");
      goto cleanup;
    } else {
      printf("Successfully add node (0, 1)\n");
    }
  }

  if (!(crf_find_node(crfp, p))) {
    fleprintf0("Fail to find node\n");
    goto cleanup;
  } else {
    printf("Successfully find node for (0, 1)\n");
  }

  pair4 p4 = { 0 };

  if (crf_add_edge(crfp, p, p4)) {
    fleprintf0("Fail to add edge\n");
    goto cleanup;
  } else {
    printf("Successfully add edge from (0, 1) to (0, 0, 0, 0)\n");
  }

  p4.u = 1;

  if (crf_add_edge(crfp, p, p4)) {
    fleprintf0("Fail to add edge\n");
    goto cleanup;
  } else {
    printf("Successfully add edge from (0, 1) to (0, 0, 0, 1)\n");
  }

  p4.u = 9;

  if (crf_add_edge(crfp, p, p4)) {
    fleprintf0("Fail to add edge\n");
    goto cleanup;
  } else {
    printf("Successfully add edge from (0, 1) to (0, 0, 0, 9)\n");
  }

  if (crf_find_edge(crfp, p, p4)) {
    printf("Successfully found edge from (0, 1) to (0, 0, 0, 9)\n");
  } else {
    fleprintf0("Fail to find edge from (0, 1) to (0, 0, 0, 9)\n");
    goto cleanup;
  }

  crf_print(crfp);

  /* check spa */

  spap = new_spa(g, input_len);

  if (spap == NULL) {
    fleprintf0("fail to create new spa\n");
    goto cleanup;
  }

  pair3 p3 = (pair3) { 0, 1, 9 };

  if (spa_insert(spap, p3)) {
    fleprintf0("Fail to insert\n");
    goto cleanup;
  }

  printf("Successfully inserted into SPA\n");

  if (spa_belong(spap, p3)) {
    printf("Successfully found p3\n");
  } else {
    fleprintf0("Fail to find p3\n");
    goto cleanup;
  }

  pair2 first_two = (pair2) { 0, 1 };

  spa_map(spap, first_two, print_label3);

  /* check prodecor */

  BOOL error = 0;

  pr = new_procr(g, input_len, &error);
  pu = new_procu(g, input_len, &error);

  printf("Successfully created procr and procu\n");

  p4 = (pair4) { 0 };

  p4.x = 1;

  if (desc_add(pr, pu, 2, p4)) {
    fleprintf0("Fail to add descriptor\n");
    goto cleanup;
  }

  printf("Successfully added to procr and procu\n");

  print_procr(pr);

  NUM grade = 0;

  prod = pop_procr(pr, pu, &grade);

  if (prod == NULL) {
    printf("The first pop failed.  "
           "But maybe it occurs due to some misses in the minimal "
           "weight.\n");
    printf("So we shall pop again\n");
    prod = pop_procr(pr, pu, &grade);

    if (prod == NULL) {
      fleprintf0("Fail to pop\n");
      goto cleanup;
    }
  }

  printf("popped a process descriptor: (%ld, %ld, %ld, %ld, %ld)\n",
         prod->x, prod->y, prod->z, prod->u, grade);

 cleanup:

  if (crfp) destroy_crf(crfp);
  if (spap) destroy_spa(spap);

  destroy_grammar(g, 1);
  destroy_list(rules, 1);

  if (pr) destroy_procr(pr);
  if (pu) destroy_procu(pu);
  if (prod) free(prod);

  return 0;
}