1 /*
2  * Copyright (c) 2018-2019 The Linux Foundation. All rights reserved.
3  *
4  * Permission to use, copy, modify, and/or distribute this software for
5  * any purpose with or without fee is hereby granted, provided that the
6  * above copyright notice and this permission notice appear in all
7  * copies.
8  *
9  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
10  * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
11  * WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
12  * AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
13  * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
14  * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
15  * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
16  * PERFORMANCE OF THIS SOFTWARE.
17  */
18 
19 #include "qdf_hashtable.h"
20 #include "qdf_hashtable_test.h"
21 #include "qdf_trace.h"
22 
23 /* 16 buckets */
24 #define QDF_HT_HASH_BITS 4
25 
26 struct qdf_ht_test_item {
27 	struct qdf_ht_entry entry;
28 	uintptr_t key;
29 };
30 
qdf_ht_test_single(void)31 static uint32_t qdf_ht_test_single(void)
32 {
33 	const int bits = QDF_HT_HASH_BITS;
34 	struct qdf_ht_test_item item = { .key = (uintptr_t)&bits };
35 	struct qdf_ht_test_item *cursor;
36 	int i, count;
37 
38 	qdf_ht_declare(ht, QDF_HT_HASH_BITS);
39 
40 	qdf_ht_init(ht);
41 	qdf_ht_add(ht, &item.entry, item.key);
42 
43 	qdf_ht_get(ht, cursor, entry, item.key, key);
44 	QDF_BUG(cursor);
45 	QDF_BUG(cursor->key == item.key);
46 
47 	count = 0;
48 	qdf_ht_for_each(ht, i, cursor, entry) {
49 		QDF_BUG(cursor->key == item.key);
50 		count++;
51 	}
52 	QDF_BUG(count == 1);
53 
54 	count = 0;
55 	qdf_ht_for_each_in_bucket(ht, cursor, entry, item.key) {
56 		QDF_BUG(cursor->key == item.key);
57 		count++;
58 	}
59 	QDF_BUG(count == 1);
60 
61 	count = 0;
62 	qdf_ht_for_each_match(ht, cursor, entry, item.key, key) {
63 		QDF_BUG(cursor->key == item.key);
64 		count++;
65 	}
66 	QDF_BUG(count == 1);
67 
68 	qdf_ht_remove(&item.entry);
69 
70 	QDF_BUG(qdf_ht_empty(ht));
71 
72 	qdf_ht_deinit(ht);
73 
74 	return 0;
75 }
76 
qdf_ht_unit_test(void)77 uint32_t qdf_ht_unit_test(void)
78 {
79 	uint32_t errors = 0;
80 
81 	errors += qdf_ht_test_single();
82 
83 	return errors;
84 }
85 
86