OSDN Git Service

implement Rabin-Karp search algoristhm.
[bbk/bchan.git] / src / test_tadsearch.c
1 /*
2  * test_tadsearch.c
3  *
4  * Copyright (c) 2011 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    <bstdio.h>
28 #include    <tcode.h>
29
30 #include    "test.h"
31
32 #include    "tadsearch.h"
33
34 LOCAL TC test_tadsearch_testdata01[] = {TK_A, TK_B, TK_C, TNULL};
35 LOCAL TC test_tadsearch_testdata02[] = {TK_B, TK_C, TK_D, TNULL};
36 LOCAL TC test_tadsearch_testdata03[] = {TK_E, TK_F, TK_G, TK_E, TNULL};
37 LOCAL TC test_tadsearch_testdata04[] = {TK_a, TK_b, TK_c, TK_d, TK_e, TNULL};
38 LOCAL TC test_tadsearch_testdata05[] = {TK_a, TK_b, TK_A, TK_B, TK_C, TNULL};
39
40 LOCAL TEST_RESULT test_tadsearch_1()
41 {
42         tcstrbuffer_t set[3];
43         tadlib_rk_result_t result;
44         W err;
45
46         set[0].buffer = test_tadsearch_testdata01;
47         set[0].strlen = 3;
48         set[1].buffer = test_tadsearch_testdata02;
49         set[1].strlen = 3;
50         set[2].buffer = test_tadsearch_testdata03;
51         set[2].strlen = 4;
52
53         err = tadlib_rabinkarpsearch(test_tadsearch_testdata05, 5, set, 3, &result);
54         if (err != 1) {
55                 printf("return value failure: %d\n", err);
56                 return TEST_RESULT_FAIL;
57         }
58         if (result.i != 0) {
59                 printf("result failure: %d\n", result.i);
60                 return TEST_RESULT_FAIL;
61         }
62
63         return TEST_RESULT_PASS;
64 }
65
66 LOCAL TEST_RESULT test_tadsearch_2()
67 {
68         tcstrbuffer_t set[3];
69         tadlib_rk_result_t result;
70         W err;
71
72         set[0].buffer = test_tadsearch_testdata01;
73         set[0].strlen = 3;
74         set[1].buffer = test_tadsearch_testdata02;
75         set[1].strlen = 3;
76         set[2].buffer = test_tadsearch_testdata03;
77         set[2].strlen = 4;
78
79         err = tadlib_rabinkarpsearch(test_tadsearch_testdata04, 5, set, 3, &result);
80         if (err != 0) {
81                 printf("return value failure: %d\n", err);
82                 return TEST_RESULT_FAIL;
83         }
84
85         return TEST_RESULT_PASS;
86 }
87
88 LOCAL TEST_RESULT test_tadsearch_3()
89 {
90         tcstrbuffer_t set[1];
91         tadlib_rk_result_t result;
92         W err;
93
94         set[0].buffer = test_tadsearch_testdata01;
95         set[0].strlen = 3;
96
97         err = tadlib_rabinkarpsearch(test_tadsearch_testdata05, 5, set, 1, &result);
98         if (err != 1) {
99                 printf("return value failure: %d\n", err);
100                 return TEST_RESULT_FAIL;
101         }
102         if (result.i != 0) {
103                 printf("result failure: %d\n", result.i);
104                 return TEST_RESULT_FAIL;
105         }
106
107         return TEST_RESULT_PASS;
108 }
109
110 LOCAL VOID test_tadsearch_printresult(TEST_RESULT (*proc)(), B *test_name)
111 {
112         TEST_RESULT result;
113
114         printf("test_tadsearch: %s\n", test_name);
115         printf("---------------------------------------------\n");
116         result = proc();
117         if (result == TEST_RESULT_PASS) {
118                 printf("--pass---------------------------------------\n");
119         } else {
120                 printf("--fail---------------------------------------\n");
121         }
122         printf("---------------------------------------------\n");
123 }
124
125 EXPORT VOID test_tadsearch_main()
126 {
127         test_tadsearch_printresult(test_tadsearch_1, "test_tadsearch_1");
128         test_tadsearch_printresult(test_tadsearch_2, "test_tadsearch_2");
129         test_tadsearch_printresult(test_tadsearch_3, "test_tadsearch_3");
130 }