-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsv_parse_uint8.c
86 lines (72 loc) · 2.2 KB
/
sv_parse_uint8.c
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
#define SV_IMPLEMENTATION
#include "sv.h"
#include <stdio.h>
int test_sv_parse_uint8(cstring_view input, int will_fail, uint8_t expected);
void print_check();
void print_x();
#define TEST_PARSE_UINT8(num) \
do { \
if (!test_sv_parse_uint8(svl(#num), 0, num)) return 1; \
} while (0)
#define TEST_PARSE_UINT8_FAIL(num) \
do { \
if (!test_sv_parse_uint8(svl(num), 1, 0)) return 1; \
} while (0)
int main() {
TEST_PARSE_UINT8(123);
TEST_PARSE_UINT8(255);
TEST_PARSE_UINT8(12);
TEST_PARSE_UINT8(25);
TEST_PARSE_UINT8(42);
TEST_PARSE_UINT8(145);
TEST_PARSE_UINT8(44);
TEST_PARSE_UINT8(41);
TEST_PARSE_UINT8(0);
TEST_PARSE_UINT8_FAIL("256"); // overflow
TEST_PARSE_UINT8_FAIL("-123"); // uint8_t can't be negative
TEST_PARSE_UINT8_FAIL("-123 ");
TEST_PARSE_UINT8_FAIL(" -123");
TEST_PARSE_UINT8_FAIL(" -123 s");
TEST_PARSE_UINT8_FAIL("-12fd");
TEST_PARSE_UINT8_FAIL("-3432.6");
TEST_PARSE_UINT8_FAIL("-1.5E-1");
TEST_PARSE_UINT8_FAIL("-122-343");
TEST_PARSE_UINT8_FAIL("10000000000000000000000000000000000");
TEST_PARSE_UINT8_FAIL("4654654651651651616516516165165165454654");
TEST_PARSE_UINT8_FAIL("-12345646484131646165165184");
TEST_PARSE_UINT8_FAIL("-12345646484131646164");
return 0;
}
int test_sv_parse_uint8(cstring_view input, int will_fail, uint8_t expected) {
uint8_t inputi;
if (!sv_parse_uint8(input, &inputi)) {
if (will_fail) {
print_check();
printf("Expected parse failure: '" sv_fmt "'\n", sv_arg(input));
return 1;
} else {
print_x();
printf("Unexpected parse failure: '" sv_fmt "'\n", sv_arg(input));
return 0;
}
}
if (will_fail) {
print_x();
printf("Expected parse to fail: '" sv_fmt "' == %lu\n", sv_arg(input), inputi);
return 0;
}
if (expected == inputi) {
print_check();
printf("'" sv_fmt "' == %lu\n", sv_arg(input), inputi);
return 1;
}
print_check();
printf("'" sv_fmt "' != %lu\n", sv_arg(input), inputi);
return 0;
}
void print_check() {
printf("[\xE2\x9C\x93] ");
}
void print_x() {
printf("[x] ");
}