xref: /netbsd-src/external/bsd/ntp/dist/tests/libntp/strtolfp.c (revision 63aea4bd5b445e491ff0389fe27ec78b3099dba3)
1 /*	$NetBSD: strtolfp.c,v 1.1.1.3 2015/10/23 17:47:45 christos Exp $	*/
2 
3 #include "config.h"
4 
5 #include "ntp_stdlib.h"
6 #include "ntp_calendar.h"
7 
8 #include "unity.h"
9 #include "lfptest.h"
10 
11 /* This file tests both atolfp and mstolfp */
12 
13 void test_PositiveInteger(void);
14 void test_NegativeInteger(void);
15 void test_PositiveFraction(void);
16 void test_NegativeFraction(void);
17 void test_PositiveMsFraction(void);
18 void test_NegativeMsFraction(void);
19 void test_InvalidChars(void);
20 
21 
22 void test_PositiveInteger(void) {
23 	const char *str = "500";
24 	const char *str_ms = "500000";
25 
26 	l_fp expected = {{500},0};
27 	l_fp actual, actual_ms;
28 
29 	TEST_ASSERT_TRUE(atolfp(str, &actual));
30 	TEST_ASSERT_TRUE(mstolfp(str_ms, &actual_ms));
31 
32 	TEST_ASSERT_TRUE(IsEqual(expected, actual));
33 	TEST_ASSERT_TRUE(IsEqual(expected, actual_ms));
34 }
35 
36 void test_NegativeInteger(void) {
37 	const char *str = "-300";
38 	const char *str_ms = "-300000";
39 
40 	l_fp expected;
41 	expected.l_i = -300;
42 	expected.l_uf = 0;
43 
44 	l_fp actual, actual_ms;
45 
46 	TEST_ASSERT_TRUE(atolfp(str, &actual));
47 	TEST_ASSERT_TRUE(mstolfp(str_ms, &actual_ms));
48 
49 	TEST_ASSERT_TRUE(IsEqual(expected, actual));
50 	TEST_ASSERT_TRUE(IsEqual(expected, actual_ms));
51 }
52 
53 void test_PositiveFraction(void) {
54 	const char *str = "+500.5";
55 	const char *str_ms = "500500.0";
56 
57 	l_fp expected = {{500}, HALF};
58 	l_fp actual, actual_ms;
59 
60 	TEST_ASSERT_TRUE(atolfp(str, &actual));
61 	TEST_ASSERT_TRUE(mstolfp(str_ms, &actual_ms));
62 
63 	TEST_ASSERT_TRUE(IsEqual(expected, actual));
64 	TEST_ASSERT_TRUE(IsEqual(expected, actual_ms));
65 }
66 
67 void test_NegativeFraction(void) {
68 	const char *str = "-300.75";
69 	const char *str_ms = "-300750";
70 
71 	l_fp expected;
72 	expected.l_i = -301;
73 	expected.l_uf = QUARTER;
74 
75 	l_fp actual, actual_ms;
76 
77 	TEST_ASSERT_TRUE(atolfp(str, &actual));
78 	TEST_ASSERT_TRUE(mstolfp(str_ms, &actual_ms));
79 
80 	TEST_ASSERT_TRUE(IsEqual(expected, actual));
81 	TEST_ASSERT_TRUE(IsEqual(expected, actual_ms));
82 }
83 
84 void test_PositiveMsFraction(void) {
85 	const char *str = "300.00025";
86 	const char *str_ms = "300000.25";
87 
88 	l_fp expected = {{300}, QUARTER_PROMILLE_APPRX};
89 	l_fp actual, actual_ms;
90 
91 
92 	TEST_ASSERT_TRUE(atolfp(str, &actual));
93 	TEST_ASSERT_TRUE(mstolfp(str_ms, &actual_ms));
94 
95 	TEST_ASSERT_TRUE(IsEqual(expected, actual));
96 	TEST_ASSERT_TRUE(IsEqual(expected, actual_ms));
97 
98 }
99 
100 void test_NegativeMsFraction(void) {
101 	const char *str = "-199.99975";
102 	const char *str_ms = "-199999.75";
103 
104 	l_fp expected;
105 	expected.l_i = -200;
106 	expected.l_uf = QUARTER_PROMILLE_APPRX;
107 
108 	l_fp actual, actual_ms;
109 
110 	TEST_ASSERT_TRUE(atolfp(str, &actual));
111 	TEST_ASSERT_TRUE(mstolfp(str_ms, &actual_ms));
112 
113 	TEST_ASSERT_TRUE(IsEqual(expected, actual));
114 	TEST_ASSERT_TRUE(IsEqual(expected, actual_ms));
115 
116 }
117 
118 void test_InvalidChars(void) {
119 	const char *str = "500.4a2";
120 	l_fp actual, actual_ms;
121 
122 	TEST_ASSERT_FALSE(atolfp(str, &actual));
123 	TEST_ASSERT_FALSE(mstolfp(str, &actual_ms));
124 }
125 
126