PKUOS - Pintos
Pintos source browser for PKU Operating System course
stdlib.c
Go to the documentation of this file.
1/** Test program for sorting and searching in lib/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
21static void shuffle (int[], size_t);
22static int compare_ints (const void *, const void *);
23static void verify_order (const int[], size_t);
24static void verify_bsearch (const int[], size_t);
25
26/** Test sorting and searching implementations. */
27void
28test (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 printf ("stdlib: PASS\n");
57}
58
59/** Shuffles the CNT elements in ARRAY into random order. */
60static void
61shuffle (int *array, size_t cnt)
62{
63 size_t i;
64
65 for (i = 0; i < cnt; i++)
66 {
67 size_t j = i + random_ulong () % (cnt - i);
68 int t = array[j];
69 array[j] = array[i];
70 array[i] = t;
71 }
72}
73
74/** Returns 1 if *A is greater than *B,
75 0 if *A equals *B,
76 -1 if *A is less than *B. */
77static int
78compare_ints (const void *a_, const void *b_)
79{
80 const int *a = a_;
81 const int *b = b_;
82
83 return *a < *b ? -1 : *a > *b;
84}
85
86/** Verifies that ARRAY contains the CNT ints 0...CNT-1. */
87static void
88verify_order (const int *array, size_t cnt)
89{
90 int i;
91
92 for (i = 0; (size_t) i < cnt; i++)
93 ASSERT (array[i] == i);
94}
95
96/** Checks that bsearch() works properly in ARRAY. ARRAY must
97 contain the values 0...CNT-1. */
98static void
99verify_bsearch (const int *array, size_t cnt)
100{
101 int not_in_array[] = {0, -1, INT_MAX, MAX_CNT, MAX_CNT + 1, MAX_CNT * 2};
102 int i;
103
104 /* Check that all the values in the array are found properly. */
105 for (i = 0; (size_t) i < cnt; i++)
106 ASSERT (bsearch (&i, array, cnt, sizeof *array, compare_ints)
107 == array + i);
108
109 /* Check that some values not in the array are not found. */
110 not_in_array[0] = cnt;
111 for (i = 0; (size_t) i < sizeof not_in_array / sizeof *not_in_array; i++)
112 ASSERT (bsearch (&not_in_array[i], array, cnt, sizeof *array, compare_ints)
113 == NULL);
114}
#define ASSERT(CONDITION)
This is outside the header guard so that debug.h may be included multiple times with different settin...
Definition: debug.h:31
int printf(const char *format,...)
Writes formatted output to the console.
Definition: stdio.c:79
void * bsearch(const void *key, const void *array, size_t cnt, size_t size, int(*compare)(const void *, const void *))
Searches ARRAY, which contains CNT elements of SIZE bytes each, for the given KEY.
Definition: stdlib.c:166
void qsort(void *array, size_t cnt, size_t size, int(*compare)(const void *, const void *))
Sorts ARRAY, which contains CNT elements of SIZE bytes each, using COMPARE.
Definition: stdlib.c:58
#define INT_MAX
Definition: limits.h:22
unsigned long random_ulong(void)
Returns a pseudo-random unsigned long.
Definition: random.c:78
#define NULL
Definition: stddef.h:4
__SIZE_TYPE__ size_t
lib/stddef.h
Definition: stddef.h:10
static void verify_bsearch(const int[], size_t)
void test(void)
Test sorting and searching implementations.
Definition: stdlib.c:28
static void shuffle(int[], size_t)
#define MAX_CNT
Test program for sorting and searching in lib/stdlib.c.
Definition: stdlib.c:19
static void verify_order(const int[], size_t)
static int compare_ints(const void *, const void *)
Returns 1 if *A is greater than *B, 0 if *A equals *B, -1 if *A is less than *B.
Definition: stdlib.c:78