commit 5deb09eaf8f1d91b87d7458a9f810a9cbace8c82
parent 99bac5027f67eed38109b8c2a756c98e911edd5a
Author: vaplv <vaplv@free.fr>
Date: Thu, 28 Nov 2013 18:20:10 +0100
Add clock_time test
Diffstat:
4 files changed, 52 insertions(+), 39 deletions(-)
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
@@ -66,12 +66,17 @@ add_executable(test_mem_allocator test_mem_allocator.c)
target_link_libraries(test_mem_allocator rsys)
add_test(test_mem_allocator test_mem_allocator)
+add_executable(test_time test_time.c)
+target_link_libraries(test_time rsys)
+add_test(test_time test_time)
+
add_executable(test_atomic test_atomic.c)
add_test(test_atomic test_atomic)
add_executable(test_ref test_ref.c)
add_test(test_ref test_ref)
+
if(NOT OPENMP_FOUND)
message(STATUS "No OpenMP support: multi-threaded tests cannot be generated")
else()
diff --git a/src/clock_time.c b/src/clock_time.c
@@ -2,6 +2,9 @@
#include <inttypes.h>
#include <string.h>
+#define TIME_TO_NSEC(Time) \
+ (((Time)->tv_usec + (Time)->tv_sec * 1000000L) * 1000L)
+
#define NSEC_PER_USEC 1000L
#define NSEC_PER_MSEC (1000L * NSEC_PER_USEC)
#define NSEC_PER_SEC (1000L * NSEC_PER_MSEC)
@@ -12,7 +15,7 @@
int64_t
time_val(const time_T* time, enum time_unit unit)
{
- int64_t val = TIME_TO_NSEC__(time);
+ int64_t val = TIME_TO_NSEC(time);
switch(unit) {
case TIME_NSEC:
/* Do nothing. */
@@ -72,7 +75,7 @@ time_dump
} \
} (void) 0
- time_nsec = TIME_TO_NSEC__(time);
+ time_nsec = TIME_TO_NSEC(time);
if(flag & TIME_DAY) {
const int64_t nb_days = time_nsec / NSEC_PER_DAY;
DUMP(nb_days, "day");
diff --git a/src/clock_time.h b/src/clock_time.h
@@ -2,37 +2,14 @@
#define TIME_H
#include "rsys.h"
-
#ifndef PLATFORM_UNIX
#error "Unsupported platform"
#endif
-
-#if _POSIX_C_SOURCE < 200112L
- #include <sys/time.h>
-
- #define CURRENT_TIME__(Time) gettimeofday((Time), NULL)
- #define GREATER_TIME_UNIT__(Time) (Time)->tv_sec
- #define SMALLER_TIME_UNIT__(Time) (Time)->tv_usec
- #define GREATER_TO_SMALLER_TIME_UNIT__ 1000000L
- #define TIME_TO_NSEC__(Time) \
- (((Time)->tv_usec + (Time)->tv_sec * 1000000L) * 1000L)
-
- typedef struct timeval time_T;
-#else
- #include <time.h>
-
- #define CURRENT_TIME__(Time) clock_gettime(CLOCK_REALTIME, (Time))
- #define GREATER_TIME_UNIT__(Time) (Time)->tv_sec
- #define SMALLER_TIME_UNIT__(Time) (Time)->tv_nsec
- #define GREATER_TO_SMALLER_TIME_UNIT__ 1000000000L
- #define TIME_TO_NSEC__(Time) \
- ((time)->tv_nsec + (Time)->tv_sec * 1000000000L)
-
- typedef struct timespec time_T;
-#endif
-
+#include <sys/time.h>
#include <stddef.h>
+typedef struct timeval time_T;
+
enum time_unit {
TIME_NSEC = BIT(0),
TIME_USEC = BIT(1),
@@ -48,7 +25,7 @@ time_current(time_T* time)
{
int err = 0; (void) err;
ASSERT(time);
- err = CURRENT_TIME__(time);
+ err = gettimeofday(time, NULL);
ASSERT(err == 0);
}
@@ -57,11 +34,11 @@ static FINLINE void
time_sub(time_T* res, const time_T* a, const time_T* b)
{
ASSERT(res && a && b);
- GREATER_TIME_UNIT__(res) = GREATER_TIME_UNIT__(a) - GREATER_TIME_UNIT__(b);
- SMALLER_TIME_UNIT__(res) = SMALLER_TIME_UNIT__(a) - SMALLER_TIME_UNIT__(b);
- if(SMALLER_TIME_UNIT__(res) < 0) {
- --GREATER_TIME_UNIT__(res);
- SMALLER_TIME_UNIT__(res) += GREATER_TO_SMALLER_TIME_UNIT__;
+ res->tv_sec = a->tv_sec - b->tv_sec;
+ res->tv_usec = a->tv_usec - b->tv_usec;
+ if(res->tv_usec < 0) {
+ --res->tv_sec;
+ res->tv_usec += 1000000L;
}
}
@@ -70,11 +47,11 @@ time_add(time_T* res, const time_T* a, const time_T* b)
{
ASSERT(res && a && b);
- GREATER_TIME_UNIT__(res) = GREATER_TIME_UNIT__(a) + GREATER_TIME_UNIT__(b);
- SMALLER_TIME_UNIT__(res) = SMALLER_TIME_UNIT__(a) + SMALLER_TIME_UNIT__(b);
- if(SMALLER_TIME_UNIT__(res) >= GREATER_TO_SMALLER_TIME_UNIT__) {
- ++GREATER_TIME_UNIT__(res);
- SMALLER_TIME_UNIT__(res) -= GREATER_TO_SMALLER_TIME_UNIT__;
+ res->tv_sec = a->tv_sec + b->tv_sec;
+ res->tv_usec = a->tv_usec + b->tv_usec;
+ if(res->tv_usec >= 1000000L) {
+ ++res->tv_sec;
+ res->tv_usec -= 1000000L;
}
}
diff --git a/src/test_time.c b/src/test_time.c
@@ -0,0 +1,28 @@
+#include "clock_time.h"
+
+#include <stdlib.h>
+#include <unistd.h>
+
+int
+main(int argc, char** argv)
+{
+ time_T start, end, res;
+ int64_t time = 0;
+ (void)argc, (void)argv;
+
+ time_current(&start);
+ sleep(2);
+ time_current(&end);
+
+ time_sub(&res, &end, &start);
+ time = time_val(&res, TIME_NSEC);
+ CHECK(llabs((long long)(time - 2000000000)) <= 1000000 , 1);
+ time = time_val(&res, TIME_USEC);
+ CHECK(llabs((long long)(time - 2000000)) <= 1000 , 1);
+ time = time_val(&res, TIME_MSEC);
+ CHECK(llabs((long long)(time - 2000)) <= 1 , 1);
+ time = time_val(&res, TIME_SEC);
+ CHECK(llabs((long long)(time - 2)) <= 0 , 1);
+
+ return 0;
+}