fc15481ed3772e31014427027e9c2365e2b7801a
[pintos-anon] / src / tests / threads / stdlib.c
1 /* Test program for sorting and searching in lib/kernel/stdlib.c.
2
3    Attempts to test the sorting and searching functionality that
4    is not sufficiently tested elsewhere in Pintos.
5
6    This is not a test we will run on your submitted projects.
7    It is here for completeness.
8 */
9
10 #undef NDEBUG
11 #include <debug.h>
12 #include <limits.h>
13 #include <random.h>
14 #include <stdlib.h>
15 #include <stdio.h>
16 #include "threads/test.h"
17
18 /* Maximum number of elements in an array that we will test. */
19 #define MAX_CNT 4096
20
21 static void shuffle (int[], size_t);
22 static int compare_ints (const void *, const void *);
23 static void verify_order (const int[], size_t);
24 static void verify_bsearch (const int[], size_t);
25
26 /* Test sorting and searching implementations. */
27 void
28 test (void) 
29 {
30   int cnt;
31
32   printf ("testing various size arrays:");
33   for (cnt = 0; cnt < MAX_CNT; cnt = cnt * 4 / 3 + 1)
34     {
35       int repeat;
36
37       printf (" %zu", cnt);
38       for (repeat = 0; repeat < 10; repeat++) 
39         {
40           static int values[MAX_CNT];
41           int i;
42
43           /* Put values 0...CNT in random order in VALUES. */
44           for (i = 0; i < cnt; i++)
45             values[i] = i;
46           shuffle (values, cnt);
47   
48           /* Sort VALUES, then verify ordering. */
49           qsort (values, cnt, sizeof *values, compare_ints);
50           verify_order (values, cnt);
51           verify_bsearch (values, cnt);
52         }
53     }
54   
55   printf (" done\n");
56 }
57
58 /* Shuffles the CNT elements in ARRAY into random order. */
59 static void
60 shuffle (int *array, size_t cnt) 
61 {
62   size_t i;
63
64   for (i = 0; i < cnt; i++)
65     {
66       size_t j = i + random_ulong () % (cnt - i);
67       int t = array[j];
68       array[j] = array[i];
69       array[i] = t;
70     }
71 }
72
73 /* Returns 1 if *A is greater than *B,
74    0 if *A equals *B,
75    -1 if *A is less than *B. */
76 static int
77 compare_ints (const void *a_, const void *b_) 
78 {
79   const int *a = a_;
80   const int *b = b_;
81
82   return *a < *b ? -1 : *a > *b;
83 }
84
85 /* Verifies that ARRAY contains the CNT ints 0...CNT-1. */
86 static void
87 verify_order (const int *array, size_t cnt) 
88 {
89   int i;
90
91   for (i = 0; (size_t) i < cnt; i++) 
92     ASSERT (array[i] == i);
93 }
94
95 /* Checks that bsearch() works properly in ARRAY.  ARRAY must
96    contain the values 0...CNT-1. */
97 static void
98 verify_bsearch (const int *array, size_t cnt) 
99 {
100   int not_in_array[] = {0, -1, INT_MAX, MAX_CNT, MAX_CNT + 1, MAX_CNT * 2};
101   int i;
102
103   /* Check that all the values in the array are found properly. */
104   for (i = 0; (size_t) i < cnt; i++) 
105     ASSERT (bsearch (&i, array, cnt, sizeof *array, compare_ints)
106             == array + i);
107
108   /* Check that some values not in the array are not found. */
109   not_in_array[0] = cnt;
110   for (i = 0; (size_t) i < sizeof not_in_array / sizeof *not_in_array; i++) 
111     ASSERT (bsearch (&not_in_array[i], array, cnt, sizeof *array, compare_ints)
112             == NULL);
113 }