1290001Sglebius/*
2290001Sglebius * This file contains test for both fptoa and fptoms (which uses dofptoa),
3290001Sglebius * since all these functions are very similar.
4290001Sglebius */
5290001Sglebius#include "config.h"
6290001Sglebius#include "ntp_fp.h"
7293896Sglebius#include "ntp_stdlib.h"
8290001Sglebius#include "unity.h"
9290001Sglebius
10290001Sglebius#define SFP_MAX_PRECISION 6
11290001Sglebius
12293896Sglebiusvoid setUp(void);
13290001Sglebiusvoid test_PositiveInteger(void);
14290001Sglebiusvoid test_NegativeInteger(void);
15290001Sglebiusvoid test_PositiveIntegerPositiveFraction(void);
16290001Sglebiusvoid test_NegativeIntegerNegativeFraction(void);
17290001Sglebiusvoid test_PositiveIntegerNegativeFraction(void);
18290001Sglebiusvoid test_NegativeIntegerPositiveFraction(void);
19290001Sglebiusvoid test_SingleDecimalInteger(void);
20290001Sglebiusvoid test_SingleDecimalRounding(void);
21290001Sglebius
22290001Sglebius
23293896Sglebiusvoid
24293896SglebiussetUp(void)
25293896Sglebius{
26293896Sglebius	init_lib();
27293896Sglebius
28293896Sglebius	return;
29293896Sglebius}
30293896Sglebius
31293896Sglebius
32290001Sglebiusvoid test_PositiveInteger(void)
33290001Sglebius{
34290001Sglebius	s_fp test = 300 << 16; // exact 300.000000
35290001Sglebius
36290001Sglebius	TEST_ASSERT_EQUAL_STRING("300.000000", fptoa(test, SFP_MAX_PRECISION));
37290001Sglebius	TEST_ASSERT_EQUAL_STRING("300000.000", fptoms(test, SFP_MAX_PRECISION));
38290001Sglebius}
39290001Sglebius
40290001Sglebiusvoid test_NegativeInteger(void)
41290001Sglebius{
42310419Sdelphij	s_fp test = -(200 << 16); // exact -200.000000
43290001Sglebius
44290001Sglebius	TEST_ASSERT_EQUAL_STRING("-200.000000", fptoa(test, SFP_MAX_PRECISION));
45290001Sglebius	TEST_ASSERT_EQUAL_STRING("-200000.000", fptoms(test, SFP_MAX_PRECISION));
46290001Sglebius}
47290001Sglebius
48290001Sglebiusvoid test_PositiveIntegerPositiveFraction(void)
49290001Sglebius{
50290001Sglebius	s_fp test = (300 << 16) + (1 << 15); // 300 + 0.5
51290001Sglebius
52290001Sglebius	TEST_ASSERT_EQUAL_STRING("300.500000", fptoa(test, SFP_MAX_PRECISION));
53290001Sglebius	TEST_ASSERT_EQUAL_STRING("300500.000", fptoms(test, SFP_MAX_PRECISION));
54290001Sglebius}
55290001Sglebius
56290001Sglebiusvoid test_NegativeIntegerNegativeFraction(void)
57290001Sglebius{
58310419Sdelphij	s_fp test = -(200 << 16) - (1 << 15); // -200 - 0.5
59290001Sglebius
60290001Sglebius	TEST_ASSERT_EQUAL_STRING("-200.500000", fptoa(test, SFP_MAX_PRECISION));
61290001Sglebius	TEST_ASSERT_EQUAL_STRING("-200500.000", fptoms(test, SFP_MAX_PRECISION));
62290001Sglebius}
63290001Sglebius
64290001Sglebiusvoid test_PositiveIntegerNegativeFraction(void)
65290001Sglebius{
66290001Sglebius	s_fp test = (300 << 16) - (1 << 14); // 300 - 0.25
67290001Sglebius
68290001Sglebius	TEST_ASSERT_EQUAL_STRING("299.750000", fptoa(test, SFP_MAX_PRECISION));
69290001Sglebius	TEST_ASSERT_EQUAL_STRING("299750.000", fptoms(test, SFP_MAX_PRECISION));
70290001Sglebius}
71290001Sglebius
72290001Sglebiusvoid test_NegativeIntegerPositiveFraction(void)
73290001Sglebius{
74310419Sdelphij	s_fp test = -(200 << 16) + (1 << 14)*3; // -200 + 0.75
75290001Sglebius
76290001Sglebius	TEST_ASSERT_EQUAL_STRING("-199.250000", fptoa(test, SFP_MAX_PRECISION));
77290001Sglebius	TEST_ASSERT_EQUAL_STRING("-199250.000", fptoms(test, SFP_MAX_PRECISION));
78290001Sglebius}
79290001Sglebius
80290001Sglebiusvoid test_SingleDecimalInteger(void)
81290001Sglebius{
82290001Sglebius	s_fp test = 300 << 16; // 300
83290001Sglebius
84290001Sglebius	TEST_ASSERT_EQUAL_STRING("300.0", fptoa(test, 1));
85290001Sglebius	TEST_ASSERT_EQUAL_STRING("300000.0", fptoms(test, 1));
86290001Sglebius}
87290001Sglebius
88290001Sglebiusvoid test_SingleDecimalRounding(void)
89290001Sglebius{
90290001Sglebius	s_fp test = (2 << 16) + (1 << 14)*3; // 2 + 0.25*3 = 2.75
91290001Sglebius
92290001Sglebius	TEST_ASSERT_EQUAL_STRING("2.8", fptoa(test, 1));
93290001Sglebius	TEST_ASSERT_EQUAL_STRING("2750.0", fptoms(test, 1));
94290001Sglebius}
95