General: separation of marav-io as a library
[marnav.git] / test / marnav / units / Test_basic_quantity.cpp
blob7053992ec4bd8b1812c6b7272f876c0920c305a9
1 #include <marnav/units/units.hpp>
2 #include <gtest/gtest.h>
4 namespace
6 using namespace marnav::units;
8 TEST(basic_quantity, get_dimension_from_quantity)
10 EXPECT_EQ(1, length::dimension::len);
11 EXPECT_EQ(0, length::dimension::mas);
12 EXPECT_EQ(0, length::dimension::tim);
13 EXPECT_EQ(0, length::dimension::tem);
14 EXPECT_EQ(0, length::dimension::cur);
15 EXPECT_EQ(0, length::dimension::amt);
16 EXPECT_EQ(0, length::dimension::lum);
19 TEST(basic_quantity, construct_quantity_from_unit)
21 const length q1(meters{1});
23 EXPECT_EQ(1.0, q1.value());
25 // using squaremeters = basic_unit<basic_squaremeter<double>>;
26 // length q2(squaremeters{});
29 TEST(basic_quantity, retrieve_units_from_quantity)
31 const length q1(feet{1.0});
32 EXPECT_EQ(0.3048, q1.get<meters>().value());
33 EXPECT_EQ(1.0, q1.get<feet>().value());
35 // using meter2 = basic_unit<basic_squaremeter<double>>;
36 // const meter2 sq = q1.get<meter2>();
39 TEST(basic_quantity, retrieve_units_from_quantity_different_ratio)
41 const length q1(meters{100.0});
42 EXPECT_EQ(0.1, q1.get<kilometers>().value());
43 EXPECT_EQ(100000.0, q1.get<millimeters>().value());
46 TEST(basic_quantity, scale_onto_self_with_value_type)
49 length q{meters{1.0}};
50 q *= 2.0;
51 EXPECT_EQ(2.0, q.value());
54 length q{meters{1.0}};
55 q /= 2.0;
56 EXPECT_EQ(0.5, q.value());
60 TEST(basic_quantity, scale_onto_self_with_convertible_type)
63 length q{meters{1.0}};
64 q *= 2.0f;
65 EXPECT_EQ(2.0, q.value());
68 length q{meters{1.0}};
69 q *= 2;
70 EXPECT_EQ(2.0, q.value());
73 length q{meters{1.0}};
74 q /= 2.0f;
75 EXPECT_EQ(0.5, q.value());
78 length q{meters{1.0}};
79 q /= 2;
80 EXPECT_EQ(0.5, q.value());
83 struct foo {
84 operator double() const { return 2.0; }
87 length q{meters{1.0}};
88 q *= foo{};
89 EXPECT_EQ(2.0, q.value());
92 struct foo {
93 operator int() const { return 2; }
96 length q{meters{1.0}};
97 q *= foo{};
98 EXPECT_EQ(2.0, q.value());
102 TEST(basic_quantity, unary_minus)
104 auto l = length{meters{2.0}};
105 EXPECT_EQ(-2.0, (-l).value());
108 TEST(basic_quantity, add_onto_self)
110 length q{meters{2}};
112 q += length{meters{1}};
114 EXPECT_EQ(3.0, q.value());
117 TEST(basic_quantity, sub_onto_self)
119 length q{meters{2}};
121 q -= length{meters{1}};
123 EXPECT_EQ(1.0, q.value());
126 TEST(basic_quantity, add)
128 const length q1{meters{2}};
129 const length q2{feet{1}};
131 const length q = q1 + q2;
133 EXPECT_EQ(2.3048, q.value());
136 TEST(basic_quantity, add_units_resulting_quanity)
138 const auto l1 = meters{2};
139 const auto l2 = feet{1};
141 const length q = l1 + l2;
143 EXPECT_EQ(2.3048, q.value());
146 TEST(basic_quantity, sub)
148 const length q1{meters{2}};
149 const length q2{feet{1}};
151 const length q = q1 - q2;
153 EXPECT_EQ(1.6952, q.value());
156 TEST(basic_quantity, sub_units_resulting_quantity)
158 const auto l1 = meters{2};
159 const auto l2 = feet{1};
161 const length q = l1 - l2;
163 EXPECT_EQ(1.6952, q.value());
166 TEST(basic_quantity, mul)
168 const auto q = length{meters{2}} * length{meters{3}};
170 EXPECT_EQ(6.0, q.value());
172 using dim = decltype(q)::dimension;
174 EXPECT_EQ(2, dim::len);
175 EXPECT_EQ(0, dim::mas);
176 EXPECT_EQ(0, dim::tim);
177 EXPECT_EQ(0, dim::tem);
178 EXPECT_EQ(0, dim::cur);
179 EXPECT_EQ(0, dim::amt);
180 EXPECT_EQ(0, dim::lum);
183 TEST(basic_quantity, mul_units_resulting_in_quantity)
186 const auto q = meters{2} * meters{3};
188 EXPECT_EQ(6.0, q.value());
190 using dim = decltype(q)::dimension;
192 EXPECT_EQ(2, dim::len);
193 EXPECT_EQ(0, dim::mas);
194 EXPECT_EQ(0, dim::tim);
195 EXPECT_EQ(0, dim::tem);
196 EXPECT_EQ(0, dim::cur);
197 EXPECT_EQ(0, dim::amt);
198 EXPECT_EQ(0, dim::lum);
201 const auto q = meters{2} * feet{3};
203 EXPECT_NEAR(1.8288, q.value(), 1e-4);
205 using dim = decltype(q)::dimension;
207 EXPECT_EQ(2, dim::len);
208 EXPECT_EQ(0, dim::mas);
209 EXPECT_EQ(0, dim::tim);
210 EXPECT_EQ(0, dim::tem);
211 EXPECT_EQ(0, dim::cur);
212 EXPECT_EQ(0, dim::amt);
213 EXPECT_EQ(0, dim::lum);
217 TEST(basic_quantity, div)
219 const auto q = area{squaremeters{6}} / length{meters{2}};
221 EXPECT_EQ(3.0, q.value());
223 using dim = decltype(q)::dimension;
225 EXPECT_EQ(1, dim::len);
226 EXPECT_EQ(0, dim::mas);
227 EXPECT_EQ(0, dim::tim);
228 EXPECT_EQ(0, dim::tem);
229 EXPECT_EQ(0, dim::cur);
230 EXPECT_EQ(0, dim::amt);
231 EXPECT_EQ(0, dim::lum);
234 TEST(basic_quantity, div_units_resuling_in_quantity)
237 const auto q = squaremeters{6} / meters{2};
239 EXPECT_EQ(3.0, q.value());
241 using dim = decltype(q)::dimension;
243 EXPECT_EQ(1, dim::len);
244 EXPECT_EQ(0, dim::mas);
245 EXPECT_EQ(0, dim::tim);
246 EXPECT_EQ(0, dim::tem);
247 EXPECT_EQ(0, dim::cur);
248 EXPECT_EQ(0, dim::amt);
249 EXPECT_EQ(0, dim::lum);
252 const auto q = squaremeters{6} / feet{2};
254 EXPECT_NEAR(9.84252, q.value(), 1e-5);
256 using dim = decltype(q)::dimension;
258 EXPECT_EQ(1, dim::len);
259 EXPECT_EQ(0, dim::mas);
260 EXPECT_EQ(0, dim::tim);
261 EXPECT_EQ(0, dim::tem);
262 EXPECT_EQ(0, dim::cur);
263 EXPECT_EQ(0, dim::amt);
264 EXPECT_EQ(0, dim::lum);
268 TEST(basic_quantity, floor)
270 EXPECT_EQ(+1.0, floor(length{meters{+1.0}}).value());
271 EXPECT_EQ(+1.0, floor(length{meters{+1.4}}).value());
272 EXPECT_EQ(+1.0, floor(length{meters{+1.5}}).value());
273 EXPECT_EQ(+1.0, floor(length{meters{+1.6}}).value());
274 EXPECT_EQ(+2.0, floor(length{meters{+2.0}}).value());
275 EXPECT_EQ(+2.0, floor(length{meters{+2.5}}).value());
276 EXPECT_EQ(-1.0, floor(length{meters{-1.0}}).value());
277 EXPECT_EQ(-2.0, floor(length{meters{-1.5}}).value());
278 EXPECT_EQ(-2.0, floor(length{meters{-2.0}}).value());
279 EXPECT_EQ(-3.0, floor(length{meters{-2.5}}).value());
282 TEST(basic_quantity, ceil)
284 EXPECT_EQ(+1.0, ceil(length{meters{+1.0}}).value());
285 EXPECT_EQ(+2.0, ceil(length{meters{+1.4}}).value());
286 EXPECT_EQ(+2.0, ceil(length{meters{+1.5}}).value());
287 EXPECT_EQ(+2.0, ceil(length{meters{+1.6}}).value());
288 EXPECT_EQ(+2.0, ceil(length{meters{+2.0}}).value());
289 EXPECT_EQ(+3.0, ceil(length{meters{+2.5}}).value());
290 EXPECT_EQ(-1.0, ceil(length{meters{-1.0}}).value());
291 EXPECT_EQ(-1.0, ceil(length{meters{-1.5}}).value());
292 EXPECT_EQ(-2.0, ceil(length{meters{-2.0}}).value());
293 EXPECT_EQ(-2.0, ceil(length{meters{-2.5}}).value());
296 TEST(basic_quantity, round)
298 EXPECT_EQ(+1.0, round(length{meters{+1.0}}).value());
299 EXPECT_EQ(+1.0, round(length{meters{+1.4}}).value());
300 EXPECT_EQ(+2.0, round(length{meters{+1.5}}).value());
301 EXPECT_EQ(+2.0, round(length{meters{+1.6}}).value());
302 EXPECT_EQ(+2.0, round(length{meters{+2.0}}).value());
303 EXPECT_EQ(+3.0, round(length{meters{+2.5}}).value());
304 EXPECT_EQ(-1.0, round(length{meters{-1.0}}).value());
305 EXPECT_EQ(-2.0, round(length{meters{-1.5}}).value());
306 EXPECT_EQ(-2.0, round(length{meters{-2.0}}).value());
307 EXPECT_EQ(-3.0, round(length{meters{-2.5}}).value());
310 TEST(basic_quantity, abs)
312 EXPECT_EQ(1.0, abs(length{meters{+1.0}}).value());
313 EXPECT_EQ(1.4, abs(length{meters{+1.4}}).value());
314 EXPECT_EQ(1.5, abs(length{meters{+1.5}}).value());
315 EXPECT_EQ(1.6, abs(length{meters{+1.6}}).value());
316 EXPECT_EQ(2.0, abs(length{meters{+2.0}}).value());
317 EXPECT_EQ(2.5, abs(length{meters{+2.5}}).value());
318 EXPECT_EQ(1.0, abs(length{meters{-1.0}}).value());
319 EXPECT_EQ(1.5, abs(length{meters{-1.5}}).value());
320 EXPECT_EQ(2.0, abs(length{meters{-2.0}}).value());
321 EXPECT_EQ(2.5, abs(length{meters{-2.5}}).value());
324 TEST(basic_quantity, comparison_equal)
326 EXPECT_TRUE(length{meters{1}} == length{meters{1}});
327 EXPECT_FALSE(length{meters{1}} == length{feet{1}});
330 TEST(basic_quantity, comparison_less)
332 EXPECT_TRUE(length{meters{1}} < length{meters{2}});
333 EXPECT_TRUE(length{meters{1}} < length{feet{8}});
334 EXPECT_FALSE(length{meters{1}} < length{meters{1}});
335 EXPECT_FALSE(length{meters{2}} < length{meters{1}});
336 EXPECT_FALSE(length{feet{4}} < length{meters{1}});