]>
Commit | Line | Data |
---|---|---|
b9d29568 DW |
1 | // SPDX-License-Identifier: GPL-2.0-or-later |
2 | /* | |
3 | * Copyright (C) 2023 Oracle. All Rights Reserved. | |
4 | * Author: Darrick J. Wong <djwong@kernel.org> | |
5 | */ | |
6 | #include "libfrog/randbytes.h" | |
7 | ||
8 | #ifndef __LIBFROG_DAHASHSELFTEST_H__ | |
9 | #define __LIBFROG_DAHASHSELFTEST_H__ | |
10 | ||
11 | /* 100 test cases */ | |
12 | static struct dahash_test { | |
13 | uint16_t start; /* random 12 bit offset in buf */ | |
14 | uint16_t length; /* random 8 bit length of test */ | |
15 | xfs_dahash_t dahash; /* expected dahash result */ | |
16 | } dahash_tests[] = | |
17 | { | |
18 | {0x0567, 0x0097, 0x96951389}, | |
19 | {0x0869, 0x0055, 0x6455ab4f}, | |
20 | {0x0c51, 0x00be, 0x8663afde}, | |
21 | {0x044a, 0x00fc, 0x98fbe432}, | |
22 | {0x0f29, 0x0079, 0x42371997}, | |
23 | {0x08ba, 0x0052, 0x942be4f7}, | |
24 | {0x01f2, 0x0013, 0x5262687e}, | |
25 | {0x09e3, 0x00e2, 0x8ffb0908}, | |
26 | {0x007c, 0x0051, 0xb3158491}, | |
27 | {0x0854, 0x001f, 0x83bb20d9}, | |
28 | {0x031b, 0x0008, 0x98970bdf}, | |
29 | {0x0de7, 0x0027, 0xbfbf6f6c}, | |
30 | {0x0f76, 0x0005, 0x906a7105}, | |
31 | {0x092e, 0x00d0, 0x86631850}, | |
32 | {0x0233, 0x0082, 0xdbdd914e}, | |
33 | {0x04c9, 0x0075, 0x5a400a9e}, | |
34 | {0x0b66, 0x0099, 0xae128b45}, | |
35 | {0x000d, 0x00ed, 0xe61c216a}, | |
36 | {0x0a31, 0x003d, 0xf69663b9}, | |
37 | {0x00a3, 0x0052, 0x643c39ae}, | |
38 | {0x0125, 0x00d5, 0x7c310b0d}, | |
39 | {0x0105, 0x004a, 0x06a77e74}, | |
40 | {0x0858, 0x008e, 0x265bc739}, | |
41 | {0x045e, 0x0095, 0x13d6b192}, | |
42 | {0x0dab, 0x003c, 0xc4498704}, | |
43 | {0x00cd, 0x00b5, 0x802a4e2d}, | |
44 | {0x069b, 0x008c, 0x5df60f71}, | |
45 | {0x0454, 0x006c, 0x5f03d8bb}, | |
46 | {0x040e, 0x0032, 0x0ce513b5}, | |
47 | {0x0874, 0x00e2, 0x6a811fb3}, | |
48 | {0x0521, 0x00b4, 0x93296833}, | |
49 | {0x0ddc, 0x00cf, 0xf9305338}, | |
50 | {0x0a70, 0x0023, 0x239549ea}, | |
51 | {0x083e, 0x0027, 0x2d88ba97}, | |
52 | {0x0241, 0x00a7, 0xfe0b32e1}, | |
53 | {0x0dfc, 0x0096, 0x1a11e815}, | |
54 | {0x023e, 0x001e, 0xebc9a1f3}, | |
55 | {0x067e, 0x0066, 0xb1067f81}, | |
56 | {0x09ea, 0x000e, 0x46fd7247}, | |
57 | {0x036b, 0x008c, 0x1a39acdf}, | |
58 | {0x078f, 0x0030, 0x964042ab}, | |
59 | {0x085c, 0x008f, 0x1829edab}, | |
60 | {0x02ec, 0x009f, 0x6aefa72d}, | |
61 | {0x043b, 0x00ce, 0x65642ff5}, | |
62 | {0x0a32, 0x00b8, 0xbd82759e}, | |
63 | {0x0d3c, 0x0087, 0xf4d66d54}, | |
64 | {0x09ec, 0x008a, 0x06bfa1ff}, | |
65 | {0x0902, 0x0015, 0x755025d2}, | |
66 | {0x08fe, 0x000e, 0xf690ce2d}, | |
67 | {0x00fb, 0x00dc, 0xe55f1528}, | |
68 | {0x0eaa, 0x003a, 0x0fe0a8d7}, | |
69 | {0x05fb, 0x0006, 0x86281cfb}, | |
70 | {0x0dd1, 0x00a7, 0x60ab51b4}, | |
71 | {0x0005, 0x001b, 0xf51d969b}, | |
72 | {0x077c, 0x00dd, 0xc2fed268}, | |
73 | {0x0575, 0x00f5, 0x432c0b1a}, | |
74 | {0x05be, 0x0088, 0x78baa04b}, | |
75 | {0x0c89, 0x0068, 0xeda9e428}, | |
76 | {0x0f5c, 0x0068, 0xec143c76}, | |
77 | {0x06a8, 0x0009, 0xd72651ce}, | |
78 | {0x060f, 0x008e, 0x765426cd}, | |
79 | {0x07b1, 0x0047, 0x2cfcfa0c}, | |
80 | {0x04f1, 0x0041, 0x55b172f9}, | |
81 | {0x0e05, 0x00ac, 0x61efde93}, | |
82 | {0x0bf7, 0x0097, 0x05b83eee}, | |
83 | {0x04e9, 0x00f3, 0x9928223a}, | |
84 | {0x023a, 0x0005, 0xdfada9bc}, | |
85 | {0x0acb, 0x000e, 0x2217cecd}, | |
86 | {0x0148, 0x0060, 0xbc3f7405}, | |
87 | {0x0764, 0x0059, 0xcbc201b1}, | |
88 | {0x021f, 0x0059, 0x5d6b2256}, | |
89 | {0x0f1e, 0x006c, 0xdefeeb45}, | |
90 | {0x071c, 0x00b9, 0xb9b59309}, | |
91 | {0x0564, 0x0063, 0xae064271}, | |
92 | {0x0b14, 0x0044, 0xdb867d9b}, | |
93 | {0x0e5a, 0x0055, 0xff06b685}, | |
94 | {0x015e, 0x00ba, 0x1115ccbc}, | |
95 | {0x0379, 0x00e6, 0x5f4e58dd}, | |
96 | {0x013b, 0x0067, 0x4897427e}, | |
97 | {0x0e64, 0x0071, 0x7af2b7a4}, | |
98 | {0x0a11, 0x0050, 0x92105726}, | |
99 | {0x0109, 0x0055, 0xd0d000f9}, | |
100 | {0x00aa, 0x0022, 0x815d229d}, | |
101 | {0x09ac, 0x004f, 0x02f9d985}, | |
102 | {0x0e1b, 0x00ce, 0x5cf92ab4}, | |
103 | {0x08af, 0x00d8, 0x17ca72d1}, | |
104 | {0x0e33, 0x000a, 0xda2dba6b}, | |
105 | {0x0ee3, 0x006a, 0xb00048e5}, | |
106 | {0x0648, 0x001a, 0x2364b8cb}, | |
107 | {0x0315, 0x0085, 0x0596fd0d}, | |
108 | {0x0fbb, 0x003e, 0x298230ca}, | |
109 | {0x0422, 0x006a, 0x78ada4ab}, | |
110 | {0x04ba, 0x0073, 0xced1fbc2}, | |
111 | {0x007d, 0x0061, 0x4b7ff236}, | |
112 | {0x070b, 0x00d0, 0x261cf0ae}, | |
113 | {0x0c1a, 0x0035, 0x8be92ee2}, | |
114 | {0x0af8, 0x0063, 0x824dcf03}, | |
115 | {0x08f8, 0x006d, 0xd289710c}, | |
116 | {0x021b, 0x00ee, 0x6ac1c41d}, | |
117 | {0x05b5, 0x00da, 0x8e52f0e2}, | |
118 | }; | |
119 | ||
120 | /* Don't print anything to stdout. */ | |
121 | #define DAHASHTEST_QUIET (1U << 0) | |
122 | ||
123 | static int | |
124 | dahash_test( | |
125 | unsigned int flags) | |
126 | { | |
127 | int i; | |
128 | int errors = 0; | |
129 | int bytes = 0; | |
130 | struct timeval start, stop; | |
131 | uint64_t usec; | |
132 | ||
133 | /* keep static to prevent cache warming code from | |
134 | * getting eliminated by the compiler */ | |
135 | static xfs_dahash_t hash; | |
136 | ||
137 | /* pre-warm the cache */ | |
138 | for (i = 0; i < ARRAY_SIZE(dahash_tests); i++) { | |
139 | bytes += 2 * dahash_tests[i].length; | |
140 | ||
141 | hash ^= libxfs_da_hashname( | |
142 | randbytes_test_buf + dahash_tests[i].start, | |
143 | dahash_tests[i].length); | |
144 | } | |
145 | ||
146 | gettimeofday(&start, NULL); | |
147 | for (i = 0; i < ARRAY_SIZE(dahash_tests); i++) { | |
148 | hash = libxfs_da_hashname( | |
149 | randbytes_test_buf + dahash_tests[i].start, | |
150 | dahash_tests[i].length); | |
151 | if (hash != dahash_tests[i].dahash) | |
152 | errors++; | |
153 | } | |
154 | gettimeofday(&stop, NULL); | |
155 | ||
156 | usec = stop.tv_usec - start.tv_usec + | |
157 | 1000000 * (stop.tv_sec - start.tv_sec); | |
158 | ||
159 | if (flags & DAHASHTEST_QUIET) | |
160 | return errors; | |
161 | ||
162 | if (errors) | |
163 | printf("dahash: %d self tests failed\n", errors); | |
164 | else { | |
165 | printf("dahash: tests passed, %d bytes in %" PRIu64 " usec\n", | |
166 | bytes, usec); | |
167 | } | |
168 | ||
169 | return errors; | |
170 | } | |
171 | ||
172 | #endif /* __LIBFROG_DAHASHSELFTEST_H__ */ |