1 /*
2  * multiorder.c: Multi-order radix tree entry testing
3  * Copyright (c) 2016 Intel Corporation
4  * Author: Ross Zwisler <ross.zwisler@linux.intel.com>
5  * Author: Matthew Wilcox <matthew.r.wilcox@intel.com>
6  *
7  * This program is free software; you can redistribute it and/or modify it
8  * under the terms and conditions of the GNU General Public License,
9  * version 2, as published by the Free Software Foundation.
10  *
11  * This program is distributed in the hope it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
14  * more details.
15  */
16 #include <linux/radix-tree.h>
17 #include <linux/slab.h>
18 #include <linux/errno.h>
19 
20 #include "test.h"
21 
22 static void multiorder_check(unsigned long index, int order)
23 {
24 	unsigned long i;
25 	unsigned long min = index & ~((1UL << order) - 1);
26 	unsigned long max = min + (1UL << order);
27 	RADIX_TREE(tree, GFP_KERNEL);
28 
29 	printf("Multiorder index %ld, order %d\n", index, order);
30 
31 	assert(item_insert_order(&tree, index, order) == 0);
32 
33 	for (i = min; i < max; i++) {
34 		struct item *item = item_lookup(&tree, i);
35 		assert(item != 0);
36 		assert(item->index == index);
37 	}
38 	for (i = 0; i < min; i++)
39 		item_check_absent(&tree, i);
40 	for (i = max; i < 2*max; i++)
41 		item_check_absent(&tree, i);
42 
43 	assert(item_delete(&tree, index) != 0);
44 
45 	for (i = 0; i < 2*max; i++)
46 		item_check_absent(&tree, i);
47 }
48 
49 static void multiorder_shrink(unsigned long index, int order)
50 {
51 	unsigned long i;
52 	unsigned long max = 1 << order;
53 	RADIX_TREE(tree, GFP_KERNEL);
54 	struct radix_tree_node *node;
55 
56 	printf("Multiorder shrink index %ld, order %d\n", index, order);
57 
58 	assert(item_insert_order(&tree, 0, order) == 0);
59 
60 	node = tree.rnode;
61 
62 	assert(item_insert(&tree, index) == 0);
63 	assert(node != tree.rnode);
64 
65 	assert(item_delete(&tree, index) != 0);
66 	assert(node == tree.rnode);
67 
68 	for (i = 0; i < max; i++) {
69 		struct item *item = item_lookup(&tree, i);
70 		assert(item != 0);
71 		assert(item->index == 0);
72 	}
73 	for (i = max; i < 2*max; i++)
74 		item_check_absent(&tree, i);
75 
76 	if (!item_delete(&tree, 0)) {
77 		printf("failed to delete index %ld (order %d)\n", index, order);		abort();
78 	}
79 
80 	for (i = 0; i < 2*max; i++)
81 		item_check_absent(&tree, i);
82 }
83 
84 void multiorder_checks(void)
85 {
86 	int i;
87 
88 	for (i = 0; i < 20; i++) {
89 		multiorder_check(200, i);
90 		multiorder_check(0, i);
91 		multiorder_check((1UL << i) + 1, i);
92 	}
93 
94 	for (i = 0; i < 15; i++)
95 		multiorder_shrink((1UL << (i + RADIX_TREE_MAP_SHIFT)), i);
96 
97 }
98