2 * Copyright 2017 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the OpenSSL license (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
11 * Copyright (c) 2017 Oracle and/or its affiliates. All rights reserved.
17 #include <openssl/opensslconf.h>
18 #include <openssl/lhash.h>
19 #include <openssl/err.h>
20 #include <openssl/crypto.h>
26 * The macros below generate unused functions which error out one of the clang
27 * builds. We disable this check here.
30 #pragma clang diagnostic ignored "-Wunused-function"
35 static int int_tests[] = { 65537, 13, 1, 3, -5, 6, 7, 4, -10, -12, -14, 22, 9,
36 -17, 16, 17, -23, 35, 37, 173, 11 };
37 static const unsigned int n_int_tests = OSSL_NELEM(int_tests);
38 static short int_found[OSSL_NELEM(int_tests)];
40 static unsigned long int int_hash(const int *p)
42 return 3 & *p; /* To force collisions */
45 static int int_cmp(const int *p, const int *q)
50 static int int_find(int n)
54 for (i = 0; i < n_int_tests; i++)
55 if (int_tests[i] == n)
60 static void int_doall(int *v)
62 int_found[int_find(*v)]++;
65 static void int_doall_arg(int *p, short *f)
70 IMPLEMENT_LHASH_DOALL_ARG(int, short);
72 static int test_int_lhash(void)
85 const unsigned int n_dels = OSSL_NELEM(dels);
86 LHASH_OF(int) *h = lh_int_new(&int_hash, &int_cmp);
88 int testresult = 0, j, *p;
94 for (i = 0; i < n_int_tests; i++)
95 if (!TEST_ptr_null(lh_int_insert(h, int_tests + i))) {
96 TEST_info("int insert %d", i);
101 if (!TEST_int_eq(lh_int_num_items(h), n_int_tests))
105 for (i = 0; i < n_int_tests; i++)
106 if (!TEST_int_eq(*lh_int_retrieve(h, int_tests + i), int_tests[i])) {
107 TEST_info("lhash int retrieve value %d", i);
110 for (i = 0; i < n_int_tests; i++)
111 if (!TEST_ptr_eq(lh_int_retrieve(h, int_tests + i), int_tests + i)) {
112 TEST_info("lhash int retrieve address %d", i);
116 if (!TEST_ptr_eq(lh_int_retrieve(h, &j), int_tests + 2))
121 if (!TEST_ptr(p = lh_int_insert(h, &j)))
123 if (!TEST_ptr_eq(p, int_tests + 1))
125 if (!TEST_ptr_eq(lh_int_retrieve(h, int_tests + 1), &j))
129 memset(int_found, 0, sizeof(int_found));
130 lh_int_doall(h, &int_doall);
131 for (i = 0; i < n_int_tests; i++)
132 if (!TEST_int_eq(int_found[i], 1)) {
133 TEST_info("lhash int doall %d", i);
138 memset(int_found, 0, sizeof(int_found));
139 lh_int_doall_short(h, int_doall_arg, int_found);
140 for (i = 0; i < n_int_tests; i++)
141 if (!TEST_int_eq(int_found[i], 1)) {
142 TEST_info("lhash int doall arg %d", i);
147 for (i = 0; i < n_dels; i++) {
148 const int b = lh_int_delete(h, &dels[i].data) == NULL;
149 if (!TEST_int_eq(b ^ dels[i].null, 0)) {
150 TEST_info("lhash int delete %d", i);
156 if (!TEST_int_eq(lh_int_error(h), 0))
165 static unsigned long int stress_hash(const int *p)
170 static int test_stress(void)
172 LHASH_OF(int) *h = lh_int_new(&stress_hash, &int_cmp);
173 const unsigned int n = 2500000;
175 int testresult = 0, *p;
181 for (i = 0; i < n; i++) {
182 p = OPENSSL_malloc(sizeof(i));
184 TEST_info("lhash stress out of memory %d", i);
192 if (!TEST_int_eq(lh_int_num_items(h), n))
195 fprintf(stderr, "hash full statistics:\n");
196 OPENSSL_LH_stats((OPENSSL_LHASH *)h, stderr);
197 fprintf(stderr, "\nhash full node usage:\n");
198 OPENSSL_LH_node_usage_stats((OPENSSL_LHASH *)h, stderr);
200 /* delete in a different order */
201 for (i = 0; i < n; i++) {
202 const int j = (7 * i + 4) % n * 3 + 1;
204 if (!TEST_ptr(p = lh_int_delete(h, &j))) {
205 TEST_info("lhash stress delete %d\n", i);
208 if (!TEST_int_eq(*p, j)) {
209 TEST_info("lhash stress bad value %d", i);
215 fprintf(stderr, "\nhash empty statistics:\n");
216 OPENSSL_LH_stats((OPENSSL_LHASH *)h, stderr);
217 fprintf(stderr, "\nhash empty node usage:\n");
218 OPENSSL_LH_node_usage_stats((OPENSSL_LHASH *)h, stderr);
226 void register_tests(void)
228 ADD_TEST(test_int_lhash);
229 ADD_TEST(test_stress);