summaryrefslogtreecommitdiff
path: root/lib/ccan/time/test
diff options
context:
space:
mode:
authorRusty Russell <rusty@rustcorp.com.au>2012-02-22 14:59:32 +1030
committerAmitay Isaacs <amitay@gmail.com>2012-03-07 13:16:16 +1100
commit361f3ea9ee577c5a3e2fed687a0b417b257c31de (patch)
tree6d356c3aa64317c609ff4e208be76e18996a55f8 /lib/ccan/time/test
parent4f5412dda687c3ff76b426842bf284d01d56a997 (diff)
downloadsamba-361f3ea9ee577c5a3e2fed687a0b417b257c31de.tar.gz
samba-361f3ea9ee577c5a3e2fed687a0b417b257c31de.tar.bz2
samba-361f3ea9ee577c5a3e2fed687a0b417b257c31de.zip
lib/ccan: import failtest and required ccan modules for TDB2 unit tests.
New modules: failtest, list, time, read_write_all and tlist. Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
Diffstat (limited to 'lib/ccan/time/test')
-rw-r--r--lib/ccan/time/test/run.c111
1 files changed, 111 insertions, 0 deletions
diff --git a/lib/ccan/time/test/run.c b/lib/ccan/time/test/run.c
new file mode 100644
index 0000000000..80391694cd
--- /dev/null
+++ b/lib/ccan/time/test/run.c
@@ -0,0 +1,111 @@
+#include <ccan/time/time.h>
+#include <ccan/time/time.c>
+#include <ccan/tap/tap.h>
+
+int main(void)
+{
+ struct timeval t1, t2, t3, zero = { 0, 0 };
+
+ plan_tests(46);
+
+ /* Test time_now */
+ t1 = time_now();
+ t2 = time_now();
+
+ /* Test time_sub. */
+ t3 = time_sub(t2, t1);
+ ok1(t3.tv_sec > 0 || t3.tv_usec >= 0);
+ t3 = time_sub(t2, t2);
+ ok1(t3.tv_sec == 0 && t3.tv_usec == 0);
+ t3 = time_sub(t1, t1);
+ ok1(t3.tv_sec == 0 && t3.tv_usec == 0);
+
+ /* Test time_eq */
+ ok1(time_eq(t1, t1));
+ ok1(time_eq(t2, t2));
+ ok1(!time_eq(t1, t3));
+ ok1(!time_eq(t2, t3));
+
+ /* Make sure t2 > t1. */
+ t3.tv_sec = 0;
+ t3.tv_usec = 1;
+ t2 = time_add(t2, t3);
+
+ /* Test time_less and time_greater. */
+ ok1(!time_eq(t1, t2));
+ ok1(!time_greater(t1, t2));
+ ok1(time_less(t1, t2));
+ ok1(time_greater(t2, t1));
+ ok1(!time_less(t2, t1));
+ t3.tv_sec = 0;
+ t3.tv_usec = 999999;
+ t2 = time_add(t2, t3);
+ ok1(!time_eq(t1, t2));
+ ok1(!time_greater(t1, t2));
+ ok1(time_less(t1, t2));
+ ok1(time_greater(t2, t1));
+ ok1(!time_less(t2, t1));
+
+ t3 = time_sub(t2, zero);
+ ok1(time_eq(t3, t2));
+ t3 = time_sub(t2, t2);
+ ok1(time_eq(t3, zero));
+
+ /* time_from_msec / time_to_msec */
+ t3 = time_from_msec(500);
+ ok1(t3.tv_sec == 0);
+ ok1(t3.tv_usec == 500000);
+ ok1(time_to_msec(t3) == 500);
+
+ t3 = time_from_msec(1000);
+ ok1(t3.tv_sec == 1);
+ ok1(t3.tv_usec == 0);
+ ok1(time_to_msec(t3) == 1000);
+
+ t3 = time_from_msec(1500);
+ ok1(t3.tv_sec == 1);
+ ok1(t3.tv_usec == 500000);
+ ok1(time_to_msec(t3) == 1500);
+
+ /* time_from_usec */
+ t3 = time_from_usec(500000);
+ ok1(t3.tv_sec == 0);
+ ok1(t3.tv_usec == 500000);
+ ok1(time_to_usec(t3) == 500000);
+
+ t3 = time_from_usec(1000000);
+ ok1(t3.tv_sec == 1);
+ ok1(t3.tv_usec == 0);
+ ok1(time_to_usec(t3) == 1000000);
+
+ t3 = time_from_usec(1500000);
+ ok1(t3.tv_sec == 1);
+ ok1(t3.tv_usec == 500000);
+ ok1(time_to_usec(t3) == 1500000);
+
+ /* Test wrapunder */
+ t3 = time_sub(time_sub(t2, time_from_msec(500)), time_from_msec(500));
+ ok1(t3.tv_sec == t2.tv_sec - 1);
+ ok1(t3.tv_usec == t2.tv_usec);
+
+ /* time_divide and time_multiply */
+ t1.tv_usec = 100;
+ t1.tv_sec = 100;
+
+ t3 = time_divide(t1, 2);
+ ok1(t3.tv_sec == 50);
+ ok1(t3.tv_usec == 50);
+
+ t3 = time_divide(t1, 100);
+ ok1(t3.tv_sec == 1);
+ ok1(t3.tv_usec == 1);
+
+ t3 = time_multiply(t3, 100);
+ ok1(time_eq(t3, t1));
+
+ t3 = time_divide(t1, 200);
+ ok1(t3.tv_sec == 0);
+ ok1(t3.tv_usec == 500000);
+
+ return exit_status();
+}