Add alarm test into utests
authorKevin Klues <klueska@cs.berkeley.edu>
Fri, 30 May 2014 23:01:26 +0000 (16:01 -0700)
committerKevin Klues <klueska@cs.berkeley.edu>
Fri, 30 May 2014 23:01:26 +0000 (16:01 -0700)
user/utest/alarm.c [new file with mode: 0644]

diff --git a/user/utest/alarm.c b/user/utest/alarm.c
new file mode 100644 (file)
index 0000000..8fcbf61
--- /dev/null
@@ -0,0 +1,49 @@
+#include <utest.h>
+#include <alarm.h>
+
+TEST_SUITE("ALARMS");
+
+/* <--- Begin definition of test cases ---> */
+
+bool test_alarm(void) {
+       const int INTERVAL = 10000;
+       const int ITERATIONS = 100;
+       void alarm_handler(struct alarm_waiter *waiter)
+       {
+               __sync_fetch_and_add((int*)waiter->data, 1);
+               set_awaiter_inc(waiter, INTERVAL);
+               __set_alarm(waiter);
+       }
+
+       int count = 0;
+       uint64_t now, then;
+       struct alarm_waiter waiter;
+       init_awaiter(&waiter, alarm_handler);
+       waiter.data = &count;
+       set_awaiter_rel(&waiter, INTERVAL);
+       now = tsc2usec(read_tsc());
+       set_alarm(&waiter);
+       while(count < ITERATIONS)
+               cpu_relax();
+       then = tsc2usec(read_tsc());
+       unset_alarm(&waiter);
+       UT_ASSERT_M("Alarms finished too soon", then > (now + INTERVAL*count));
+       UT_ASSERT_M("Alarms finished too late", then < (now + INTERVAL*(count+1)));
+       return true;
+}
+
+/* <--- End definition of test cases ---> */
+
+struct utest utests[] = {
+       UTEST_REG(alarm),
+};
+int num_utests = sizeof(utests) / sizeof(struct utest);
+
+int main(int argc, char *argv[]) {
+       // Run test suite passing it all the args as whitelist of what tests to run.
+       char **whitelist = &argv[1];
+       int whitelist_len = argc - 1;
+       RUN_TEST_SUITE(utests, num_utests, whitelist, whitelist_len);
+}
+
+