• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *
3  *  Copyright (C) 2001-2007  Peter Johnson
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND OTHER CONTRIBUTORS ``AS IS''
15  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR OTHER CONTRIBUTORS BE
18  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
19  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
20  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
22  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
23  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
24  * POSSIBILITY OF SUCH DAMAGE.
25  */
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29 
30 #include "libyasm/bitvect.h"
31 
32 static int
test_boot(void)33 test_boot(void)
34 {
35     if (BitVector_Boot() != ErrCode_Ok)
36         return 1;
37     return 0;
38 }
39 
40 typedef struct Val_s {
41     const char *ascii;
42     unsigned char result[10];   /* 80 bit result, little endian */
43 } Val;
44 
45 Val oct_small_vals[] = {
46     {   "0",
47         {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}
48     },
49     {   "1",
50         {0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}
51     },
52     {   "77",
53         {0x3F, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}
54     },
55 };
56 
57 Val oct_large_vals[] = {
58     {   "7654321076543210",
59         {0x88, 0xC6, 0xFA, 0x88, 0xC6, 0xFA, 0x00, 0x00, 0x00, 0x00}
60     },
61     {   "12634727612534126530214",
62         {0x8C, 0xB0, 0x5A, 0xE1, 0xAA, 0xF8, 0x3A, 0x67, 0x05, 0x00}
63     },
64     {   "61076543210",
65         {0x88, 0xC6, 0xFA, 0x88, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00}
66     },
67 };
68 
69 wordptr testval;
70 
71 static void
num_family_setup(void)72 num_family_setup(void)
73 {
74     BitVector_Boot();
75     testval = BitVector_Create(80, FALSE);
76 }
77 
78 static void
num_family_teardown(void)79 num_family_teardown(void)
80 {
81     BitVector_Destroy(testval);
82 }
83 
84 static char result_msg[1024];
85 
86 static int
num_check(Val * val)87 num_check(Val *val)
88 {
89     unsigned char ascii[64], *result;
90     unsigned int len;
91     int i;
92     int ret = 0;
93 
94     strcpy((char *)ascii, val->ascii);
95     strcpy(result_msg, "parser failure");
96     if(BitVector_from_Oct(testval, ascii) != ErrCode_Ok)
97         return 1;
98 
99     result = BitVector_Block_Read(testval, &len);
100 
101     for (i=0; i<10; i++)
102         if (result[i] != val->result[i])
103             ret = 1;
104 
105     if (ret) {
106         strcpy(result_msg, val->ascii);
107         for (i=0; i<10; i++)
108             sprintf((char *)ascii+3*i, "%02x ", result[i]);
109         strcat(result_msg, ": ");
110         strcat(result_msg, (char *)ascii);
111     }
112     free(result);
113 
114     return ret;
115 }
116 
117 static int
test_oct_small_num(void)118 test_oct_small_num(void)
119 {
120     Val *vals = oct_small_vals;
121     int i, num = sizeof(oct_small_vals)/sizeof(Val);
122 
123     for (i=0; i<num; i++) {
124         if (num_check(&vals[i]) != 0)
125             return 1;
126     }
127     return 0;
128 }
129 
130 static int
test_oct_large_num(void)131 test_oct_large_num(void)
132 {
133     Val *vals = oct_large_vals;
134     int i, num = sizeof(oct_large_vals)/sizeof(Val);
135 
136     for (i=0; i<num; i++) {
137         if (num_check(&vals[i]) != 0)
138             return 1;
139     }
140     return 0;
141 }
142 
143 char failed[1000];
144 
145 static int
runtest_(const char * testname,int (* testfunc)(void),void (* setup)(void),void (* teardown)(void))146 runtest_(const char *testname, int (*testfunc)(void), void (*setup)(void),
147          void (*teardown)(void))
148 {
149     int nf;
150     if (setup)
151         setup();
152     nf = testfunc();
153     if (teardown)
154         teardown();
155     printf("%c", nf>0 ? 'F':'.');
156     fflush(stdout);
157     if (nf > 0)
158         sprintf(failed, "%s ** F: %s failed!\n", failed, testname);
159     return nf;
160 }
161 #define runtest(x,y,z)  runtest_(#x,test_##x,y,z)
162 
163 int
main(void)164 main(void)
165 {
166     int nf = 0;
167 
168     failed[0] = '\0';
169     printf("Test bitvect_test: ");
170     nf += runtest(boot, NULL, NULL);
171     nf += runtest(oct_small_num, num_family_setup, num_family_teardown);
172     nf += runtest(oct_large_num, num_family_setup, num_family_teardown);
173     printf(" +%d-%d/3 %d%%\n%s",
174            3-nf, nf, 100*(3-nf)/3, failed);
175     return (nf == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
176 }
177