blob: 47711c28dbc4ee21ba8cd85e4e686bbe4dd680c8 [file] [log] [blame]
Ed Warnickecb9cada2015-12-08 15:45:58 -07001/*
2 * Copyright (c) 2015 Cisco and/or its affiliates.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15/*
16 Copyright (c) 2005 Eliot Dresselhaus
17
18 Permission is hereby granted, free of charge, to any person obtaining
19 a copy of this software and associated documentation files (the
20 "Software"), to deal in the Software without restriction, including
21 without limitation the rights to use, copy, modify, merge, publish,
22 distribute, sublicense, and/or sell copies of the Software, and to
23 permit persons to whom the Software is furnished to do so, subject to
24 the following conditions:
25
26 The above copyright notice and this permission notice shall be
27 included in all copies or substantial portions of the Software.
28
29 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
30 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
31 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
32 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
33 LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
34 OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
35 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
36*/
37
38#include <vppinfra/phash.h>
39#include <vppinfra/format.h>
40#include <vppinfra/random.h>
41
42static int verbose;
43#define if_verbose(format,args...) \
44 if (verbose) { clib_warning(format, ## args); }
45
Dave Barachc3799992016-08-15 11:12:27 -040046int
47test_phash_main (unformat_input_t * input)
Ed Warnickecb9cada2015-12-08 15:45:58 -070048{
Dave Barachc3799992016-08-15 11:12:27 -040049 phash_main_t _pm = { 0 }, *pm = &_pm;
Ed Warnickecb9cada2015-12-08 15:45:58 -070050 int n_keys, random_keys;
51 u32 seed;
Dave Barachc3799992016-08-15 11:12:27 -040052 clib_error_t *error;
Ed Warnickecb9cada2015-12-08 15:45:58 -070053
54 random_keys = 1;
55 n_keys = 1000;
56 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
57 {
58 if (0 == unformat (input, "keys %d", &n_keys)
59 && 0 == unformat (input, "verbose %=", &verbose, 1)
60 && 0 == unformat (input, "random-keys %=", &random_keys, 1)
61 && 0 == unformat (input, "sequential-keys %=", &random_keys, 0)
62 && 0 == unformat (input, "seed %d", &pm->random_seed)
63 && 0 == unformat (input, "64-bit %|", &pm->flags, PHASH_FLAG_MIX64)
64 && 0 == unformat (input, "32-bit %|", &pm->flags, PHASH_FLAG_MIX32)
Dave Barachc3799992016-08-15 11:12:27 -040065 && 0 == unformat (input, "fast %|", &pm->flags,
66 PHASH_FLAG_FAST_MODE)
67 && 0 == unformat (input, "slow %|", &pm->flags,
68 PHASH_FLAG_SLOW_MODE)
69 && 0 == unformat (input, "minimal %|", &pm->flags,
70 PHASH_FLAG_MINIMAL)
71 && 0 == unformat (input, "non-minimal %|", &pm->flags,
72 PHASH_FLAG_NON_MINIMAL))
Ed Warnickecb9cada2015-12-08 15:45:58 -070073 clib_error ("unknown input `%U'", format_unformat_error, input);
74 }
75
Dave Barachc3799992016-08-15 11:12:27 -040076 if (!pm->random_seed)
Ed Warnickecb9cada2015-12-08 15:45:58 -070077 pm->random_seed = random_default_seed ();
78
Dave Barachc3799992016-08-15 11:12:27 -040079 if_verbose
80 ("%d %d-bit keys, random seed %d, %s mode, looking for %sminimal hash",
81 n_keys, (pm->flags & PHASH_FLAG_MIX64) ? 64 : 32, pm->random_seed,
82 (pm->flags & PHASH_FLAG_FAST_MODE) ? "fast" : "slow",
83 (pm->flags & PHASH_FLAG_MINIMAL) ? "" : "non-");
Ed Warnickecb9cada2015-12-08 15:45:58 -070084
85 seed = pm->random_seed;
86
87 /* Initialize random keys. */
88 {
Dave Barachc3799992016-08-15 11:12:27 -040089 phash_key_t *k;
Ed Warnickecb9cada2015-12-08 15:45:58 -070090
91 vec_resize (pm->keys, n_keys);
92 vec_foreach (k, pm->keys)
Dave Barachc3799992016-08-15 11:12:27 -040093 {
94 k->key = k - pm->keys;
95 if (random_keys)
96 {
97 if (pm->flags & PHASH_FLAG_MIX64)
98 k->key = random_u64 (&seed);
99 else
100 k->key = random_u32 (&seed);
101 }
102 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700103 }
104
105 error = phash_find_perfect_hash (pm);
106 if (error)
107 {
108 clib_error_report (error);
109 return 1;
110 }
111 else
112 {
Dave Barachc3799992016-08-15 11:12:27 -0400113 if_verbose ("(%d,%d) (a,b) bits, %d seeds tried, %d tree walks",
114 pm->a_bits, pm->b_bits,
115 pm->n_seed_trials, pm->n_perfect_calls);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700116
117 error = phash_validate (pm);
118 if (error)
119 {
120 clib_error_report (error);
121 return 1;
122 }
123 }
124
125 return 0;
126}
127
128#ifdef CLIB_UNIX
Dave Barachc3799992016-08-15 11:12:27 -0400129int
130main (int argc, char *argv[])
Ed Warnickecb9cada2015-12-08 15:45:58 -0700131{
132 unformat_input_t i;
133 int res;
134
Damjan Marion4dffd1c2018-09-03 12:30:36 +0200135 clib_mem_init (0, 64ULL << 20);
136
Ed Warnickecb9cada2015-12-08 15:45:58 -0700137 verbose = (argc > 1);
138 unformat_init_command_line (&i, argv);
139 res = test_phash_main (&i);
140 unformat_free (&i);
141 return res;
142}
143#endif
Dave Barachc3799992016-08-15 11:12:27 -0400144
145/*
146 * fd.io coding-style-patch-verification: ON
147 *
148 * Local Variables:
149 * eval: (c-set-style "gnu")
150 * End:
151 */