summaryrefslogtreecommitdiffstats
path: root/tests/btparser/utils.at
blob: 13644972dc35b77c115ae4660d9105ff73335f2f (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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
# Checking the btparser. -*- Autotest -*-

AT_BANNER([Utils])

## ------------ ##
## btp_strcmp0  ##
## ------------ ##

AT_TESTFUN([btp_strcmp0],
[[
#include <utils.h>
#include <assert.h>
int main(void)
{
  assert(0 == btp_strcmp0(NULL, NULL));
  assert(0 == btp_strcmp0("abab", "abab"));
  assert(0 > btp_strcmp0("aba", "abab"));
  assert(0 > btp_strcmp0(NULL, "abab"));
  assert(0 < btp_strcmp0("abab", NULL));
  assert(0 < btp_strcmp0("abab", "aba"));
  return 0;
}
]])

## ------------------- ##
## btp_strchr_location ##
## ------------------- ##

AT_TESTFUN([btp_strchr_location],
[[
#include <utils.h>
#include <assert.h>
int main(void)
{
  /* The character is on the first line.*/
  int line, column;
  char *result = btp_strchr_location("test string", 'r', &line, &column);
  assert(0 == strcmp(result, "ring"));
  assert(1 == line);
  assert(7 == column);

  /* The character is on the third line. */
  result = btp_strchr_location("\ntest\n string", 'r', &line, &column);
  assert(0 == strcmp(result, "ring"));
  assert(3 == line);
  assert(3 == column);

  /* The character is not found. */
  result = btp_strchr_location("\ntest\n string", 'z', &line, &column);
  assert(!result);

  /* The character _is_ a newline. */
  result = btp_strchr_location("abcd\nefgh", '\n', &line, &column);
  assert(0 == strcmp(result, "\nefgh"));
  assert(1 == line);
  assert(4 == column);
  return 0;
}
]])

## ------------------- ##
## btp_strstr_location ##
## ------------------- ##

AT_TESTFUN([btp_strstr_location],
[[
#include <utils.h>
#include <assert.h>
int main(void)
{
  /* The substring is on the first line.*/
  int line, column;
  char *result = btp_strstr_location("test string", "ri", &line, &column);
  assert(0 == strcmp(result, "ring"));
  assert(1 == line);
  assert(7 == column);

  /* The substring is on the third line. */
  result = btp_strstr_location("\ntest\n string", "ri", &line, &column);
  assert(0 == strcmp(result, "ring"));
  assert(3 == line);
  assert(3 == column);

  /* The substring is not found, but the first char is. */
  result = btp_strstr_location("\ntest\n string", "rz", &line, &column);
  assert(!result);

  /* The substring is not found. */
  result = btp_strstr_location("\ntest\n string", "zr", &line, &column);
  assert(!result);

  /* The substring _is_ a newline. */
  result = btp_strstr_location("abcd\nefgh", "\n", &line, &column);
  assert(0 == strcmp(result, "\nefgh"));
  assert(1 == line);
  assert(4 == column);
  return 0;
}
]])

## ------------------- ##
## btp_strspn_location ##
## ------------------- ##

AT_TESTFUN([btp_strspn_location],
[[
#include <utils.h>
#include <assert.h>
int main(void)
{
  /* No newline in the input.*/
  int line, column;
  size_t count = btp_strspn_location("test string",
                                     "tes ",
				     &line,
				     &column);
  assert(7 == count);
  assert(1 == line);
  assert(7 == column);

  /* With some newlines. */
  count = btp_strspn_location("te\nst \nstring",
                              "tes \n",
			      &line,
			      &column);
  assert(9 == count);
  assert(3 == line);
  assert(2 == column);

  return 0;
}
]])

## ------------- ##
## btp_skip_char ##
## ------------- ##

AT_TESTFUN([btp_skip_char],
[[
#include <utils.h>
#include <assert.h>
int main(void)
{
  char *input = "abc";
  assert(btp_skip_char(&input, 'a'));
  assert(!btp_skip_char(&input, 'c'));
  return 0;
}
]])

## --------------------- ##
## btp_skip_char_limited ##
## --------------------- ##

AT_TESTFUN([btp_skip_char_limited],
[[
#include <utils.h>
#include <assert.h>
int main(void)
{
  char *input = "abc";
  assert(btp_skip_char_limited(&input, "ab"));
  assert(btp_skip_char_limited(&input, "ab"));
  assert(!btp_skip_char_limited(&input, "ab"));
  return 0;
}
]])

## ---------------------- ##
## btp_parse_char_limited ##
## ---------------------- ##

AT_TESTFUN([btp_parse_char_limited],
[[
#include <utils.h>
#include <assert.h>
int main(void)
{
  char *input = "abc";
  char result;

  /* First char in allowed is used. */
  assert(btp_parse_char_limited(&input, "ab", &result));
  assert(*input == 'b' && result == 'a');

  /* No char in allowed is used. */
  assert(!btp_parse_char_limited(&input, "cd", &result));
  assert(*input == 'b' && result == 'a');

  /* Second char in allowed is used. */
  assert(btp_parse_char_limited(&input, "ab", &result));
  assert(*input == 'c' && result == 'b');

  /* Empty set of allowed chars. */
  assert(!btp_parse_char_limited(&input, "", &result));
  assert(*input == 'c' && result == 'b');

  /* Char is multiple times in allowed. */
  assert(btp_parse_char_limited(&input, "cdc", &result));
  assert(*input == '\0' && result == 'c');

  return 0;
}
]])

## ---------------------- ##
## btp_skip_char_sequence ##
## ---------------------- ##

AT_TESTFUN([btp_skip_char_sequence],
[[
#include <utils.h>
#include <assert.h>
int main(void)
{
  char *input = "aaaaabc";
  assert(5 == btp_skip_char_sequence(&input, 'a'));
  assert(1 == btp_skip_char_sequence(&input, 'b'));
  assert(0 == btp_skip_char_sequence(&input, 'b'));
  return 0;
}
]])

## ------------------ ##
## btp_skip_char_span ##
## ------------------ ##

AT_TESTFUN([btp_skip_char_span],
[[
#include <utils.h>
#include <assert.h>
int main(void)
{
  char *input = "aabaabc";
  assert(6 == btp_skip_char_span(&input, "ba"));
  assert(0 == btp_skip_char_span(&input, "b"));
  assert(1 == btp_skip_char_span(&input, "bc"));

  /* Test that it can parse empty string. */
  assert(0 == btp_skip_char_span(&input, "abc"));

  return 0;
}
]])

## --------------------------- ##
## btp_skip_char_span_location ##
## --------------------------- ##

AT_TESTFUN([btp_skip_char_span_location],
[[
#include <utils.h>
#include <assert.h>
int main(void)
{
  char *input = "aab\naabc";
  int line, column;

  assert(7 == btp_skip_char_span_location(&input, "ba\n", &line, &column));
  assert(2 == line);
  assert(3 == column);

  assert(0 == btp_skip_char_span_location(&input, "b", &line, &column));
  assert(1 == line);
  assert(0 == column);

  assert(1 == btp_skip_char_span_location(&input, "bc", &line, &column));
  assert(1 == line);
  assert(1 == column);

  /* Test that it can parse empty string. */
  assert(0 == btp_skip_char_span_location(&input, "abc", &line, &column));
  assert(1 == line);
  assert(0 == column);

  return 0;
}
]])

## ------------------- ##
## btp_parse_char_span ##
## ------------------- ##

AT_TESTFUN([btp_parse_char_span],
[[
#include <utils.h>
#include <assert.h>
int main(void)
{
  char *input = "abcd";
  char *result;
  assert(2 == btp_parse_char_span(&input, "ba", &result));
  assert(*input == 'c' && strcmp(result, "ab") == 0);
  assert(0 == btp_parse_char_span(&input, "ba", &result));
  assert(*input == 'c' && strcmp(result, "ab") == 0);
  free(result);
  assert(2 == btp_parse_char_span(&input, "ecfd", &result));
  assert(*input == '\0' && strcmp(result, "cd") == 0);
  free(result);
  return 0;
}
]])

## -------------------- ##
## btp_parse_char_cspan ##
## -------------------- ##

AT_TESTFUN([btp_parse_char_cspan],
[[
#include <utils.h>
#include <assert.h>
int main(void)
{
  char *input = "abcd";
  char *result;
  assert(btp_parse_char_cspan(&input, "c", &result));
  assert(*input == 'c' && strcmp(result, "ab") == 0);
  assert(!btp_parse_char_cspan(&input, "c", &result));
  assert(*input == 'c' && strcmp(result, "ab") == 0);
  free(result);
  assert(btp_parse_char_cspan(&input, "e", &result));
  assert(*input == '\0' && strcmp(result, "cd") == 0);
  free(result);
  return 0;
}
]])

## --------------- ##
## btp_skip_string ##
## --------------- ##

AT_TESTFUN([btp_skip_string],
[[
#include <utils.h>
#include <assert.h>
int main(void)
{
  char *input = "abcd";
  assert(2 == btp_skip_string(&input, "ab"));
  assert(*input == 'c');
  assert(0 == btp_skip_string(&input, "cde"));
  assert(2 == btp_skip_string(&input, "cd"));
  assert(0 == btp_skip_string(&input, "cd"));
  return 0;
}
]])

## ---------------- ##
## btp_parse_string ##
## ---------------- ##

AT_TESTFUN([btp_parse_string],
[[
#include <utils.h>
#include <assert.h>
int main(void)
{
  char *input = "abcd";
  char *result;
  assert(btp_parse_string(&input, "ab", &result));
  assert(0 == strcmp(result, "ab"));
  assert(*input == 'c');
  free(result);
  return 0;
}
]])

## ------------------------- ##
## btp_skip_unsigned_integer ##
## ------------------------- ##

AT_TESTFUN([btp_skip_unsigned_integer],
[[
#include <utils.h>
#include <assert.h>
int main(void)
{
  char *input = "10";
  assert(2 == btp_skip_unsigned_integer(&input));
  assert(*input == '\0');
  return 0;
}
]])

## ------------------------- ##
## btp_parse_unsigned_integer ##
## ------------------------- ##

AT_TESTFUN([btp_parse_unsigned_integer],
[[
#include <utils.h>
#include <assert.h>
int main(void)
{
  char *input = "10";
  unsigned result;
  assert(2 == btp_parse_unsigned_integer(&input, &result));
  assert('\0' == *input);
  assert(10 == result);
  return 0;
}
]])

## --------------------------- ##
## btp_skip_hexadecimal_number ##
## --------------------------- ##

AT_TESTFUN([btp_skip_hexadecimal_number],
[[
#include <utils.h>
#include <assert.h>
int main(void)
{
  char *input = "0xffddffddff";
  assert(12 == btp_skip_hexadecimal_number(&input));
  assert(*input == '\0');
  return 0;
}
]])

## ---------------------------- ##
## btp_parse_hexadecimal_number ##
## ---------------------------- ##

AT_TESTFUN([btp_parse_hexadecimal_number],
[[
#include <utils.h>
#include <assert.h>
int main(void)
{
  char *input = "0x0fafaffff 0x2badf00dbaadf00d";
  uint64_t num;
  assert(11 == btp_parse_hexadecimal_number(&input, &num));
  assert(*input == ' ');
  assert(num == 0xfafaffff);
  *input++;
  assert(18 == btp_parse_hexadecimal_number(&input, &num));
  assert(*input == '\0');
  assert(num == 0x2badf00dbaadf00d);
  return 0;
}
]])