1 | /*
|
---|
2 | * Copyright (c) 2019 Matthieu Riolo
|
---|
3 | * All rights reserved.
|
---|
4 | *
|
---|
5 | * Redistribution and use in source and binary forms, with or without
|
---|
6 | * modification, are permitted provided that the following conditions
|
---|
7 | * are met:
|
---|
8 | *
|
---|
9 | * - Redistributions of source code must retain the above copyright
|
---|
10 | * notice, this list of conditions and the following disclaimer.
|
---|
11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
12 | * notice, this list of conditions and the following disclaimer in the
|
---|
13 | * documentation and/or other materials provided with the distribution.
|
---|
14 | * - The name of the author may not be used to endorse or promote products
|
---|
15 | * derived from this software without specific prior written permission.
|
---|
16 | *
|
---|
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
27 | */
|
---|
28 |
|
---|
29 | #include <pcut/pcut.h>
|
---|
30 | #include <ieee_double.h>
|
---|
31 | #include <double_to_str.h>
|
---|
32 |
|
---|
33 | PCUT_INIT;
|
---|
34 |
|
---|
35 | PCUT_TEST_SUITE(double_to_str);
|
---|
36 |
|
---|
37 | PCUT_TEST(double_to_short_str_pos_zero)
|
---|
38 | {
|
---|
39 | size_t size = 255;
|
---|
40 | char buf[size];
|
---|
41 | int dec;
|
---|
42 | ieee_double_t d = extract_ieee_double(0.0);
|
---|
43 | int ret = double_to_short_str(d, buf, size, &dec);
|
---|
44 |
|
---|
45 | PCUT_ASSERT_INT_EQUALS(1, ret);
|
---|
46 | PCUT_ASSERT_INT_EQUALS(0, dec);
|
---|
47 | PCUT_ASSERT_STR_EQUALS("0", buf);
|
---|
48 | }
|
---|
49 |
|
---|
50 | PCUT_TEST(double_to_short_str_neg_zero)
|
---|
51 | {
|
---|
52 | size_t size = 255;
|
---|
53 | char buf[size];
|
---|
54 | int dec;
|
---|
55 | ieee_double_t d = extract_ieee_double(-0.0);
|
---|
56 | int ret = double_to_short_str(d, buf, size, &dec);
|
---|
57 |
|
---|
58 | PCUT_ASSERT_INT_EQUALS(1, ret);
|
---|
59 | PCUT_ASSERT_INT_EQUALS(0, dec);
|
---|
60 | PCUT_ASSERT_STR_EQUALS("0", buf);
|
---|
61 | }
|
---|
62 |
|
---|
63 | PCUT_TEST(double_to_short_str_pos_one)
|
---|
64 | {
|
---|
65 | size_t size = 255;
|
---|
66 | char buf[size];
|
---|
67 | int dec;
|
---|
68 | ieee_double_t d = extract_ieee_double(1.0);
|
---|
69 | int ret = double_to_short_str(d, buf, size, &dec);
|
---|
70 |
|
---|
71 | PCUT_ASSERT_INT_EQUALS(1, ret);
|
---|
72 | PCUT_ASSERT_INT_EQUALS(0, dec);
|
---|
73 | PCUT_ASSERT_STR_EQUALS("1", buf);
|
---|
74 | }
|
---|
75 |
|
---|
76 | PCUT_TEST(double_to_short_str_neg_one)
|
---|
77 | {
|
---|
78 | size_t size = 255;
|
---|
79 | char buf[size];
|
---|
80 | int dec;
|
---|
81 | ieee_double_t d = extract_ieee_double(-1.0);
|
---|
82 | int ret = double_to_short_str(d, buf, size, &dec);
|
---|
83 |
|
---|
84 | PCUT_ASSERT_INT_EQUALS(1, ret);
|
---|
85 | PCUT_ASSERT_INT_EQUALS(0, dec);
|
---|
86 | PCUT_ASSERT_STR_EQUALS("1", buf);
|
---|
87 | }
|
---|
88 |
|
---|
89 | PCUT_TEST(double_to_short_str_small)
|
---|
90 | {
|
---|
91 | size_t size = 255;
|
---|
92 | char buf[size];
|
---|
93 | int dec;
|
---|
94 | ieee_double_t d = extract_ieee_double(1.1);
|
---|
95 | int ret = double_to_short_str(d, buf, size, &dec);
|
---|
96 |
|
---|
97 | PCUT_ASSERT_INT_EQUALS(2, ret);
|
---|
98 | PCUT_ASSERT_INT_EQUALS(-1, dec);
|
---|
99 | PCUT_ASSERT_STR_EQUALS("11", buf);
|
---|
100 | }
|
---|
101 |
|
---|
102 | PCUT_TEST(double_to_short_str_large)
|
---|
103 | {
|
---|
104 | size_t size = 255;
|
---|
105 | char buf[size];
|
---|
106 | int dec;
|
---|
107 | ieee_double_t d = extract_ieee_double(1234.56789);
|
---|
108 | int ret = double_to_short_str(d, buf, size, &dec);
|
---|
109 |
|
---|
110 | PCUT_ASSERT_INT_EQUALS(9, ret);
|
---|
111 | PCUT_ASSERT_INT_EQUALS(-5, dec);
|
---|
112 | PCUT_ASSERT_STR_EQUALS("123456789", buf);
|
---|
113 | }
|
---|
114 |
|
---|
115 | PCUT_TEST(double_to_short_str_mill)
|
---|
116 | {
|
---|
117 | size_t size = 255;
|
---|
118 | char buf[size];
|
---|
119 | int dec;
|
---|
120 | ieee_double_t d = extract_ieee_double(1000000.0);
|
---|
121 | int ret = double_to_short_str(d, buf, size, &dec);
|
---|
122 |
|
---|
123 | PCUT_ASSERT_INT_EQUALS(1, ret);
|
---|
124 | PCUT_ASSERT_INT_EQUALS(6, dec);
|
---|
125 | PCUT_ASSERT_STR_EQUALS("1", buf);
|
---|
126 | }
|
---|
127 |
|
---|
128 | PCUT_TEST(double_to_fixed_str_zero)
|
---|
129 | {
|
---|
130 | size_t size = 255;
|
---|
131 | char buf[size];
|
---|
132 | int dec;
|
---|
133 | ieee_double_t d = extract_ieee_double(0.0);
|
---|
134 | int ret = double_to_fixed_str(d, -1, 3, buf, size, &dec);
|
---|
135 |
|
---|
136 | PCUT_ASSERT_INT_EQUALS(1, ret);
|
---|
137 | PCUT_ASSERT_INT_EQUALS(0, dec);
|
---|
138 | PCUT_ASSERT_STR_EQUALS("0", buf);
|
---|
139 | }
|
---|
140 |
|
---|
141 | PCUT_TEST(double_to_fixed_str_pos_one)
|
---|
142 | {
|
---|
143 | size_t size = 255;
|
---|
144 | char buf[size];
|
---|
145 | int dec;
|
---|
146 | ieee_double_t d = extract_ieee_double(1.0);
|
---|
147 | int ret = double_to_fixed_str(d, -1, 3, buf, size, &dec);
|
---|
148 |
|
---|
149 | PCUT_ASSERT_INT_EQUALS(4, ret);
|
---|
150 | PCUT_ASSERT_INT_EQUALS(-3, dec);
|
---|
151 | PCUT_ASSERT_STR_EQUALS("1000", buf);
|
---|
152 | }
|
---|
153 |
|
---|
154 | PCUT_TEST(double_to_fixed_str_neg_one)
|
---|
155 | {
|
---|
156 | size_t size = 255;
|
---|
157 | char buf[size];
|
---|
158 | int dec;
|
---|
159 | ieee_double_t d = extract_ieee_double(-1.0);
|
---|
160 | int ret = double_to_fixed_str(d, -1, 3, buf, size, &dec);
|
---|
161 |
|
---|
162 | PCUT_ASSERT_INT_EQUALS(4, ret);
|
---|
163 | PCUT_ASSERT_INT_EQUALS(-3, dec);
|
---|
164 | PCUT_ASSERT_STR_EQUALS("1000", buf);
|
---|
165 | }
|
---|
166 |
|
---|
167 | PCUT_TEST(double_to_fixed_str_small)
|
---|
168 | {
|
---|
169 | size_t size = 255;
|
---|
170 | char buf[size];
|
---|
171 | int dec;
|
---|
172 | ieee_double_t d = extract_ieee_double(1.1);
|
---|
173 | int ret = double_to_fixed_str(d, -1, 3, buf, size, &dec);
|
---|
174 |
|
---|
175 | PCUT_ASSERT_INT_EQUALS(4, ret);
|
---|
176 | PCUT_ASSERT_INT_EQUALS(-3, dec);
|
---|
177 | PCUT_ASSERT_STR_EQUALS("1100", buf);
|
---|
178 | }
|
---|
179 |
|
---|
180 | PCUT_TEST(double_to_fixed_str_large)
|
---|
181 | {
|
---|
182 | size_t size = 255;
|
---|
183 | char buf[size];
|
---|
184 | int dec;
|
---|
185 | ieee_double_t d = extract_ieee_double(1234.56789);
|
---|
186 | int ret = double_to_fixed_str(d, -1, 3, buf, size, &dec);
|
---|
187 |
|
---|
188 | PCUT_ASSERT_INT_EQUALS(7, ret);
|
---|
189 | PCUT_ASSERT_INT_EQUALS(-3, dec);
|
---|
190 | PCUT_ASSERT_STR_EQUALS("1234567", buf);
|
---|
191 | }
|
---|
192 |
|
---|
193 | PCUT_TEST(double_to_fixed_str_nodecimals)
|
---|
194 | {
|
---|
195 | size_t size = 255;
|
---|
196 | char buf[size];
|
---|
197 | int dec;
|
---|
198 | ieee_double_t d = extract_ieee_double(1.999);
|
---|
199 | int ret = double_to_fixed_str(d, -1, 0, buf, size, &dec);
|
---|
200 |
|
---|
201 | PCUT_ASSERT_INT_EQUALS(1, ret);
|
---|
202 | PCUT_ASSERT_INT_EQUALS(0, dec);
|
---|
203 | PCUT_ASSERT_STR_EQUALS("1", buf);
|
---|
204 | }
|
---|
205 |
|
---|
206 | PCUT_EXPORT(double_to_str);
|
---|