aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--meson.build9
-rw-r--r--test/main.c4
-rw-r--r--test/meson.build3
-rw-r--r--test/time.c227
-rw-r--r--test/time_test.h28
-rw-r--r--test/time_test_private.h36
6 files changed, 305 insertions, 2 deletions
diff --git a/meson.build b/meson.build
index af2e1b3..0e9f476 100644
--- a/meson.build
+++ b/meson.build
@@ -39,4 +39,13 @@ if get_option('tests')
test('compare doubles', test_e, args : ['float', 'comparedouble'])
test('diagnostic', test_e, args : ['diagnostic', 'none'])
test('gpio', test_e, args : ['gpio', 'none'])
+ test('time add', test_e, args : ['time', 'add'])
+ test('time subtract', test_e, args : ['time', 'sub'])
+ test('time normalize', test_e, args : ['time', 'normalize'])
+ test('time compare less', test_e, args : ['time', 'cmpless'])
+ test('time compare more', test_e, args : ['time', 'cmpmore'])
+ test('time compare less or equal', test_e, args : ['time', 'cmplessequal'])
+ test('time compare more or equal', test_e, args : ['time', 'cmpmoreequal'])
+ test('time compare equal', test_e, args : ['time', 'cmpequal'])
+ test('time compare nonequal', test_e, args : ['time', 'cmpnonequal'])
endif
diff --git a/test/main.c b/test/main.c
index c0217eb..c87c24c 100644
--- a/test/main.c
+++ b/test/main.c
@@ -22,6 +22,7 @@
#include "float_test.h"
#include "diagnostic_test.h"
#include "gpio_test.h"
+#include "time_test.h"
/* TODO: allow individual tests to be selected */
@@ -30,7 +31,8 @@ int main(int argc, char **argv)
static const struct section sections[] = {
{"float", float_test},
{"diagnostic", diagnostic_test},
- {"gpio", gpio_test} };
+ {"gpio", gpio_test},
+ {"time", time_test} };
size_t i;
if (argc != 3) {
diff --git a/test/meson.build b/test/meson.build
index 3cc925b..b23a438 100644
--- a/test/meson.build
+++ b/test/meson.build
@@ -3,7 +3,8 @@ test_filenames = [
'test.c',
'float.c',
'diagnostic.c',
- 'gpio.c'
+ 'gpio.c',
+ 'time.c'
]
test_sources = files(test_filenames)
diff --git a/test/time.c b/test/time.c
new file mode 100644
index 0000000..fe8a195
--- /dev/null
+++ b/test/time.c
@@ -0,0 +1,227 @@
+/*
+ * The Rin Library – library "conformance" tests
+ *
+ * Copyright (C) 2017 Gediminas Jakutis
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; version 2.1
+ * of the License.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
+ */
+
+#include "test.h"
+#include "time_test_private.h"
+
+int time_test(char *testname)
+{
+ static const struct test tests[] = {
+ {"add", rin_time_add_test},
+ {"sub", rin_time_sub_test},
+ {"normalize", rin_time_normalize_test},
+ {"cmpless", rin_time_cmpless_test},
+ {"cmpmore", rin_time_cmpmore_test},
+ {"cmplessequal", rin_time_cmplessequal_test},
+ {"cmpmoreequal", rin_time_cmpmoreequal_test},
+ {"cmpequal",rin_time_cmpequal_test},
+ {"cmpnonequal", rin_time_cmpnonequal_test} };
+ size_t i;
+
+ for (i = 0; i < arrlen(tests); ++i) {
+ if (!strcmp(testname, tests[i].name)) {
+ return tests[i].testfunc();
+ }
+ }
+
+ return EXIT_FAILURE;
+}
+static int rin_time_add_test(void)
+{
+ int ret;
+ size_t i;
+ static const struct timespec in_a[] = { {187, 12289}, {0, 999999999}, {0, 999999998}, {0, 0} };
+ static const struct timespec in_b[] = { {23, 17711}, {0, 21}, {999999999, 2}, {0, 0} };
+ static const struct timespec expected[] = { {210, 30000}, {1, 20}, {1000000000, 0}, {0, 0} };
+ struct timespec tmp;
+
+ for (i = 0; i < arrlen(expected); ++i) {
+ tmp = rin_time_add(in_a + i, in_b + i);
+ ret = ok(tmp.tv_sec != expected[i].tv_sec || tmp.tv_nsec != expected[i].tv_nsec,
+ "%s: expected: %li %li, got: %li %li",
+ __func__,
+ expected[i].tv_sec,
+ expected[i].tv_nsec,
+ tmp.tv_sec,
+ tmp.tv_nsec);
+ }
+
+ return ret;
+}
+
+static int rin_time_sub_test(void)
+{
+ int ret;
+ size_t i;
+ static const struct timespec in_a[] = { {187, 17289}, {0, 999999999}, {0, 99}, {0, 0} };
+ static const struct timespec in_b[] = { {27, 12711}, {0, 1}, {999999999, 999999999}, {0, 0} };
+ static const struct timespec expected[] = { {160, 4578}, {0, 999999998}, {-1000000000, 100}, {0, 0} };
+ struct timespec tmp;
+
+ for (i = 0; i < arrlen(expected); ++i) {
+ tmp = rin_time_sub(in_a + i, in_b + i);
+ ret = ok(tmp.tv_sec != expected[i].tv_sec || tmp.tv_nsec != expected[i].tv_nsec,
+ "%s: expected: %li %li, got: %li %li",
+ __func__,
+ expected[i].tv_sec,
+ expected[i].tv_nsec,
+ tmp.tv_sec,
+ tmp.tv_nsec);
+ }
+
+ return ret;
+}
+
+static int rin_time_normalize_test(void)
+{
+ int ret;
+ size_t i;
+ static const struct timespec in[] = { {187, -100}, {0, 1999999999}, {2, 99000000000}, {10, 10}, {0, 1000000000}, {0, 0} };
+ static const struct timespec expected[] = { {186, 999999899}, {1, 999999999}, {101, 0}, {10, 10}, {1, 0}, {0, 0} };
+ struct timespec tmp;
+
+ for (i = 0; i < arrlen(expected); ++i) {
+ tmp = rin_time_normalize(in + i);
+ ret = ok(tmp.tv_sec != expected[i].tv_sec || tmp.tv_nsec != expected[i].tv_nsec,
+ "%s: expected: %li %li, got: %li %li",
+ __func__,
+ expected[i].tv_sec,
+ expected[i].tv_nsec,
+ tmp.tv_sec,
+ tmp.tv_nsec);
+ }
+
+ return ret;
+}
+
+static int rin_time_cmpless_test(void)
+{
+ int ret;
+ size_t i;
+ static const struct timespec in_a[] = { {187, 100}, {1, 100}, {1, 200}, {1, 100}, {9, 10}, {3, 100}, {4, 101}, {0, 0} };
+ static const struct timespec in_b[] = { {187, 100}, {0, 100}, {1, 200}, {1, 200}, {10, 10}, {4, 101}, {3, 100}, {0, 0} };
+ static const unsigned int expected[] = {0, 0, 0, 1, 1, 1, 0, 0};
+
+ for (i = 0; i < arrlen(expected); ++i) {
+ ret = ok(rin_time_cmp_less(in_a + i, in_b + i) != expected[i],
+ "%s: expected: %u, got: %u",
+ __func__,
+ expected[i],
+ rin_time_cmp_less(in_a + i, in_b + i));
+ }
+
+ return ret;
+}
+
+static int rin_time_cmpmore_test(void)
+{
+ int ret;
+ size_t i;
+ static const struct timespec in_a[] = { {187, 100}, {1, 100}, {1, 200}, {1, 100}, {9, 10}, {3, 100}, {4, 101}, {0, 0} };
+ static const struct timespec in_b[] = { {187, 100}, {0, 100}, {1, 200}, {1, 200}, {10, 10}, {4, 101}, {3, 100}, {0, 0} };
+ static const unsigned int expected[] = {0, 1, 0, 0, 0, 0, 1, 0};
+
+ for (i = 0; i < arrlen(expected); ++i) {
+ ret = ok(rin_time_cmp_more(in_a + i, in_b + i) != expected[i],
+ "%s: expected: %u, got: %u",
+ __func__,
+ expected[i],
+ rin_time_cmp_more(in_a + i, in_b + i));
+ }
+
+ return ret;
+}
+
+static int rin_time_cmplessequal_test(void)
+{
+ int ret;
+ size_t i;
+ static const struct timespec in_a[] = { {187, 100}, {1, 100}, {1, 200}, {1, 100}, {9, 10}, {3, 100}, {4, 101}, {0, 0} };
+ static const struct timespec in_b[] = { {187, 100}, {0, 100}, {1, 200}, {1, 200}, {10, 10}, {4, 101}, {3, 100}, {0, 0} };
+ static const unsigned int expected[] = {1, 0, 1, 1, 1, 1, 0, 1};
+
+ for (i = 0; i < arrlen(expected); ++i) {
+ ret = ok(rin_time_cmp_lessequal(in_a + i, in_b + i) != expected[i],
+ "%s: expected: %u, got: %u",
+ __func__,
+ expected[i],
+ rin_time_cmp_lessequal(in_a + i, in_b + i));
+ }
+
+ return ret;
+}
+
+static int rin_time_cmpmoreequal_test(void)
+{
+ int ret;
+ size_t i;
+ static const struct timespec in_a[] = { {187, 100}, {1, 100}, {1, 200}, {1, 100}, {9, 10}, {3, 100}, {4, 101}, {0, 0} };
+ static const struct timespec in_b[] = { {187, 100}, {0, 100}, {1, 200}, {1, 200}, {10, 10}, {4, 101}, {3, 100}, {0, 0} };
+ static const unsigned int expected[] = {1, 1, 1, 0, 0, 0, 1, 1};
+
+ for (i = 0; i < arrlen(expected); ++i) {
+ ret = ok(rin_time_cmp_moreequal(in_a + i, in_b + i) != expected[i],
+ "%s: expected: %u, got: %u",
+ __func__,
+ expected[i],
+ rin_time_cmp_moreequal(in_a + i, in_b + i));
+ }
+
+ return ret;
+}
+
+static int rin_time_cmpequal_test(void)
+{
+ int ret;
+ size_t i;
+ static const struct timespec in_a[] = { {187, 100}, {1, 100}, {1, 200}, {1, 100}, {9, 10}, {3, 100}, {4, 101}, {0, 0} };
+ static const struct timespec in_b[] = { {187, 100}, {0, 100}, {1, 200}, {1, 200}, {10, 10}, {4, 101}, {3, 100}, {0, 0} };
+ static const unsigned int expected[] = {1, 0, 1, 0, 0, 0, 0, 1};
+
+ for (i = 0; i < arrlen(expected); ++i) {
+ ret = ok(rin_time_cmp_equal(in_a + i, in_b + i) != expected[i],
+ "%s: expected: %u, got: %u",
+ __func__,
+ expected[i],
+ rin_time_cmp_equal(in_a + i, in_b + i));
+ }
+
+ return ret;
+}
+
+static int rin_time_cmpnonequal_test(void)
+{
+ int ret;
+ size_t i;
+ static const struct timespec in_a[] = { {187, 100}, {1, 100}, {1, 200}, {1, 100}, {9, 10}, {3, 100}, {4, 101}, {0, 0} };
+ static const struct timespec in_b[] = { {187, 100}, {0, 100}, {1, 200}, {1, 200}, {10, 10}, {4, 101}, {3, 100}, {0, 0} };
+ static const unsigned int expected[] = {0, 1, 0, 1, 1, 1, 1, 0};
+
+ for (i = 0; i < arrlen(expected); ++i) {
+ ret = ok(rin_time_cmp_nonequal(in_a + i, in_b + i) != expected[i],
+ "%s: expected: %u, got: %u",
+ __func__,
+ expected[i],
+ rin_time_cmp_nonequal(in_a + i, in_b + i));
+ }
+
+ return ret;
+}
+
diff --git a/test/time_test.h b/test/time_test.h
new file mode 100644
index 0000000..9a96d4f
--- /dev/null
+++ b/test/time_test.h
@@ -0,0 +1,28 @@
+/*
+ * The Rin Library – library "conformance" tests
+ *
+ * Copyright (C) 2017 Gediminas Jakutis
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; version 2.1
+ * of the License.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
+ */
+
+#ifndef LIBRIN_TIME_TEST_INCLUDED
+#define LIBRIN_TIME_TEST_INCLUDED
+
+#include "test.h"
+
+int time_test(char *testname);
+
+#endif /* LIBRIN_TIME_TEST_INCLUDED */
diff --git a/test/time_test_private.h b/test/time_test_private.h
new file mode 100644
index 0000000..8ccd192
--- /dev/null
+++ b/test/time_test_private.h
@@ -0,0 +1,36 @@
+/*
+ * The Rin Library – library "conformance" tests
+ *
+ * Copyright (C) 2017 Gediminas Jakutis
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; version 2.1
+ * of the License.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
+ */
+
+#ifndef LIBRIN_TIME_TEST_INCLUDED
+#define LIBRIN_TIME_TEST_INCLUDED
+
+#include "rin/time.h"
+
+static int rin_time_add_test(void);
+static int rin_time_sub_test(void);
+static int rin_time_normalize_test(void);
+static int rin_time_cmpless_test(void);
+static int rin_time_cmpmore_test(void);
+static int rin_time_cmplessequal_test(void);
+static int rin_time_cmpmoreequal_test(void);
+static int rin_time_cmpequal_test(void);
+static int rin_time_cmpnonequal_test(void);
+
+#endif /* LIBRIN_TIME_TEST_INCLUDED */