Line data Source code
1 : // Copyright (c) 2015-2022 Made to Order Software Corp. All Rights Reserved
2 : //
3 : // https://snapwebsites.org/project/csspp
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 2 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 along
17 : // with this program; if not, write to the Free Software Foundation, Inc.,
18 : // 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 :
20 : /** \file
21 : * \brief Test the csspp.cpp file.
22 : *
23 : * This test runs a battery of tests agains the csspp.cpp
24 : * implementation to ensure full coverage.
25 : */
26 :
27 : // css
28 : //
29 : #include <csspp/lexer.h>
30 :
31 : #include <csspp/exception.h>
32 : #include <csspp/unicode_range.h>
33 :
34 :
35 : // self
36 : //
37 : #include "catch_main.h"
38 :
39 :
40 : // C++
41 : //
42 : #include <sstream>
43 :
44 :
45 : // C lib
46 : //
47 : #include <string.h>
48 :
49 :
50 : // last include
51 : //
52 : #include <snapdev/poison.h>
53 :
54 :
55 :
56 : namespace
57 : {
58 :
59 :
60 : } // no name namespace
61 :
62 :
63 1 : CATCH_TEST_CASE("Version string", "[csspp] [version]")
64 : {
65 : // we expect the test suite to be compiled with the exact same version
66 1 : CATCH_REQUIRE(csspp::csspp_library_version() == std::string(CSSPP_VERSION));
67 :
68 : // no error left over
69 1 : VERIFY_ERRORS("");
70 1 : }
71 :
72 1 : CATCH_TEST_CASE("Safe boolean", "[csspp] [output]")
73 : {
74 : {
75 1 : bool flag(false);
76 1 : CATCH_REQUIRE(flag == false);
77 : {
78 1 : csspp::safe_bool_t safe(flag);
79 1 : CATCH_REQUIRE(flag == true);
80 1 : }
81 1 : CATCH_REQUIRE(flag == false);
82 : }
83 :
84 : {
85 1 : bool flag(false);
86 1 : CATCH_REQUIRE(flag == false);
87 : {
88 1 : csspp::safe_bool_t safe(flag);
89 1 : CATCH_REQUIRE(flag == true);
90 1 : flag = false;
91 1 : CATCH_REQUIRE(flag == false);
92 1 : }
93 1 : CATCH_REQUIRE(flag == false);
94 : }
95 :
96 : {
97 1 : bool flag(true);
98 1 : CATCH_REQUIRE(flag == true);
99 : {
100 1 : csspp::safe_bool_t safe(flag);
101 1 : CATCH_REQUIRE(flag == true);
102 1 : flag = false;
103 1 : CATCH_REQUIRE(flag == false);
104 1 : }
105 1 : CATCH_REQUIRE(flag == true);
106 : }
107 :
108 : {
109 1 : bool flag(false);
110 1 : CATCH_REQUIRE(flag == false);
111 : {
112 1 : csspp::safe_bool_t safe(flag, true);
113 1 : CATCH_REQUIRE(flag == true);
114 1 : flag = false;
115 1 : CATCH_REQUIRE(flag == false);
116 1 : }
117 1 : CATCH_REQUIRE(flag == false);
118 : }
119 :
120 : {
121 1 : bool flag(false);
122 1 : CATCH_REQUIRE(flag == false);
123 : {
124 1 : csspp::safe_bool_t safe(flag, false);
125 1 : CATCH_REQUIRE(flag == false);
126 1 : flag = true;
127 1 : CATCH_REQUIRE(flag == true);
128 1 : }
129 1 : CATCH_REQUIRE(flag == false);
130 : }
131 :
132 : {
133 1 : bool flag(true);
134 1 : CATCH_REQUIRE(flag == true);
135 : {
136 1 : csspp::safe_bool_t safe(flag, true);
137 1 : CATCH_REQUIRE(flag == true);
138 1 : flag = false;
139 1 : CATCH_REQUIRE(flag == false);
140 1 : }
141 1 : CATCH_REQUIRE(flag == true);
142 : }
143 :
144 : {
145 1 : bool flag(true);
146 1 : CATCH_REQUIRE(flag == true);
147 : {
148 1 : csspp::safe_bool_t safe(flag, false);
149 1 : CATCH_REQUIRE(flag == false);
150 1 : flag = true;
151 1 : CATCH_REQUIRE(flag == true);
152 1 : }
153 1 : CATCH_REQUIRE(flag == true);
154 : }
155 1 : }
156 :
157 1 : CATCH_TEST_CASE("Decimal number output", "[csspp] [output]")
158 : {
159 1 : CATCH_REQUIRE(csspp::decimal_number_to_string(1.0, false) == "1");
160 1 : CATCH_REQUIRE(csspp::decimal_number_to_string(1.2521, false) == "1.252");
161 1 : CATCH_REQUIRE(csspp::decimal_number_to_string(1.2526, false) == "1.253");
162 1 : CATCH_REQUIRE(csspp::decimal_number_to_string(0.0, false) == "0");
163 1 : CATCH_REQUIRE(csspp::decimal_number_to_string(0.2521, false) == "0.252");
164 1 : CATCH_REQUIRE(csspp::decimal_number_to_string(0.2526, false) == "0.253");
165 : {
166 1 : csspp::safe_precision_t precision(2);
167 1 : CATCH_REQUIRE(csspp::decimal_number_to_string(1.2513, false) == "1.25");
168 1 : CATCH_REQUIRE(csspp::decimal_number_to_string(1.2561, false) == "1.26");
169 1 : }
170 1 : CATCH_REQUIRE(csspp::decimal_number_to_string(-1.2526, false) == "-1.253");
171 1 : CATCH_REQUIRE(csspp::decimal_number_to_string(-0.9, false) == "-0.9");
172 1 : CATCH_REQUIRE(csspp::decimal_number_to_string(-0.0009, false) == "-0.001");
173 1 : CATCH_REQUIRE(csspp::decimal_number_to_string(-1000.0, false) == "-1000");
174 1 : CATCH_REQUIRE(csspp::decimal_number_to_string(1000.0, false) == "1000");
175 1 : CATCH_REQUIRE(csspp::decimal_number_to_string(100.0, false) == "100");
176 1 : CATCH_REQUIRE(csspp::decimal_number_to_string(10.0, false) == "10");
177 :
178 1 : CATCH_REQUIRE(csspp::decimal_number_to_string(1.0, true) == "1");
179 1 : CATCH_REQUIRE(csspp::decimal_number_to_string(1.2521, true) == "1.252");
180 1 : CATCH_REQUIRE(csspp::decimal_number_to_string(1.2526, true) == "1.253");
181 1 : CATCH_REQUIRE(csspp::decimal_number_to_string(0.0, true) == "0");
182 1 : CATCH_REQUIRE(csspp::decimal_number_to_string(0.2521, true) == ".252");
183 1 : CATCH_REQUIRE(csspp::decimal_number_to_string(0.2526, true) == ".253");
184 : {
185 1 : csspp::safe_precision_t precision(2);
186 1 : CATCH_REQUIRE(csspp::decimal_number_to_string(1.2513, true) == "1.25");
187 1 : CATCH_REQUIRE(csspp::decimal_number_to_string(1.2561, true) == "1.26");
188 1 : }
189 1 : CATCH_REQUIRE(csspp::decimal_number_to_string(-1.2526, true) == "-1.253");
190 1 : CATCH_REQUIRE(csspp::decimal_number_to_string(-0.9, true) == "-.9");
191 1 : CATCH_REQUIRE(csspp::decimal_number_to_string(-0.0009, true) == "-.001");
192 1 : CATCH_REQUIRE(csspp::decimal_number_to_string(-1000.0, true) == "-1000");
193 1 : CATCH_REQUIRE(csspp::decimal_number_to_string(1000.0, true) == "1000");
194 1 : CATCH_REQUIRE(csspp::decimal_number_to_string(100.0, true) == "100");
195 1 : CATCH_REQUIRE(csspp::decimal_number_to_string(10.0, true) == "10");
196 :
197 : // super small negative numbers must be output as "0"
198 1 : CATCH_REQUIRE(csspp::decimal_number_to_string(-1.2526e-10, true) == "0");
199 1 : }
200 :
201 1 : CATCH_TEST_CASE("Invalid precision", "[csspp] [invalid]")
202 : {
203 : // we want to keep the default precision in place
204 1 : csspp::safe_precision_t precision;
205 :
206 : // negative not available
207 11 : for(int i(-10); i < 0; ++i)
208 : {
209 10 : CATCH_REQUIRE_THROWS_AS(csspp::set_precision(i), csspp::csspp_exception_overflow);
210 : }
211 :
212 : // too large not acceptable
213 11 : for(int i(11); i <= 20; ++i)
214 : {
215 10 : CATCH_REQUIRE_THROWS_AS(csspp::set_precision(i), csspp::csspp_exception_overflow);
216 : }
217 2 : }
218 :
219 : // vim: ts=4 sw=4 et
|