lfptostr.c revision 293896
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
13static const int LFP_MAX_PRECISION = 10;
14static const int LFP_MAX_PRECISION_MS = 7;
15
16static const int ONE_FOURTH = 1073741824; /* (1 << 30) */
17static const int HALF = (1 << 31);
18static const int THREE_FOURTH = -1073741824;
19static const int HALF_PROMILLE_UP = 2147484; /* slightly more than 0.0005 */
20static const int HALF_PROMILLE_DOWN = 2147483; /* slightly less than 0.0005 */
21
22
23void setUp(void);
24void test_PositiveInteger(void);
25void test_NegativeInteger(void);
26void test_PositiveIntegerWithFraction(void);
27void test_NegativeIntegerWithFraction(void);
28void test_RoundingDownToInteger(void);
29void test_RoundingMiddleToInteger(void);
30void test_RoundingUpToInteger(void);
31void test_SingleDecimal(void);
32void test_MillisecondsRoundingUp(void);
33void test_MillisecondsRoundingDown(void);
34void test_UnsignedInteger(void);
35
36
37void
38setUp(void)
39{
40	init_lib();
41
42	return;
43}
44
45
46void
47test_PositiveInteger(void) {
48	l_fp test = {{200}, 0}; /* exact 200.0000000000 */
49
50	TEST_ASSERT_EQUAL_STRING("200.0000000000", mfptoa(test.l_ui, test.l_uf, LFP_MAX_PRECISION));
51	TEST_ASSERT_EQUAL_STRING("200000.0000000", mfptoms(test.l_ui, test.l_uf, LFP_MAX_PRECISION_MS));
52}
53
54void
55test_NegativeInteger(void) {
56	l_fp test = {{-100}, 0}; /* -100 */
57
58	TEST_ASSERT_EQUAL_STRING("-100.0000000000", lfptoa(&test, LFP_MAX_PRECISION));
59	TEST_ASSERT_EQUAL_STRING("-100000.0000000", lfptoms(&test, LFP_MAX_PRECISION_MS));
60}
61
62void
63test_PositiveIntegerWithFraction(void) {
64	l_fp test = {{200}, ONE_FOURTH}; /* 200.25 */
65
66	TEST_ASSERT_EQUAL_STRING("200.2500000000", lfptoa(&test, LFP_MAX_PRECISION));
67	TEST_ASSERT_EQUAL_STRING("200250.0000000", lfptoms(&test, LFP_MAX_PRECISION_MS));
68}
69
70void
71test_NegativeIntegerWithFraction(void) {
72	l_fp test = {{-100}, ONE_FOURTH}; /* -99.75 */
73
74	TEST_ASSERT_EQUAL_STRING("-99.7500000000", lfptoa(&test, LFP_MAX_PRECISION));
75	TEST_ASSERT_EQUAL_STRING("-99750.0000000", lfptoms(&test, LFP_MAX_PRECISION_MS));
76}
77
78void
79test_RoundingDownToInteger(void) {
80	l_fp test = {{10}, ONE_FOURTH}; /* 10.25 */
81
82	TEST_ASSERT_EQUAL_STRING("10", lfptoa(&test, 0));
83	TEST_ASSERT_EQUAL_STRING("10250", lfptoms(&test, 0));
84}
85
86void
87test_RoundingMiddleToInteger(void) {
88	l_fp test = {{10}, HALF}; /* 10.5 */
89
90	TEST_ASSERT_EQUAL_STRING("11", lfptoa(&test, 0));
91	TEST_ASSERT_EQUAL_STRING("10500", lfptoms(&test, 0));
92}
93
94void
95test_RoundingUpToInteger(void) {
96	l_fp test = {{5}, THREE_FOURTH}; /* 5.75 */
97
98	TEST_ASSERT_EQUAL_STRING("6", lfptoa(&test, 0));
99	TEST_ASSERT_EQUAL_STRING("5750", lfptoms(&test, 0));
100}
101
102void
103test_SingleDecimal(void) {
104	l_fp test = {{8}, ONE_FOURTH}; /* 8.25 */
105
106	TEST_ASSERT_EQUAL_STRING("8.3", lfptoa(&test, 1));
107	TEST_ASSERT_EQUAL_STRING("8250.0", lfptoms(&test, 1));
108}
109
110void
111test_MillisecondsRoundingUp(void) {
112	l_fp test = {{1}, HALF_PROMILLE_UP}; /* slightly more than 1.0005 */
113
114	TEST_ASSERT_EQUAL_STRING("1.0", lfptoa(&test, 1));
115
116	TEST_ASSERT_EQUAL_STRING("1000.5", lfptoms(&test, 1));
117	TEST_ASSERT_EQUAL_STRING("1001", lfptoms(&test, 0));
118}
119
120void
121test_MillisecondsRoundingDown(void) {
122	l_fp test = {{1}, HALF_PROMILLE_DOWN}; /* slightly less than 1.0005 */
123
124	TEST_ASSERT_EQUAL_STRING("1.0", lfptoa(&test, 1));
125
126	TEST_ASSERT_EQUAL_STRING("1000.5", lfptoms(&test, 1));
127	TEST_ASSERT_EQUAL_STRING("1000", lfptoms(&test, 0));
128}
129
130void test_UnsignedInteger(void) {
131	l_fp test = {{3000000000UL}, 0};
132
133	TEST_ASSERT_EQUAL_STRING("3000000000.0", ulfptoa(&test, 1));
134}
135