blob: ee3be8fa08b0a8e52b5907b934d55cbb7cfb7e6a [file] [log] [blame]
Austin Schuh70cc9552019-01-21 19:46:48 -08001//
2// * Redistributions of source code must retain the above copyright notice,
3// this list of conditions and the following disclaimer.
4// * Redistributions in binary form must reproduce the above copyright notice,
5// this list of conditions and the following disclaimer in the documentation
6// and/or other materials provided with the distribution.
7// * Neither the name of Google Inc. nor the names of its contributors may be
8// used to endorse or promote products derived from this software without
9// specific prior written permission.
10//
11// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
12// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
13// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
14// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
15// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
16// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
17// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
18// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
19// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
20// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
21// POSSIBILITY OF SUCH DAMAGE.
22//
23// Author: jodebo_beck@gmx.de (Johannes Beck)
24
25#include "ceres/internal/parameter_dims.h"
26
27#include <gtest/gtest.h>
Austin Schuh1d1e6ea2020-12-23 21:56:30 -080028
Austin Schuh70cc9552019-01-21 19:46:48 -080029#include <type_traits>
Austin Schuh1d1e6ea2020-12-23 21:56:30 -080030#include <utility>
Austin Schuh70cc9552019-01-21 19:46:48 -080031
32namespace ceres {
33namespace internal {
34
35// Is valid parameter dims unit test
Austin Schuh1d1e6ea2020-12-23 21:56:30 -080036static_assert(IsValidParameterDimensionSequence(std::integer_sequence<int>()) ==
Austin Schuh70cc9552019-01-21 19:46:48 -080037 true,
38 "Unit test of is valid parameter dimension sequence failed.");
Austin Schuh1d1e6ea2020-12-23 21:56:30 -080039static_assert(IsValidParameterDimensionSequence(
40 std::integer_sequence<int, 2, 1>()) == true,
41 "Unit test of is valid parameter dimension sequence failed.");
42static_assert(IsValidParameterDimensionSequence(
43 std::integer_sequence<int, 0, 1>()) == false,
44 "Unit test of is valid parameter dimension sequence failed.");
45static_assert(IsValidParameterDimensionSequence(
46 std::integer_sequence<int, 3, 0>()) == false,
47 "Unit test of is valid parameter dimension sequence failed.");
Austin Schuh70cc9552019-01-21 19:46:48 -080048
49// Static parameter dims unit test
50static_assert(
51 std::is_same<StaticParameterDims<4, 2, 1>::Parameters,
Austin Schuh1d1e6ea2020-12-23 21:56:30 -080052 std::integer_sequence<int, 4, 2, 1>>::value == true,
Austin Schuh70cc9552019-01-21 19:46:48 -080053 "Unit test of type 'parameters' for static parameter dims failed.");
54
55static_assert(StaticParameterDims<4, 2, 1>::kIsValid == true,
56 "Unit test of is valid for static parameter dims failed.");
57static_assert(StaticParameterDims<4, 2, 1>::kIsDynamic == false,
58 "Unit test of is dynamic for static parameter dims failed.");
59static_assert(StaticParameterDims<4, 2, 1>::kNumParameterBlocks == 3,
60 "Unit test of number of parameter blocks for static parameter "
61 "dims failed.");
62static_assert(
63 StaticParameterDims<4, 2, 1>::kNumParameters == 7,
64 "Unit test of number of parameters for static parameter dims failed.");
65
66// Dynamic parameter dims unit test
67static_assert(DynamicParameterDims::kIsValid == true,
68 "Unit test of is valid for dynamic parameter dims failed.");
69static_assert(DynamicParameterDims::kIsDynamic == true,
70 "Unit test of is dynamic for dynamic parameter dims failed.");
71static_assert(DynamicParameterDims::kNumParameterBlocks == 0,
72 "Unit test of number if parameter blocks for dynamic parameter "
73 "dims failed.");
74static_assert(
75 DynamicParameterDims::kNumParameters == 0,
76 "Unit test of number of parameters for dynamic parameter dims failed.");
77
78TEST(ParameterDims, GetDims) {
79 constexpr int N0 = 3;
80 constexpr int N1 = 4;
81 constexpr int N2 = 2;
82
83 StaticParameterDims<N0, N1, N2> params;
84 EXPECT_EQ(N0, params.GetDim(0));
85 EXPECT_EQ(N1, params.GetDim(1));
86 EXPECT_EQ(N2, params.GetDim(2));
87}
88
89TEST(ParameterDims, GetUnpackedParameters) {
90 constexpr int N0 = 3;
91 constexpr int N1 = 4;
92 constexpr int N2 = 2;
93
94 using ParameterDims = StaticParameterDims<N0, N1, N2>;
95
96 std::array<double, ParameterDims::kNumParameters> packed_parameters{};
97 std::array<double*, 3> unpacked_parameters =
98 ParameterDims::GetUnpackedParameters(packed_parameters.data());
99
100 EXPECT_EQ(packed_parameters.data(), unpacked_parameters[0]);
101 EXPECT_EQ(packed_parameters.data() + N0, unpacked_parameters[1]);
102 EXPECT_EQ(packed_parameters.data() + N0 + N1, unpacked_parameters[2]);
103}
104
105} // namespace internal
106} // namespace ceres