Improved time source for test programs.

This commit is contained in:
LoRd_MuldeR 2022-12-02 15:16:54 +01:00
parent 466a502adf
commit 5eca7d7f17
11 changed files with 70 additions and 16 deletions

View File

@ -15,18 +15,15 @@ Here is a simple example of how to use LibHashSet in your application:
```C ```C
#include <hash_set.h> #include <hash_set.h>
#include <time.h>
#include <stdio.h> #include <stdio.h>
#define SEED ((uint32_t)time(NULL))
int main(void) int main(void)
{ {
uint64_t item; uint64_t item;
uintptr_t cursor = 0U; uintptr_t cursor = 0U;
/* create new hash set instance */ /* 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) if (!hash_set)
{ {
fputs("Allocation has failed!\n", stderr); fputs("Allocation has failed!\n", stderr);

View File

@ -6,11 +6,8 @@
#include <hash_map.h> #include <hash_map.h>
#include <stdio.h> #include <stdio.h>
#include <inttypes.h> #include <inttypes.h>
#include <time.h>
#include "input.h" #include "input.h"
#define SEED ((uint32_t)time(NULL))
/* ========================================================================= */ /* ========================================================================= */
/* MAIN */ /* MAIN */
/* ========================================================================= */ /* ========================================================================= */
@ -26,7 +23,7 @@ int main(void)
HASHSET_VERSION_MAJOR, HASHSET_VERSION_MINOR, HASHSET_VERSION_PATCH, HASHSET_BUILD_DATE); HASHSET_VERSION_MAJOR, HASHSET_VERSION_MINOR, HASHSET_VERSION_PATCH, HASHSET_BUILD_DATE);
/* create new hash map instance */ /* 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) if (!hash_map)
{ {
fputs("Allocation has failed!\n", stderr); fputs("Allocation has failed!\n", stderr);

View File

@ -6,11 +6,8 @@
#include <hash_set.h> #include <hash_set.h>
#include <stdio.h> #include <stdio.h>
#include <inttypes.h> #include <inttypes.h>
#include <time.h>
#include "input.h" #include "input.h"
#define SEED ((uint32_t)time(NULL))
/* ========================================================================= */ /* ========================================================================= */
/* MAIN */ /* MAIN */
/* ========================================================================= */ /* ========================================================================= */
@ -26,7 +23,7 @@ int main(void)
HASHSET_VERSION_MAJOR, HASHSET_VERSION_MINOR, HASHSET_VERSION_PATCH, HASHSET_BUILD_DATE); HASHSET_VERSION_MAJOR, HASHSET_VERSION_MINOR, HASHSET_VERSION_PATCH, HASHSET_BUILD_DATE);
/* create new hash set instance */ /* 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) if (!hash_set)
{ {
fputs("Allocation has failed!\n", stderr); fputs("Allocation has failed!\n", stderr);

View File

@ -4,9 +4,10 @@
/******************************************************************************/ /******************************************************************************/
#include "tests.h" #include "tests.h"
#include "../../shared/include/time_in.h"
#include <stdio.h> #include <stdio.h>
#include <inttypes.h> #include <inttypes.h>
#include <time.h>
#define RUN_TEST_CASE(X) do \ #define RUN_TEST_CASE(X) do \
{ \ { \
@ -17,7 +18,7 @@
} \ } \
while(0) while(0)
#define SEED ((uint32_t)time(NULL)) #define SEED ((uint32_t)clock_now())
/* ========================================================================= */ /* ========================================================================= */
/* MAIN */ /* MAIN */

View File

@ -40,11 +40,13 @@
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClCompile Include="..\shared\src\random_in.c" /> <ClCompile Include="..\shared\src\random_in.c" />
<ClCompile Include="..\shared\src\time_in.c" />
<ClCompile Include="src\main.c" /> <ClCompile Include="src\main.c" />
<ClCompile Include="src\tests.c" /> <ClCompile Include="src\tests.c" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClInclude Include="..\shared\include\random_in.h" /> <ClInclude Include="..\shared\include\random_in.h" />
<ClInclude Include="..\shared\include\time_in.h" />
<ClInclude Include="src\tests.h" /> <ClInclude Include="src\tests.h" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>

View File

@ -24,6 +24,9 @@
<ClCompile Include="..\shared\src\random_in.c"> <ClCompile Include="..\shared\src\random_in.c">
<Filter>Source Files</Filter> <Filter>Source Files</Filter>
</ClCompile> </ClCompile>
<ClCompile Include="..\shared\src\time_in.c">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClInclude Include="src\tests.h"> <ClInclude Include="src\tests.h">
@ -32,5 +35,8 @@
<ClInclude Include="..\shared\include\random_in.h"> <ClInclude Include="..\shared\include\random_in.h">
<Filter>Header Files</Filter> <Filter>Header Files</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="..\shared\include\time_in.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup> </ItemGroup>
</Project> </Project>

View File

@ -4,9 +4,10 @@
/******************************************************************************/ /******************************************************************************/
#include "tests.h" #include "tests.h"
#include "../../shared/include/time_in.h"
#include <stdio.h> #include <stdio.h>
#include <inttypes.h> #include <inttypes.h>
#include <time.h>
#define RUN_TEST_CASE(X) do \ #define RUN_TEST_CASE(X) do \
{ \ { \
@ -17,7 +18,7 @@
} \ } \
while(0) while(0)
#define SEED ((uint32_t)time(NULL)) #define SEED ((uint32_t)clock_now())
/* ========================================================================= */ /* ========================================================================= */
/* MAIN */ /* MAIN */

View File

@ -40,11 +40,13 @@
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClCompile Include="..\shared\src\random_in.c" /> <ClCompile Include="..\shared\src\random_in.c" />
<ClCompile Include="..\shared\src\time_in.c" />
<ClCompile Include="src\main.c" /> <ClCompile Include="src\main.c" />
<ClCompile Include="src\tests.c" /> <ClCompile Include="src\tests.c" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClInclude Include="..\shared\include\random_in.h" /> <ClInclude Include="..\shared\include\random_in.h" />
<ClInclude Include="..\shared\include\time_in.h" />
<ClInclude Include="src\tests.h" /> <ClInclude Include="src\tests.h" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>

View File

@ -24,6 +24,9 @@
<ClCompile Include="..\shared\src\random_in.c"> <ClCompile Include="..\shared\src\random_in.c">
<Filter>Source Files</Filter> <Filter>Source Files</Filter>
</ClCompile> </ClCompile>
<ClCompile Include="..\shared\src\time_in.c">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClInclude Include="src\tests.h"> <ClInclude Include="src\tests.h">
@ -32,5 +35,8 @@
<ClInclude Include="..\shared\include\random_in.h"> <ClInclude Include="..\shared\include\random_in.h">
<Filter>Header Files</Filter> <Filter>Header Files</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="..\shared\include\time_in.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup> </ItemGroup>
</Project> </Project>

View File

@ -0,0 +1,14 @@
/******************************************************************************/
/* HashSet for C99, by LoRd_MuldeR <MuldeR2@GMX.de> */
/* This work has been released under the CC0 1.0 Universal license! */
/******************************************************************************/
#ifndef _TEST_TIME_INCLUDED
#define _TEST_TIME_INCLUDED
#include <stdlib.h>
#include <stdint.h>
uint64_t clock_now(void);
#endif /*_TEST_TIME_INCLUDED*/

31
test/shared/src/time_in.c Normal file
View File

@ -0,0 +1,31 @@
/******************************************************************************/
/* HashSet for C99, by LoRd_MuldeR <MuldeR2@GMX.de> */
/* 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 <Windows.h>
#else
# include <time.h>
#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;
}