]>
git.ipfire.org Git - thirdparty/openssl.git/blob - test/lhash_test.c
2 * Copyright 2017-2020 The OpenSSL Project Authors. All Rights Reserved.
3 * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
5 * Licensed under the Apache License 2.0 (the "License"). You may not use
6 * this file except in compliance with the License. You can obtain a copy
7 * in the file LICENSE in the source distribution or at
8 * https://www.openssl.org/source/license.html
14 #include <openssl/opensslconf.h>
15 #include <openssl/lhash.h>
16 #include <openssl/err.h>
17 #include <openssl/crypto.h>
19 #include "internal/nelem.h"
23 * The macros below generate unused functions which error out one of the clang
24 * builds. We disable this check here.
27 #pragma clang diagnostic ignored "-Wunused-function"
30 DEFINE_LHASH_OF_EX(int);
32 static int int_tests
[] = { 65537, 13, 1, 3, -5, 6, 7, 4, -10, -12, -14, 22, 9,
33 -17, 16, 17, -23, 35, 37, 173, 11 };
34 static const unsigned int n_int_tests
= OSSL_NELEM(int_tests
);
35 static short int_found
[OSSL_NELEM(int_tests
)];
36 static short int_not_found
;
38 static unsigned long int int_hash(const int *p
)
40 return 3 & *p
; /* To force collisions */
43 static int int_cmp(const int *p
, const int *q
)
48 static int int_find(int n
)
52 for (i
= 0; i
< n_int_tests
; i
++)
53 if (int_tests
[i
] == n
)
58 static void int_doall(int *v
)
60 const int n
= int_find(*v
);
68 static void int_doall_arg(int *p
, short *f
)
70 const int n
= int_find(*p
);
78 IMPLEMENT_LHASH_DOALL_ARG(int, short);
80 static int test_int_lhash(void)
93 const unsigned int n_dels
= OSSL_NELEM(dels
);
94 LHASH_OF(int) *h
= lh_int_new(&int_hash
, &int_cmp
);
96 int testresult
= 0, j
, *p
;
102 for (i
= 0; i
< n_int_tests
; i
++)
103 if (!TEST_ptr_null(lh_int_insert(h
, int_tests
+ i
))) {
104 TEST_info("int insert %d", i
);
109 if (!TEST_int_eq(lh_int_num_items(h
), n_int_tests
))
113 for (i
= 0; i
< n_int_tests
; i
++)
114 if (!TEST_int_eq(*lh_int_retrieve(h
, int_tests
+ i
), int_tests
[i
])) {
115 TEST_info("lhash int retrieve value %d", i
);
118 for (i
= 0; i
< n_int_tests
; i
++)
119 if (!TEST_ptr_eq(lh_int_retrieve(h
, int_tests
+ i
), int_tests
+ i
)) {
120 TEST_info("lhash int retrieve address %d", i
);
124 if (!TEST_ptr_eq(lh_int_retrieve(h
, &j
), int_tests
+ 2))
129 if (!TEST_ptr(p
= lh_int_insert(h
, &j
)))
131 if (!TEST_ptr_eq(p
, int_tests
+ 1))
133 if (!TEST_ptr_eq(lh_int_retrieve(h
, int_tests
+ 1), &j
))
137 memset(int_found
, 0, sizeof(int_found
));
139 lh_int_doall(h
, &int_doall
);
140 if (!TEST_int_eq(int_not_found
, 0)) {
141 TEST_info("lhash int doall encountered a not found condition");
144 for (i
= 0; i
< n_int_tests
; i
++)
145 if (!TEST_int_eq(int_found
[i
], 1)) {
146 TEST_info("lhash int doall %d", i
);
151 memset(int_found
, 0, sizeof(int_found
));
153 lh_int_doall_short(h
, int_doall_arg
, int_found
);
154 if (!TEST_int_eq(int_not_found
, 0)) {
155 TEST_info("lhash int doall arg encountered a not found condition");
158 for (i
= 0; i
< n_int_tests
; i
++)
159 if (!TEST_int_eq(int_found
[i
], 1)) {
160 TEST_info("lhash int doall arg %d", i
);
165 for (i
= 0; i
< n_dels
; i
++) {
166 const int b
= lh_int_delete(h
, &dels
[i
].data
) == NULL
;
167 if (!TEST_int_eq(b
^ dels
[i
].null
, 0)) {
168 TEST_info("lhash int delete %d", i
);
174 if (!TEST_int_eq(lh_int_error(h
), 0))
183 static unsigned long int stress_hash(const int *p
)
188 static int test_stress(void)
190 LHASH_OF(int) *h
= lh_int_new(&stress_hash
, &int_cmp
);
191 const unsigned int n
= 2500000;
193 int testresult
= 0, *p
;
199 for (i
= 0; i
< n
; i
++) {
200 p
= OPENSSL_malloc(sizeof(i
));
202 TEST_info("lhash stress out of memory %d", i
);
210 if (!TEST_int_eq(lh_int_num_items(h
), n
))
213 /* delete in a different order */
214 for (i
= 0; i
< n
; i
++) {
215 const int j
= (7 * i
+ 4) % n
* 3 + 1;
217 if (!TEST_ptr(p
= lh_int_delete(h
, &j
))) {
218 TEST_info("lhash stress delete %d\n", i
);
221 if (!TEST_int_eq(*p
, j
)) {
222 TEST_info("lhash stress bad value %d", i
);
234 int setup_tests(void)
236 ADD_TEST(test_int_lhash
);
237 ADD_TEST(test_stress
);