inv-sw-linux/test/test_timer_utils.c
2023-12-27 22:32:07 +01:00

115 lines
2.5 KiB
C

#include <stdio.h>
#include <time.h>
#include <inttypes.h>
#include "CUnit/Basic.h"
#include "timer_utils.h"
#include "time_scales.h"
int init_suite1(void)
{
return 0;
}
int clean_suite1(void)
{
return 0;
}
void test_add_time_1(void)
{
struct itimerspec test_time = {
.it_value = {
.tv_nsec = 0,
.tv_sec = 0}};
tmr_add_ns_to_current_time(&test_time, 1);
CU_ASSERT(test_time.it_value.tv_nsec == 1);
}
void test_add_time_2(void)
{
struct itimerspec test_time = {
.it_value = {
.tv_nsec = 999999999,
.tv_sec = 0}};
tmr_add_ns_to_current_time(&test_time, 1);
CU_ASSERT(test_time.it_value.tv_sec == 1);
CU_ASSERT(test_time.it_value.tv_nsec == 0);
}
void test_add_time_3(void)
{
struct itimerspec test_time = {
.it_value = {
.tv_nsec = 999999111,
.tv_sec = 0}};
tmr_add_ns_to_current_time(&test_time, 1000);
CU_ASSERT(test_time.it_value.tv_sec == 1);
CU_ASSERT(test_time.it_value.tv_nsec == 111);
}
void test_add_time_4(void)
{
const uint32_t start = 999999990;
struct itimerspec time = {
.it_value = {
.tv_nsec = start,
.tv_sec = 0}};
for (uint32_t i = start; i < 999999999; i++)
{
tmr_add_ns_to_current_time(&time, 1);
printf("%u:\t", i);
printf("%u,%u\n", time.it_value.tv_sec, time.it_value.tv_nsec);
CU_ASSERT(time.it_value.tv_nsec == i + 1);
}
tmr_add_ns_to_current_time(&time, 1);
printf("1000000000:\t");
printf("%u,%u\n", time.it_value.tv_sec, time.it_value.tv_nsec);
CU_ASSERT(time.it_value.tv_nsec == 0);
CU_ASSERT(time.it_value.tv_sec == 1);
}
void test_big_number(void)
{
struct itimerspec time = {
.it_value = {
.tv_nsec = 999999999,
.tv_sec = 0,
},
};
tmr_add_ns_to_current_time(&time, 1*SEC);
CU_ASSERT(time.it_value.tv_nsec == 999999999);
CU_ASSERT(time.it_value.tv_sec == 1);
}
int main()
{
CU_pSuite pSuite = NULL;
if (CUE_SUCCESS != CU_initialize_registry())
return CU_get_error();
pSuite = CU_add_suite("Suite_1", init_suite1, clean_suite1);
if (NULL == pSuite)
{
CU_cleanup_registry();
return CU_get_error();
}
CU_add_test(pSuite, "test if time is added correctly with tmr_add_ns_to_current_time()", test_add_time_1);
CU_add_test(pSuite, "test if time is added correctly with tmr_add_ns_to_current_time()", test_add_time_2);
CU_add_test(pSuite, "test if time is added correctly with tmr_add_ns_to_current_time()", test_add_time_3);
CU_add_test(pSuite, "test adding time in loop", test_add_time_4);
CU_add_test(pSuite, "test adding big numbers", test_big_number);
CU_basic_set_mode(CU_BRM_VERBOSE);
CU_basic_run_tests();
CU_cleanup_registry();
return CU_get_error();
}