]> CyberLeo.Net >> Repos - FreeBSD/releng/10.2.git/blob - contrib/ntp/tests/libntp/strtolfp.c
Upgrade NTP to 4.2.8p4.
[FreeBSD/releng/10.2.git] / contrib / ntp / tests / libntp / strtolfp.c
1 #include "config.h"
2
3 #include "ntp_stdlib.h"
4 #include "ntp_calendar.h"
5
6 #include "unity.h"
7 #include "lfptest.h"
8
9 /* This file tests both atolfp and mstolfp */
10
11 void test_PositiveInteger(void);
12 void test_NegativeInteger(void);
13 void test_PositiveFraction(void);
14 void test_NegativeFraction(void);
15 void test_PositiveMsFraction(void);
16 void test_NegativeMsFraction(void);
17 void test_InvalidChars(void);
18
19
20 void test_PositiveInteger(void) {
21         const char *str = "500";
22         const char *str_ms = "500000";
23
24         l_fp expected = {{500},0};
25         l_fp actual, actual_ms;
26
27         TEST_ASSERT_TRUE(atolfp(str, &actual));
28         TEST_ASSERT_TRUE(mstolfp(str_ms, &actual_ms));
29
30         TEST_ASSERT_TRUE(IsEqual(expected, actual));
31         TEST_ASSERT_TRUE(IsEqual(expected, actual_ms));
32 }
33
34 void test_NegativeInteger(void) {
35         const char *str = "-300";
36         const char *str_ms = "-300000";
37
38         l_fp expected;
39         expected.l_i = -300;
40         expected.l_uf = 0;
41
42         l_fp actual, actual_ms;
43
44         TEST_ASSERT_TRUE(atolfp(str, &actual));
45         TEST_ASSERT_TRUE(mstolfp(str_ms, &actual_ms));
46
47         TEST_ASSERT_TRUE(IsEqual(expected, actual));
48         TEST_ASSERT_TRUE(IsEqual(expected, actual_ms));
49 }
50
51 void test_PositiveFraction(void) {
52         const char *str = "+500.5";
53         const char *str_ms = "500500.0";
54
55         l_fp expected = {{500}, HALF};
56         l_fp actual, actual_ms;
57
58         TEST_ASSERT_TRUE(atolfp(str, &actual));
59         TEST_ASSERT_TRUE(mstolfp(str_ms, &actual_ms));
60
61         TEST_ASSERT_TRUE(IsEqual(expected, actual));
62         TEST_ASSERT_TRUE(IsEqual(expected, actual_ms));
63 }
64
65 void test_NegativeFraction(void) {
66         const char *str = "-300.75";
67         const char *str_ms = "-300750";
68
69         l_fp expected;
70         expected.l_i = -301;
71         expected.l_uf = QUARTER;
72
73         l_fp actual, actual_ms;
74
75         TEST_ASSERT_TRUE(atolfp(str, &actual));
76         TEST_ASSERT_TRUE(mstolfp(str_ms, &actual_ms));
77
78         TEST_ASSERT_TRUE(IsEqual(expected, actual));
79         TEST_ASSERT_TRUE(IsEqual(expected, actual_ms));
80 }
81
82 void test_PositiveMsFraction(void) {
83         const char *str = "300.00025";
84         const char *str_ms = "300000.25";
85
86         l_fp expected = {{300}, QUARTER_PROMILLE_APPRX};
87         l_fp actual, actual_ms;
88
89
90         TEST_ASSERT_TRUE(atolfp(str, &actual));
91         TEST_ASSERT_TRUE(mstolfp(str_ms, &actual_ms));
92
93         TEST_ASSERT_TRUE(IsEqual(expected, actual));
94         TEST_ASSERT_TRUE(IsEqual(expected, actual_ms));
95
96 }
97
98 void test_NegativeMsFraction(void) {
99         const char *str = "-199.99975";
100         const char *str_ms = "-199999.75";
101
102         l_fp expected;
103         expected.l_i = -200;
104         expected.l_uf = QUARTER_PROMILLE_APPRX;
105
106         l_fp actual, actual_ms;
107
108         TEST_ASSERT_TRUE(atolfp(str, &actual));
109         TEST_ASSERT_TRUE(mstolfp(str_ms, &actual_ms));
110
111         TEST_ASSERT_TRUE(IsEqual(expected, actual));
112         TEST_ASSERT_TRUE(IsEqual(expected, actual_ms));
113
114 }
115
116 void test_InvalidChars(void) {
117         const char *str = "500.4a2";
118         l_fp actual, actual_ms;
119
120         TEST_ASSERT_FALSE(atolfp(str, &actual));
121         TEST_ASSERT_FALSE(mstolfp(str, &actual_ms));
122 }
123