From 5eca7d7f17ab4d7d9f1af4577b67151042e1d4fc Mon Sep 17 00:00:00 2001 From: LoRd_MuldeR Date: Fri, 2 Dec 2022 15:16:54 +0100 Subject: [PATCH] Improved time source for test programs. --- README.md | 5 +--- example/hash-map/src/main.c | 5 +--- example/hash-set/src/main.c | 5 +--- test/hash-map/src/main.c | 5 ++-- test/hash-map/test-hash-map.vcxproj | 2 ++ test/hash-map/test-hash-map.vcxproj.filters | 6 ++++ test/hash-set/src/main.c | 5 ++-- test/hash-set/test-hash-set.vcxproj | 2 ++ test/hash-set/test-hash-set.vcxproj.filters | 6 ++++ test/shared/include/time_in.h | 14 ++++++++++ test/shared/src/time_in.c | 31 +++++++++++++++++++++ 11 files changed, 70 insertions(+), 16 deletions(-) create mode 100644 test/shared/include/time_in.h create mode 100644 test/shared/src/time_in.c diff --git a/README.md b/README.md index 607dfd2..2c24458 100644 --- a/README.md +++ b/README.md @@ -15,18 +15,15 @@ Here is a simple example of how to use LibHashSet in your application: ```C #include -#include #include -#define SEED ((uint32_t)time(NULL)) - int main(void) { uint64_t item; uintptr_t cursor = 0U; /* create new hash set instance */ - hash_set64_t* const hash_set = hash_set_create64(0U, -1.0, SEED); + hash_set64_t* const hash_set = hash_set_create64(0U, -1.0, 42U); if (!hash_set) { fputs("Allocation has failed!\n", stderr); diff --git a/example/hash-map/src/main.c b/example/hash-map/src/main.c index b8a0872..46dddfc 100644 --- a/example/hash-map/src/main.c +++ b/example/hash-map/src/main.c @@ -6,11 +6,8 @@ #include #include #include -#include #include "input.h" -#define SEED ((uint32_t)time(NULL)) - /* ========================================================================= */ /* MAIN */ /* ========================================================================= */ @@ -26,7 +23,7 @@ int main(void) HASHSET_VERSION_MAJOR, HASHSET_VERSION_MINOR, HASHSET_VERSION_PATCH, HASHSET_BUILD_DATE); /* create new hash map instance */ - hash_map = hash_map_create64(0U, -1.0, SEED); + hash_map = hash_map_create64(0U, -1.0, 42U); if (!hash_map) { fputs("Allocation has failed!\n", stderr); diff --git a/example/hash-set/src/main.c b/example/hash-set/src/main.c index f4c5ab9..fc3078d 100644 --- a/example/hash-set/src/main.c +++ b/example/hash-set/src/main.c @@ -6,11 +6,8 @@ #include #include #include -#include #include "input.h" -#define SEED ((uint32_t)time(NULL)) - /* ========================================================================= */ /* MAIN */ /* ========================================================================= */ @@ -26,7 +23,7 @@ int main(void) HASHSET_VERSION_MAJOR, HASHSET_VERSION_MINOR, HASHSET_VERSION_PATCH, HASHSET_BUILD_DATE); /* create new hash set instance */ - hash_set = hash_set_create64(0U, -1.0, SEED); + hash_set = hash_set_create64(0U, -1.0, 42U); if (!hash_set) { fputs("Allocation has failed!\n", stderr); diff --git a/test/hash-map/src/main.c b/test/hash-map/src/main.c index 3487db5..918abd4 100644 --- a/test/hash-map/src/main.c +++ b/test/hash-map/src/main.c @@ -4,9 +4,10 @@ /******************************************************************************/ #include "tests.h" +#include "../../shared/include/time_in.h" + #include #include -#include #define RUN_TEST_CASE(X) do \ { \ @@ -17,7 +18,7 @@ } \ while(0) -#define SEED ((uint32_t)time(NULL)) +#define SEED ((uint32_t)clock_now()) /* ========================================================================= */ /* MAIN */ diff --git a/test/hash-map/test-hash-map.vcxproj b/test/hash-map/test-hash-map.vcxproj index 759c685..d947395 100644 --- a/test/hash-map/test-hash-map.vcxproj +++ b/test/hash-map/test-hash-map.vcxproj @@ -40,11 +40,13 @@ + + diff --git a/test/hash-map/test-hash-map.vcxproj.filters b/test/hash-map/test-hash-map.vcxproj.filters index df46186..c233206 100644 --- a/test/hash-map/test-hash-map.vcxproj.filters +++ b/test/hash-map/test-hash-map.vcxproj.filters @@ -24,6 +24,9 @@ Source Files + + Source Files + @@ -32,5 +35,8 @@ Header Files + + Header Files + \ No newline at end of file diff --git a/test/hash-set/src/main.c b/test/hash-set/src/main.c index 8cffa0f..572ebc2 100644 --- a/test/hash-set/src/main.c +++ b/test/hash-set/src/main.c @@ -4,9 +4,10 @@ /******************************************************************************/ #include "tests.h" +#include "../../shared/include/time_in.h" + #include #include -#include #define RUN_TEST_CASE(X) do \ { \ @@ -17,7 +18,7 @@ } \ while(0) -#define SEED ((uint32_t)time(NULL)) +#define SEED ((uint32_t)clock_now()) /* ========================================================================= */ /* MAIN */ diff --git a/test/hash-set/test-hash-set.vcxproj b/test/hash-set/test-hash-set.vcxproj index dfb13e1..e641a8e 100644 --- a/test/hash-set/test-hash-set.vcxproj +++ b/test/hash-set/test-hash-set.vcxproj @@ -40,11 +40,13 @@ + + diff --git a/test/hash-set/test-hash-set.vcxproj.filters b/test/hash-set/test-hash-set.vcxproj.filters index df46186..c233206 100644 --- a/test/hash-set/test-hash-set.vcxproj.filters +++ b/test/hash-set/test-hash-set.vcxproj.filters @@ -24,6 +24,9 @@ Source Files + + Source Files + @@ -32,5 +35,8 @@ Header Files + + Header Files + \ No newline at end of file diff --git a/test/shared/include/time_in.h b/test/shared/include/time_in.h new file mode 100644 index 0000000..7707726 --- /dev/null +++ b/test/shared/include/time_in.h @@ -0,0 +1,14 @@ +/******************************************************************************/ +/* HashSet for C99, by LoRd_MuldeR */ +/* This work has been released under the CC0 1.0 Universal license! */ +/******************************************************************************/ + +#ifndef _TEST_TIME_INCLUDED +#define _TEST_TIME_INCLUDED + +#include +#include + +uint64_t clock_now(void); + +#endif /*_TEST_TIME_INCLUDED*/ diff --git a/test/shared/src/time_in.c b/test/shared/src/time_in.c new file mode 100644 index 0000000..971d55b --- /dev/null +++ b/test/shared/src/time_in.c @@ -0,0 +1,31 @@ +/******************************************************************************/ +/* HashSet for C99, by LoRd_MuldeR */ +/* This work has been released under the CC0 1.0 Universal license! */ +/******************************************************************************/ + +#include "../include/time_in.h" + +#ifdef _WIN32 +# define WIN32_LEAN_AND_MEAN 1 +# include +#else +# include +#endif + +uint64_t clock_now(void) +{ +#ifdef _WIN32 + LARGE_INTEGER counter; + if (QueryPerformanceCounter(&counter)) + { + return counter.QuadPart; + } +#else + struct timespec spec; + if (!clock_gettime(CLOCK_MONOTONIC, &spec)) + { + return (((uint64_t)spec.tv_sec) << 32) | (((uint64_t)spec.tv_nsec) & UINT64_C(0xFFFFFFFF)); + } +#endif + return 0U; +}