1248590Smm#include "config.h"
2248590Smm
3248590Smm#include "lfptest.h"
4248590Smm#include "timevalops.h"
5248590Smm
6248590Smm#include "unity.h"
7248590Smm
8248590Smm#include <math.h> /* Required on Solaris for ldexp. */
9248590Smm
10248590Smmvoid test_Seconds(void);
11248590Smmvoid test_MicrosecondsRounded(void);
12248590Smmvoid test_MicrosecondsExact(void);
13248590Smm
14248590Smmvoid
15248590Smmtest_Seconds(void)
16248590Smm{
17248590Smm	struct timeval input = {500, 0}; /* 500.0 s */
18248590Smm	l_fp expected = {{500}, 0};
19248590Smm	l_fp actual;
20248590Smm
21248590Smm	TVTOTS(&input, &actual);
22248590Smm
23248590Smm	TEST_ASSERT_TRUE(IsEqual(expected, actual));
24248590Smm}
25248590Smm
26248590Smm
27248590Smmvoid
28248590Smmtest_MicrosecondsRounded(void)
29248590Smm{
30248590Smm	/* 0.0005 can not be represented exact in a l_fp structure.
31248590Smm	 * It would equal to 2147483,648. This means that
32248590Smm	 * HALF_PROMILLE_UP (which is 2147484) should be
33248590Smm	 * the correct rounding. */
34248590Smm
35248590Smm	struct timeval input = {0, 500}; /* 0.0005 exact */
36248590Smm	l_fp expected = {{0}, HALF_PROMILLE_UP};
37248590Smm	l_fp actual;
38248590Smm
39248590Smm	TVTOTS(&input, &actual);
40248590Smm
41248590Smm	TEST_ASSERT_TRUE(IsEqual(expected, actual));
42248590Smm}
43248590Smm
44248590Smm
45248590Smmvoid
46248590Smmtest_MicrosecondsExact(void)
47248590Smm{
48	/* 0.5 can be represented exact in both l_fp and timeval. */
49	const struct timeval input = {10, 500000}; /* 0.5 exact */
50	const l_fp expected = {{10}, HALF};        /* 0.5 exact */
51	l_fp actual;
52
53	TVTOTS(&input, &actual);
54
55	/* Compare the fractional part with an absolute error given. */
56	TEST_ASSERT_EQUAL_UINT(expected.l_ui, actual.l_ui);
57
58	double expectedDouble, actualDouble;
59	M_LFPTOD(0, expected.l_uf, expectedDouble);
60	M_LFPTOD(0, actual.l_uf, actualDouble);
61
62	/* The error should be less than 0.5 us */
63	TEST_ASSERT_DOUBLE_WITHIN(0.0000005, expectedDouble, actualDouble);
64}
65