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