xref: /wlan-dirver/qca-wifi-host-cmn/qdf/test/qdf_hashtable_test.c (revision 1397a33f48ea6455be40871470b286e535820eb8)
1 /*
2  * Copyright (c) 2018 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 struct qdf_ht_test_item {
24 	struct qdf_ht_entry entry;
25 	uintptr_t key;
26 };
27 
28 static uint32_t qdf_ht_test_single(void)
29 {
30 	const int bits = 4; /* 16 buckets */
31 	struct qdf_ht_test_item item = { .key = (uintptr_t)&bits };
32 	struct qdf_ht_test_item *cursor;
33 	int i, count;
34 
35 	qdf_ht_declare(ht, bits);
36 
37 	qdf_ht_init(ht);
38 	qdf_ht_add(ht, &item.entry, item.key);
39 
40 	qdf_ht_get(ht, cursor, entry, item.key, key);
41 	QDF_BUG(cursor);
42 	QDF_BUG(cursor->key == item.key);
43 
44 	count = 0;
45 	qdf_ht_for_each(ht, i, cursor, entry) {
46 		QDF_BUG(cursor->key == item.key);
47 		count++;
48 	}
49 	QDF_BUG(count == 1);
50 
51 	count = 0;
52 	qdf_ht_for_each_in_bucket(ht, cursor, entry, item.key) {
53 		QDF_BUG(cursor->key == item.key);
54 		count++;
55 	}
56 	QDF_BUG(count == 1);
57 
58 	count = 0;
59 	qdf_ht_for_each_match(ht, cursor, entry, item.key, key) {
60 		QDF_BUG(cursor->key == item.key);
61 		count++;
62 	}
63 	QDF_BUG(count == 1);
64 
65 	qdf_ht_remove(&item.entry);
66 
67 	QDF_BUG(qdf_ht_empty(ht));
68 
69 	qdf_ht_deinit(ht);
70 
71 	return 0;
72 }
73 
74 uint32_t qdf_ht_unit_test(void)
75 {
76 	uint32_t errors = 0;
77 
78 	errors += qdf_ht_test_single();
79 
80 	return errors;
81 }
82 
83