]> CyberLeo.Net >> Repos - FreeBSD/releng/10.2.git/blob - contrib/ntp/tests/libntp/lfptostr.c
Upgrade NTP to 4.2.8p4.
[FreeBSD/releng/10.2.git] / contrib / ntp / tests / libntp / lfptostr.c
1 /* 
2  * This file contains test for both mfptoa and mfptoms (which uses dolfptoa),
3  * since all these functions are very similar. It also tests ulfptoa, which is
4  * a macro.
5  */
6
7 #include "config.h"
8 #include "ntp_stdlib.h"
9 #include "ntp_fp.h"
10
11 #include "unity.h"
12
13 static const int LFP_MAX_PRECISION = 10;
14 static const int LFP_MAX_PRECISION_MS = 7;
15
16 static const int ONE_FOURTH = 1073741824; /* (1 << 30) */
17 static const int HALF = (1 << 31);
18 static const int THREE_FOURTH = -1073741824;
19 static const int HALF_PROMILLE_UP = 2147484; /* slightly more than 0.0005 */
20 static const int HALF_PROMILLE_DOWN = 2147483; /* slightly less than 0.0005 */
21
22
23 void test_PositiveInteger(void);
24 void test_NegativeInteger(void);
25 void test_PositiveIntegerWithFraction(void);
26 void test_NegativeIntegerWithFraction(void);
27 void test_RoundingDownToInteger(void);
28 void test_RoundingMiddleToInteger(void);
29 void test_RoundingUpToInteger(void);
30 void test_SingleDecimal(void);
31 void test_MillisecondsRoundingUp(void);
32 void test_MillisecondsRoundingDown(void);
33 void test_UnsignedInteger(void);
34
35
36
37 void
38 test_PositiveInteger(void) {
39         l_fp test = {{200}, 0}; /* exact 200.0000000000 */
40
41         TEST_ASSERT_EQUAL_STRING("200.0000000000", mfptoa(test.l_ui, test.l_uf, LFP_MAX_PRECISION));
42         TEST_ASSERT_EQUAL_STRING("200000.0000000", mfptoms(test.l_ui, test.l_uf, LFP_MAX_PRECISION_MS));
43 }
44
45 void
46 test_NegativeInteger(void) {
47         l_fp test = {{-100}, 0}; /* -100 */
48
49         TEST_ASSERT_EQUAL_STRING("-100.0000000000", lfptoa(&test, LFP_MAX_PRECISION));
50         TEST_ASSERT_EQUAL_STRING("-100000.0000000", lfptoms(&test, LFP_MAX_PRECISION_MS));
51 }
52
53 void
54 test_PositiveIntegerWithFraction(void) {
55         l_fp test = {{200}, ONE_FOURTH}; /* 200.25 */
56
57         TEST_ASSERT_EQUAL_STRING("200.2500000000", lfptoa(&test, LFP_MAX_PRECISION));
58         TEST_ASSERT_EQUAL_STRING("200250.0000000", lfptoms(&test, LFP_MAX_PRECISION_MS));
59 }
60
61 void
62 test_NegativeIntegerWithFraction(void) {
63         l_fp test = {{-100}, ONE_FOURTH}; /* -99.75 */
64
65         TEST_ASSERT_EQUAL_STRING("-99.7500000000", lfptoa(&test, LFP_MAX_PRECISION));
66         TEST_ASSERT_EQUAL_STRING("-99750.0000000", lfptoms(&test, LFP_MAX_PRECISION_MS));
67 }
68
69 void
70 test_RoundingDownToInteger(void) {
71         l_fp test = {{10}, ONE_FOURTH}; /* 10.25 */
72
73         TEST_ASSERT_EQUAL_STRING("10", lfptoa(&test, 0));
74         TEST_ASSERT_EQUAL_STRING("10250", lfptoms(&test, 0));
75 }
76
77 void
78 test_RoundingMiddleToInteger(void) {
79         l_fp test = {{10}, HALF}; /* 10.5 */
80
81         TEST_ASSERT_EQUAL_STRING("11", lfptoa(&test, 0));
82         TEST_ASSERT_EQUAL_STRING("10500", lfptoms(&test, 0));
83 }
84
85 void
86 test_RoundingUpToInteger(void) {
87         l_fp test = {{5}, THREE_FOURTH}; /* 5.75 */
88
89         TEST_ASSERT_EQUAL_STRING("6", lfptoa(&test, 0));
90         TEST_ASSERT_EQUAL_STRING("5750", lfptoms(&test, 0));
91 }
92
93 void
94 test_SingleDecimal(void) {
95         l_fp test = {{8}, ONE_FOURTH}; /* 8.25 */
96
97         TEST_ASSERT_EQUAL_STRING("8.3", lfptoa(&test, 1));
98         TEST_ASSERT_EQUAL_STRING("8250.0", lfptoms(&test, 1));
99 }
100
101 void
102 test_MillisecondsRoundingUp(void) {
103         l_fp test = {{1}, HALF_PROMILLE_UP}; /* slightly more than 1.0005 */
104
105         TEST_ASSERT_EQUAL_STRING("1.0", lfptoa(&test, 1));
106
107         TEST_ASSERT_EQUAL_STRING("1000.5", lfptoms(&test, 1));
108         TEST_ASSERT_EQUAL_STRING("1001", lfptoms(&test, 0));
109 }
110
111 void
112 test_MillisecondsRoundingDown(void) {
113         l_fp test = {{1}, HALF_PROMILLE_DOWN}; /* slightly less than 1.0005 */
114
115         TEST_ASSERT_EQUAL_STRING("1.0", lfptoa(&test, 1));
116
117         TEST_ASSERT_EQUAL_STRING("1000.5", lfptoms(&test, 1));
118         TEST_ASSERT_EQUAL_STRING("1000", lfptoms(&test, 0));
119 }
120
121 void test_UnsignedInteger(void) {
122         l_fp test = {{3000000000UL}, 0};
123
124         TEST_ASSERT_EQUAL_STRING("3000000000.0", ulfptoa(&test, 1));
125 }