summaryrefslogtreecommitdiff
path: root/testsuite/tests/ephe-c-api/stubs.c
blob: 2ea819f32963e42ffdf375f0090c204a4d22d2f8 (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
#include<stdio.h>
#include "caml/alloc.h"
#include "caml/memory.h"
#include "caml/weak.h"

/* C version of ephetest.ml */

void is_true(const char* test, const char* s, int b) {
  if(b) printf("%s %s: OK\n", test, s);
  else printf("%s %s: FAIL\n", test, s);
}

void is_false(const char* test, const char* s, int b) {
  is_true(test, s, !b);
}

void is_data_value(const char* test, value eph, intnat v) {
  CAMLparam1(eph);
  CAMLlocal1(x);

  if(caml_ephemeron_get_data_copy(eph, &x))
    if(Long_val(Field(x, 0)) == v) printf("%s data set: OK\n", test);
    else printf("%s data set: FAIL(bad value %li)\n", test,
                (long int)Long_val(Field(x, 0)));
  else
    printf("%s data set: FAIL\n", test);

  CAMLreturn0;
}

void is_key_value(const char* test, value eph, intnat v) {
  CAMLparam1(eph);
  CAMLlocal1(x);

  if(caml_ephemeron_get_key_copy(eph, 0, &x))
    if(Long_val(Field(x, 0)) == v) printf("%s key set: OK\n", test);
    else printf("%s key set: FAIL(bad value %li)\n", test,
                (long int)Long_val(Field(x, 0)));
  else
    printf("%s key unset: FAIL\n", test);

  CAMLreturn0;
}

void is_key_unset(const char* test, value eph) {
  is_false(test, "key unset", caml_ephemeron_key_is_set(eph, 0));
}

void is_data_unset(const char* test, value eph) {
  is_false(test, "data unset", caml_ephemeron_data_is_set(eph));
}

extern value caml_gc_minor(value);
extern value caml_gc_full_major(value);

CAMLprim value test1(value ra, value rb) {
  CAMLparam2(ra, rb);
  CAMLlocal1(eph);
  value x;

  const char* test = "test1";
  caml_gc_minor(Val_unit);
  caml_gc_full_major(Val_unit);
  eph = caml_ephemeron_create(1);
  caml_ephemeron_set_key(eph, 0, Field(ra, 0));
  x = caml_alloc_small(1, 0);
  Field(x, 0) = Val_long(42);
  caml_ephemeron_set_data(eph, x);
  is_key_value(test, eph, 1);
  is_data_value(test, eph, 42);
  caml_gc_minor(Val_unit);
  is_key_value(test, eph, 1);
  is_data_value(test, eph, 42);
  caml_gc_full_major(Val_unit);
  is_key_value(test, eph, 1);
  is_data_value(test, eph, 42);
  x = caml_alloc_small(1, 0);
  Field(x, 0) = Val_long(12);
  caml_modify(&Field(ra, 0), x);
  caml_gc_full_major(Val_unit);
  is_key_unset(test, eph);
  is_data_unset(test, eph);

  CAMLreturn(Val_unit);
}

CAMLprim value test2(value ra, value rb) {
  CAMLparam2(ra, rb);
  CAMLlocal1(eph);
  value x;

  const char* test = "test2";
  caml_gc_minor(Val_unit);
  caml_gc_full_major(Val_unit);
  eph = caml_ephemeron_create(1);
  x = caml_alloc_small(1, 0);
  Field(x, 0) = Val_long(125);
  caml_ephemeron_set_key(eph, 0, x);
  x = caml_alloc_small(1, 0);
  Field(x, 0) = Val_long(42);
  caml_ephemeron_set_data(eph, x);
  is_key_value(test, eph, 125);
  is_data_value(test, eph, 42);
  x = caml_alloc_small(1, 0);
  Field(x, 0) = Val_long(13);
  caml_modify(&Field(ra, 0), x);
  caml_gc_minor(Val_unit);
  is_key_unset(test, eph);
  is_data_unset(test, eph);

  CAMLreturn(Val_unit);
}

CAMLprim value test3(value ra, value rb) {
  CAMLparam2(ra, rb);
  CAMLlocal1(eph);
  value x;

  const char* test = "test3";
  caml_gc_minor(Val_unit);
  caml_gc_full_major(Val_unit);
  eph = caml_ephemeron_create(1);
  x = caml_alloc_small(1, 0);
  Field(x, 0) = Val_long(125);
  caml_ephemeron_set_key(eph, 0, x);
  caml_ephemeron_set_data(eph, Field(ra, 0));
  is_key_value(test, eph, 125);
  is_data_value(test, eph, 13);
  x = caml_alloc_small(1, 0);
  Field(x, 0) = Val_long(14);
  caml_modify(&Field(ra, 0), x);
  caml_gc_minor(Val_unit);
  is_key_unset(test, eph);
  is_data_unset(test, eph);

  CAMLreturn(Val_unit);
}

CAMLprim value test4(value ra, value rb) {
  CAMLparam2(ra, rb);
  CAMLlocal2(eph, y);
  value x;

  const char* test = "test4";
  caml_gc_minor(Val_unit);
  caml_gc_full_major(Val_unit);
  eph = caml_ephemeron_create(1);
  y = caml_alloc(1, 0);
  x = caml_alloc_small(1, 0);
  Field(x, 0) = y;
  caml_modify(&Field(y, 0), Val_long(3));
  caml_modify(&Field(rb, 0), x);
  y = Val_unit;
  caml_ephemeron_set_key(eph, 0, Field(Field(rb, 0), 0));
  x = caml_alloc_small(1, 0);
  Field(x, 0) = Val_long(43);
  caml_ephemeron_set_data(eph, x);
  is_key_value(test, eph, 3);
  is_data_value(test, eph, 43);
  caml_gc_minor(Val_unit);
  caml_gc_minor(Val_unit);
  is_key_value(test, eph, 3);
  is_data_value(test, eph, 43);

  CAMLreturn(Val_unit);
}

CAMLprim value test5(value ra, value rb) {
  CAMLparam2(ra, rb);
  CAMLlocal2(eph, y);
  value x;

  const char* test = "test5";
  caml_gc_minor(Val_unit);
  caml_gc_full_major(Val_unit);
  eph = caml_ephemeron_create(1);
  y = caml_alloc(1, 0);
  x = caml_alloc_small(1, 0);
  Field(x, 0) = y;
  caml_modify(&Field(y, 0), Val_long(3));
  caml_modify(&Field(rb, 0), x);
  y = Val_unit;
  caml_ephemeron_set_key(eph, 0, Field(Field(rb, 0), 0));
  x = caml_alloc_small(1, 0);
  Field(x, 0) = Val_long(43);
  caml_ephemeron_set_data(eph, x);
  is_key_value(test, eph, 3);
  is_data_value(test, eph, 43);
  x = caml_alloc_small(1, 0);
  Field(x, 0) = Val_long(4);
  caml_modify(&Field(rb, 0), x);
  caml_gc_minor(Val_unit);
  caml_gc_minor(Val_unit);
  is_key_unset(test, eph);
  is_data_unset(test, eph);

  CAMLreturn(Val_unit);
}

CAMLprim value test6(value ra, value rb) {
  CAMLparam2(ra, rb);
  CAMLlocal2(eph, y);
  value x;

  const char* test = "test6";
  caml_gc_minor(Val_unit);
  caml_gc_full_major(Val_unit);
  eph = caml_ephemeron_create(1);
  y = caml_alloc(1, 0);
  x = caml_alloc_small(1, 0);
  Field(x, 0) = y;
  caml_modify(&Field(y, 0), Val_long(3));
  caml_modify(&Field(rb, 0), x);
  y = Val_unit;
  caml_ephemeron_set_key(eph, 0, Field(Field(rb, 0), 0));
  x = caml_alloc_small(1, 0);
  Field(x, 0) = Field(Field(rb, 0), 0);
  caml_ephemeron_set_data(eph, x);
  caml_gc_minor(Val_unit);
  is_key_value(test, eph, 3);
  x = caml_alloc_small(1, 0);
  Field(x, 0) = Val_long(4);
  caml_modify(&Field(rb, 0), x);
  caml_gc_full_major(Val_unit);
  is_key_unset(test, eph);
  is_data_unset(test, eph);

  CAMLreturn(Val_unit);
}

CAMLprim value test7(value ra, value rb) {
  CAMLparam2(ra, rb);
  CAMLlocal4(eph, weak, y, rc);
  value x;

  const char* test = "test7";
  caml_gc_minor(Val_unit);
  caml_gc_full_major(Val_unit);
  x = caml_alloc_small(1, 0);
  Field(x, 0) = Val_long(42);
  caml_modify(&Field(ra, 0), x);
  weak = caml_weak_array_create(1);
  y = caml_ephemeron_create(1);
  eph = caml_alloc_small(1, 0);
  Field(eph, 0) = y;
  y = Val_unit;
  rc = caml_alloc_small(1, 0);
  Field(rc, 0) = Field(eph, 0);
  caml_weak_array_set(weak, 0, Field(rc, 0));
  caml_ephemeron_set_key(Field(eph, 0), 0, Field(ra, 0));
  caml_ephemeron_set_data(Field(eph, 0), Field(rc, 0));
  caml_gc_minor(Val_unit);
  is_true(test, "before", caml_weak_array_check(weak, 0));
  caml_modify(&Field(eph, 0), caml_ephemeron_create(1));
  caml_modify(&Field(rc, 0), Val_unit);
  caml_gc_full_major(Val_unit);
  caml_gc_full_major(Val_unit);
  caml_gc_full_major(Val_unit);
  is_false(test, "after", caml_weak_array_check(weak, 0));

  CAMLreturn(Val_unit);
}

CAMLprim value test8(value ra, value rb) {
  CAMLparam2(ra, rb);
  CAMLlocal3(x, y, z);

  const char* test = "test8";

  x = caml_ephemeron_create(15);
  z = caml_ephemeron_create(3);
  is_true(test, "eph length=15", caml_ephemeron_num_keys(x) == 15);
  is_true(test, "eph length=3", caml_ephemeron_num_keys(z) == 3);

  is_false(test, "eph get empty nonull", caml_ephemeron_get_key(x, 5, &y));
  is_false(test, "eph get copy empty nonnull", caml_ephemeron_get_key_copy(x, 5, &y));
  caml_ephemeron_set_key(x, 5, ra);
  is_true(test, "eph get nonull", caml_ephemeron_get_key(x, 5, &y));
  is_true(test, "eph get eq", y == ra);
  is_true(test, "eph get copy nonnull", caml_ephemeron_get_key_copy(x, 5, &y));
  is_true(test, "eph get copy eq", y != ra);
  caml_ephemeron_blit_key(x, 4, z, 0, 3);
  caml_ephemeron_unset_key(x, 5);
  is_false(test, "eph get unset nonull", caml_ephemeron_get_key(x, 5, &y));
  is_false(test, "eph get copy unset nonnull", caml_ephemeron_get_key_copy(x, 5, &y));
  is_true(test, "eph get nonull z", caml_ephemeron_get_key(z, 1, &y));
  is_true(test, "eph get eq z", y == ra);
  is_false(test, "eph get empty z", caml_ephemeron_get_key(z, 0, &y));

  is_false(test, "eph get data empty nonull", caml_ephemeron_get_data(x, &y));
  is_false(test, "eph get data copy empty nonnull", caml_ephemeron_get_data_copy(x, &y));
  caml_ephemeron_set_data(x, ra);
  is_true(test, "eph get data nonull", caml_ephemeron_get_data(x, &y));
  is_true(test, "eph get data eq", y == ra);
  is_true(test, "eph get data copy nonnull", caml_ephemeron_get_data_copy(x, &y));
  is_true(test, "eph get data copy eq", y != ra);
  caml_ephemeron_blit_data(x, z);
  caml_ephemeron_unset_data(x);
  is_false(test, "eph get data unset nonull", caml_ephemeron_get_data(x, &y));
  is_false(test, "eph get data copy unset nonnull", caml_ephemeron_get_data_copy(x, &y));
  is_true(test, "eph get nonull z", caml_ephemeron_get_data(z, &y));
  is_true(test, "eph get eq z", y == ra);

  x = caml_weak_array_create(15);
  z = caml_weak_array_create(3);
  is_true(test, "eph length=15", caml_weak_array_length(x) == 15);
  is_true(test, "eph length=3", caml_weak_array_length(z) == 3);

  is_false(test, "eph get empty nonull", caml_weak_array_get(x, 5, &y));
  is_false(test, "eph get copy empty nonnull", caml_weak_array_get_copy(x, 5, &y));
  caml_weak_array_set(x, 5, ra);
  is_true(test, "eph get nonull", caml_weak_array_get(x, 5, &y));
  is_true(test, "eph get eq", y == ra);
  is_true(test, "eph get copy nonnull", caml_weak_array_get_copy(x, 5, &y));
  is_true(test, "eph get copy eq", y != ra);
  caml_weak_array_blit(x, 4, z, 0, 3);
  caml_weak_array_unset(x, 5);
  is_false(test, "eph get unset nonull", caml_weak_array_get(x, 5, &y));
  is_false(test, "eph get copy unset nonnull", caml_weak_array_get_copy(x, 5, &y));
  is_true(test, "eph get nonull z", caml_weak_array_get(z, 1, &y));
  is_true(test, "eph get eq z", y == ra);
  is_false(test, "eph get empty z", caml_weak_array_get(z, 0, &y));

  CAMLreturn(Val_unit);
}