]> CyberLeo.Net >> Repos - FreeBSD/releng/10.2.git/blob - contrib/ntp/tests/libntp/strtolfp.c
- Copy stable/10@285827 to releng/10.2 in preparation for 10.2-RC1
[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         const char *str = "500";
13         const char *str_ms = "500000";
14
15         l_fp expected = {500,0};
16         l_fp actual, actual_ms;
17
18         TEST_ASSERT_TRUE(atolfp(str, &actual));
19         TEST_ASSERT_TRUE(mstolfp(str_ms, &actual_ms));
20
21         TEST_ASSERT_TRUE(IsEqual(expected, actual));
22         TEST_ASSERT_TRUE(IsEqual(expected, actual_ms));
23 }
24
25 void test_NegativeInteger(void) {
26         const char *str = "-300";
27         const char *str_ms = "-300000";
28
29         l_fp expected;
30         expected.l_i = -300;
31         expected.l_uf = 0;
32
33         l_fp actual, actual_ms;
34
35         TEST_ASSERT_TRUE(atolfp(str, &actual));
36         TEST_ASSERT_TRUE(mstolfp(str_ms, &actual_ms));
37
38         TEST_ASSERT_TRUE(IsEqual(expected, actual));
39         TEST_ASSERT_TRUE(IsEqual(expected, actual_ms));
40 }
41
42 void test_PositiveFraction(void) {
43         const char *str = "+500.5";
44         const char *str_ms = "500500.0";
45
46         l_fp expected = {500, HALF};
47         l_fp actual, actual_ms;
48
49         TEST_ASSERT_TRUE(atolfp(str, &actual));
50         TEST_ASSERT_TRUE(mstolfp(str_ms, &actual_ms));
51
52         TEST_ASSERT_TRUE(IsEqual(expected, actual));
53         TEST_ASSERT_TRUE(IsEqual(expected, actual_ms));
54 }
55
56 void test_NegativeFraction(void) {
57         const char *str = "-300.75";
58         const char *str_ms = "-300750";
59
60         l_fp expected;
61         expected.l_i = -301;
62         expected.l_uf = QUARTER;
63
64         l_fp actual, actual_ms;
65
66         TEST_ASSERT_TRUE(atolfp(str, &actual));
67         TEST_ASSERT_TRUE(mstolfp(str_ms, &actual_ms));
68
69         TEST_ASSERT_TRUE(IsEqual(expected, actual));
70         TEST_ASSERT_TRUE(IsEqual(expected, actual_ms));
71 }
72
73 void test_PositiveMsFraction(void) {
74         const char *str = "300.00025";
75         const char *str_ms = "300000.25";
76
77         l_fp expected = {300, QUARTER_PROMILLE_APPRX};
78         l_fp actual, actual_ms;
79
80
81         TEST_ASSERT_TRUE(atolfp(str, &actual));
82         TEST_ASSERT_TRUE(mstolfp(str_ms, &actual_ms));
83
84         TEST_ASSERT_TRUE(IsEqual(expected, actual));
85         TEST_ASSERT_TRUE(IsEqual(expected, actual_ms));
86
87 }
88
89 void test_NegativeMsFraction(void) {
90         const char *str = "-199.99975";
91         const char *str_ms = "-199999.75";
92
93         l_fp expected;
94         expected.l_i = -200;
95         expected.l_uf = QUARTER_PROMILLE_APPRX;
96
97         l_fp actual, actual_ms;
98
99         TEST_ASSERT_TRUE(atolfp(str, &actual));
100         TEST_ASSERT_TRUE(mstolfp(str_ms, &actual_ms));
101
102         TEST_ASSERT_TRUE(IsEqual(expected, actual));
103         TEST_ASSERT_TRUE(IsEqual(expected, actual_ms));
104
105 }
106
107 void test_InvalidChars(void) {
108         const char *str = "500.4a2";
109         l_fp actual, actual_ms;
110
111         TEST_ASSERT_FALSE(atolfp(str, &actual));
112         TEST_ASSERT_FALSE(mstolfp(str, &actual_ms));
113 }
114