xref: /netbsd-src/external/bsd/ntp/dist/tests/libntp/buftvtots.c (revision cdfa2a7ef92791ba9db70a584a1d904730e6fb46)
1*cdfa2a7eSchristos /*	$NetBSD: buftvtots.c,v 1.2 2020/05/25 20:47:36 christos Exp $	*/
2067f5680Schristos 
3f17b710fSchristos #include "config.h"
4f17b710fSchristos #include "ntp_types.h"
5f17b710fSchristos #include "ntp_stdlib.h"
6f17b710fSchristos 
7f17b710fSchristos #include "lfptest.h"
8f17b710fSchristos 
9f17b710fSchristos #include "ntp_unixtime.h"
10f17b710fSchristos 
11f17b710fSchristos #include "unity.h"
12f17b710fSchristos 
13a6f3f22fSchristos /* Required for Solaris. */
14f17b710fSchristos #include <math.h>
15f17b710fSchristos 
16a6f3f22fSchristos void test_ZeroBuffer(void);
17a6f3f22fSchristos void test_IntegerAndFractionalBuffer(void);
18a6f3f22fSchristos void test_IllegalMicroseconds(void);
19a6f3f22fSchristos void test_AlwaysFalseOnWindows(void);
20f17b710fSchristos 
21f17b710fSchristos 
22a6f3f22fSchristos void
test_ZeroBuffer(void)234c290c01Schristos test_ZeroBuffer(void)
244c290c01Schristos {
25f17b710fSchristos #ifndef SYS_WINNT
26f17b710fSchristos 	const struct timeval input = {0, 0};
27a6f3f22fSchristos 	const l_fp expected = {{0 + JAN_1970}, 0};
28f17b710fSchristos 
29f17b710fSchristos 	l_fp actual;
30f17b710fSchristos 
31f17b710fSchristos 	TEST_ASSERT_TRUE(buftvtots((const char*)(&input), &actual));
32f17b710fSchristos 	TEST_ASSERT_TRUE(IsEqual(expected, actual));
33f17b710fSchristos #else
34f17b710fSchristos 	TEST_IGNORE_MESSAGE("Test only for Windows, skipping...");
35f17b710fSchristos #endif
364c290c01Schristos 
374c290c01Schristos 	return;
38f17b710fSchristos }
39f17b710fSchristos 
404c290c01Schristos 
41a6f3f22fSchristos void
test_IntegerAndFractionalBuffer(void)424c290c01Schristos test_IntegerAndFractionalBuffer(void)
434c290c01Schristos {
44f17b710fSchristos #ifndef SYS_WINNT
45a6f3f22fSchristos 	const struct timeval input = {5, 500000}; /* 5.5 */
46a6f3f22fSchristos 	const l_fp expected = {{5 + JAN_1970}, HALF};
47a6f3f22fSchristos 	double expectedDouble, actualDouble;
48f17b710fSchristos 	l_fp actual;
49f17b710fSchristos 
50f17b710fSchristos 	TEST_ASSERT_TRUE(buftvtots((const char*)(&input), &actual));
51f17b710fSchristos 
52a6f3f22fSchristos 	/* Compare the fractional part with an absolute error given. */
53f17b710fSchristos 	TEST_ASSERT_EQUAL(expected.l_ui, actual.l_ui);
54f17b710fSchristos 
55f17b710fSchristos 	M_LFPTOD(0, expected.l_uf, expectedDouble);
56f17b710fSchristos 	M_LFPTOD(0, actual.l_uf, actualDouble);
57f17b710fSchristos 
58a6f3f22fSchristos 	/* The error should be less than 0.5 us */
59a6f3f22fSchristos 	TEST_ASSERT_DOUBLE_WITHIN(0.0000005, expectedDouble, actualDouble);
60f17b710fSchristos #else
61f17b710fSchristos 	TEST_IGNORE_MESSAGE("Test only for Windows, skipping...");
62f17b710fSchristos #endif
634c290c01Schristos 
644c290c01Schristos 	return;
65f17b710fSchristos }
66f17b710fSchristos 
67a6f3f22fSchristos void
test_IllegalMicroseconds(void)684c290c01Schristos test_IllegalMicroseconds(void)
694c290c01Schristos {
70f17b710fSchristos #ifndef SYS_WINNT
71a6f3f22fSchristos 	const struct timeval input = {0, 1100000}; /* > 999 999 microseconds. */
72f17b710fSchristos 
73f17b710fSchristos 	l_fp actual;
74f17b710fSchristos 
75f17b710fSchristos 	TEST_ASSERT_FALSE(buftvtots((const char*)(&input), &actual));
76f17b710fSchristos #else
77f17b710fSchristos 	TEST_IGNORE_MESSAGE("Test only for Windows, skipping...");
78f17b710fSchristos #endif
794c290c01Schristos 
804c290c01Schristos 	return;
81f17b710fSchristos }
82f17b710fSchristos 
83f17b710fSchristos 
84a6f3f22fSchristos void
test_AlwaysFalseOnWindows(void)854c290c01Schristos test_AlwaysFalseOnWindows(void)
864c290c01Schristos {
87f17b710fSchristos #ifdef SYS_WINNT
88f17b710fSchristos 	/*
89f17b710fSchristos 	 * Under Windows, buftvtots will just return
90f17b710fSchristos 	 * 0 (false).
91f17b710fSchristos 	 */
92f17b710fSchristos 	l_fp actual;
93f17b710fSchristos 	TEST_ASSERT_FALSE(buftvtots("", &actual));
94f17b710fSchristos #else
95f17b710fSchristos 	TEST_IGNORE_MESSAGE("Non-Windows test, skipping...");
96f17b710fSchristos #endif
97f17b710fSchristos 
984c290c01Schristos 	return;
994c290c01Schristos }
100