]> CyberLeo.Net >> Repos - FreeBSD/releng/10.2.git/blob - contrib/ntp/tests/libntp/g_sfptostr.cpp
Fix a regression with SA-15:24 patch that prevented NIS from
[FreeBSD/releng/10.2.git] / contrib / ntp / tests / libntp / g_sfptostr.cpp
1 /* 
2  * This file contains test for both fptoa and fptoms (which uses dofptoa),
3  * since all these functions are very similar.
4  */
5
6 #include "g_libntptest.h"
7
8 extern "C" {
9 #include "ntp_fp.h"
10 };
11
12 class sfptostr : public libntptest {
13 protected:
14         static const int SFP_MAX_PRECISION = 6;
15 };
16
17 TEST_F(sfptostr, PositiveInteger) {
18         s_fp test = 300 << 16; // exact 300.000000
19
20         EXPECT_STREQ("300.000000", fptoa(test, SFP_MAX_PRECISION));
21         EXPECT_STREQ("300000.000", fptoms(test, SFP_MAX_PRECISION));
22 }
23
24 TEST_F(sfptostr, NegativeInteger) {
25         s_fp test = -200 << 16; // exact -200.000000
26
27         EXPECT_STREQ("-200.000000", fptoa(test, SFP_MAX_PRECISION));
28         EXPECT_STREQ("-200000.000", fptoms(test, SFP_MAX_PRECISION));
29 }
30
31 TEST_F(sfptostr, PositiveIntegerPositiveFraction) {
32         s_fp test = (300 << 16) + (1 << 15); // 300 + 0.5
33
34         EXPECT_STREQ("300.500000", fptoa(test, SFP_MAX_PRECISION));
35         EXPECT_STREQ("300500.000", fptoms(test, SFP_MAX_PRECISION));
36 }
37
38 TEST_F(sfptostr, NegativeIntegerNegativeFraction) {
39         s_fp test = (-200 << 16) - (1 << 15); // -200 - 0.5
40
41         EXPECT_STREQ("-200.500000", fptoa(test, SFP_MAX_PRECISION));
42         EXPECT_STREQ("-200500.000", fptoms(test, SFP_MAX_PRECISION));
43 }
44
45 TEST_F(sfptostr, PositiveIntegerNegativeFraction) {
46         s_fp test = (300 << 16) - (1 << 14); // 300 - 0.25
47
48         EXPECT_STREQ("299.750000", fptoa(test, SFP_MAX_PRECISION));
49         EXPECT_STREQ("299750.000", fptoms(test, SFP_MAX_PRECISION));
50 }
51
52 TEST_F(sfptostr, NegativeIntegerPositiveFraction) {
53         s_fp test = (-200 << 16) + (1 << 14)*3; // -200 + 0.75
54
55         EXPECT_STREQ("-199.250000", fptoa(test, SFP_MAX_PRECISION));
56         EXPECT_STREQ("-199250.000", fptoms(test, SFP_MAX_PRECISION));
57 }
58
59 TEST_F(sfptostr, SingleDecimalInteger) {
60         s_fp test = 300 << 16; // 300
61
62         EXPECT_STREQ("300.0", fptoa(test, 1));
63         EXPECT_STREQ("300000.0", fptoms(test, 1));
64 }
65
66 TEST_F(sfptostr, SingleDecimalRounding) {
67         s_fp test = (2 << 16) + (1 << 14)*3; // 2 + 0.25*3 = 2.75
68
69         EXPECT_STREQ("2.8", fptoa(test, 1));
70         EXPECT_STREQ("2750.0", fptoms(test, 1));
71 }