1 /* Copyright (c) 2014, Google Inc.
3 * Permission to use, copy, modify, and/or distribute this software for any
4 * purpose with or without fee is hereby granted, provided that the above
5 * copyright notice and this permission notice appear in all copies.
7 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
8 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
9 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
10 * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
11 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
12 * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
13 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
17 #include <openssl/crypto.h>
18 #include <openssl/lhash.h>
24 struct dummy_lhash_node {
26 struct dummy_lhash_node *next;
30 struct dummy_lhash_node *head;
33 static void dummy_lh_free(struct dummy_lhash *lh) {
34 struct dummy_lhash_node *cur, *next;
36 for (cur = lh->head; cur != NULL; cur = next) {
43 static size_t dummy_lh_num_items(const struct dummy_lhash *lh) {
45 struct dummy_lhash_node *cur;
47 for (cur = lh->head; cur != NULL; cur = cur->next) {
54 static char *dummy_lh_retrieve(struct dummy_lhash *lh, const char *s) {
55 struct dummy_lhash_node *cur;
57 for (cur = lh->head; cur != NULL; cur = cur->next) {
58 if (strcmp(cur->s, s) == 0) {
66 static int dummy_lh_insert(struct dummy_lhash *lh, char **old_data, char *s) {
67 struct dummy_lhash_node *node, *cur;
69 for (cur = lh->head; cur != NULL; cur = cur->next) {
70 if (strcmp(cur->s, s) == 0) {
77 node = malloc(sizeof(struct dummy_lhash_node));
80 node->next = lh->head;
85 static char *dummy_lh_delete(struct dummy_lhash *lh, const void *s) {
86 struct dummy_lhash_node *cur, **next_ptr;
90 for (cur = lh->head; cur != NULL; cur = cur->next) {
91 if (strcmp(cur->s, s) == 0) {
93 *next_ptr = cur->next;
97 next_ptr = &cur->next;
103 static char *rand_string(void) {
104 unsigned len = 1 + (rand() % 3);
105 char *ret = malloc(len + 1);
108 for (i = 0; i < len; i++) {
109 ret[i] = '0' + (rand() & 7);
116 int main(int argc, char **argv) {
118 struct dummy_lhash dummy_lh = {NULL};
121 CRYPTO_library_init();
123 lh = lh_new(NULL, NULL);
125 for (i = 0; i < 100000; i++) {
129 if (dummy_lh_num_items(&dummy_lh) != lh_num_items(lh)) {
130 fprintf(stderr, "Length mismatch\n");
138 s1 = (char *)lh_retrieve(lh, s);
139 s2 = dummy_lh_retrieve(&dummy_lh, s);
140 if (s1 != NULL && (s2 == NULL || strcmp(s1, s2) != 0)) {
141 fprintf(stderr, "lh_retrieve failure\n");
149 lh_insert(lh, (void **)&s1, s);
150 dummy_lh_insert(&dummy_lh, &s2, strdup(s));
152 if (s1 != NULL && (s2 == NULL || strcmp(s1, s2) != 0)) {
153 fprintf(stderr, "lh_insert failure\n");
167 s1 = lh_delete(lh, s);
168 s2 = dummy_lh_delete(&dummy_lh, s);
170 if (s1 != NULL && (s2 == NULL || strcmp(s1, s2) != 0)) {
171 fprintf(stderr, "lh_insert failure\n");
191 dummy_lh_free(&dummy_lh);