1 /* $NetBSD: hextolfp.c,v 1.2 2020/05/25 20:47:36 christos Exp $ */
2
3 #include "config.h"
4
5 #include "ntp_stdlib.h"
6 #include "ntp_calendar.h"
7
8 #include "unity.h"
9 #include "lfptest.h"
10
11 void test_PositiveInteger(void);
12 void test_NegativeInteger(void);
13 void test_PositiveFraction(void);
14 void test_NegativeFraction(void);
15 void test_IllegalNumberOfInteger(void);
16 void test_IllegalChar(void);
17
18
19 void
test_PositiveInteger(void)20 test_PositiveInteger(void) {
21 const char *str = "00001000.00000000";
22 l_fp actual;
23
24 l_fp expected = {{4096}, 0}; /* 16^3, no fraction part. */
25
26 TEST_ASSERT_TRUE(hextolfp(str, &actual));
27 TEST_ASSERT_TRUE(IsEqual(expected, actual));
28 }
29
30 void
test_NegativeInteger(void)31 test_NegativeInteger(void) {
32 const char *str = "ffffffff.00000000"; /* -1 decimal */
33 l_fp actual;
34
35 l_fp expected = {{-1}, 0};
36
37 TEST_ASSERT_TRUE(hextolfp(str, &actual));
38 TEST_ASSERT_TRUE(IsEqual(expected, actual));
39 }
40
41 void
test_PositiveFraction(void)42 test_PositiveFraction(void) {
43 const char *str = "00002000.80000000"; /* 8196.5 decimal */
44 l_fp actual;
45
46 l_fp expected = {{8192}, HALF};
47
48 TEST_ASSERT_TRUE(hextolfp(str, &actual));
49 TEST_ASSERT_TRUE(IsEqual(expected, actual));
50 }
51
52 void
test_NegativeFraction(void)53 test_NegativeFraction(void) {
54 const char *str = "ffffffff.40000000"; /* -1 + 0.25 decimal */
55 l_fp actual;
56
57 l_fp expected = {{-1}, QUARTER}; /* -1 + 0.25 */
58
59 TEST_ASSERT_TRUE(hextolfp(str, &actual));
60 TEST_ASSERT_TRUE(IsEqual(expected, actual));
61 }
62
63 void
test_IllegalNumberOfInteger(void)64 test_IllegalNumberOfInteger(void) {
65 const char *str = "1000000.00000000"; /* Missing one digit in integral part. */
66 l_fp actual;
67
68 TEST_ASSERT_FALSE(hextolfp(str, &actual));
69 }
70
71 void
test_IllegalChar(void)72 test_IllegalChar(void) {
73 const char *str = "10000000.0000h000"; /* Illegal character h. */
74 l_fp actual;
75
76 TEST_ASSERT_FALSE(hextolfp(str, &actual));
77 }
78