4 * Copyright (c) 2012 project bchan
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.
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:
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.
19 * 2. Altered source versions must be plainly marked as such, and must not be
20 * misrepresented as being the original software.
22 * 3. This notice may not be removed or altered from any source
27 #include "unittest_driver.h"
35 #include <btron/btron.h>
36 #include <bsys/queue.h>
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)
43 # define DP_ER(msg, err) /**/
46 struct unittest_driverentry_t_ {
50 unittest_driver_testfunc testfunc;
51 UNITTEST_RESULT result;
53 typedef struct unittest_driverentry_t_ unittest_driverentry_t;
55 LOCAL VOID unittest_driverentry_QueInsert(unittest_driverentry_t *entry, unittest_driverentry_t *que)
57 QueInsert(&entry->que, &que->que);
60 LOCAL unittest_driverentry_t* unittest_driverentry_nextnode(unittest_driverentry_t *entry)
62 return (unittest_driverentry_t*)entry->que.next;
65 LOCAL VOID unittest_driverentry_runtest(unittest_driverentry_t *entry)
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");
73 printf("--fail---------------------------------------\n");
75 printf("---------------------------------------------\n");
78 LOCAL unittest_driverentry_t* unittest_driverentry_new(UB *name, W name_len, unittest_driver_testfunc testfunc)
80 unittest_driverentry_t *entry;
82 entry = (unittest_driverentry_t *)malloc(sizeof(unittest_driverentry_t));
86 entry->name = malloc(sizeof(UB)*name_len);
87 if (entry->name == NULL) {
91 memcpy(entry->name, name, sizeof(UB)*name_len);
92 entry->name[name_len] = '\0';
93 entry->name_len = name_len;
95 entry->testfunc = testfunc;
96 entry->result = UNITTEST_RESULT_FAIL;
101 LOCAL VOID unittest_driverentry_delete(unittest_driverentry_t *entry)
104 QueRemove(&entry->que);
108 struct unittest_driver_t_ {
112 LOCAL unittest_driverentry_t* unittest_driver_sentinelnode(unittest_driver_t *driver)
114 return (unittest_driverentry_t*)&driver->sentinel;
117 EXPORT W unittest_driver_registfunc(unittest_driver_t *driver, UB *name, unittest_driver_testfunc testfunc)
119 unittest_driverentry_t *entry, *senti;
121 entry = unittest_driverentry_new(name, strlen(name), testfunc);
123 return -1; /* TODO */
125 senti = unittest_driver_sentinelnode(driver);
126 unittest_driverentry_QueInsert(entry, senti);
131 EXPORT W unittest_driver_runnning(unittest_driver_t *driver)
133 unittest_driverentry_t *senti, *next;
136 senti = unittest_driver_sentinelnode(driver);
137 next = unittest_driverentry_nextnode(senti);
142 unittest_driverentry_runtest(next);
143 next = unittest_driverentry_nextnode(next);
146 senti = unittest_driver_sentinelnode(driver);
147 next = unittest_driverentry_nextnode(senti);
152 if (next->result == UNITTEST_RESULT_FAIL) {
153 printf("failed: %s\n", next->name);
156 next = unittest_driverentry_nextnode(next);
160 printf("all test is succeed\n");
166 EXPORT unittest_driver_t* unittest_driver_new()
168 unittest_driver_t *driver;
170 driver = (unittest_driver_t*)malloc(sizeof(unittest_driver_t));
171 if (driver == NULL) {
175 QueInit(&driver->sentinel);
180 EXPORT VOID unittest_driver_delete(unittest_driver_t *driver)
182 unittest_driverentry_t *senti, *entry;
185 senti = unittest_driver_sentinelnode(driver);
187 empty = isQueEmpty(&driver->sentinel);
188 if (empty != False) {
191 entry = unittest_driverentry_nextnode(senti);
192 unittest_driverentry_delete(entry);