Line data Source code
1 : // Copyright (c) 2021-2025 Made to Order Software Corp. All Rights Reserved
2 : //
3 : // https://snapwebsites.org/project/snapdev
4 : // contact@m2osw.com
5 : //
6 : // This program is free software: you can redistribute it and/or modify
7 : // it under the terms of the GNU General Public License as published by
8 : // the Free Software Foundation, either version 3 of the License, or
9 : // (at your option) any later version.
10 : //
11 : // This program is distributed in the hope that it will be useful,
12 : // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 : // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 : // GNU General Public License for more details.
15 : //
16 : // You should have received a copy of the GNU General Public License
17 : // along with this program. If not, see <https://www.gnu.org/licenses/>.
18 :
19 : /** \file
20 : * \brief Verify the floating point to string convertions.
21 : *
22 : * This file implements tests for the floating point to string.
23 : */
24 :
25 : // self
26 : //
27 : #include <snapdev/floating_point_to_string.h>
28 :
29 : #include "catch_main.h"
30 :
31 :
32 :
33 : // C++
34 : //
35 : //#include <iomanip>
36 : //#include <set>
37 :
38 :
39 : // last include
40 : //
41 : #include <snapdev/poison.h>
42 :
43 :
44 :
45 : namespace
46 : {
47 :
48 :
49 :
50 :
51 :
52 : } // no name namespace
53 :
54 :
55 :
56 1 : CATCH_TEST_CASE("floating_point_to_string", "[float][string]")
57 : {
58 1 : CATCH_START_SECTION("floating_point_to_string: verify conversion of floating points to string")
59 : {
60 : struct conversion_t
61 : {
62 : double f_floating_point = 0.0;
63 : bool f_keep_period = true;
64 : std::string f_string = std::string();
65 : };
66 :
67 1 : conversion_t const conversions[] =
68 : {
69 : {
70 : .f_floating_point = 0.0,
71 : .f_keep_period = true,
72 : .f_string = "0.0",
73 : },
74 : {
75 : .f_floating_point = 0.0,
76 : .f_keep_period = false,
77 : .f_string = "0",
78 : },
79 : {
80 : .f_floating_point = 1.0,
81 : .f_keep_period = true,
82 : .f_string = "1.0",
83 : },
84 : {
85 : .f_floating_point = 1.0,
86 : .f_keep_period = false,
87 : .f_string = "1",
88 : },
89 : {
90 : .f_floating_point = -1.0,
91 : .f_keep_period = true,
92 : .f_string = "-1.0",
93 : },
94 : {
95 : .f_floating_point = -1.0,
96 : .f_keep_period = false,
97 : .f_string = "-1",
98 : },
99 : {
100 : .f_floating_point = std::numeric_limits<double>::infinity(),
101 : .f_keep_period = true,
102 : .f_string = "Infinity",
103 : },
104 : {
105 : .f_floating_point = std::numeric_limits<double>::infinity(),
106 : .f_keep_period = false,
107 : .f_string = "Infinity",
108 : },
109 : {
110 : .f_floating_point = -std::numeric_limits<double>::infinity(),
111 : .f_keep_period = true,
112 : .f_string = "-Infinity",
113 : },
114 : {
115 : .f_floating_point = -std::numeric_limits<double>::infinity(),
116 : .f_keep_period = false,
117 : .f_string = "-Infinity",
118 : },
119 : {
120 : .f_floating_point = std::numeric_limits<double>::quiet_NaN(),
121 : .f_keep_period = true,
122 : .f_string = "NaN",
123 : },
124 : {
125 : .f_floating_point = std::numeric_limits<double>::quiet_NaN(),
126 : .f_keep_period = false,
127 : .f_string = "NaN",
128 : },
129 15 : };
130 :
131 13 : for(auto const & c : conversions)
132 : {
133 12 : if(c.f_keep_period)
134 : {
135 : // try with the default
136 : //
137 6 : std::string const default_keep_period(snapdev::floating_point_to_string<double, char>(c.f_floating_point));
138 6 : CATCH_REQUIRE(c.f_string == default_keep_period);
139 6 : }
140 :
141 12 : std::string const result(snapdev::floating_point_to_string<double, char>(c.f_floating_point, c.f_keep_period));
142 12 : CATCH_REQUIRE(c.f_string == result);
143 12 : }
144 13 : }
145 1 : CATCH_END_SECTION()
146 1 : }
147 :
148 :
149 :
150 : // vim: ts=4 sw=4 et
|