aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChristian C <cc@localhost>2025-03-08 00:47:14 -0800
committerChristian C <cc@localhost>2025-03-08 00:47:14 -0800
commit316554287a6cfa7c61c009b745c8f42f3aa30111 (patch)
tree0639a202477ffd10be1293fbc7e4cbc0a9a39cbc
parent8e1541680d15a5489089bbe367892797fa59c8f2 (diff)
Time tests
-rw-r--r--include/lib/time.h2
-rw-r--r--include/test/lib/time.h16
-rw-r--r--lib/time.c2
-rw-r--r--src/test/test.c7
-rw-r--r--test/lib/time.c61
5 files changed, 86 insertions, 2 deletions
diff --git a/include/lib/time.h b/include/lib/time.h
index 9d69b44..1adf428 100644
--- a/include/lib/time.h
+++ b/include/lib/time.h
@@ -7,7 +7,7 @@
// Difference in Time
// Compute the difference between timespec structs
-double diff_time(struct timespec *time1, struct timespec *time0);
+double diff_time(const struct timespec *time1, const struct timespec *time0);
// Get Current Time
void get_time(struct timespec *ts);
diff --git a/include/test/lib/time.h b/include/test/lib/time.h
new file mode 100644
index 0000000..cdf464e
--- /dev/null
+++ b/include/test/lib/time.h
@@ -0,0 +1,16 @@
+#ifndef INC_TEST_LIB_TIME_H
+#define INC_TEST_LIB_TIME_H
+
+#include <test/__meta__.h>
+#ifndef TEST_RESULT
+#define TEST_RESULT(subtest,result,n,n_success) _TEST_RESULT("LIB/TIME",subtest,result,n,n_success)
+#endif
+
+#include <lib/time.h>
+
+bool_t test_diff_time(const struct timespec *time1, const struct timespec *time0, double result);
+
+// Meta test function
+bool_t TEST_lib_time();
+
+#endif
diff --git a/lib/time.c b/lib/time.c
index 085ef80..623a696 100644
--- a/lib/time.c
+++ b/lib/time.c
@@ -2,7 +2,7 @@
// Difference in Time
// Compute the difference between timespec structs
-double diff_time(struct timespec *time1, struct timespec *time0) {
+double diff_time(const struct timespec *time1, const struct timespec *time0) {
return (time1->tv_sec - time0->tv_sec)
+ (time1->tv_nsec - time0->tv_nsec) / 1000000000.0;
}
diff --git a/src/test/test.c b/src/test/test.c
index 9ef2534..b3b2fce 100644
--- a/src/test/test.c
+++ b/src/test/test.c
@@ -1,5 +1,6 @@
#include <test/lib/color.h>
#include <test/lib/dir.h>
+#include <test/lib/time.h>
#define _META_TEST_RESULT(name,result) if (result) { fprintf(stderr, " \x1b[92mPASS\x1b[0m %s\n", name);} else { fprintf(stderr, "%s: \x1b[91mFAIL\x1b[0m\n", name);}
@@ -17,6 +18,12 @@ int main()
all_success &= test_success;
_META_TEST_RESULT("LIB/DIR", test_success)
+
+ // lib/time.c Test
+ test_success = TEST_lib_time();
+ all_success &= test_success;
+ _META_TEST_RESULT("LIB/TIME", test_success)
+
if (all_success == TRUE) {
return 0;
}
diff --git a/test/lib/time.c b/test/lib/time.c
new file mode 100644
index 0000000..5c29bbc
--- /dev/null
+++ b/test/lib/time.c
@@ -0,0 +1,61 @@
+#include <test/lib/time.h>
+
+const struct timespec zero = {0,0};
+const struct timespec one_s = {1,0};
+const struct timespec one_ns = {0,1};
+const struct timespec one_s_ns = {1,1};
+
+bool_t test_diff_time(const struct timespec *time1, const struct timespec *time0, double result)
+{
+ double fcall_result = diff_time(time1, time0);
+ if (fcall_result == result) {
+ return TRUE;
+ }
+ return FALSE;
+}
+
+void _TEST_diff_time(bool_t* result, uint16_t* test_count, uint16_t* test_pass)
+{
+ bool_t sub_result;
+ // Test 1: 0-0=0
+ sub_result = test_diff_time(&zero, &zero, 0.0);
+ *result &= sub_result;
+ TEST_RESULT("DIFF_TIME",sub_result, *test_count, (*test_pass));
+
+ // Test 2: 1s-0=1s
+ sub_result = test_diff_time(&one_s, &zero, 1.0);
+ *result &= sub_result;
+ TEST_RESULT("DIFF_TIME",sub_result, *test_count, (*test_pass));
+
+ // Test 3: 1ns-0=1ns
+ sub_result = test_diff_time(&one_ns, &zero, 0.000000001);
+ *result &= sub_result;
+ TEST_RESULT("DIFF_TIME",sub_result, *test_count, (*test_pass));
+
+ // Test 4: 1s1ns-0=1s1ns
+ sub_result = test_diff_time(&one_s_ns, &zero, 1.000000001);
+ *result &= sub_result;
+ TEST_RESULT("DIFF_TIME",sub_result, *test_count, (*test_pass));
+
+ // Test 5: 1s1ns-1ns=1s
+ sub_result = test_diff_time(&one_s_ns, &one_ns, 1.0);
+ *result &= sub_result;
+ TEST_RESULT("DIFF_TIME",sub_result, *test_count, (*test_pass));
+
+ // Test 6: 1s1ns-1s=1ns
+ sub_result = test_diff_time(&one_s_ns, &one_s, 0.000000001);
+ *result &= sub_result;
+ TEST_RESULT("DIFF_TIME",sub_result, *test_count, (*test_pass));
+}
+
+bool_t TEST_lib_time()
+{
+ uint16_t test_count = 0;
+ uint16_t test_pass = 0;
+ bool_t result = TRUE;
+
+ // Testing directory existence
+ _TEST_diff_time(&result, &test_count, &test_pass);
+
+ return test_count == test_pass;
+}