malloc
[protos/libecoli.git] / lib / ecoli_test.c
1 /*
2  * Copyright (c) 2016, Olivier MATZ <zer0@droids-corp.org>
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *
7  *     * Redistributions of source code must retain the above copyright
8  *       notice, this list of conditions and the following disclaimer.
9  *     * Redistributions in binary form must reproduce the above copyright
10  *       notice, this list of conditions and the following disclaimer in the
11  *       documentation and/or other materials provided with the distribution.
12  *     * Neither the name of the University of California, Berkeley nor the
13  *       names of its contributors may be used to endorse or promote products
14  *       derived from this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND ANY
17  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19  * DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS BE LIABLE FOR ANY
20  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27
28 #include <stdlib.h>
29 #include <stdio.h>
30 #include <string.h>
31
32 #include <ecoli_test.h>
33 #include <ecoli_tk.h>
34
35 static struct ec_test_list test_list = TAILQ_HEAD_INITIALIZER(test_list);
36
37 /* register a driver */
38 void ec_test_register(struct ec_test *test)
39 {
40         TAILQ_INSERT_TAIL(&test_list, test, next);
41 }
42
43 int ec_test_check_tk_parse(const struct ec_tk *tk, const char *input,
44         const char *expected)
45 {
46         struct ec_parsed_tk *p;
47         const char *s;
48         int ret = -1;
49
50         p = ec_tk_parse(tk, input);
51         s = ec_parsed_tk_to_string(p);
52         if (s == NULL && expected == NULL)
53                 ret = 0;
54         else if (s != NULL && expected != NULL &&
55                 !strcmp(s, expected))
56                 ret = 0;
57
58         if (expected == NULL && ret != 0)
59                 printf("tk should not match but matches <%s>\n", s);
60         if (expected != NULL && ret != 0)
61                 printf("tk should match <%s> but matches <%s>\n",
62                         expected, s);
63
64         ec_parsed_tk_free(p);
65
66         return ret;
67 }
68
69 int ec_test_check_tk_complete(const struct ec_tk *tk, const char *input,
70         const char *expected)
71 {
72         struct ec_completed_tk *p;
73         const char *s;
74         int ret = -1;
75
76         p = ec_tk_complete(tk, input);
77         s = ec_completed_tk_smallest_start(p);
78         if (s == NULL && expected == NULL)
79                 ret = 0;
80         else if (s != NULL && expected != NULL &&
81                 !strcmp(s, expected))
82                 ret = 0;
83
84         if (expected == NULL && ret != 0)
85                 printf("tk should not complete but completes with <%s>\n", s);
86         if (expected != NULL && ret != 0)
87                 printf("tk should complete with <%s> but completes with <%s>\n",
88                         expected, s);
89
90         ec_completed_tk_free(p);
91
92         return ret;
93 }
94
95 /* int ec_test_check_tk_complete_list(const struct ec_tk *tk, */
96 /*      const char *input, ...) */
97
98 int ec_test_all(void)
99 {
100         struct ec_test *test;
101         int ret = 0;
102
103         // XXX register a new malloc to trac memleaks
104
105         TAILQ_FOREACH(test, &test_list, next) {
106                 if (test->test() == 0) {
107                         printf("== test %-20s success\n", test->name);
108                 } else {
109                         printf("== test %-20s failed\n", test->name);
110                         ret = -1;
111                 }
112         }
113
114         return ret;
115 }