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