|
| 1 | +// Copyright (C) 2025 Julien Michot. All Rights reserved. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +#if CATCH2_VERSION == 2 |
| 16 | +#include <catch2/catch.hpp> |
| 17 | +#else |
| 18 | +#include <catch2/catch_approx.hpp> |
| 19 | +#include <catch2/catch_test_macros.hpp> |
| 20 | +#endif |
| 21 | + |
| 22 | +#include <tinyopt/tinyopt.h> |
| 23 | +#include <tinyopt/traits.h> |
| 24 | + |
| 25 | +using namespace tinyopt; |
| 26 | +using namespace tinyopt::nlls; |
| 27 | + |
| 28 | +TEST_CASE("tinyopt_params_traits_stl") { |
| 29 | + SECTION("std::array<float, 5>") { |
| 30 | + using Params = std::array<float, 5>; |
| 31 | + Params x; |
| 32 | + REQUIRE(traits::params_trait<Params>::Dims == 5); |
| 33 | + REQUIRE(traits::params_trait<Params>::dims(x) == 5); |
| 34 | + } |
| 35 | + SECTION("std::vector<float>") { |
| 36 | + using Params = std::vector<double>; |
| 37 | + Params x{{0.4, 0.5}}; |
| 38 | + REQUIRE(traits::params_trait<Params>::Dims == Dynamic); |
| 39 | + REQUIRE(traits::params_trait<Params>::dims(x) == 2); |
| 40 | + } |
| 41 | + SECTION("std::array<Vec4, 5>") { |
| 42 | + using Params = std::array<Vec4, 5>; |
| 43 | + Params x; |
| 44 | + REQUIRE(traits::params_trait<Params>::Dims == 4 * 5); |
| 45 | + REQUIRE(traits::params_trait<Params>::dims(x) == 4 * 5); |
| 46 | + } |
| 47 | + SECTION("std::array<VecX, 2>") { |
| 48 | + using Params = std::array<VecX, 2>; |
| 49 | + Params x; |
| 50 | + REQUIRE(traits::params_trait<Params>::Dims == Dynamic); |
| 51 | + REQUIRE(traits::params_trait<Params>::dims(x) == 0); |
| 52 | + } |
| 53 | + SECTION("std::vector<Vec2>") { |
| 54 | + using Params = std::vector<Vec2>; |
| 55 | + Params x{{Vec2::Zero(), Vec2::Zero(), Vec2::Zero()}}; |
| 56 | + REQUIRE(traits::params_trait<Params>::Dims == Dynamic); |
| 57 | + REQUIRE(traits::params_trait<Params>::dims(x) == 6); |
| 58 | + } |
| 59 | + SECTION("std::pair<Vec2, Vec3>") { |
| 60 | + using Params = std::pair<Vec2, Vec3>; |
| 61 | + Params x; |
| 62 | + REQUIRE(traits::params_trait<Params>::Dims == 2 + 3); |
| 63 | + REQUIRE(traits::params_trait<Params>::dims(x) == 2 + 3); |
| 64 | + } |
| 65 | + SECTION("std::pair<Vec2, VecX>") { |
| 66 | + using Params = std::pair<Vec2, VecX>; |
| 67 | + Params x = std::make_pair(Vec2::Zero(), VecX::Random(4)); |
| 68 | + REQUIRE(traits::params_trait<Params>::Dims == Dynamic); |
| 69 | + REQUIRE(traits::params_trait<Params>::dims(x) == 2 + 4); |
| 70 | + } |
| 71 | + SECTION("std::pair<vector<float>, Vec3>") { |
| 72 | + using Params = std::pair<std::vector<float>, Vec3>; |
| 73 | + Params x = std::make_pair(std::vector<float>{{1, 2, 3, 4}}, Vec3::Zero()); |
| 74 | + REQUIRE(traits::params_trait<Params>::Dims == Dynamic); |
| 75 | + REQUIRE(traits::params_trait<Params>::dims(x) == 4 + 3); |
| 76 | + } |
| 77 | + SECTION("std::pair<std::vector<Vec3>, std::array<VecX, 4>>") { |
| 78 | + using Params = std::pair<std::vector<Vec3>, std::array<VecX, 4>>; |
| 79 | + std::vector<Vec3> a{Vec3::Zero(), Vec3::Zero()}; |
| 80 | + std::array<VecX, 4> b{{VecX::Random(5), VecX::Random(2), VecX::Random(0), VecX::Random(0)}}; |
| 81 | + Params x = std::make_pair(a, b); |
| 82 | + REQUIRE(traits::params_trait<Params>::Dims == Dynamic); |
| 83 | + REQUIRE(traits::params_trait<Params>::dims(x) == 6 + 7); |
| 84 | + } |
| 85 | + SECTION("std::pair<std::array<Vec3>, std::array<Vec2, 4>>") { |
| 86 | + using Params = std::pair<std::array<Vec3, 5>, std::array<Vec2, 4>>; |
| 87 | + std::array<Vec3, 5> a; |
| 88 | + std::array<Vec2, 4> b; |
| 89 | + Params x = std::make_pair(a, b); |
| 90 | + REQUIRE(traits::params_trait<Params>::Dims == 15 + 8); |
| 91 | + REQUIRE(traits::params_trait<Params>::dims(x) == 15 + 8); |
| 92 | + } |
| 93 | +} |
0 commit comments