]> CyberLeo.Net >> Repos - FreeBSD/releng/10.2.git/blob - contrib/ntp/tests/libntp/sfptostr.c
Upgrade NTP to 4.2.8p4.
[FreeBSD/releng/10.2.git] / contrib / ntp / tests / libntp / sfptostr.c
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 void test_NegativeInteger(void);
13 void test_PositiveIntegerPositiveFraction(void);
14 void test_NegativeIntegerNegativeFraction(void);
15 void test_PositiveIntegerNegativeFraction(void);
16 void test_NegativeIntegerPositiveFraction(void);
17 void test_SingleDecimalInteger(void);
18 void test_SingleDecimalRounding(void);
19
20
21 void test_PositiveInteger(void)
22 {
23         s_fp test = 300 << 16; // exact 300.000000
24
25         TEST_ASSERT_EQUAL_STRING("300.000000", fptoa(test, SFP_MAX_PRECISION));
26         TEST_ASSERT_EQUAL_STRING("300000.000", fptoms(test, SFP_MAX_PRECISION));
27 }
28
29 void test_NegativeInteger(void)
30 {
31         s_fp test = -200 << 16; // exact -200.000000
32
33         TEST_ASSERT_EQUAL_STRING("-200.000000", fptoa(test, SFP_MAX_PRECISION));
34         TEST_ASSERT_EQUAL_STRING("-200000.000", fptoms(test, SFP_MAX_PRECISION));
35 }
36
37 void test_PositiveIntegerPositiveFraction(void)
38 {
39         s_fp test = (300 << 16) + (1 << 15); // 300 + 0.5
40
41         TEST_ASSERT_EQUAL_STRING("300.500000", fptoa(test, SFP_MAX_PRECISION));
42         TEST_ASSERT_EQUAL_STRING("300500.000", fptoms(test, SFP_MAX_PRECISION));
43 }
44
45 void test_NegativeIntegerNegativeFraction(void)
46 {
47         s_fp test = (-200 << 16) - (1 << 15); // -200 - 0.5
48
49         TEST_ASSERT_EQUAL_STRING("-200.500000", fptoa(test, SFP_MAX_PRECISION));
50         TEST_ASSERT_EQUAL_STRING("-200500.000", fptoms(test, SFP_MAX_PRECISION));
51 }
52
53 void test_PositiveIntegerNegativeFraction(void)
54 {
55         s_fp test = (300 << 16) - (1 << 14); // 300 - 0.25
56
57         TEST_ASSERT_EQUAL_STRING("299.750000", fptoa(test, SFP_MAX_PRECISION));
58         TEST_ASSERT_EQUAL_STRING("299750.000", fptoms(test, SFP_MAX_PRECISION));
59 }
60
61 void test_NegativeIntegerPositiveFraction(void)
62 {
63         s_fp test = (-200 << 16) + (1 << 14)*3; // -200 + 0.75
64
65         TEST_ASSERT_EQUAL_STRING("-199.250000", fptoa(test, SFP_MAX_PRECISION));
66         TEST_ASSERT_EQUAL_STRING("-199250.000", fptoms(test, SFP_MAX_PRECISION));
67 }
68
69 void test_SingleDecimalInteger(void)
70 {
71         s_fp test = 300 << 16; // 300
72
73         TEST_ASSERT_EQUAL_STRING("300.0", fptoa(test, 1));
74         TEST_ASSERT_EQUAL_STRING("300000.0", fptoms(test, 1));
75 }
76
77 void test_SingleDecimalRounding(void)
78 {
79         s_fp test = (2 << 16) + (1 << 14)*3; // 2 + 0.25*3 = 2.75
80
81         TEST_ASSERT_EQUAL_STRING("2.8", fptoa(test, 1));
82         TEST_ASSERT_EQUAL_STRING("2750.0", fptoms(test, 1));
83 }