blob: ee3be8fa08b0a8e52b5907b934d55cbb7cfb7e6a [file] [log] [blame]
Johannes Beck7edb3a62018-10-08 22:15:22 +02001//
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>
Nikolaus Demmel7b8f6752020-09-20 21:45:24 +020028
Johannes Beck7edb3a62018-10-08 22:15:22 +020029#include <type_traits>
Alex Stewart7ef83e02020-05-30 11:30:01 +010030#include <utility>
Johannes Beck7edb3a62018-10-08 22:15:22 +020031
32namespace ceres {
33namespace internal {
34
35// Is valid parameter dims unit test
Alex Stewart7ef83e02020-05-30 11:30:01 +010036static_assert(IsValidParameterDimensionSequence(std::integer_sequence<int>()) ==
Johannes Beck7edb3a62018-10-08 22:15:22 +020037 true,
38 "Unit test of is valid parameter dimension sequence failed.");
Nikolaus Demmel7b8f6752020-09-20 21:45:24 +020039static_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.");
Johannes Beck7edb3a62018-10-08 22:15:22 +020048
49// Static parameter dims unit test
50static_assert(
51 std::is_same<StaticParameterDims<4, 2, 1>::Parameters,
Alex Stewart7ef83e02020-05-30 11:30:01 +010052 std::integer_sequence<int, 4, 2, 1>>::value == true,
Johannes Beck7edb3a62018-10-08 22:15:22 +020053 "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
Johannes Beck8eef94d2018-10-13 22:34:53 +020094 using ParameterDims = StaticParameterDims<N0, N1, N2>;
Johannes Beck7edb3a62018-10-08 22:15:22 +020095
Johannes Beck8eef94d2018-10-13 22:34:53 +020096 std::array<double, ParameterDims::kNumParameters> packed_parameters{};
Johannes Beck7edb3a62018-10-08 22:15:22 +020097 std::array<double*, 3> unpacked_parameters =
Johannes Beck8eef94d2018-10-13 22:34:53 +020098 ParameterDims::GetUnpackedParameters(packed_parameters.data());
Johannes Beck7edb3a62018-10-08 22:15:22 +020099
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