diff options
author | Artem Bityutskiy <Artem.Bityutskiy@nokia.com> | 2010-10-26 17:23:06 -0400 |
---|---|---|
committer | Linus Torvalds <torvalds@linux-foundation.org> | 2010-10-26 19:52:19 -0400 |
commit | eeee9ebb54b76a33a13d2c926ffb018a4aea410f (patch) | |
tree | 96e40d9c6690878008d6d4f1ca672fc6578d6d2d /lib | |
parent | bb2ab10fa693110cffa7087ffe2749d6e9a27d5f (diff) |
lib/list_sort: test: use generic random32
Instead of using own pseudo-random generator, use generic linux
'random32()' function. Presumably, this should improve test coverage.
At the same time, do the following changes:
o Use shorter macro name for test list length
o Do not use strange 'l_h' name for 'struct list_head' element,
use 'list', because it is traditional name and thus, makes the
code more obvious and readable.
Signed-off-by: Artem Bityutskiy <Artem.Bityutskiy@nokia.com>
Cc: Don Mullis <don.mullis@gmail.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Diffstat (limited to 'lib')
-rw-r--r-- | lib/list_sort.c | 27 |
1 files changed, 15 insertions, 12 deletions
diff --git a/lib/list_sort.c b/lib/list_sort.c index 679b3a060e7e..8f3c24415ae5 100644 --- a/lib/list_sort.c +++ b/lib/list_sort.c | |||
@@ -142,42 +142,45 @@ void list_sort(void *priv, struct list_head *head, | |||
142 | EXPORT_SYMBOL(list_sort); | 142 | EXPORT_SYMBOL(list_sort); |
143 | 143 | ||
144 | #ifdef CONFIG_TEST_LIST_SORT | 144 | #ifdef CONFIG_TEST_LIST_SORT |
145 | |||
146 | #include <linux/random.h> | ||
147 | |||
145 | struct debug_el { | 148 | struct debug_el { |
146 | struct list_head l_h; | 149 | struct list_head list; |
147 | int value; | 150 | int value; |
148 | unsigned serial; | 151 | unsigned serial; |
149 | }; | 152 | }; |
150 | 153 | ||
151 | static int cmp(void *priv, struct list_head *a, struct list_head *b) | 154 | static int cmp(void *priv, struct list_head *a, struct list_head *b) |
152 | { | 155 | { |
153 | return container_of(a, struct debug_el, l_h)->value | 156 | return container_of(a, struct debug_el, list)->value |
154 | - container_of(b, struct debug_el, l_h)->value; | 157 | - container_of(b, struct debug_el, list)->value; |
155 | } | 158 | } |
156 | 159 | ||
157 | /* | 160 | /* |
158 | * The pattern of set bits in the list length determines which cases | 161 | * The pattern of set bits in the list length determines which cases |
159 | * are hit in list_sort(). | 162 | * are hit in list_sort(). |
160 | */ | 163 | */ |
161 | #define LIST_SORT_TEST_LENGTH (512+128+2) /* not including head */ | 164 | #define TEST_LIST_LEN (512+128+2) /* not including head */ |
162 | 165 | ||
163 | static int __init list_sort_test(void) | 166 | static int __init list_sort_test(void) |
164 | { | 167 | { |
165 | int i, r = 1, count; | 168 | int i, count; |
166 | struct list_head *head = kmalloc(sizeof(*head), GFP_KERNEL); | 169 | struct list_head *head = kmalloc(sizeof(*head), GFP_KERNEL); |
167 | struct list_head *cur; | 170 | struct list_head *cur; |
168 | 171 | ||
169 | printk(KERN_DEBUG "testing list_sort()\n"); | 172 | printk(KERN_DEBUG "testing list_sort()\n"); |
170 | 173 | ||
171 | cur = head; | 174 | cur = head; |
172 | for (i = 0; i < LIST_SORT_TEST_LENGTH; i++) { | 175 | for (i = 0; i < TEST_LIST_LEN; i++) { |
173 | struct debug_el *el = kmalloc(sizeof(*el), GFP_KERNEL); | 176 | struct debug_el *el = kmalloc(sizeof(*el), GFP_KERNEL); |
174 | BUG_ON(!el); | 177 | BUG_ON(!el); |
175 | /* force some equivalencies */ | 178 | /* force some equivalencies */ |
176 | el->value = (r = (r * 725861) % 6599) % (LIST_SORT_TEST_LENGTH/3); | 179 | el->value = random32() % (TEST_LIST_LEN/3); |
177 | el->serial = i; | 180 | el->serial = i; |
178 | 181 | ||
179 | el->l_h.prev = cur; | 182 | el->list.prev = cur; |
180 | cur->next = &el->l_h; | 183 | cur->next = &el->list; |
181 | cur = cur->next; | 184 | cur = cur->next; |
182 | } | 185 | } |
183 | head->prev = cur; | 186 | head->prev = cur; |
@@ -186,7 +189,7 @@ static int __init list_sort_test(void) | |||
186 | 189 | ||
187 | count = 1; | 190 | count = 1; |
188 | for (cur = head->next; cur->next != head; cur = cur->next) { | 191 | for (cur = head->next; cur->next != head; cur = cur->next) { |
189 | struct debug_el *el = container_of(cur, struct debug_el, l_h); | 192 | struct debug_el *el = container_of(cur, struct debug_el, list); |
190 | int cmp_result = cmp(NULL, cur, cur->next); | 193 | int cmp_result = cmp(NULL, cur, cur->next); |
191 | if (cur->next->prev != cur) { | 194 | if (cur->next->prev != cur) { |
192 | printk(KERN_ERR "list_sort() returned " | 195 | printk(KERN_ERR "list_sort() returned " |
@@ -197,7 +200,7 @@ static int __init list_sort_test(void) | |||
197 | return 1; | 200 | return 1; |
198 | } else if (cmp_result == 0 && | 201 | } else if (cmp_result == 0 && |
199 | el->serial >= container_of(cur->next, | 202 | el->serial >= container_of(cur->next, |
200 | struct debug_el, l_h)->serial) { | 203 | struct debug_el, list)->serial) { |
201 | printk(KERN_ERR "list_sort() failed to preserve order " | 204 | printk(KERN_ERR "list_sort() failed to preserve order " |
202 | "of equivalent elements!\n"); | 205 | "of equivalent elements!\n"); |
203 | return 1; | 206 | return 1; |
@@ -206,7 +209,7 @@ static int __init list_sort_test(void) | |||
206 | count++; | 209 | count++; |
207 | } | 210 | } |
208 | kfree(cur); | 211 | kfree(cur); |
209 | if (count != LIST_SORT_TEST_LENGTH) { | 212 | if (count != TEST_LIST_LEN) { |
210 | printk(KERN_ERR "list_sort() returned list of " | 213 | printk(KERN_ERR "list_sort() returned list of " |
211 | "different length!\n"); | 214 | "different length!\n"); |
212 | return 1; | 215 | return 1; |