summaryrefslogtreecommitdiffstats
path: root/src/tests/internal/stdlib.c
blob: ad0f0f9a82816cd991300ed97f6740a94f7a3386 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/* Test program for sorting and searching in lib/stdlib.c.

   Attempts to test the sorting and searching functionality that
   is not sufficiently tested elsewhere in Pintos.

   This is not a test we will run on your submitted projects.
   It is here for completeness.
*/

#undef NDEBUG
#include <debug.h>
#include <limits.h>
#include <random.h>
#include <stdlib.h>
#include <stdio.h>
#include "threads/test.h"

/* Maximum number of elements in an array that we will test. */
#define MAX_CNT 4096

static void shuffle (int[], size_t);
static int compare_ints (const void *, const void *);
static void verify_order (const int[], size_t);
static void verify_bsearch (const int[], size_t);

/* Test sorting and searching implementations. */
void
test (void) 
{
  int cnt;

  printf ("testing various size arrays:");
  for (cnt = 0; cnt < MAX_CNT; cnt = cnt * 4 / 3 + 1)
    {
      int repeat;

      printf (" %zu", cnt);
      for (repeat = 0; repeat < 10; repeat++) 
        {
          static int values[MAX_CNT];
          int i;

          /* Put values 0...CNT in random order in VALUES. */
          for (i = 0; i < cnt; i++)
            values[i] = i;
          shuffle (values, cnt);
  
          /* Sort VALUES, then verify ordering. */
          qsort (values, cnt, sizeof *values, compare_ints);
          verify_order (values, cnt);
          verify_bsearch (values, cnt);
        }
    }
  
  printf (" done\n");
  printf ("stdlib: PASS\n");
}

/* Shuffles the CNT elements in ARRAY into random order. */
static void
shuffle (int *array, size_t cnt) 
{
  size_t i;

  for (i = 0; i < cnt; i++)
    {
      size_t j = i + random_ulong () % (cnt - i);
      int t = array[j];
      array[j] = array[i];
      array[i] = t;
    }
}

/* Returns 1 if *A is greater than *B,
   0 if *A equals *B,
   -1 if *A is less than *B. */
static int
compare_ints (const void *a_, const void *b_) 
{
  const int *a = a_;
  const int *b = b_;

  return *a < *b ? -1 : *a > *b;
}

/* Verifies that ARRAY contains the CNT ints 0...CNT-1. */
static void
verify_order (const int *array, size_t cnt) 
{
  int i;

  for (i = 0; (size_t) i < cnt; i++) 
    ASSERT (array[i] == i);
}

/* Checks that bsearch() works properly in ARRAY.  ARRAY must
   contain the values 0...CNT-1. */
static void
verify_bsearch (const int *array, size_t cnt) 
{
  int not_in_array[] = {0, -1, INT_MAX, MAX_CNT, MAX_CNT + 1, MAX_CNT * 2};
  int i;

  /* Check that all the values in the array are found properly. */
  for (i = 0; (size_t) i < cnt; i++) 
    ASSERT (bsearch (&i, array, cnt, sizeof *array, compare_ints)
            == array + i);

  /* Check that some values not in the array are not found. */
  not_in_array[0] = cnt;
  for (i = 0; (size_t) i < sizeof not_in_array / sizeof *not_in_array; i++) 
    ASSERT (bsearch (&not_in_array[i], array, cnt, sizeof *array, compare_ints)
            == NULL);
}