summaryrefslogtreecommitdiffstats
path: root/pintos-progos/tests/internal
diff options
context:
space:
mode:
Diffstat (limited to 'pintos-progos/tests/internal')
-rw-r--r--pintos-progos/tests/internal/list.c174
-rw-r--r--pintos-progos/tests/internal/stdio.c208
-rw-r--r--pintos-progos/tests/internal/stdlib.c114
3 files changed, 496 insertions, 0 deletions
diff --git a/pintos-progos/tests/internal/list.c b/pintos-progos/tests/internal/list.c
new file mode 100644
index 0000000..836c69e
--- /dev/null
+++ b/pintos-progos/tests/internal/list.c
@@ -0,0 +1,174 @@
1/* Test program for lib/kernel/list.c.
2
3 Attempts to test the list functionality that is not
4 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 <list.h>
13#include <random.h>
14#include <stdio.h>
15#include "threads/test.h"
16
17/* Maximum number of elements in a linked list that we will
18 test. */
19#define MAX_SIZE 64
20
21/* A linked list element. */
22struct value
23 {
24 struct list_elem elem; /* List element. */
25 int value; /* Item value. */
26 };
27
28static void shuffle (struct value[], size_t);
29static bool value_less (const struct list_elem *, const struct list_elem *,
30 void *);
31static void verify_list_fwd (struct list *, int size);
32static void verify_list_bkwd (struct list *, int size);
33
34/* Test the linked list implementation. */
35void
36test (void)
37{
38 int size;
39
40 printf ("testing various size lists:");
41 for (size = 0; size < MAX_SIZE; size++)
42 {
43 int repeat;
44
45 printf (" %d", size);
46 for (repeat = 0; repeat < 10; repeat++)
47 {
48 static struct value values[MAX_SIZE * 4];
49 struct list list;
50 struct list_elem *e;
51 int i, ofs;
52
53 /* Put values 0...SIZE in random order in VALUES. */
54 for (i = 0; i < size; i++)
55 values[i].value = i;
56 shuffle (values, size);
57
58 /* Assemble list. */
59 list_init (&list);
60 for (i = 0; i < size; i++)
61 list_push_back (&list, &values[i].elem);
62
63 /* Verify correct minimum and maximum elements. */
64 e = list_min (&list, value_less, NULL);
65 ASSERT (size ? list_entry (e, struct value, elem)->value == 0
66 : e == list_begin (&list));
67 e = list_max (&list, value_less, NULL);
68 ASSERT (size ? list_entry (e, struct value, elem)->value == size - 1
69 : e == list_begin (&list));
70
71 /* Sort and verify list. */
72 list_sort (&list, value_less, NULL);
73 verify_list_fwd (&list, size);
74
75 /* Reverse and verify list. */
76 list_reverse (&list);
77 verify_list_bkwd (&list, size);
78
79 /* Shuffle, insert using list_insert_ordered(),
80 and verify ordering. */
81 shuffle (values, size);
82 list_init (&list);
83 for (i = 0; i < size; i++)
84 list_insert_ordered (&list, &values[i].elem,
85 value_less, NULL);
86 verify_list_fwd (&list, size);
87
88 /* Duplicate some items, uniquify, and verify. */
89 ofs = size;
90 for (e = list_begin (&list); e != list_end (&list);
91 e = list_next (e))
92 {
93 struct value *v = list_entry (e, struct value, elem);
94 int copies = random_ulong () % 4;
95 while (copies-- > 0)
96 {
97 values[ofs].value = v->value;
98 list_insert (e, &values[ofs++].elem);
99 }
100 }
101 ASSERT ((size_t) ofs < sizeof values / sizeof *values);
102 list_unique (&list, NULL, value_less, NULL);
103 verify_list_fwd (&list, size);
104 }
105 }
106
107 printf (" done\n");
108 printf ("list: PASS\n");
109}
110
111/* Shuffles the CNT elements in ARRAY into random order. */
112static void
113shuffle (struct value *array, size_t cnt)
114{
115 size_t i;
116
117 for (i = 0; i < cnt; i++)
118 {
119 size_t j = i + random_ulong () % (cnt - i);
120 struct value t = array[j];
121 array[j] = array[i];
122 array[i] = t;
123 }
124}
125
126/* Returns true if value A is less than value B, false
127 otherwise. */
128static bool
129value_less (const struct list_elem *a_, const struct list_elem *b_,
130 void *aux UNUSED)
131{
132 const struct value *a = list_entry (a_, struct value, elem);
133 const struct value *b = list_entry (b_, struct value, elem);
134
135 return a->value < b->value;
136}
137
138/* Verifies that LIST contains the values 0...SIZE when traversed
139 in forward order. */
140static void
141verify_list_fwd (struct list *list, int size)
142{
143 struct list_elem *e;
144 int i;
145
146 for (i = 0, e = list_begin (list);
147 i < size && e != list_end (list);
148 i++, e = list_next (e))
149 {
150 struct value *v = list_entry (e, struct value, elem);
151 ASSERT (i == v->value);
152 }
153 ASSERT (i == size);
154 ASSERT (e == list_end (list));
155}
156
157/* Verifies that LIST contains the values 0...SIZE when traversed
158 in reverse order. */
159static void
160verify_list_bkwd (struct list *list, int size)
161{
162 struct list_elem *e;
163 int i;
164
165 for (i = 0, e = list_rbegin (list);
166 i < size && e != list_rend (list);
167 i++, e = list_prev (e))
168 {
169 struct value *v = list_entry (e, struct value, elem);
170 ASSERT (i == v->value);
171 }
172 ASSERT (i == size);
173 ASSERT (e == list_rend (list));
174}
diff --git a/pintos-progos/tests/internal/stdio.c b/pintos-progos/tests/internal/stdio.c
new file mode 100644
index 0000000..fb60cda
--- /dev/null
+++ b/pintos-progos/tests/internal/stdio.c
@@ -0,0 +1,208 @@
1/* Test program for printf() in lib/stdio.c.
2
3 Attempts to test printf() functionality that is not
4 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 <limits.h>
12#include <stdarg.h>
13#include <stddef.h>
14#include <stdlib.h>
15#include <stdio.h>
16#include <string.h>
17#include "threads/test.h"
18
19/* Number of failures so far. */
20static int failure_cnt;
21
22static void
23checkf (const char *expect, const char *format, ...)
24{
25 char output[128];
26 va_list args;
27
28 printf ("\"%s\" -> \"%s\": ", format, expect);
29
30 va_start (args, format);
31 vsnprintf (output, sizeof output, format, args);
32 va_end (args);
33
34 if (strcmp (expect, output))
35 {
36 printf ("\nFAIL: actual output \"%s\"\n", output);
37 failure_cnt++;
38 }
39 else
40 printf ("okay\n");
41}
42
43/* Test printf() implementation. */
44void
45test (void)
46{
47 printf ("Testing formats:");
48
49 /* Check that commas show up in the right places, for positive
50 numbers. */
51 checkf ("1", "%'d", 1);
52 checkf ("12", "%'d", 12);
53 checkf ("123", "%'d", 123);
54 checkf ("1,234", "%'d", 1234);
55 checkf ("12,345", "%'d", 12345);
56 checkf ("123,456", "%'ld", 123456L);
57 checkf ("1,234,567", "%'ld", 1234567L);
58 checkf ("12,345,678", "%'ld", 12345678L);
59 checkf ("123,456,789", "%'ld", 123456789L);
60 checkf ("1,234,567,890", "%'ld", 1234567890L);
61 checkf ("12,345,678,901", "%'lld", 12345678901LL);
62 checkf ("123,456,789,012", "%'lld", 123456789012LL);
63 checkf ("1,234,567,890,123", "%'lld", 1234567890123LL);
64 checkf ("12,345,678,901,234", "%'lld", 12345678901234LL);
65 checkf ("123,456,789,012,345", "%'lld", 123456789012345LL);
66 checkf ("1,234,567,890,123,456", "%'lld", 1234567890123456LL);
67 checkf ("12,345,678,901,234,567", "%'lld", 12345678901234567LL);
68 checkf ("123,456,789,012,345,678", "%'lld", 123456789012345678LL);
69 checkf ("1,234,567,890,123,456,789", "%'lld", 1234567890123456789LL);
70
71 /* Check that commas show up in the right places, for positive
72 numbers. */
73 checkf ("-1", "%'d", -1);
74 checkf ("-12", "%'d", -12);
75 checkf ("-123", "%'d", -123);
76 checkf ("-1,234", "%'d", -1234);
77 checkf ("-12,345", "%'d", -12345);
78 checkf ("-123,456", "%'ld", -123456L);
79 checkf ("-1,234,567", "%'ld", -1234567L);
80 checkf ("-12,345,678", "%'ld", -12345678L);
81 checkf ("-123,456,789", "%'ld", -123456789L);
82 checkf ("-1,234,567,890", "%'ld", -1234567890L);
83 checkf ("-12,345,678,901", "%'lld", -12345678901LL);
84 checkf ("-123,456,789,012", "%'lld", -123456789012LL);
85 checkf ("-1,234,567,890,123", "%'lld", -1234567890123LL);
86 checkf ("-12,345,678,901,234", "%'lld", -12345678901234LL);
87 checkf ("-123,456,789,012,345", "%'lld", -123456789012345LL);
88 checkf ("-1,234,567,890,123,456", "%'lld", -1234567890123456LL);
89 checkf ("-12,345,678,901,234,567", "%'lld", -12345678901234567LL);
90 checkf ("-123,456,789,012,345,678", "%'lld", -123456789012345678LL);
91 checkf ("-1,234,567,890,123,456,789", "%'lld", -1234567890123456789LL);
92
93 /* Check signed integer conversions. */
94 checkf (" 0", "%5d", 0);
95 checkf ("0 ", "%-5d", 0);
96 checkf (" +0", "%+5d", 0);
97 checkf ("+0 ", "%+-5d", 0);
98 checkf (" 0", "% 5d", 0);
99 checkf ("00000", "%05d", 0);
100 checkf (" ", "%5.0d", 0);
101 checkf (" 00", "%5.2d", 0);
102 checkf ("0", "%d", 0);
103
104 checkf (" 1", "%5d", 1);
105 checkf ("1 ", "%-5d", 1);
106 checkf (" +1", "%+5d", 1);
107 checkf ("+1 ", "%+-5d", 1);
108 checkf (" 1", "% 5d", 1);
109 checkf ("00001", "%05d", 1);
110 checkf (" 1", "%5.0d", 1);
111 checkf (" 01", "%5.2d", 1);
112 checkf ("1", "%d", 1);
113
114 checkf (" -1", "%5d", -1);
115 checkf ("-1 ", "%-5d", -1);
116 checkf (" -1", "%+5d", -1);
117 checkf ("-1 ", "%+-5d", -1);
118 checkf (" -1", "% 5d", -1);
119 checkf ("-0001", "%05d", -1);
120 checkf (" -1", "%5.0d", -1);
121 checkf (" -01", "%5.2d", -1);
122 checkf ("-1", "%d", -1);
123
124 checkf ("12345", "%5d", 12345);
125 checkf ("12345", "%-5d", 12345);
126 checkf ("+12345", "%+5d", 12345);
127 checkf ("+12345", "%+-5d", 12345);
128 checkf (" 12345", "% 5d", 12345);
129 checkf ("12345", "%05d", 12345);
130 checkf ("12345", "%5.0d", 12345);
131 checkf ("12345", "%5.2d", 12345);
132 checkf ("12345", "%d", 12345);
133
134 checkf ("123456", "%5d", 123456);
135 checkf ("123456", "%-5d", 123456);
136 checkf ("+123456", "%+5d", 123456);
137 checkf ("+123456", "%+-5d", 123456);
138 checkf (" 123456", "% 5d", 123456);
139 checkf ("123456", "%05d", 123456);
140 checkf ("123456", "%5.0d", 123456);
141 checkf ("123456", "%5.2d", 123456);
142 checkf ("123456", "%d", 123456);
143
144 /* Check unsigned integer conversions. */
145 checkf (" 0", "%5u", 0);
146 checkf (" 0", "%5o", 0);
147 checkf (" 0", "%5x", 0);
148 checkf (" 0", "%5X", 0);
149 checkf (" 0", "%#5o", 0);
150 checkf (" 0", "%#5x", 0);
151 checkf (" 0", "%#5X", 0);
152 checkf (" 00000000", "%#10.8x", 0);
153
154 checkf (" 1", "%5u", 1);
155 checkf (" 1", "%5o", 1);
156 checkf (" 1", "%5x", 1);
157 checkf (" 1", "%5X", 1);
158 checkf (" 01", "%#5o", 1);
159 checkf (" 0x1", "%#5x", 1);
160 checkf (" 0X1", "%#5X", 1);
161 checkf ("0x00000001", "%#10.8x", 1);
162
163 checkf ("123456", "%5u", 123456);
164 checkf ("361100", "%5o", 123456);
165 checkf ("1e240", "%5x", 123456);
166 checkf ("1E240", "%5X", 123456);
167 checkf ("0361100", "%#5o", 123456);
168 checkf ("0x1e240", "%#5x", 123456);
169 checkf ("0X1E240", "%#5X", 123456);
170 checkf ("0x0001e240", "%#10.8x", 123456);
171
172 /* Character and string conversions. */
173 checkf ("foobar", "%c%c%c%c%c%c", 'f', 'o', 'o', 'b', 'a', 'r');
174 checkf (" left-right ", "%6s%s%-7s", "left", "-", "right");
175 checkf ("trim", "%.4s", "trimoff");
176 checkf ("%%", "%%%%");
177
178 /* From Cristian Cadar's automatic test case generator. */
179 checkf (" abcdefgh", "%9s", "abcdefgh");
180 checkf ("36657730000", "%- o", (unsigned) 036657730000);
181 checkf ("4139757568", "%- u", (unsigned) 4139757568UL);
182 checkf ("f6bfb000", "%- x", (unsigned) 0xf6bfb000);
183 checkf ("36657730000", "%-to", (ptrdiff_t) 036657730000);
184 checkf ("4139757568", "%-tu", (ptrdiff_t) 4139757568UL);
185 checkf ("-155209728", "%-zi", (size_t) -155209728);
186 checkf ("-155209728", "%-zd", (size_t) -155209728);
187 checkf ("036657730000", "%+#o", (unsigned) 036657730000);
188 checkf ("0xf6bfb000", "%+#x", (unsigned) 0xf6bfb000);
189 checkf ("-155209728", "% zi", (size_t) -155209728);
190 checkf ("-155209728", "% zd", (size_t) -155209728);
191 checkf ("4139757568", "% tu", (ptrdiff_t) 4139757568UL);
192 checkf ("036657730000", "% #o", (unsigned) 036657730000);
193 checkf ("0xf6bfb000", "% #x", (unsigned) 0xf6bfb000);
194 checkf ("0xf6bfb000", "%# x", (unsigned) 0xf6bfb000);
195 checkf ("-155209728", "%#zd", (size_t) -155209728);
196 checkf ("-155209728", "%0zi", (size_t) -155209728);
197 checkf ("4,139,757,568", "%'tu", (ptrdiff_t) 4139757568UL);
198 checkf ("-155,209,728", "%-'d", -155209728);
199 checkf ("-155209728", "%.zi", (size_t) -155209728);
200 checkf ("-155209728", "%zi", (size_t) -155209728);
201 checkf ("-155209728", "%zd", (size_t) -155209728);
202 checkf ("-155209728", "%+zi", (size_t) -155209728);
203
204 if (failure_cnt == 0)
205 printf ("\nstdio: PASS\n");
206 else
207 printf ("\nstdio: FAIL: %d tests failed\n", failure_cnt);
208}
diff --git a/pintos-progos/tests/internal/stdlib.c b/pintos-progos/tests/internal/stdlib.c
new file mode 100644
index 0000000..ad0f0f9
--- /dev/null
+++ b/pintos-progos/tests/internal/stdlib.c
@@ -0,0 +1,114 @@
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}