"Das U-Boot" Source Tree
at master 124 lines 2.7 kB view raw
1// SPDX-License-Identifier: GPL-2.0 2/* 3 * (C) Copyright 2019 4 * Roman Kapl, SYSGO, rka@sysgo.com 5 */ 6 7#include <command.h> 8#include <log.h> 9#include <search.h> 10#include <stdio.h> 11#include <vsprintf.h> 12#include <test/env.h> 13#include <test/ut.h> 14 15#define SIZE 32 16#define ITERATIONS 10000 17 18static int htab_fill(struct unit_test_state *uts, 19 struct hsearch_data *htab, size_t size) 20{ 21 size_t i; 22 struct env_entry item; 23 struct env_entry *ritem; 24 char key[20]; 25 26 for (i = 0; i < size; i++) { 27 sprintf(key, "%d", (int)i); 28 item.callback = NULL; 29 item.data = key; 30 item.flags = 0; 31 item.key = key; 32 ut_asserteq(1, hsearch_r(item, ENV_ENTER, &ritem, htab, 0)); 33 } 34 35 return 0; 36} 37 38static int htab_check_fill(struct unit_test_state *uts, 39 struct hsearch_data *htab, size_t size) 40{ 41 size_t i; 42 struct env_entry item; 43 struct env_entry *ritem; 44 char key[20]; 45 46 for (i = 0; i < size; i++) { 47 sprintf(key, "%d", (int)i); 48 item.callback = NULL; 49 item.flags = 0; 50 item.data = key; 51 item.key = key; 52 hsearch_r(item, ENV_FIND, &ritem, htab, 0); 53 ut_assert(ritem); 54 ut_asserteq_str(key, ritem->key); 55 ut_asserteq_str(key, ritem->data); 56 } 57 58 return 0; 59} 60 61static int htab_create_delete(struct unit_test_state *uts, 62 struct hsearch_data *htab, size_t iterations) 63{ 64 size_t i; 65 struct env_entry item; 66 struct env_entry *ritem; 67 char key[20]; 68 69 for (i = 0; i < iterations; i++) { 70 sprintf(key, "cd-%d", (int)i); 71 item.callback = NULL; 72 item.flags = 0; 73 item.data = key; 74 item.key = key; 75 hsearch_r(item, ENV_ENTER, &ritem, htab, 0); 76 ritem = NULL; 77 78 hsearch_r(item, ENV_FIND, &ritem, htab, 0); 79 ut_assert(ritem); 80 ut_asserteq_str(key, ritem->key); 81 ut_asserteq_str(key, ritem->data); 82 83 ut_asserteq(0, hdelete_r(key, htab, 0)); 84 } 85 86 return 0; 87} 88 89/* Completely fill up the hash table */ 90static int env_test_htab_fill(struct unit_test_state *uts) 91{ 92 struct hsearch_data htab; 93 94 memset(&htab, 0, sizeof(htab)); 95 ut_asserteq(1, hcreate_r(SIZE, &htab)); 96 97 ut_assertok(htab_fill(uts, &htab, SIZE)); 98 ut_assertok(htab_check_fill(uts, &htab, SIZE)); 99 ut_asserteq(SIZE, htab.filled); 100 101 hdestroy_r(&htab); 102 return 0; 103} 104ENV_TEST(env_test_htab_fill, 0); 105 106/* Fill the hashtable up halfway an repeateadly delete/create elements 107 * and check for corruption 108 */ 109static int env_test_htab_deletes(struct unit_test_state *uts) 110{ 111 struct hsearch_data htab; 112 113 memset(&htab, 0, sizeof(htab)); 114 ut_asserteq(1, hcreate_r(SIZE, &htab)); 115 116 ut_assertok(htab_fill(uts, &htab, SIZE / 2)); 117 ut_assertok(htab_create_delete(uts, &htab, ITERATIONS)); 118 ut_assertok(htab_check_fill(uts, &htab, SIZE / 2)); 119 ut_asserteq(SIZE / 2, htab.filled); 120 121 hdestroy_r(&htab); 122 return 0; 123} 124ENV_TEST(env_test_htab_deletes, 0);