]>
Commit | Line | Data |
---|---|---|
e2073b02 JAK |
1 | #!/bin/sh |
2 | . $(dirname $(readlink -f $0))/framework.sh | |
3 | ||
4 | # Check for non-existing files | |
5 | testfailure triehash -C /does/not/exist1 -H /does/not/exist1 /does/not/exist/input | |
6 | ||
7 | # Check that we can specify - for -C and -H | |
8 | testsuccessequal "#ifndef TRIE_HASH_PerfectHash | |
9 | #define TRIE_HASH_PerfectHash | |
10 | #include <stddef.h> | |
11 | #include <stdint.h> | |
12 | enum PerfectKey { | |
13 | Unknown = -1, | |
14 | }; | |
15 | static enum PerfectKey PerfectHash(const char *string, size_t length); | |
16 | static enum PerfectKey PerfectHash(const char *string, size_t length) | |
17 | { | |
18 | switch (length) { | |
19 | default: | |
20 | return Unknown; | |
21 | } | |
22 | } | |
23 | #endif /* TRIE_HASH_PerfectHash */" triehash --multi-byte=0 -C - -H - | |
24 | ||
25 | # Check that split files work | |
26 | testsuccess triehash -C test.c -H test.h --multi-byte=0 | |
27 | testfileequal "#include \"test.h\" | |
28 | enum PerfectKey PerfectHash(const char *string, size_t length) | |
29 | { | |
30 | switch (length) { | |
31 | default: | |
32 | return Unknown; | |
33 | } | |
34 | }" test.c | |
35 | testfileequal "#ifndef TRIE_HASH_PerfectHash | |
36 | #define TRIE_HASH_PerfectHash | |
37 | #include <stddef.h> | |
38 | #include <stdint.h> | |
39 | enum PerfectKey { | |
40 | Unknown = -1, | |
41 | }; | |
42 | enum PerfectKey PerfectHash(const char *string, size_t length); | |
43 | #endif /* TRIE_HASH_PerfectHash */" test.h | |
44 | ||
45 | ||
46 | # Check the C code generator | |
47 | testsuccess triehash -C test.c -H test.c /dev/stdin | |
48 | testfileequal "#ifndef TRIE_HASH_PerfectHash | |
49 | #define TRIE_HASH_PerfectHash | |
50 | #include <stddef.h> | |
51 | #include <stdint.h> | |
52 | enum PerfectKey { | |
53 | VeryLongWord = 43, | |
54 | Word = 42, | |
55 | Word___0 = 0, | |
56 | Label = 44, | |
57 | Unknown = -9, | |
58 | }; | |
59 | static enum PerfectKey PerfectHash(const char *string, size_t length); | |
60 | #ifdef __GNUC__ | |
61 | typedef uint16_t __attribute__((aligned (1))) triehash_uu16; | |
62 | typedef char static_assert16[__alignof__(triehash_uu16) == 1 ? 1 : -1]; | |
63 | typedef uint32_t __attribute__((aligned (1))) triehash_uu32; | |
64 | typedef char static_assert32[__alignof__(triehash_uu32) == 1 ? 1 : -1]; | |
65 | typedef uint64_t __attribute__((aligned (1))) triehash_uu64; | |
66 | typedef char static_assert64[__alignof__(triehash_uu64) == 1 ? 1 : -1]; | |
67 | #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ | |
68 | #define onechar(c, s, l) (((uint64_t)(c)) << (s)) | |
69 | #else | |
70 | #define onechar(c, s, l) (((uint64_t)(c)) << (l-8-s)) | |
71 | #endif | |
72 | #if (!defined(__ARM_ARCH) || defined(__ARM_FEATURE_UNALIGNED)) && !defined(TRIE_HASH_NO_MULTI_BYTE) | |
73 | #define TRIE_HASH_MULTI_BYTE | |
74 | #endif | |
75 | #endif /*GNUC */ | |
76 | #ifdef TRIE_HASH_MULTI_BYTE | |
77 | static enum PerfectKey PerfectHash4(const char *string) | |
78 | { | |
79 | switch(*((triehash_uu32*) &string[0])) { | |
80 | case 0| onechar('W', 0, 32)| onechar('o', 8, 32)| onechar('r', 16, 32)| onechar('d', 24, 32): | |
81 | return Word; | |
82 | } | |
83 | return Unknown; | |
84 | } | |
85 | static enum PerfectKey PerfectHash5(const char *string) | |
86 | { | |
87 | switch(*((triehash_uu32*) &string[0])) { | |
88 | case 0| onechar('W', 0, 32)| onechar('o', 8, 32)| onechar('r', 16, 32)| onechar('d', 24, 32): | |
89 | switch(string[4]) { | |
90 | case 0| onechar('2', 0, 8): | |
91 | return Label; | |
92 | } | |
93 | } | |
94 | return Unknown; | |
95 | } | |
96 | static enum PerfectKey PerfectHash7(const char *string) | |
97 | { | |
98 | switch(*((triehash_uu32*) &string[0])) { | |
99 | case 0| onechar('W', 0, 32)| onechar('o', 8, 32)| onechar('r', 16, 32)| onechar('d', 24, 32): | |
100 | switch(string[4]) { | |
101 | case 0| onechar('-', 0, 8): | |
102 | switch(string[5]) { | |
103 | case 0| onechar('_', 0, 8): | |
104 | switch(string[6]) { | |
105 | case 0| onechar('0', 0, 8): | |
106 | return Word___0; | |
107 | } | |
108 | } | |
109 | } | |
110 | } | |
111 | return Unknown; | |
112 | } | |
113 | static enum PerfectKey PerfectHash12(const char *string) | |
114 | { | |
115 | switch(*((triehash_uu64*) &string[0])) { | |
116 | case 0| onechar('V', 0, 64)| onechar('e', 8, 64)| onechar('r', 16, 64)| onechar('y', 24, 64)| onechar('L', 32, 64)| onechar('o', 40, 64)| onechar('n', 48, 64)| onechar('g', 56, 64): | |
117 | switch(*((triehash_uu32*) &string[8])) { | |
118 | case 0| onechar('W', 0, 32)| onechar('o', 8, 32)| onechar('r', 16, 32)| onechar('d', 24, 32): | |
119 | return VeryLongWord; | |
120 | } | |
121 | } | |
122 | return Unknown; | |
123 | } | |
124 | #else | |
125 | static enum PerfectKey PerfectHash4(const char *string) | |
126 | { | |
127 | switch(string[0]) { | |
128 | case 'W': | |
129 | switch(string[1]) { | |
130 | case 'o': | |
131 | switch(string[2]) { | |
132 | case 'r': | |
133 | switch(string[3]) { | |
134 | case 'd': | |
135 | return Word; | |
136 | } | |
137 | } | |
138 | } | |
139 | } | |
140 | return Unknown; | |
141 | } | |
142 | static enum PerfectKey PerfectHash5(const char *string) | |
143 | { | |
144 | switch(string[0]) { | |
145 | case 'W': | |
146 | switch(string[1]) { | |
147 | case 'o': | |
148 | switch(string[2]) { | |
149 | case 'r': | |
150 | switch(string[3]) { | |
151 | case 'd': | |
152 | switch(string[4]) { | |
153 | case '2': | |
154 | return Label; | |
155 | } | |
156 | } | |
157 | } | |
158 | } | |
159 | } | |
160 | return Unknown; | |
161 | } | |
162 | static enum PerfectKey PerfectHash7(const char *string) | |
163 | { | |
164 | switch(string[0]) { | |
165 | case 'W': | |
166 | switch(string[1]) { | |
167 | case 'o': | |
168 | switch(string[2]) { | |
169 | case 'r': | |
170 | switch(string[3]) { | |
171 | case 'd': | |
172 | switch(string[4]) { | |
173 | case '-': | |
174 | switch(string[5]) { | |
175 | case '_': | |
176 | switch(string[6]) { | |
177 | case '0': | |
178 | return Word___0; | |
179 | } | |
180 | } | |
181 | } | |
182 | } | |
183 | } | |
184 | } | |
185 | } | |
186 | return Unknown; | |
187 | } | |
188 | static enum PerfectKey PerfectHash12(const char *string) | |
189 | { | |
190 | switch(string[0]) { | |
191 | case 'V': | |
192 | switch(string[1]) { | |
193 | case 'e': | |
194 | switch(string[2]) { | |
195 | case 'r': | |
196 | switch(string[3]) { | |
197 | case 'y': | |
198 | switch(string[4]) { | |
199 | case 'L': | |
200 | switch(string[5]) { | |
201 | case 'o': | |
202 | switch(string[6]) { | |
203 | case 'n': | |
204 | switch(string[7]) { | |
205 | case 'g': | |
206 | switch(string[8]) { | |
207 | case 'W': | |
208 | switch(string[9]) { | |
209 | case 'o': | |
210 | switch(string[10]) { | |
211 | case 'r': | |
212 | switch(string[11]) { | |
213 | case 'd': | |
214 | return VeryLongWord; | |
215 | } | |
216 | } | |
217 | } | |
218 | } | |
219 | } | |
220 | } | |
221 | } | |
222 | } | |
223 | } | |
224 | } | |
225 | } | |
226 | } | |
227 | return Unknown; | |
228 | } | |
229 | #endif /* TRIE_HASH_MULTI_BYTE */ | |
230 | static enum PerfectKey PerfectHash(const char *string, size_t length) | |
231 | { | |
232 | switch (length) { | |
233 | case 4: | |
234 | return PerfectHash4(string); | |
235 | case 5: | |
236 | return PerfectHash5(string); | |
237 | case 7: | |
238 | return PerfectHash7(string); | |
239 | case 12: | |
240 | return PerfectHash12(string); | |
241 | default: | |
242 | return Unknown; | |
243 | } | |
244 | } | |
245 | #endif /* TRIE_HASH_PerfectHash */" test.c |