summaryrefslogtreecommitdiff
path: root/triehash/tests/test-basic
blob: 19cb086842f56adfe41c8c904ea373a249888f0c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
#!/bin/sh
. $(dirname $(readlink -f $0))/framework.sh

# Check for non-existing files
testfailure triehash -C /does/not/exist1 -H /does/not/exist1 /does/not/exist/input

# Check that we can specify - for -C and -H
testsuccessequal "#ifndef TRIE_HASH_PerfectHash
#define TRIE_HASH_PerfectHash
#include <stddef.h>
#include <stdint.h>
enum PerfectKey {
    Unknown = -1,
};
static enum PerfectKey PerfectHash(const char *string, size_t length);
static enum PerfectKey PerfectHash(const char *string, size_t length)
{
    switch (length) {
    default:
        return Unknown;
    }
}
#endif                       /* TRIE_HASH_PerfectHash */" triehash --multi-byte=0 -C - -H -

# Check that split files work
testsuccess triehash -C test.c -H test.h --multi-byte=0
testfileequal "#include \"test.h\"
 enum PerfectKey PerfectHash(const char *string, size_t length)
{
    switch (length) {
    default:
        return Unknown;
    }
}" test.c
testfileequal "#ifndef TRIE_HASH_PerfectHash
#define TRIE_HASH_PerfectHash
#include <stddef.h>
#include <stdint.h>
enum PerfectKey {
    Unknown = -1,
};
 enum PerfectKey PerfectHash(const char *string, size_t length);
#endif                       /* TRIE_HASH_PerfectHash */" test.h


# Check the C code generator
testsuccess triehash -C test.c -H test.c /dev/stdin
testfileequal "#ifndef TRIE_HASH_PerfectHash
#define TRIE_HASH_PerfectHash
#include <stddef.h>
#include <stdint.h>
enum PerfectKey {
    VeryLongWord = 43,
    Word = 42,
    Word___0 = 0,
    Label = 44,
    Unknown = -9,
};
static enum PerfectKey PerfectHash(const char *string, size_t length);
#ifdef __GNUC__
typedef uint16_t __attribute__((aligned (1))) triehash_uu16;
typedef char static_assert16[__alignof__(triehash_uu16) == 1 ? 1 : -1];
typedef uint32_t __attribute__((aligned (1))) triehash_uu32;
typedef char static_assert32[__alignof__(triehash_uu32) == 1 ? 1 : -1];
typedef uint64_t __attribute__((aligned (1))) triehash_uu64;
typedef char static_assert64[__alignof__(triehash_uu64) == 1 ? 1 : -1];
#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
#define onechar(c, s, l) (((uint64_t)(c)) << (s))
#else
#define onechar(c, s, l) (((uint64_t)(c)) << (l-8-s))
#endif
#if (!defined(__ARM_ARCH) || defined(__ARM_FEATURE_UNALIGNED)) && !defined(TRIE_HASH_NO_MULTI_BYTE)
#define TRIE_HASH_MULTI_BYTE
#endif
#endif /*GNUC */
#ifdef TRIE_HASH_MULTI_BYTE
static enum PerfectKey PerfectHash4(const char *string)
{
    switch(*((triehash_uu32*) &string[0])) {
    case 0| onechar('W', 0, 32)| onechar('o', 8, 32)| onechar('r', 16, 32)| onechar('d', 24, 32):
        return Word;
    }
    return Unknown;
}
static enum PerfectKey PerfectHash5(const char *string)
{
    switch(*((triehash_uu32*) &string[0])) {
    case 0| onechar('W', 0, 32)| onechar('o', 8, 32)| onechar('r', 16, 32)| onechar('d', 24, 32):
        switch(string[4]) {
        case 0| onechar('2', 0, 8):
            return Label;
        }
    }
    return Unknown;
}
static enum PerfectKey PerfectHash7(const char *string)
{
    switch(*((triehash_uu32*) &string[0])) {
    case 0| onechar('W', 0, 32)| onechar('o', 8, 32)| onechar('r', 16, 32)| onechar('d', 24, 32):
        switch(string[4]) {
        case 0| onechar('-', 0, 8):
            switch(string[5]) {
            case 0| onechar('_', 0, 8):
                switch(string[6]) {
                case 0| onechar('0', 0, 8):
                    return Word___0;
                }
            }
        }
    }
    return Unknown;
}
static enum PerfectKey PerfectHash12(const char *string)
{
    switch(*((triehash_uu64*) &string[0])) {
    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):
        switch(*((triehash_uu32*) &string[8])) {
        case 0| onechar('W', 0, 32)| onechar('o', 8, 32)| onechar('r', 16, 32)| onechar('d', 24, 32):
            return VeryLongWord;
        }
    }
    return Unknown;
}
#else
static enum PerfectKey PerfectHash4(const char *string)
{
    switch(string[0]) {
    case 'W':
        switch(string[1]) {
        case 'o':
            switch(string[2]) {
            case 'r':
                switch(string[3]) {
                case 'd':
                    return Word;
                }
            }
        }
    }
    return Unknown;
}
static enum PerfectKey PerfectHash5(const char *string)
{
    switch(string[0]) {
    case 'W':
        switch(string[1]) {
        case 'o':
            switch(string[2]) {
            case 'r':
                switch(string[3]) {
                case 'd':
                    switch(string[4]) {
                    case '2':
                        return Label;
                    }
                }
            }
        }
    }
    return Unknown;
}
static enum PerfectKey PerfectHash7(const char *string)
{
    switch(string[0]) {
    case 'W':
        switch(string[1]) {
        case 'o':
            switch(string[2]) {
            case 'r':
                switch(string[3]) {
                case 'd':
                    switch(string[4]) {
                    case '-':
                        switch(string[5]) {
                        case '_':
                            switch(string[6]) {
                            case '0':
                                return Word___0;
                            }
                        }
                    }
                }
            }
        }
    }
    return Unknown;
}
static enum PerfectKey PerfectHash12(const char *string)
{
    switch(string[0]) {
    case 'V':
        switch(string[1]) {
        case 'e':
            switch(string[2]) {
            case 'r':
                switch(string[3]) {
                case 'y':
                    switch(string[4]) {
                    case 'L':
                        switch(string[5]) {
                        case 'o':
                            switch(string[6]) {
                            case 'n':
                                switch(string[7]) {
                                case 'g':
                                    switch(string[8]) {
                                    case 'W':
                                        switch(string[9]) {
                                        case 'o':
                                            switch(string[10]) {
                                            case 'r':
                                                switch(string[11]) {
                                                case 'd':
                                                    return VeryLongWord;
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    return Unknown;
}
#endif /* TRIE_HASH_MULTI_BYTE */
static enum PerfectKey PerfectHash(const char *string, size_t length)
{
    switch (length) {
    case 4:
        return PerfectHash4(string);
    case 5:
        return PerfectHash5(string);
    case 7:
        return PerfectHash7(string);
    case 12:
        return PerfectHash12(string);
    default:
        return Unknown;
    }
}
#endif                       /* TRIE_HASH_PerfectHash */"  test.c