forked from miloyip/json-tutorial
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_fun.c
More file actions
184 lines (161 loc) · 4.99 KB
/
test_fun.c
File metadata and controls
184 lines (161 loc) · 4.99 KB
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
#include "sbz_json.h"
#include <stdio.h>
#include <string.h>
static int main_ret;
static int test_count;
static int test_pass;
/* void */
/* test_t(format) */
/* { */
/* fprintf(stderr, */
/* "%s:%d: expect: " format " actual: " format "\n", */
/* __FILE__, */
/* __LINE__, */
/* expect, */
/* actual); */
/* } */
/* void */
/* expect_eq_int(int expect, int actual) */
/* { */
/* expect_eq_base(expect == actual, expect, actual, EXPECT_MSG("")); */
/* } */
void
expect_eq_base(int equality, char* format, void* expect, void* actual)
{
test_count++;
if (equality) {
test_pass++;
} else {
fprintf(stderr, format, __FILE__, __LINE__, expect, actual);
main_ret = 1;
}
}
#define EXPECT_MSG(format) "%s:%d: expect: " format " actual: " format "\n"
void
expect_eq_int(void* expect, void* actual)
{
expect_eq_base(*(int*)(expect) == *(int*)(actual), EXPECT_MSG("%d"), expect, actual);
}
void
expect_eq_double(void* expect, void* actual)
{
expect_eq_base(expect == actual, EXPECT_MSG("%.17g"), expect, actual);
}
void
expect_eq_string(void* expect, void* actual, int alen)
{
expect_eq_base(sizeof(expect) - 1 == alen &&
memcmp(expect, actual, alen + 1) == 0,
EXPECT_MSG("%s"),
expect,
actual);
}
#define EXPECT_EQ_BASE(equality, expect, actual, format) \
do { \
test_count++; \
if (equality) { \
test_pass++; \
} else { \
fprintf(stderr, \
"%s:%d: expect: " format " actual: " format "\n", \
__FILE__, \
__LINE__, \
expect, \
actual); \
main_ret = 1; \
} \
} while (0)
#define EXPECT_EQ_INT(expect, actual) \
EXPECT_EQ_BASE((expect) == (actual), expect, actual, "%d")
#define EXPECT_EQ_DOUBLE(expect, acutal) \
EXPECT_EQ_BASE((expect) == (actual), expect, actual, "%.17g")
#define EXPECT_EQ_STRING(expect, actual, alen) \
EXPECT_EQ_BASE(sizeof(expect) - 1 = \
alen && memcmp(expect, actual, alen) == 0, \
expect, \
actual, \
"%s")
#define EXPECT_TRUE(actual) EXPECT_EQ_BASE((actual) != 0, "true", "false", "%s")
#define EXPECT_FALSE(actual) \
EXPECT_EQ_BASE((actual) == 0, "false", "true", "%s")
#define TEST_NUMBER(expect, json)
#define TEST_STRING(expect, json)
#define TEST_PARSE_ERROR(error, json)
#define TEST_ROUNDTRIP(json)
#define TEST_EQUAL(json1, json2, equality)
static void
test_parse_null()
{
sbz_value v;
sbz_init(&v);
// sbz_set_boolean(&v, 0);
int result = sbz_parse(&v, "null");
expect_eq_int(SBZ_PARSE_OK, (int*)result);
// EXPECT_EQ_INT(SBZ_PARSE_OK, result);
expect_eq_int(SBZ_NULL, (int*)sbz_get_boolean(&v));
// EXPECT_EQ_INT(SBZ_NULL, sbz_get_boolean(&v));
sbz_free(&v);
}
static void
test_parse_true()
{
sbz_value v;
sbz_init(&v);
sbz_set_boolean(&v, 0);
int result = sbz_parse(&v, "true");
EXPECT_EQ_INT(SBZ_PARSE_OK, result);
EXPECT_EQ_INT(SBZ_TRUE, sbz_get_boolean(&v));
sbz_free(&v);
}
static void
test_parse_false()
{
}
static void
test_parse_number()
{
}
static void
test_parse_string()
{
}
static void
test_parse_array()
{
}
static void
test_parse_object()
{
}
int
main()
{
int result = 0;
int res2 = 1;
double exp1 = 1.234;
double exp2 = 1.234;
double exp3 = 1234.134;
char* char1 = "hello";
char* char2 = "hello";
char* char3 = "world";
expect_eq_int(&result, &result);
expect_eq_int(&result, &res2);
expect_eq_double(&exp1, &exp2);
expect_eq_double(&exp1, &exp3);
expect_eq_string("exp","exp",3);
expect_eq_string("exp","res",3);
expect_eq_string(&char1,&char2,3);
expect_eq_string(&char2,&char3,3);
// expect_eq_double(123.325, 123.325);
expect_eq_int(0, (int*)1);
// parse ok
// type ok
// value ok
test_parse_null();
test_parse_true();
test_parse_false();
test_parse_number();
test_parse_string();
test_parse_array();
test_parse_object();
}