OSDN Git Service

implement unit tests for http_contentdecoder_t.
[bbk/bchanf.git] / src / unittest / unittest_driver.c
1 /*
2  * unittest_driver.h
3  *
4  * Copyright (c) 2012 project bchan
5  *
6  * This software is provided 'as-is', without any express or implied
7  * warranty. In no event will the authors be held liable for any damages
8  * arising from the use of this software.
9  *
10  * Permission is granted to anyone to use this software for any purpose,
11  * including commercial applications, and to alter it and redistribute it
12  * freely, subject to the following restrictions:
13  *
14  * 1. The origin of this software must not be misrepresented; you must not
15  *    claim that you wrote the original software. If you use this software
16  *    in a product, an acknowledgment in the product documentation would be
17  *    appreciated but is not required.
18  *
19  * 2. Altered source versions must be plainly marked as such, and must not be
20  *    misrepresented as being the original software.
21  *
22  * 3. This notice may not be removed or altered from any source
23  *    distribution.
24  *
25  */
26
27 #include "unittest_driver.h"
28
29 #include        <basic.h>
30 #include        <bstdio.h>
31 #include        <bstdlib.h>
32 #include        <bstring.h>
33 #include        <bctype.h>
34 #include        <errcode.h>
35 #include        <btron/btron.h>
36 #include        <bsys/queue.h>
37
38 #ifdef BCHAN_CONFIG_DEBUG
39 # define DP(arg) printf arg
40 # define DP_ER(msg, err) printf("%s (%d/%x)\n", msg, err>>16, err)
41 #else
42 # define DP(arg) /**/
43 # define DP_ER(msg, err) /**/
44 #endif
45
46 struct unittest_driverentry_t_ {
47         QUEUE que;
48         UB *name;
49         W name_len;
50         unittest_driver_testfunc testfunc;
51         UNITTEST_RESULT result;
52 };
53 typedef struct unittest_driverentry_t_ unittest_driverentry_t;
54
55 LOCAL VOID unittest_driverentry_QueInsert(unittest_driverentry_t *entry, unittest_driverentry_t *que)
56 {
57         QueInsert(&entry->que, &que->que);
58 }
59
60 LOCAL unittest_driverentry_t* unittest_driverentry_nextnode(unittest_driverentry_t *entry)
61 {
62         return (unittest_driverentry_t*)entry->que.next;
63 }
64
65 LOCAL VOID unittest_driverentry_runtest(unittest_driverentry_t *entry)
66 {
67         printf("%s\n", entry->name);
68         printf("---------------------------------------------\n");
69         entry->result = (*entry->testfunc)();
70         if (entry->result == UNITTEST_RESULT_PASS) {
71                 printf("--pass---------------------------------------\n");
72         } else {
73                 printf("--fail---------------------------------------\n");
74         }
75         printf("---------------------------------------------\n");
76 }
77
78 LOCAL unittest_driverentry_t* unittest_driverentry_new(UB *name, W name_len, unittest_driver_testfunc testfunc)
79 {
80         unittest_driverentry_t *entry;
81
82         entry = (unittest_driverentry_t *)malloc(sizeof(unittest_driverentry_t));
83         if (entry == NULL) {
84                 return NULL;
85         }
86         entry->name = malloc(sizeof(UB)*name_len);
87         if (entry->name == NULL) {
88                 free(entry);
89                 return NULL;
90         }
91         memcpy(entry->name, name, sizeof(UB)*name_len);
92         entry->name[name_len] = '\0';
93         entry->name_len = name_len;
94
95         entry->testfunc = testfunc;
96         entry->result = UNITTEST_RESULT_FAIL;
97
98         return entry;
99 }
100
101 LOCAL VOID unittest_driverentry_delete(unittest_driverentry_t *entry)
102 {
103         free(entry->name);
104         QueRemove(&entry->que);
105         free(entry);
106 }
107
108 struct unittest_driver_t_ {
109         QUEUE sentinel;
110 };
111
112 LOCAL unittest_driverentry_t* unittest_driver_sentinelnode(unittest_driver_t *driver)
113 {
114         return (unittest_driverentry_t*)&driver->sentinel;
115 }
116
117 EXPORT W unittest_driver_registfunc(unittest_driver_t *driver, UB *name, unittest_driver_testfunc testfunc)
118 {
119         unittest_driverentry_t *entry, *senti;
120
121         entry = unittest_driverentry_new(name, strlen(name), testfunc);
122         if (entry == NULL) {
123                 return -1; /* TODO */
124         }
125         senti = unittest_driver_sentinelnode(driver);
126         unittest_driverentry_QueInsert(entry, senti);
127
128         return 0;
129 }
130
131 EXPORT W unittest_driver_runnning(unittest_driver_t *driver)
132 {
133         unittest_driverentry_t *senti, *next;
134         Bool all = True;
135
136         senti = unittest_driver_sentinelnode(driver);
137         next = unittest_driverentry_nextnode(senti);
138         for (;;) {
139                 if (senti == next) {
140                         break;
141                 }
142                 unittest_driverentry_runtest(next);
143                 next = unittest_driverentry_nextnode(next);
144         }
145
146         senti = unittest_driver_sentinelnode(driver);
147         next = unittest_driverentry_nextnode(senti);
148         for (;;) {
149                 if (senti == next) {
150                         break;
151                 }
152                 if (next->result == UNITTEST_RESULT_FAIL) {
153                         printf("failed: %s\n", next->name);
154                         all = False;
155                 }
156                 next = unittest_driverentry_nextnode(next);
157         }
158
159         if (all == True) {
160                 printf("all test is succeed\n");
161         }
162
163         return 0;
164 }
165
166 EXPORT unittest_driver_t* unittest_driver_new()
167 {
168         unittest_driver_t *driver;
169
170         driver = (unittest_driver_t*)malloc(sizeof(unittest_driver_t));
171         if (driver == NULL) {
172                 return NULL;
173         }
174
175         QueInit(&driver->sentinel);
176
177         return driver;
178 }
179
180 EXPORT VOID unittest_driver_delete(unittest_driver_t *driver)
181 {
182         unittest_driverentry_t *senti, *entry;
183         Bool empty;
184
185         senti = unittest_driver_sentinelnode(driver);
186         for (;;) {
187                 empty = isQueEmpty(&driver->sentinel);
188                 if (empty != False) {
189                         break;
190                 }
191                 entry = unittest_driverentry_nextnode(senti);
192                 unittest_driverentry_delete(entry);
193         }
194
195         free(driver);
196 }