xref: /netbsd-src/external/bsd/ntp/dist/tests/libntp/sfptostr.c (revision f17b710f3d406bee67aa39c65053114ab78297c5)
1 /*
2  * This file contains test for both fptoa and fptoms (which uses dofptoa),
3  * since all these functions are very similar.
4  */
5 #include "config.h"
6 #include "ntp_fp.h"
7 #include "unity.h"
8 
9 #define SFP_MAX_PRECISION 6
10 
11 void test_PositiveInteger(void)
12 {
13 	s_fp test = 300 << 16; // exact 300.000000
14 
15 	TEST_ASSERT_EQUAL_STRING("300.000000", fptoa(test, SFP_MAX_PRECISION));
16 	TEST_ASSERT_EQUAL_STRING("300000.000", fptoms(test, SFP_MAX_PRECISION));
17 }
18 
19 void test_NegativeInteger(void)
20 {
21 	s_fp test = -200 << 16; // exact -200.000000
22 
23 	TEST_ASSERT_EQUAL_STRING("-200.000000", fptoa(test, SFP_MAX_PRECISION));
24 	TEST_ASSERT_EQUAL_STRING("-200000.000", fptoms(test, SFP_MAX_PRECISION));
25 }
26 
27 void test_PositiveIntegerPositiveFraction(void)
28 {
29 	s_fp test = (300 << 16) + (1 << 15); // 300 + 0.5
30 
31 	TEST_ASSERT_EQUAL_STRING("300.500000", fptoa(test, SFP_MAX_PRECISION));
32 	TEST_ASSERT_EQUAL_STRING("300500.000", fptoms(test, SFP_MAX_PRECISION));
33 }
34 
35 void test_NegativeIntegerNegativeFraction(void)
36 {
37 	s_fp test = (-200 << 16) - (1 << 15); // -200 - 0.5
38 
39 	TEST_ASSERT_EQUAL_STRING("-200.500000", fptoa(test, SFP_MAX_PRECISION));
40 	TEST_ASSERT_EQUAL_STRING("-200500.000", fptoms(test, SFP_MAX_PRECISION));
41 }
42 
43 void test_PositiveIntegerNegativeFraction(void)
44 {
45 	s_fp test = (300 << 16) - (1 << 14); // 300 - 0.25
46 
47 	TEST_ASSERT_EQUAL_STRING("299.750000", fptoa(test, SFP_MAX_PRECISION));
48 	TEST_ASSERT_EQUAL_STRING("299750.000", fptoms(test, SFP_MAX_PRECISION));
49 }
50 
51 void test_NegativeIntegerPositiveFraction(void)
52 {
53 	s_fp test = (-200 << 16) + (1 << 14)*3; // -200 + 0.75
54 
55 	TEST_ASSERT_EQUAL_STRING("-199.250000", fptoa(test, SFP_MAX_PRECISION));
56 	TEST_ASSERT_EQUAL_STRING("-199250.000", fptoms(test, SFP_MAX_PRECISION));
57 }
58 
59 void test_SingleDecimalInteger(void)
60 {
61 	s_fp test = 300 << 16; // 300
62 
63 	TEST_ASSERT_EQUAL_STRING("300.0", fptoa(test, 1));
64 	TEST_ASSERT_EQUAL_STRING("300000.0", fptoms(test, 1));
65 }
66 
67 void test_SingleDecimalRounding(void)
68 {
69 	s_fp test = (2 << 16) + (1 << 14)*3; // 2 + 0.25*3 = 2.75
70 
71 	TEST_ASSERT_EQUAL_STRING("2.8", fptoa(test, 1));
72 	TEST_ASSERT_EQUAL_STRING("2750.0", fptoms(test, 1));
73 }
74