]> git.proxmox.com Git - mirror_frr.git/blob - tests/lib/test_typelist.c
Merge pull request #10447 from ton31337/fix/json_with_whitespaces
[mirror_frr.git] / tests / lib / test_typelist.c
1 /*
2 * Copyright (c) 2016-2018 David Lamparter, for NetDEF, Inc.
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
16
17 #ifdef HAVE_CONFIG_H
18 #include "config.h"
19 #endif
20
21 #include <stdio.h>
22 #include <stdint.h>
23 #include <stdlib.h>
24 #include <inttypes.h>
25 #include <string.h>
26 #include <unistd.h>
27 #include <assert.h>
28 #include <arpa/inet.h>
29
30 #define WNO_ATOMLIST_UNSAFE_FIND
31
32 #include "typesafe.h"
33 #include "atomlist.h"
34 #include "memory.h"
35 #include "monotime.h"
36 #include "jhash.h"
37 #include "sha256.h"
38 #include "printfrr.h"
39
40 #include "tests/helpers/c/prng.h"
41
42 /* note: these macros are layered 2-deep because that makes the C
43 * preprocessor expand the "type" argument. Otherwise, you get
44 * "PREDECL_type" instead of "PREDECL_LIST"
45 */
46 #define _concat(a, b) a ## b
47 #define concat(a, b) _concat(a, b)
48 #define _str(x) #x
49 #define str(x) _str(x)
50
51 #define _PREDECL(type, ...) PREDECL_##type(__VA_ARGS__)
52 #define PREDECL(type, ...) _PREDECL(type, __VA_ARGS__)
53 #define _DECLARE(type, ...) DECLARE_##type(__VA_ARGS__)
54 #define DECLARE(type, ...) _DECLARE(type, __VA_ARGS__)
55
56 #define T_SORTED (1 << 0)
57 #define T_UNIQ (1 << 1)
58 #define T_HASH (1 << 2)
59 #define T_HEAP (1 << 3)
60 #define T_ATOMIC (1 << 4)
61 #define T_REVERSE (1 << 5)
62
63 #define _T_LIST (0)
64 #define _T_DLIST (0 | T_REVERSE)
65 #define _T_ATOMLIST (0 | T_ATOMIC)
66 #define _T_HEAP (T_SORTED | T_HEAP)
67 #define _T_SORTLIST_UNIQ (T_SORTED | T_UNIQ)
68 #define _T_SORTLIST_NONUNIQ (T_SORTED)
69 #define _T_HASH (T_SORTED | T_UNIQ | T_HASH)
70 #define _T_SKIPLIST_UNIQ (T_SORTED | T_UNIQ)
71 #define _T_SKIPLIST_NONUNIQ (T_SORTED)
72 #define _T_RBTREE_UNIQ (T_SORTED | T_UNIQ | T_REVERSE)
73 #define _T_RBTREE_NONUNIQ (T_SORTED | T_REVERSE)
74 #define _T_ATOMSORT_UNIQ (T_SORTED | T_UNIQ | T_ATOMIC)
75 #define _T_ATOMSORT_NONUNIQ (T_SORTED | T_ATOMIC)
76
77 #define _T_TYPE(type) _T_##type
78 #define IS_SORTED(type) (_T_TYPE(type) & T_SORTED)
79 #define IS_UNIQ(type) (_T_TYPE(type) & T_UNIQ)
80 #define IS_HASH(type) (_T_TYPE(type) & T_HASH)
81 #define IS_HEAP(type) (_T_TYPE(type) & T_HEAP)
82 #define IS_ATOMIC(type) (_T_TYPE(type) & T_ATOMIC)
83 #define IS_REVERSE(type) (_T_TYPE(type) & T_REVERSE)
84
85 static struct timeval ref, ref0;
86
87 static void ts_start(void)
88 {
89 monotime(&ref0);
90 monotime(&ref);
91 }
92 static void ts_ref(const char *text)
93 {
94 int64_t us;
95 us = monotime_since(&ref, NULL);
96 printfrr("%7"PRId64"us %s\n", us, text);
97 monotime(&ref);
98 }
99 static void ts_end(void)
100 {
101 int64_t us;
102 us = monotime_since(&ref0, NULL);
103 printfrr("%7"PRId64"us total\n", us);
104 }
105
106 #define TYPE LIST
107 #include "test_typelist.h"
108
109 #define TYPE DLIST
110 #include "test_typelist.h"
111
112 #define TYPE ATOMLIST
113 #include "test_typelist.h"
114
115 #define TYPE HEAP
116 #include "test_typelist.h"
117
118 #define TYPE SORTLIST_UNIQ
119 #include "test_typelist.h"
120
121 #define TYPE SORTLIST_NONUNIQ
122 #include "test_typelist.h"
123
124 #define TYPE HASH
125 #include "test_typelist.h"
126
127 #define TYPE HASH_collisions
128 #define REALTYPE HASH
129 #define SHITTY_HASH
130 #include "test_typelist.h"
131 #undef SHITTY_HASH
132
133 #define TYPE SKIPLIST_UNIQ
134 #include "test_typelist.h"
135
136 #define TYPE SKIPLIST_NONUNIQ
137 #include "test_typelist.h"
138
139 #define TYPE RBTREE_UNIQ
140 #include "test_typelist.h"
141
142 #define TYPE RBTREE_NONUNIQ
143 #include "test_typelist.h"
144
145 #define TYPE ATOMSORT_UNIQ
146 #include "test_typelist.h"
147
148 #define TYPE ATOMSORT_NONUNIQ
149 #include "test_typelist.h"
150
151 int main(int argc, char **argv)
152 {
153 srandom(1);
154
155 test_LIST();
156 test_DLIST();
157 test_ATOMLIST();
158 test_HEAP();
159 test_SORTLIST_UNIQ();
160 test_SORTLIST_NONUNIQ();
161 test_HASH();
162 test_HASH_collisions();
163 test_SKIPLIST_UNIQ();
164 test_SKIPLIST_NONUNIQ();
165 test_RBTREE_UNIQ();
166 test_RBTREE_NONUNIQ();
167 test_ATOMSORT_UNIQ();
168 test_ATOMSORT_NONUNIQ();
169
170 log_memstats_stderr("test: ");
171 return 0;
172 }