]> CyberLeo.Net >> Repos - FreeBSD/releng/10.2.git/blob - contrib/ntp/tests/libntp/lfptostr.c
- Copy stable/10@285827 to releng/10.2 in preparation for 10.2-RC1
[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_calendar.h"
10 #include "ntp_fp.h"
11
12 #include "unity.h"
13
14 static const int LFP_MAX_PRECISION = 10;
15 static const int LFP_MAX_PRECISION_MS = 7;
16
17 static const int ONE_FOURTH = 1073741824; // (1 << 30)
18 static const int HALF = (1 << 31);
19 static const int THREE_FOURTH = -1073741824;
20 static const int HALF_PROMILLE_UP = 2147484; // slightly more than 0.0005
21 static const int HALF_PROMILLE_DOWN = 2147483; // slightly less than 0.0005
22
23 void test_PositiveInteger(void) {
24         l_fp test = {200, 0}; // exact 200.0000000000
25
26         TEST_ASSERT_EQUAL_STRING("200.0000000000", mfptoa(test.l_ui, test.l_uf, LFP_MAX_PRECISION));
27         TEST_ASSERT_EQUAL_STRING("200000.0000000", mfptoms(test.l_ui, test.l_uf, LFP_MAX_PRECISION_MS));
28 }
29
30 void test_NegativeInteger(void) {
31         l_fp test = {-100, 0}; // -100
32
33         TEST_ASSERT_EQUAL_STRING("-100.0000000000", lfptoa(&test, LFP_MAX_PRECISION));
34         TEST_ASSERT_EQUAL_STRING("-100000.0000000", lfptoms(&test, LFP_MAX_PRECISION_MS));
35 }
36
37 void test_PositiveIntegerWithFraction(void) {
38         l_fp test = {200, ONE_FOURTH}; // 200.25
39
40         TEST_ASSERT_EQUAL_STRING("200.2500000000", lfptoa(&test, LFP_MAX_PRECISION));
41         TEST_ASSERT_EQUAL_STRING("200250.0000000", lfptoms(&test, LFP_MAX_PRECISION_MS));
42 }
43
44 void test_NegativeIntegerWithFraction(void) {
45         l_fp test = {-100, ONE_FOURTH}; // -99.75
46
47         TEST_ASSERT_EQUAL_STRING("-99.7500000000", lfptoa(&test, LFP_MAX_PRECISION));
48         TEST_ASSERT_EQUAL_STRING("-99750.0000000", lfptoms(&test, LFP_MAX_PRECISION_MS));
49 }
50
51 void test_RoundingDownToInteger(void) {
52         l_fp test = {10, ONE_FOURTH}; // 10.25
53
54         TEST_ASSERT_EQUAL_STRING("10", lfptoa(&test, 0));
55         TEST_ASSERT_EQUAL_STRING("10250", lfptoms(&test, 0));
56 }
57
58 void test_RoundingMiddleToInteger(void) {
59         l_fp test = {10, HALF}; // 10.5
60
61         TEST_ASSERT_EQUAL_STRING("11", lfptoa(&test, 0));
62         TEST_ASSERT_EQUAL_STRING("10500", lfptoms(&test, 0));
63 }
64
65 void test_RoundingUpToInteger(void) {
66         l_fp test = {5, THREE_FOURTH}; // 5.75
67
68         TEST_ASSERT_EQUAL_STRING("6", lfptoa(&test, 0));
69         TEST_ASSERT_EQUAL_STRING("5750", lfptoms(&test, 0));
70 }
71
72 void test_SingleDecimal(void) {
73         l_fp test = {8, ONE_FOURTH}; // 8.25
74
75         TEST_ASSERT_EQUAL_STRING("8.3", lfptoa(&test, 1));
76         TEST_ASSERT_EQUAL_STRING("8250.0", lfptoms(&test, 1));
77 }
78
79 void test_MillisecondsRoundingUp(void) {
80         l_fp test = {1, HALF_PROMILLE_UP}; //slightly more than 1.0005
81
82         TEST_ASSERT_EQUAL_STRING("1.0", lfptoa(&test, 1));
83
84         TEST_ASSERT_EQUAL_STRING("1000.5", lfptoms(&test, 1));
85         TEST_ASSERT_EQUAL_STRING("1001", lfptoms(&test, 0));
86 }
87
88 void test_MillisecondsRoundingDown(void) {
89         l_fp test = {1, HALF_PROMILLE_DOWN}; // slightly less than 1.0005
90
91         TEST_ASSERT_EQUAL_STRING("1.0", lfptoa(&test, 1));
92
93         TEST_ASSERT_EQUAL_STRING("1000.5", lfptoms(&test, 1));
94         TEST_ASSERT_EQUAL_STRING("1000", lfptoms(&test, 0));
95 }
96
97 void test_UnsignedInteger(void) {
98         l_fp test = {3000000000UL, 0};
99
100         TEST_ASSERT_EQUAL_STRING("3000000000.0", ulfptoa(&test, 1));
101 }
102
103