summaryrefslogtreecommitdiff
path: root/src/test/check_tuple.c
blob: 74a60b22855aed52fdb825021eaa34b5c6920d68 (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
#include <stdio.h>
#include "../util.h"
#include "../tuple.h"

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

void print_label5(pair5 label)
{
  printf("(%ld, %ld, %ld, %ld, %ld)\n",
         label.x, label.y, label.z, label.u, label.v);
}

void print_label6(pair6 label)
{
  printf("(%ld, %ld, %ld, %ld, %ld, %ld)\n",
         label.x, label.y, label.z, label.u, label.v, label.w);
}

int
main(int UNUSED argc, char ** UNUSED argv)
{
  NUM *lengths = NULL;

  SAFE_MALLOC(NUM, lengths, 3, return 1;);

  *lengths = 2;
  *(lengths+1) = 2;
  *(lengths+2) = 6;

  luple3 *l3 = new_luple3(lengths);

  printf("Successfully created 3 tuple!\n");

  if (add_to_luple3(l3, (pair3) { 0 }, 1)) {
    fleprintf0("Fail to add to L3\n");
    destroy_luple3(l3);
    goto cleanup;
  }

  if (add_to_luple3(l3, (pair3) { 0, 0, 1 }, 1)) {
    fleprintf0("Fail to add to L3\n");
    destroy_luple3(l3);
    goto cleanup;
  }

  if (add_to_luple3(l3, (pair3) { 0, 0, 5 }, 1)) {
    fleprintf0("Fail to add to L3\n");
    destroy_luple3(l3);
    goto cleanup;
  }

  printf("Successfully added to L3\n");

  NUM *result = luple3_find(l3, (pair3) { 0 });

  if (result) {
    printf("Successfully found value %ld for (0, 0, 0)\n",
           *result);
  } else {
    fleprintf0("fail to find value for (0, 0, 0)\n");
    destroy_luple3(l3);
    goto cleanup;
  }

  /* the first is one and the rest are all zero */
  result = luple3_find(l3, (pair3) { 1 });

  if (result) {
    fleprintf("Accidentally found value %ld for (1, 0, 0)\n",
               *result);
    destroy_luple3(l3);
    goto cleanup;
  } else {
    printf("Fail to find value for (1, 0, 0), as expected\n");
  }

  printf("Printing values starting from (0, 0)...\n");
  luple3_map_2(l3, (pair2) { 0 }, print_label3);

  destroy_luple3(l3);

  SAFE_MALLOC(NUM, lengths, 5, goto cleanup;);

  for (int i = 0; i < 4;) *(lengths+i++) = 2;
  *(lengths+4) = 10;

  luple5 *l5 = new_luple5(lengths);

  printf("Successfully created 5 tuple!\n");

  pair5 p5 = (pair5) { 1 };

  if (add_to_luple5(l5, p5, 1)) {
    fleprintf0("Fail to add to L5\n");
    destroy_luple5(l5);
    goto cleanup;
  }

  p5.v = 2;

  if (add_to_luple5(l5, p5, 1)) {
    fleprintf0("Fail to add to L5\n");
    destroy_luple5(l5);
    goto cleanup;
  }

  p5.v = 9;

  if (add_to_luple5(l5, p5, 1)) {
    fleprintf0("Fail to add to L5\n");
    destroy_luple5(l5);
    goto cleanup;
  }

  printf("Successfully added to L5\n");

  printf("Printing values starting from (1, 0, 0)...\n");
  luple5_map_3(l5, (pair3) { 1 }, print_label5);

  destroy_luple5(l5);

  SAFE_MALLOC(NUM, lengths, 6, return 1;);

  for (int i = 0; i < 5;) *(lengths+i++) = 2;
  *(lengths+5) = 10;

  luple6 *l6 = new_luple6(lengths);

  printf("Successfully created 6 tuple!\n");

  pair6 p6 = (pair6) { 1, 0, 1 };

  if (add_to_luple6(l6, p6, 1)) {
    fleprintf0("Fail to add to L6\n");
    destroy_luple6(l6);
    goto cleanup;
  }

  p6.w = 2;

  if (add_to_luple6(l6, p6, 1)) {
    fleprintf0("Fail to add to L6\n");
    destroy_luple6(l6);
    goto cleanup;
  }

  p6.w = 9;

  if (add_to_luple6(l6, p6, 1)) {
    fleprintf0("Fail to add to L6\n");
    destroy_luple6(l6);
    goto cleanup;
  }

  p6.w = 7;

  if (add_to_luple6(l6, p6, 1)) {
    fleprintf0("Fail to add to L6\n");
    destroy_luple6(l6);
    goto cleanup;
  }

  printf("Successfully added to L6\n");

  if (add_to_luple6_pt_2(l6, (pair2) { 0 })) {
    fleprintf0("Fail to add partially to L6\n");
    destroy_luple6(l6);
    goto cleanup;
  }

  printf("Successfully added partially to L6\n");

  result = luple6_find(l6, p6);

  if (result) {
    printf("Successfully found value %ld for "
           "(1, 0, 1, 0, 0, 0)\n",
           *result);
  } else {
    fleprintf0("fail to find value for "
               "(1, 0, 1, 0, 0, 0)\n");
    destroy_luple6(l6);
    goto cleanup;
  }

  result = luple6_find(l6, (pair6) { 1, 4 });

  if (result) {
    fleprintf("Accidentally found value %ld for "
              "(1, 4, 0, 0, 0, 0)\n",
               *result);
    destroy_luple6(l6);
    goto cleanup;
  } else {
    printf("Fail to find value for "
           "(1, 4, 0, 0, 0, 0), as expected\n");
  }

  if (luple6_pf_2(l6, (pair2) { 1 })) {
    printf("Successfully detected existence of (1, 0)\n");
  } else {
    fleprintf0("fail to detect existence of (1, 0)\n");
    destroy_luple6(l6);
    goto cleanup;
  }

  if (luple6_pf_2(l6, (pair2) { 1, 1 })) {
    fleprintf0("Accidentally detected existence of (1, 1)\n");
    destroy_luple6(l6);
    goto cleanup;
  } else {
    printf("Fail to detect existence of (1, 0), as expected\n");
  }

  if (luple6_pf_2(l6, (pair2) { 0 })) {
    printf("Successfully detected partial existence of (0, 0)\n");
  } else {
    fleprintf0("fail to detect partial existence of (0, 0)\n");
    destroy_luple6(l6);
    goto cleanup;
  }

  printf("Printing values starting from (1, 0) under 7...\n");
  luple6_map_2(l6, (pair2) { 1 }, 7, print_label6);

  printf("Printing values starting from (1, 0) under 2...\n");
  luple6_map_2(l6, (pair2) { 1 }, 2, print_label6);

  printf("Printing values starting from (1, 0) under 9...\n");
  luple6_map_2(l6, (pair2) { 1 }, 9, print_label6);

  printf("Printing values starting from (1, 0, 1, 0, 0)...\n");
  luple6_map_5(l6, (pair5) { 1, 0, 1 }, print_label6);

  destroy_luple6(l6);

  return 0;

 cleanup:
  return 1;
}