11 #include <catch2/catch_test_macros.hpp>
12 #include <catch2/matchers/catch_matchers_string.hpp>
24 using Catch::Matchers::ContainsSubstring;
35 std::string parse_string(
const std::string& unit_definition) {
40 correctness_driver.
table->print_units_sorted(ss);
41 correctness_driver.
table->print_base_units(ss);
46 SCENARIO(
"Unit parser accepting valid units definition",
"[unit][parser]") {
47 GIVEN(
"A base unit") {
48 WHEN(
"Base unit is *a*") {
49 THEN(
"parser accepts without an error") {
53 WHEN(
"Base unit is *b*") {
54 THEN(
"parser accepts without an error") {
58 WHEN(
"Base unit is *d*") {
59 THEN(
"parser accepts without an error") {
63 WHEN(
"Base unit is *i*") {
64 THEN(
"parser accepts without an error") {
68 WHEN(
"Base unit is *c*") {
69 THEN(
"parser accepts without an error") {
74 GIVEN(
"A double number") {
75 WHEN(
"Double number is writen like 3.14") {
76 THEN(
"parser accepts without an error") {
80 WHEN(
"Double number is writen like 1") {
81 THEN(
"parser accepts without an error") {
85 WHEN(
"Double number is negative") {
86 THEN(
"parser accepts without an error") {
91 GIVEN(
"A dimensionless constant") {
92 WHEN(
"Constant expression is double / constant") {
93 THEN(
"parser accepts without an error") {
98 GIVEN(
"A power of another unit") {
100 THEN(
"parser accepts without an error") {
105 THEN(
"parser accepts without an error") {
110 GIVEN(
"Divisions and multiplications of units") {
111 WHEN(
"Units are multiplied") {
112 THEN(
"parser accepts without an error") {
116 WHEN(
"There are both divisions and multiplications") {
117 THEN(
"parser accepts without an error") {
121 WHEN(
"There is only division") {
122 THEN(
"parser accepts without an error") {
126 WHEN(
"Nominator is unknown") {
128 REQUIRE_THROWS(parse_string(
"foo 1 pew/m\n"));
131 WHEN(
"Denominator is unknown") {
133 REQUIRE_THROWS(parse_string(
"foo 1 m/pew\n"));
137 GIVEN(
"A double number and some units") {
138 WHEN(
"Double number is multiplied by a power of 10 with division of multiple units") {
139 THEN(
"parser accepts without an error") {
143 WHEN(
"Double number is writen like .9") {
144 THEN(
"parser accepts without an error") {
148 WHEN(
"A 's' is added") {
149 THEN(
"parser remove it to find the units") {
150 REQUIRE_NOTHROW(parse_string(
"pew 1 m\nfoo 2 pews\n"));
151 REQUIRE_NOTHROW(parse_string(
"pew 1 m\nfoo 2 /pews\n"));
154 WHEN(
"No unit but only a prefix factor") {
155 THEN(
"parser multiply the number by the factor") {
156 std::string parsed_unit{};
157 REQUIRE_NOTHROW(parsed_unit = parse_string(
"pew 1 1/milli"));
158 REQUIRE_THAT(parsed_unit, ContainsSubstring(
"pew 0.001: 0 0 0 0 0 0 0 0 0 0"));
162 GIVEN(
"A fraction and some units") {
163 WHEN(
"Fraction is writen like 1|2") {
164 THEN(
"parser accepts without an error") {
168 WHEN(
"Fraction is writen like 1|8.988e9") {
169 THEN(
"parser accepts without an error") {
176 SCENARIO(
"Unit parser accepting dependent/nested units definition",
"[unit][parser]") {
177 GIVEN(
"Parsed the nrnunits.lib file") {
178 WHEN(
"Multiple units definitions based on the units defined in nrnunits.lib") {
179 THEN(
"parser accepts the units correctly") {
180 std::string units_definitions = R
"(
192 R1 8.314 volt-coul/degC
195 std::string parsed_units = parse_string(reindent_text(units_definitions));
196 REQUIRE_THAT(parsed_units, ContainsSubstring("mV 0.001: 2 1 -2 -1 0 0 0 0 0 0"));
197 REQUIRE_THAT(parsed_units, ContainsSubstring(
"mM 1: -3 0 0 0 0 0 0 0 0 0"));
198 REQUIRE_THAT(parsed_units, ContainsSubstring(
"mA 0.001: 0 0 -1 1 0 0 0 0 0 0"));
199 REQUIRE_THAT(parsed_units,
200 ContainsSubstring(
"KTOMV 8.53e-05: 2 1 -2 -1 0 0 0 0 0 -1"));
201 REQUIRE_THAT(parsed_units, ContainsSubstring(
"B 26: -1 0 0 -1 0 0 0 0 0 0"));
202 REQUIRE_THAT(parsed_units, ContainsSubstring(
"dummy1 0.025: -2 0 0 0 0 0 0 0 0 0"));
203 REQUIRE_THAT(parsed_units, ContainsSubstring(
"dummy2 0.025: -2 0 0 0 0 0 0 0 0 0"));
204 REQUIRE_THAT(parsed_units, ContainsSubstring(
"dummy3 0.025: -2 0 0 0 0 0 0 0 0 0"));
205 REQUIRE_THAT(parsed_units,
206 ContainsSubstring(
"dummy4 -0.025: -2 0 0 0 0 0 0 0 0 0"));
207 REQUIRE_THAT(parsed_units, ContainsSubstring(
"dummy5 0.025: 0 0 0 0 0 0 0 0 0 0"));
208 REQUIRE_THAT(parsed_units,
209 ContainsSubstring(
"newR 8.31446: 2 1 -2 0 0 0 0 0 0 -1"));
210 REQUIRE_THAT(parsed_units, ContainsSubstring(
"R1 8.314: 2 1 -2 0 0 0 0 0 0 -1"));
211 REQUIRE_THAT(parsed_units, ContainsSubstring(
"R2 8.314: 2 1 -2 0 0 0 0 0 0 -1"));
212 REQUIRE_THAT(parsed_units,
213 ContainsSubstring(
"m kg sec coul candela dollar bit erlang K"));