| 1 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | // Copyright 2019 Juan Palacios <[email protected]> |
| 3 | |
| 4 | #include <catch2/catch_all.hpp> |
| 5 | |
| 6 | #include "core/components/commonutils.h" |
| 7 | #include <units.h> |
| 8 | |
| 9 | namespace Tests::Utils::Common { |
| 10 | |
| 11 | TEST_CASE("CommonUtils tests", "[Utils][Common]") |
| 12 | { |
| 13 | SECTION("normalizePoints...") |
| 14 | { |
| 15 | SECTION("clamp points into range") |
| 16 | { |
| 17 | std::vector<std::pair<units::temperature::celsius_t, units::concentration::percent_t>> |
| 18 | points{std::make_pair(units::temperature::celsius_t(0), |
| 19 | units::concentration::percent_t(100)), |
| 20 | std::make_pair(units::temperature::celsius_t(50), |
| 21 | units::concentration::percent_t(150)), |
| 22 | std::make_pair(units::temperature::celsius_t(90), |
| 23 | units::concentration::percent_t(200))}; |
| 24 | |
| 25 | auto tempRange = std::make_pair(units::temperature::celsius_t(0), |
| 26 | units::temperature::celsius_t(50)); |
| 27 | |
| 28 | ::Utils::Common::normalizePoints(points, tempRange); |
| 29 | |
| 30 | // temperature range |
| 31 | REQUIRE_FALSE( |
| 32 | std::any_of(points.cbegin(), points.cend(), [&](auto const &point) { |
| 33 | return point.first < tempRange.first || |
| 34 | point.first > tempRange.second; |
| 35 | })); |
| 36 | |
| 37 | // percentage range |
| 38 | REQUIRE_FALSE( |
| 39 | std::any_of(points.cbegin(), points.cend(), [&](auto const &point) { |
| 40 | return point.second < units::concentration::percent_t(0) || |
| 41 | point.second > units::concentration::percent_t(100); |
| 42 | })); |
| 43 | } |
| 44 | |
| 45 | SECTION("clamp inner points y-axis coordinates into [prev.y, next.y] range") |
| 46 | { |
| 47 | std::vector<std::pair<units::temperature::celsius_t, units::concentration::percent_t>> |
| 48 | points{std::make_pair(units::temperature::celsius_t(0), |
| 49 | units::concentration::percent_t(20)), |
| 50 | std::make_pair(units::temperature::celsius_t(30), |
| 51 | units::concentration::percent_t(10)), |
| 52 | std::make_pair(units::temperature::celsius_t(60), |
| 53 | units::concentration::percent_t(30)), |
| 54 | std::make_pair(units::temperature::celsius_t(100), |
| 55 | units::concentration::percent_t(25))}; |
| 56 | |
| 57 | std::vector<std::pair<units::temperature::celsius_t, units::concentration::percent_t>> |
| 58 | normalizedPoints{std::make_pair(units::temperature::celsius_t(0), |
| 59 | units::concentration::percent_t(20)), |
| 60 | std::make_pair(units::temperature::celsius_t(30), |
| 61 | units::concentration::percent_t(20)), |
| 62 | std::make_pair(units::temperature::celsius_t(60), |
| 63 | units::concentration::percent_t(30)), |
| 64 | std::make_pair(units::temperature::celsius_t(100), |
| 65 | units::concentration::percent_t(30))}; |
| 66 | |
| 67 | auto tempRange = std::make_pair(units::temperature::celsius_t(0), |
| 68 | units::temperature::celsius_t(100)); |
| 69 | |
| 70 | ::Utils::Common::normalizePoints(points, tempRange); |
| 71 | |
| 72 | REQUIRE_THAT(points, Catch::Matchers::Equals(normalizedPoints)); |
| 73 | } |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | } // namespace Tests::Utils::Common |