• R/O
  • SSH
  • HTTPS

cstl: Commit


Commit MetaInfo

Revision410 (tree)
Time2010-10-07 22:51:51
Authorkatono

Log Message

FAIL時にexpectedとactualの内容を表示できるように修正。
PTRとNSTRINGのマクロを追加。

Change Summary

Incremental Difference

--- branches/try-vfunc/unittest/TestCase.c (revision 409)
+++ branches/try-vfunc/unittest/TestCase.c (revision 410)
@@ -5,7 +5,8 @@
55 static TestCase *current_case;
66 static jmp_buf fatal_jmp;
77
8-static TestAssertion *TestAssertion_new(int passed_flag, const char *expr, const char *file, size_t line);
8+static int copy_string(char **dst1, char **dst2, const char *src1, const char *src2, long len);
9+static TestAssertion *TestAssertion_new(unsigned long expected, unsigned long actual, unsigned long type, const char *expr, const char *file, size_t line);
910 static void TestAssertion_delete(TestAssertion *self);
1011 static void list_push(TestAssertion *list, TestAssertion *node);
1112 static TestAssertion *list_pop(TestAssertion *list);
@@ -55,17 +56,10 @@
5556
5657 void TestCase_test(TestCase *self, size_t *nasserts, size_t *nasserts_failed)
5758 {
58- int failed_flag = 0;
59- size_t n = 1;
60- TestAssertion *pos;
61- TestAssertion *list = &self->assertion_list;
6259 current_case = self;
63- *nasserts = 0;
64- *nasserts_failed = 0;
65-/* PRINTF1(" Test: %s ... ", self->name);*/
60+
6661 self->setup_error = TestCase_setup(self);
6762 if (self->setup_error) {
68-/* PRINTF1("SETUP ERROR[%d]\n", self->setup_error);*/
6963 self->result.num_errors_setup++;
7064 return;
7165 }
@@ -72,27 +66,8 @@
7266 if (SETJMP(fatal_jmp) == 0) {
7367 self->test();
7468 }
75- for (pos = LIST_BEGIN(list); pos != LIST_END(list); pos = pos->next) {
76- self->result.num_asserts++;
77- self->result.num_asserts_ran++;
78- (*nasserts)++;
79- if (!pos->passed_flag) {
80- self->result.num_asserts_failed++;
81- (*nasserts_failed)++;
82- if (!failed_flag) {
83- failed_flag = 1;
84-/* PRINTF0("FAILED\n");*/
85- }
86-/* PRINTF4(" %d. %s(%d) %s\n", n, pos->file, pos->line, pos->expr);*/
87- n++;
88- }
89- }
90- if (!failed_flag) {
91-/* PRINTF0("passed\n");*/
92- }
9369 self->teardown_error = TestCase_teardown(self);
9470 if (self->teardown_error) {
95-/* PRINTF1(" TEARDOWN ERROR[%d]\n", self->teardown_error);*/
9671 self->result.num_errors_teardown++;
9772 return;
9873 }
@@ -103,26 +78,65 @@
10378 *result = self->result;
10479 }
10580
106-void assert_impl(int passed_flag, const char *expr, const char *file, size_t line, int fatal_flag)
81+void assert_impl(int passed_flag, unsigned long expected, unsigned long actual, unsigned long type, const char *expr, const char *file, size_t line, int fatal_flag)
10782 {
108- TestAssertion *node = TestAssertion_new(passed_flag, expr, file, line);
83+ TestAssertion *node;
84+ current_case->result.num_asserts++;
85+ current_case->result.num_asserts_ran++;
86+
87+ if (passed_flag) {
88+ return;
89+ }
90+
91+ current_case->result.num_asserts_failed++;
92+
93+ node = TestAssertion_new(expected, actual, type, expr, file, line);
10994 if (node) {
11095 list_push(&current_case->assertion_list, node);
11196 }
112- if (fatal_flag && !passed_flag) {
97+ if (fatal_flag) {
11398 /* FATAL */
11499 LONGJMP(fatal_jmp, 1);
115100 }
116101 }
117102
118-static TestAssertion *TestAssertion_new(int passed_flag, const char *expr, const char *file, size_t line)
103+static TestAssertion *TestAssertion_new(unsigned long expected, unsigned long actual, unsigned long type, const char *expr, const char *file, size_t line)
119104 {
105+ unsigned long t;
106+ long len = 0;
120107 TestAssertion *self = (TestAssertion *) MALLOC(sizeof(TestAssertion));
121108 if (!self) {
122-/* PRINTF0("malloc failed!!\n");*/
109+ PRINTF0("malloc failed!!\n");
123110 return 0;
124111 }
125- self->passed_flag = passed_flag;
112+
113+ self->type = type;
114+ t = GET_ASSERT_TYPE(type);
115+ switch (t) {
116+ case T_NONE:
117+ break;
118+ case T_BOOL:
119+ case T_NUM:
120+ self->expected.num = expected;
121+ self->actual.num = actual;
122+ break;
123+ case T_PTR:
124+ self->expected.ptr = (const void *) expected;
125+ self->actual.ptr = (const void *) actual;
126+ break;
127+ case T_NSTR:
128+ len = GET_NSTR_LEN(type);
129+ case T_STR:
130+ if (!copy_string(&self->expected.str, &self->actual.str, (const char *) expected, (const char *) actual, len)) {
131+ FREE(self);
132+ return 0;
133+ }
134+ break;
135+ default:
136+ self->type = T_NONE;
137+ break;
138+ }
139+
126140 self->expr = expr;
127141 self->file = file;
128142 self->line = line;
@@ -130,9 +144,44 @@
130144 return self;
131145 }
132146
147+static int copy_string(char **dst1, char **dst2, const char *src1, const char *src2, long len)
148+{
149+ char *p1;
150+ char *p2;
151+ size_t src1_len;
152+ size_t src2_len;
153+ if (len > 0) {
154+ src1_len = len + 1;
155+ src2_len = len + 1;
156+ } else {
157+ src1_len = strlen(src1) + 1;
158+ src2_len = strlen(src2) + 1;
159+ }
160+ p1 = (char *) STR_MALLOC(sizeof(char) * (src1_len + src2_len));
161+ if (!p1) {
162+ return 0;
163+ }
164+ p2 = p1 + src1_len;
165+ if (len > 0) {
166+ strncpy(p1, src1, len);
167+ strncpy(p2, src2, len);
168+ p1[len] = '\0';
169+ p2[len] = '\0';
170+ } else {
171+ strcpy(p1, src1);
172+ strcpy(p2, src2);
173+ }
174+ *dst1 = p1;
175+ *dst2 = p2;
176+ return 1;
177+}
178+
133179 static void TestAssertion_delete(TestAssertion *self)
134180 {
135181 if (!self) return;
182+ if (GET_ASSERT_TYPE(self->type) == T_NSTR || GET_ASSERT_TYPE(self->type) == T_STR) {
183+ STR_FREE(self->expected.str);
184+ }
136185 FREE(self);
137186 }
138187
--- branches/try-vfunc/unittest/UnitTest.c (revision 409)
+++ branches/try-vfunc/unittest/UnitTest.c (revision 410)
@@ -134,13 +134,34 @@
134134 TestAssertion *list = &tc->assertion_list;
135135 int name_printed = 0;
136136 for (pos = LIST_BEGIN(list); pos != LIST_END(list); pos = pos->next) {
137- if (!pos->passed_flag) {
138- if (!name_printed) {
139- PRINTF1("\n ** %s\n", tc->name);
140- name_printed = 1;
141- }
142- PRINTF3(" %s(%d) %s\n", pos->file, pos->line, pos->expr);
137+ unsigned long type;
138+ unsigned long not_flag;
139+ const char *true_str = "true";
140+ const char *false_str = "false";
141+ if (!name_printed) {
142+ PRINTF1("\n ** %s\n", tc->name);
143+ name_printed = 1;
143144 }
145+ PRINTF3(" %s(%d) %s\n", pos->file, pos->line, pos->expr);
146+ type = GET_ASSERT_TYPE(pos->type);
147+ not_flag = GET_NOT_FLAG(pos->type);
148+ switch (type) {
149+ case T_BOOL:
150+ printf(" expected<%s>, actual<%s>\n", pos->expected.num ? true_str : false_str, pos->actual.num ? true_str : false_str);
151+ break;
152+ case T_NUM:
153+ printf(" %s expected<%ld(0x%lx)>, actual<%ld(0x%lx)>\n", not_flag ? "not" : "", pos->expected.num, pos->expected.num, pos->actual.num, pos->actual.num);
154+ break;
155+ case T_PTR:
156+ printf(" %s expected<%p>, actual<%p>\n", not_flag ? "not" : "", pos->expected.ptr, pos->actual.ptr);
157+ break;
158+ case T_STR:
159+ printf(" %s expected<\"%s\">, actual<\"%s\">\n", not_flag ? "not" : "", pos->expected.str, pos->actual.str);
160+ break;
161+ case T_NSTR:
162+ printf(" %s expected<\"%s\">, actual<\"%s\">, num<%ld>\n", not_flag ? "not" : "", pos->expected.str, pos->actual.str, GET_NSTR_LEN(pos->type));
163+ break;
164+ }
144165 }
145166 if (tc->setup_error) {
146167 PRINTF1("\n ** %s\n", tc->name);
--- branches/try-vfunc/unittest/UnitTest.h (revision 409)
+++ branches/try-vfunc/unittest/UnitTest.h (revision 410)
@@ -10,9 +10,31 @@
1010 /*
1111 * private
1212 */
13+#define T_NONE 0x01000000
14+#define T_BOOL 0x02000000
15+#define T_NUM 0x04000000
16+#define T_PTR 0x08000000
17+#define T_STR 0x10000000
18+#define T_NSTR 0x40000000
19+#define T_NOT 0x80000000
20+
21+#define GET_ASSERT_TYPE(type) (((type) & T_NSTR) ? T_NSTR : ((type) & ~(T_NSTR | T_NOT)))
22+#define GET_NSTR_LEN(type) ((type) & ~(T_NSTR | T_NOT))
23+#define GET_NOT_FLAG(type) ((type) & T_NOT)
24+
1325 typedef struct TestAssertion {
14- int passed_flag;
1526 const char *expr;
27+ union {
28+ unsigned long num;
29+ char *str;
30+ const void *ptr;
31+ } expected;
32+ union {
33+ unsigned long num;
34+ char *str;
35+ const void *ptr;
36+ } actual;
37+ unsigned long type;
1638 const char *file;
1739 size_t line;
1840 struct TestAssertion *next;
@@ -85,26 +107,40 @@
85107 /*
86108 * Assert Macros
87109 */
88-#define ASSERT(expr) assert_impl(((expr) != 0), "ASSERT(" #expr ")", __FILE__, __LINE__, 0)
89-#define ASSERT_FATAL(expr) assert_impl(((expr) != 0), "ASSERT_FATAL(" #expr ")", __FILE__, __LINE__, 1)
90-#define ASSERT_TRUE(expr) assert_impl(((expr) != 0), "ASSERT_TRUE(" #expr ")", __FILE__, __LINE__, 0)
91-#define ASSERT_TRUE_FATAL(expr) assert_impl(((expr) != 0), "ASSERT_TRUE_FATAL(" #expr ")", __FILE__, __LINE__, 1)
92-#define ASSERT_FALSE(expr) assert_impl(((expr) == 0), "ASSERT_FALSE(" #expr ")", __FILE__, __LINE__, 0)
93-#define ASSERT_FALSE_FATAL(expr) assert_impl(((expr) == 0), "ASSERT_FALSE_FATAL(" #expr ")", __FILE__, __LINE__, 1)
94-#define ASSERT_EQUAL(actual, expected) assert_impl(((actual) == (expected)), "ASSERT_EQUAL(" #actual ", " #expected ")", __FILE__, __LINE__, 0)
95-#define ASSERT_NOT_EQUAL(actual, expected) assert_impl(((actual) != (expected)), "ASSERT_NOT_EQUAL(" #actual ", " #expected ")", __FILE__, __LINE__, 0)
96-#define ASSERT_EQUAL_FATAL(actual, expected) assert_impl(((actual) == (expected)), "ASSERT_EQUAL_FATAL(" #actual ", " #expected ")", __FILE__, __LINE__, 1)
97-#define ASSERT_NOT_EQUAL_FATAL(actual, expected) assert_impl(((actual) != (expected)), "ASSERT_NOT_EQUAL_FATAL(" #actual ", " #expected ")", __FILE__, __LINE__, 1)
98-#define ASSERT_STRING_EQUAL(actual, expected) assert_impl((strcmp((actual), (expected)) == 0), "ASSERT_STRING_EQUAL(" #actual ", " #expected ")", __FILE__, __LINE__, 0)
99-#define ASSERT_STRING_NOT_EQUAL(actual, expected) assert_impl((strcmp((actual), (expected)) != 0), "ASSERT_STRING_NOT_EQUAL(" #actual ", " #expected ")", __FILE__, __LINE__, 0)
100-#define ASSERT_STRING_EQUAL_FATAL(actual, expected) assert_impl((strcmp((actual), (expected)) == 0), "ASSERT_STRING_EQUAL_FATAL(" #actual ", " #expected ")", __FILE__, __LINE__, 1)
101-#define ASSERT_STRING_NOT_EQUAL_FATAL(actual, expected) assert_impl((strcmp((actual), (expected)) != 0), "ASSERT_STRING_NOT_EQUAL_FATAL(" #actual ", " #expected ")", __FILE__, __LINE__, 1)
102-#define ASSERT_FAIL(msg) assert_impl(0, "ASSERT_FAIL(" #msg ")", __FILE__, __LINE__, 0)
103-#define ASSERT_FAIL_FATAL(msg) assert_impl(0, "ASSERT_FAIL_FATAL(" #msg ")", __FILE__, __LINE__, 1)
110+#define ASSERT(expr) assert_impl(((expr) != 0), 0, 0, T_NONE, "ASSERT(" #expr ")", __FILE__, __LINE__, 0)
111+#define ASSERT_FATAL(expr) assert_impl(((expr) != 0), 0, 0, T_NONE, "ASSERT_FATAL(" #expr ")", __FILE__, __LINE__, 1)
112+#define ASSERT_FAIL(msg) assert_impl(0, 0, 0, T_NONE, "ASSERT_FAIL(" #msg ")", __FILE__, __LINE__, 0)
113+#define ASSERT_FAIL_FATAL(msg) assert_impl(0, 0, 0, T_NONE, "ASSERT_FAIL_FATAL(" #msg ")", __FILE__, __LINE__, 1)
104114
105-void assert_impl(int passed_flag, const char *expr, const char *file, size_t line, int fatal_flag);
115+#define ASSERT_TRUE(expr) assert_impl(((expr) != 0), 1, (unsigned long)(expr), T_BOOL, "ASSERT_TRUE(" #expr ")", __FILE__, __LINE__, 0)
116+#define ASSERT_TRUE_FATAL(expr) assert_impl(((expr) != 0), 1, (unsigned long)(expr), T_BOOL, "ASSERT_TRUE_FATAL(" #expr ")", __FILE__, __LINE__, 1)
117+#define ASSERT_FALSE(expr) assert_impl(((expr) == 0), 0, (unsigned long)(expr), T_BOOL, "ASSERT_FALSE(" #expr ")", __FILE__, __LINE__, 0)
118+#define ASSERT_FALSE_FATAL(expr) assert_impl(((expr) == 0), 0, (unsigned long)(expr), T_BOOL, "ASSERT_FALSE_FATAL(" #expr ")", __FILE__, __LINE__, 1)
106119
120+#define ASSERT_EQUAL(expected, actual) assert_impl(((expected) == (actual)), (unsigned long)(expected), (unsigned long)(actual), T_NUM, "ASSERT_EQUAL(" #expected ", " #actual ")", __FILE__, __LINE__, 0)
121+#define ASSERT_NOT_EQUAL(expected, actual) assert_impl(((expected) != (actual)), (unsigned long)(expected), (unsigned long)(actual), T_NUM | T_NOT, "ASSERT_NOT_EQUAL(" #expected ", " #actual ")", __FILE__, __LINE__, 0)
122+#define ASSERT_EQUAL_FATAL(expected, actual) assert_impl(((expected) == (actual)), (unsigned long)(expected), (unsigned long)(actual), T_NUM, "ASSERT_EQUAL_FATAL(" #expected ", " #actual ")", __FILE__, __LINE__, 1)
123+#define ASSERT_NOT_EQUAL_FATAL(expected, actual) assert_impl(((expected) != (actual)), (unsigned long)(expected), (unsigned long)(actual), T_NUM | T_NOT, "ASSERT_NOT_EQUAL_FATAL(" #expected ", " #actual ")", __FILE__, __LINE__, 1)
107124
125+#define ASSERT_PTR_EQUAL(expected, actual) assert_impl(((expected) == (actual)), (unsigned long)(expected), (unsigned long)(actual), T_PTR, "ASSERT_PTR_EQUAL(" #expected ", " #actual ")", __FILE__, __LINE__, 0)
126+#define ASSERT_PTR_NOT_EQUAL(expected, actual) assert_impl(((expected) != (actual)), (unsigned long)(expected), (unsigned long)(actual), T_PTR | T_NOT, "ASSERT_PTR_NOT_EQUAL(" #expected ", " #actual ")", __FILE__, __LINE__, 0)
127+#define ASSERT_PTR_EQUAL_FATAL(expected, actual) assert_impl(((expected) == (actual)), (unsigned long)(expected), (unsigned long)(actual), T_PTR, "ASSERT_PTR_EQUAL_FATAL(" #expected ", " #actual ")", __FILE__, __LINE__, 1)
128+#define ASSERT_PTR_NOT_EQUAL_FATAL(expected, actual) assert_impl(((expected) != (actual)), (unsigned long)(expected), (unsigned long)(actual), T_PTR | T_NOT, "ASSERT_PTR_NOT_EQUAL_FATAL(" #expected ", " #actual ")", __FILE__, __LINE__, 1)
129+
130+#define ASSERT_STRING_EQUAL(expected, actual) assert_impl((strcmp((expected), (actual)) == 0), (unsigned long)(expected), (unsigned long)(actual), T_STR, "ASSERT_STRING_EQUAL(" #expected ", " #actual ")", __FILE__, __LINE__, 0)
131+#define ASSERT_STRING_NOT_EQUAL(expected, actual) assert_impl((strcmp((expected), (actual)) != 0), (unsigned long)(expected), (unsigned long)(actual), T_STR | T_NOT, "ASSERT_STRING_NOT_EQUAL(" #expected ", " #actual ")", __FILE__, __LINE__, 0)
132+#define ASSERT_STRING_EQUAL_FATAL(expected, actual) assert_impl((strcmp((expected), (actual)) == 0), (unsigned long)(expected), (unsigned long)(actual), T_STR, "ASSERT_STRING_EQUAL_FATAL(" #expected ", " #actual ")", __FILE__, __LINE__, 1)
133+#define ASSERT_STRING_NOT_EQUAL_FATAL(expected, actual) assert_impl((strcmp((expected), (actual)) != 0), (unsigned long)(expected), (unsigned long)(actual), T_STR | T_NOT, "ASSERT_STRING_NOT_EQUAL_FATAL(" #expected ", " #actual ")", __FILE__, __LINE__, 1)
134+
135+#define ASSERT_NSTRING_EQUAL(expected, actual, n) assert_impl((strncmp((expected), (actual), (n)) == 0), (unsigned long)(expected), (unsigned long)(actual), (T_NSTR | (n)), "ASSERT_NSTRING_EQUAL(" #expected ", " #actual ", " #n ")", __FILE__, __LINE__, 0)
136+#define ASSERT_NSTRING_NOT_EQUAL(expected, actual, n) assert_impl((strncmp((expected), (actual), (n)) != 0), (unsigned long)(expected), (unsigned long)(actual), (T_NSTR | T_NOT | (n)), "ASSERT_NSTRING_NOT_EQUAL(" #expected ", " #actual ", " #n ")", __FILE__, __LINE__, 0)
137+#define ASSERT_NSTRING_EQUAL_FATAL(expected, actual, n) assert_impl((strncmp((expected), (actual), (n)) == 0), (unsigned long)(expected), (unsigned long)(actual), (T_NSTR | (n)), "ASSERT_NSTRING_EQUAL_FATAL(" #expected ", " #actual ", " #n ")", __FILE__, __LINE__, 1)
138+#define ASSERT_NSTRING_NOT_EQUAL_FATAL(expected, actual, n) assert_impl((strncmp((expected), (actual), (n)) != 0), (unsigned long)(expected), (unsigned long)(actual), (T_NSTR | T_NOT | (n)), "ASSERT_NSTRING_NOT_EQUAL_FATAL(" #expected ", " #actual ", " #n ")", __FILE__, __LINE__, 1)
139+
140+
141+void assert_impl(int passed_flag, unsigned long expected, unsigned long actual, unsigned long type, const char *expr, const char *file, size_t line, int fatal_flag);
142+
143+
108144 #ifdef __cplusplus
109145 }
110146 #endif
Show on old repository browser