Make tests public. Rewrite most tests. Add tests.
[pintos-anon] / src / tests / threads / tests.c
1 #include "tests/threads/tests.h"
2 #include <debug.h>
3 #include <string.h>
4 #include <stdio.h>
5
6 struct test 
7   {
8     const char *name;
9     test_func *function;
10   };
11
12 static const struct test tests[] = 
13   {
14     {"alarm-single", test_alarm_single},
15     {"alarm-multiple", test_alarm_multiple},
16     {"alarm-priority", test_alarm_priority},
17     {"alarm-zero", test_alarm_zero},
18     {"alarm-negative", test_alarm_negative},
19     {"priority-change", test_priority_change},
20     {"priority-donate-one", test_priority_donate_one},
21     {"priority-donate-multiple", test_priority_donate_multiple},
22     {"priority-donate-nest", test_priority_donate_nest},
23     {"priority-fifo", test_priority_fifo},
24     {"priority-preempt", test_priority_preempt},
25     {"priority-sema", test_priority_sema},
26     {"priority-condvar", test_priority_condvar},
27     {"mlfqs-load-1", test_mlfqs_load_1},
28     {"mlfqs-load-60", test_mlfqs_load_60},
29     {"mlfqs-load-avg", test_mlfqs_load_avg},
30     {"mlfqs-recent-1", test_mlfqs_recent_1},
31     {"mlfqs-fair-2", test_mlfqs_fair_2},
32     {"mlfqs-fair-20", test_mlfqs_fair_20},
33     {"mlfqs-nice-2", test_mlfqs_nice_2},
34     {"mlfqs-nice-10", test_mlfqs_nice_10},
35   };
36
37 static const char *test_name;
38
39 /* Runs the test named NAME. */
40 void
41 run_test (const char *name) 
42 {
43   const struct test *t;
44
45   for (t = tests; t < tests + sizeof tests / sizeof *tests; t++)
46     if (!strcmp (name, t->name))
47       {
48         test_name = name;
49         msg ("begin");
50         t->function ();
51         msg ("end");
52         return;
53       }
54   PANIC ("no test named \"%s\"", name);
55 }
56
57 /* Prints FORMAT as if with printf(),
58    prefixing the output by the name of the test
59    and following it with a new-line character. */
60 void
61 msg (const char *format, ...) 
62 {
63   va_list args;
64   
65   printf ("(%s) ", test_name);
66   va_start (args, format);
67   vprintf (format, args);
68   va_end (args);
69   putchar ('\n');
70 }
71
72 /* Prints failure message FORMAT as if with printf(),
73    prefixing the output by the name of the test and FAIL:
74    and following it with a new-line character,
75    and then panics the kernel. */
76 void
77 fail (const char *format, ...) 
78 {
79   va_list args;
80   
81   printf ("(%s) FAIL: ", test_name);
82   va_start (args, format);
83   vprintf (format, args);
84   va_end (args);
85   putchar ('\n');
86
87   PANIC ("test failed");
88 }
89
90 /* Prints a message indicating the current test passed. */
91 void
92 pass (void) 
93 {
94   printf ("(%s) PASS\n", test_name);
95 }
96