Update demo
[ctestharness.git] / src / harness_demo.c
blobedc7eb18f9086a9af38731b88f22917fbc19627b
1 #include "harness.h"
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <unistd.h>
7 static void
8 setup(void *user_data)
10 (void)user_data;
13 static void
14 teardown(void *user_data)
16 (void)user_data;
19 static int
20 test_foo(void *user_data)
22 int *data = (int *)user_data;
23 if (data == NULL) {
24 return (HARNESS_SKIP);
27 if (*data == 0) {
28 return (HARNESS_FAIL);
31 return (HARNESS_PASS);
34 static int
35 test_fail(void *user_data)
37 (void)user_data;
38 return (HARNESS_FAIL);
41 static int
42 test_pass(void *user_data)
44 (void)user_data;
45 return (HARNESS_PASS);
48 static int
49 test_long(void *user_data)
51 (void)user_data;
52 sleep(1);
53 return (HARNESS_PASS);
56 static int
57 test_assert_eq(void *user_data)
59 (void)user_data;
61 ASSERT_EQ(1, 0);
63 return (HARNESS_PASS);
66 static int
67 test_assert_neq(void *user_data)
69 (void)user_data;
71 ASSERT_NEQ(1, 1);
73 return (HARNESS_PASS);
76 static int
77 test_assert_lt(void *user_data)
79 (void)user_data;
81 ASSERT_LT(1, 0);
83 return (HARNESS_PASS);
86 static int
87 test_assert_le(void *user_data)
89 (void)user_data;
91 ASSERT_LE(1, 0);
93 return (HARNESS_PASS);
96 static int
97 test_assert_gt(void *user_data)
99 (void)user_data;
101 ASSERT_GT(0, 1);
103 return (HARNESS_PASS);
106 static int
107 test_assert_ge(void *user_data)
109 (void)user_data;
111 ASSERT_GT(0, 1);
113 return (HARNESS_PASS);
116 static int
117 test_assert_streq(void *user_data)
119 (void)user_data;
121 ASSERT_STREQ("foo", "bar");
123 return (HARNESS_PASS);
126 static int
127 test_assert_strne(void *user_data)
129 (void)user_data;
131 ASSERT_STRNE("foo", "foo");
133 return (HARNESS_PASS);
136 static int
137 test_assert_double_eq(void *user_data)
139 (void)user_data;
141 ASSERT_DOUBLE_EQ(95.1, 100.0, 0.01);
143 return (HARNESS_PASS);
146 static int
147 test_expect_eq(void *user_data)
149 (void)user_data;
151 EXPECT_EQ(1, 0);
153 return (HARNESS_PASS);
156 static int
157 test_expect_neq(void *user_data)
159 (void)user_data;
161 EXPECT_NEQ(1, 1);
163 return (HARNESS_PASS);
166 static int
167 test_expect_lt(void *user_data)
169 (void)user_data;
171 EXPECT_LT(1, 1);
173 return (HARNESS_PASS);
176 static int
177 test_expect_le(void *user_data)
179 (void)user_data;
181 EXPECT_LE(1, 0);
183 return (HARNESS_PASS);
186 static int
187 test_expect_gt(void *user_data)
189 (void)user_data;
191 EXPECT_GT(1, 1);
193 return (HARNESS_PASS);
196 static int
197 test_expect_ge(void *user_data)
199 (void)user_data;
201 EXPECT_LE(0, 1);
203 return (HARNESS_PASS);
206 static int
207 test_expect_streq(void *user_data)
209 (void)user_data;
211 EXPECT_STREQ("foo", "bar");
213 return (HARNESS_PASS);
216 static int
217 test_expect_strne(void *user_data)
219 (void)user_data;
221 EXPECT_STRNE("foo", "foo");
223 return (HARNESS_PASS);
226 static int
227 test_expect_double_eq(void *user_data)
229 (void)user_data;
231 EXPECT_DOUBLE_EQ(95.1, 100.0, 0.01);
233 return (HARNESS_PASS);
237 main(int argc, char *argv[])
239 int ret = EXIT_SUCCESS;
240 (void)argv;
242 struct harness_test tests[] = {
243 {NULL, NULL, test_foo, "test_foo", &argc},
244 {NULL, NULL, NULL, "", NULL}
247 struct harness_cfg cfg = {
248 .setup = setup,
249 .teardown = teardown,
250 .ntests = 0,
253 if (harness_init(&cfg) == 1) {
254 return (EXIT_FAILURE);
257 harness_add_test("bar", "test_foo", test_foo, NULL, NULL,
258 (void *)&argc);
259 harness_add_test("bar", "test_fail", test_fail, NULL, NULL, NULL);
260 harness_add_test("baz", "test_foo", test_foo, NULL, NULL,
261 (void *)&argc);
262 harness_add_test("baz", "test_pass", test_pass, NULL, NULL, NULL);
263 harness_add_test("bar", "test_pass", test_pass, NULL, NULL, NULL);
264 harness_add_test("baz", "test_fail", test_fail, NULL, NULL, NULL);
265 harness_add_test("baz", "test_long", test_long, NULL, NULL, NULL);
266 harness_add_test("bar", "test_long", test_long, NULL, NULL, NULL);
268 harness_add_test("macro_assert_fail", "test_assert_eq",
269 test_assert_eq, NULL, NULL, NULL);
270 harness_add_test("macro_assert_fail", "test_assert_neq",
271 test_assert_neq, NULL, NULL, NULL);
272 harness_add_test("macro_assert_fail", "test_assert_lt",
273 test_assert_lt, NULL, NULL, NULL);
274 harness_add_test("macro_assert_fail", "test_assert_le",
275 test_assert_le, NULL, NULL, NULL);
276 harness_add_test("macro_assert_fail", "test_assert_gt",
277 test_assert_gt, NULL, NULL, NULL);
278 harness_add_test("macro_assert_fail", "test_assert_ge",
279 test_assert_ge, NULL, NULL, NULL);
280 harness_add_test("macro_assert_fail", "test_assert_streq",
281 test_assert_streq, NULL, NULL, NULL);
282 harness_add_test("macro_assert_fail", "test_assert_strne",
283 test_assert_strne, NULL, NULL, NULL);
284 harness_add_test("macro_assert_fail", "test_assert_double_eq",
285 test_assert_double_eq, NULL, NULL, NULL);
287 harness_add_test("macro_expect_fail", "test_expect_eq",
288 test_expect_eq, NULL, NULL, NULL);
289 harness_add_test("macro_expect_fail", "test_expect_neq",
290 test_expect_neq, NULL, NULL, NULL);
291 harness_add_test("macro_expect_fail", "test_expect_lt",
292 test_expect_lt, NULL, NULL, NULL);
293 harness_add_test("macro_expect_fail", "test_expect_le",
294 test_expect_le, NULL, NULL, NULL);
295 harness_add_test("macro_expect_fail", "test_expect_gt",
296 test_expect_gt, NULL, NULL, NULL);
297 harness_add_test("macro_expect_fail", "test_expect_ge",
298 test_expect_ge, NULL, NULL, NULL);
299 harness_add_test("macro_expect_fail", "test_expect_streq",
300 test_expect_streq, NULL, NULL, NULL);
301 harness_add_test("macro_expect_fail", "test_expect_strne",
302 test_expect_strne, NULL, NULL, NULL);
303 harness_add_test("macro_expect_fail", "test_expect_double_eq",
304 test_expect_double_eq, NULL, NULL, NULL);
306 harness_add_suite("example_suite", tests);
307 harness_add_test("example_suite", "test_pass", test_pass, NULL, NULL,
308 NULL);
310 ret = harness_run(NULL);
311 harness_destroy(&cfg);
313 return (ret);