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
14void setUp(void);
15void test_PositiveInteger(void);
16void test_NegativeInteger(void);
17void test_PositiveIntegerPositiveFraction(void);
18void test_NegativeIntegerNegativeFraction(void);
19void test_PositiveIntegerNegativeFraction(void);
20void test_NegativeIntegerPositiveFraction(void);
21void test_SingleDecimalInteger(void);
22void test_SingleDecimalRounding(void);
23
24
25void
26setUp(void)
27{
28	init_lib();
29
30	return;
31}
32
33
34void 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
42void 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
50void 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
58void 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
66void 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
74void 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
82void 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
90void 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