source: mainline/uspace/lib/c/test/double_to_str.c@ 9bfa8c8

Last change on this file since 9bfa8c8 was d7f7a4a, checked in by Jiří Zárevúcky <zarevucky.jiri@…>, 3 years ago

Replace some license headers with SPDX identifier

Headers are replaced using tools/transorm-copyright.sh only
when it can be matched verbatim with the license header used
throughout most of the codebase.

  • Property mode set to 100644
File size: 4.0 KB
RevLine 
[88e7dc5]1/*
[d7f7a4a]2 * SPDX-FileCopyrightText: 2019 Matthieu Riolo
[88e7dc5]3 *
[d7f7a4a]4 * SPDX-License-Identifier: BSD-3-Clause
[88e7dc5]5 */
6
7#include <pcut/pcut.h>
8#include <ieee_double.h>
9#include <double_to_str.h>
10
11PCUT_INIT;
12
13PCUT_TEST_SUITE(double_to_str);
14
15PCUT_TEST(double_to_short_str_pos_zero)
16{
17 size_t size = 255;
18 char buf[size];
19 int dec;
20 ieee_double_t d = extract_ieee_double(0.0);
21 int ret = double_to_short_str(d, buf, size, &dec);
22
23 PCUT_ASSERT_INT_EQUALS(1, ret);
24 PCUT_ASSERT_INT_EQUALS(0, dec);
25 PCUT_ASSERT_STR_EQUALS("0", buf);
26}
27
28PCUT_TEST(double_to_short_str_neg_zero)
29{
30 size_t size = 255;
31 char buf[size];
32 int dec;
33 ieee_double_t d = extract_ieee_double(-0.0);
34 int ret = double_to_short_str(d, buf, size, &dec);
35
36 PCUT_ASSERT_INT_EQUALS(1, ret);
37 PCUT_ASSERT_INT_EQUALS(0, dec);
38 PCUT_ASSERT_STR_EQUALS("0", buf);
39}
40
41PCUT_TEST(double_to_short_str_pos_one)
42{
43 size_t size = 255;
44 char buf[size];
45 int dec;
46 ieee_double_t d = extract_ieee_double(1.0);
47 int ret = double_to_short_str(d, buf, size, &dec);
48
49 PCUT_ASSERT_INT_EQUALS(1, ret);
50 PCUT_ASSERT_INT_EQUALS(0, dec);
51 PCUT_ASSERT_STR_EQUALS("1", buf);
52}
53
54PCUT_TEST(double_to_short_str_neg_one)
55{
56 size_t size = 255;
57 char buf[size];
58 int dec;
59 ieee_double_t d = extract_ieee_double(-1.0);
60 int ret = double_to_short_str(d, buf, size, &dec);
61
62 PCUT_ASSERT_INT_EQUALS(1, ret);
63 PCUT_ASSERT_INT_EQUALS(0, dec);
64 PCUT_ASSERT_STR_EQUALS("1", buf);
65}
66
67PCUT_TEST(double_to_short_str_small)
68{
69 size_t size = 255;
70 char buf[size];
71 int dec;
72 ieee_double_t d = extract_ieee_double(1.1);
73 int ret = double_to_short_str(d, buf, size, &dec);
74
75 PCUT_ASSERT_INT_EQUALS(2, ret);
76 PCUT_ASSERT_INT_EQUALS(-1, dec);
77 PCUT_ASSERT_STR_EQUALS("11", buf);
78}
79
80PCUT_TEST(double_to_short_str_large)
81{
82 size_t size = 255;
83 char buf[size];
84 int dec;
85 ieee_double_t d = extract_ieee_double(1234.56789);
86 int ret = double_to_short_str(d, buf, size, &dec);
87
88 PCUT_ASSERT_INT_EQUALS(9, ret);
89 PCUT_ASSERT_INT_EQUALS(-5, dec);
90 PCUT_ASSERT_STR_EQUALS("123456789", buf);
91}
92
93PCUT_TEST(double_to_short_str_mill)
94{
95 size_t size = 255;
96 char buf[size];
97 int dec;
98 ieee_double_t d = extract_ieee_double(1000000.0);
99 int ret = double_to_short_str(d, buf, size, &dec);
100
101 PCUT_ASSERT_INT_EQUALS(1, ret);
102 PCUT_ASSERT_INT_EQUALS(6, dec);
103 PCUT_ASSERT_STR_EQUALS("1", buf);
104}
105
106PCUT_TEST(double_to_fixed_str_zero)
107{
108 size_t size = 255;
109 char buf[size];
110 int dec;
111 ieee_double_t d = extract_ieee_double(0.0);
112 int ret = double_to_fixed_str(d, -1, 3, buf, size, &dec);
113
114 PCUT_ASSERT_INT_EQUALS(1, ret);
115 PCUT_ASSERT_INT_EQUALS(0, dec);
116 PCUT_ASSERT_STR_EQUALS("0", buf);
117}
118
119PCUT_TEST(double_to_fixed_str_pos_one)
120{
121 size_t size = 255;
122 char buf[size];
123 int dec;
124 ieee_double_t d = extract_ieee_double(1.0);
125 int ret = double_to_fixed_str(d, -1, 3, buf, size, &dec);
126
127 PCUT_ASSERT_INT_EQUALS(4, ret);
128 PCUT_ASSERT_INT_EQUALS(-3, dec);
129 PCUT_ASSERT_STR_EQUALS("1000", buf);
130}
131
132PCUT_TEST(double_to_fixed_str_neg_one)
133{
134 size_t size = 255;
135 char buf[size];
136 int dec;
137 ieee_double_t d = extract_ieee_double(-1.0);
138 int ret = double_to_fixed_str(d, -1, 3, buf, size, &dec);
139
140 PCUT_ASSERT_INT_EQUALS(4, ret);
141 PCUT_ASSERT_INT_EQUALS(-3, dec);
142 PCUT_ASSERT_STR_EQUALS("1000", buf);
143}
144
145PCUT_TEST(double_to_fixed_str_small)
146{
147 size_t size = 255;
148 char buf[size];
149 int dec;
150 ieee_double_t d = extract_ieee_double(1.1);
151 int ret = double_to_fixed_str(d, -1, 3, buf, size, &dec);
152
153 PCUT_ASSERT_INT_EQUALS(4, ret);
154 PCUT_ASSERT_INT_EQUALS(-3, dec);
155 PCUT_ASSERT_STR_EQUALS("1100", buf);
156}
157
158PCUT_TEST(double_to_fixed_str_large)
159{
160 size_t size = 255;
161 char buf[size];
162 int dec;
163 ieee_double_t d = extract_ieee_double(1234.56789);
164 int ret = double_to_fixed_str(d, -1, 3, buf, size, &dec);
165
166 PCUT_ASSERT_INT_EQUALS(7, ret);
167 PCUT_ASSERT_INT_EQUALS(-3, dec);
168 PCUT_ASSERT_STR_EQUALS("1234567", buf);
169}
170
171PCUT_TEST(double_to_fixed_str_nodecimals)
172{
173 size_t size = 255;
174 char buf[size];
175 int dec;
176 ieee_double_t d = extract_ieee_double(1.999);
177 int ret = double_to_fixed_str(d, -1, 0, buf, size, &dec);
178
179 PCUT_ASSERT_INT_EQUALS(1, ret);
180 PCUT_ASSERT_INT_EQUALS(0, dec);
181 PCUT_ASSERT_STR_EQUALS("1", buf);
182}
183
184PCUT_EXPORT(double_to_str);
Note: See TracBrowser for help on using the repository browser.