sfptostr.c revision 293896
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 "ntp_stdlib.h"
8#include "unity.h"
9
10#define SFP_MAX_PRECISION 6
11
12void setUp(void);
13void test_PositiveInteger(void);
14void test_NegativeInteger(void);
15void test_PositiveIntegerPositiveFraction(void);
16void test_NegativeIntegerNegativeFraction(void);
17void test_PositiveIntegerNegativeFraction(void);
18void test_NegativeIntegerPositiveFraction(void);
19void test_SingleDecimalInteger(void);
20void test_SingleDecimalRounding(void);
21
22
23void
24setUp(void)
25{
26	init_lib();
27
28	return;
29}
30
31
32void test_PositiveInteger(void)
33{
34	s_fp test = 300 << 16; // exact 300.000000
35
36	TEST_ASSERT_EQUAL_STRING("300.000000", fptoa(test, SFP_MAX_PRECISION));
37	TEST_ASSERT_EQUAL_STRING("300000.000", fptoms(test, SFP_MAX_PRECISION));
38}
39
40void test_NegativeInteger(void)
41{
42	s_fp test = -200 << 16; // exact -200.000000
43
44	TEST_ASSERT_EQUAL_STRING("-200.000000", fptoa(test, SFP_MAX_PRECISION));
45	TEST_ASSERT_EQUAL_STRING("-200000.000", fptoms(test, SFP_MAX_PRECISION));
46}
47
48void test_PositiveIntegerPositiveFraction(void)
49{
50	s_fp test = (300 << 16) + (1 << 15); // 300 + 0.5
51
52	TEST_ASSERT_EQUAL_STRING("300.500000", fptoa(test, SFP_MAX_PRECISION));
53	TEST_ASSERT_EQUAL_STRING("300500.000", fptoms(test, SFP_MAX_PRECISION));
54}
55
56void test_NegativeIntegerNegativeFraction(void)
57{
58	s_fp test = (-200 << 16) - (1 << 15); // -200 - 0.5
59
60	TEST_ASSERT_EQUAL_STRING("-200.500000", fptoa(test, SFP_MAX_PRECISION));
61	TEST_ASSERT_EQUAL_STRING("-200500.000", fptoms(test, SFP_MAX_PRECISION));
62}
63
64void test_PositiveIntegerNegativeFraction(void)
65{
66	s_fp test = (300 << 16) - (1 << 14); // 300 - 0.25
67
68	TEST_ASSERT_EQUAL_STRING("299.750000", fptoa(test, SFP_MAX_PRECISION));
69	TEST_ASSERT_EQUAL_STRING("299750.000", fptoms(test, SFP_MAX_PRECISION));
70}
71
72void test_NegativeIntegerPositiveFraction(void)
73{
74	s_fp test = (-200 << 16) + (1 << 14)*3; // -200 + 0.75
75
76	TEST_ASSERT_EQUAL_STRING("-199.250000", fptoa(test, SFP_MAX_PRECISION));
77	TEST_ASSERT_EQUAL_STRING("-199250.000", fptoms(test, SFP_MAX_PRECISION));
78}
79
80void test_SingleDecimalInteger(void)
81{
82	s_fp test = 300 << 16; // 300
83
84	TEST_ASSERT_EQUAL_STRING("300.0", fptoa(test, 1));
85	TEST_ASSERT_EQUAL_STRING("300000.0", fptoms(test, 1));
86}
87
88void test_SingleDecimalRounding(void)
89{
90	s_fp test = (2 << 16) + (1 << 14)*3; // 2 + 0.25*3 = 2.75
91
92	TEST_ASSERT_EQUAL_STRING("2.8", fptoa(test, 1));
93	TEST_ASSERT_EQUAL_STRING("2750.0", fptoms(test, 1));
94}
95