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