Johannes Beck | 7edb3a6 | 2018-10-08 22:15:22 +0200 | [diff] [blame] | 1 | // |
| 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 Demmel | 7b8f675 | 2020-09-20 21:45:24 +0200 | [diff] [blame] | 28 | |
Johannes Beck | 7edb3a6 | 2018-10-08 22:15:22 +0200 | [diff] [blame] | 29 | #include <type_traits> |
Alex Stewart | 7ef83e0 | 2020-05-30 11:30:01 +0100 | [diff] [blame] | 30 | #include <utility> |
Johannes Beck | 7edb3a6 | 2018-10-08 22:15:22 +0200 | [diff] [blame] | 31 | |
| 32 | namespace ceres { |
| 33 | namespace internal { |
| 34 | |
| 35 | // Is valid parameter dims unit test |
Alex Stewart | 7ef83e0 | 2020-05-30 11:30:01 +0100 | [diff] [blame] | 36 | static_assert(IsValidParameterDimensionSequence(std::integer_sequence<int>()) == |
Johannes Beck | 7edb3a6 | 2018-10-08 22:15:22 +0200 | [diff] [blame] | 37 | true, |
| 38 | "Unit test of is valid parameter dimension sequence failed."); |
Nikolaus Demmel | 7b8f675 | 2020-09-20 21:45:24 +0200 | [diff] [blame] | 39 | static_assert(IsValidParameterDimensionSequence( |
| 40 | std::integer_sequence<int, 2, 1>()) == true, |
| 41 | "Unit test of is valid parameter dimension sequence failed."); |
| 42 | static_assert(IsValidParameterDimensionSequence( |
| 43 | std::integer_sequence<int, 0, 1>()) == false, |
| 44 | "Unit test of is valid parameter dimension sequence failed."); |
| 45 | static_assert(IsValidParameterDimensionSequence( |
| 46 | std::integer_sequence<int, 3, 0>()) == false, |
| 47 | "Unit test of is valid parameter dimension sequence failed."); |
Johannes Beck | 7edb3a6 | 2018-10-08 22:15:22 +0200 | [diff] [blame] | 48 | |
| 49 | // Static parameter dims unit test |
| 50 | static_assert( |
| 51 | std::is_same<StaticParameterDims<4, 2, 1>::Parameters, |
Alex Stewart | 7ef83e0 | 2020-05-30 11:30:01 +0100 | [diff] [blame] | 52 | std::integer_sequence<int, 4, 2, 1>>::value == true, |
Johannes Beck | 7edb3a6 | 2018-10-08 22:15:22 +0200 | [diff] [blame] | 53 | "Unit test of type 'parameters' for static parameter dims failed."); |
| 54 | |
| 55 | static_assert(StaticParameterDims<4, 2, 1>::kIsValid == true, |
| 56 | "Unit test of is valid for static parameter dims failed."); |
| 57 | static_assert(StaticParameterDims<4, 2, 1>::kIsDynamic == false, |
| 58 | "Unit test of is dynamic for static parameter dims failed."); |
| 59 | static_assert(StaticParameterDims<4, 2, 1>::kNumParameterBlocks == 3, |
| 60 | "Unit test of number of parameter blocks for static parameter " |
| 61 | "dims failed."); |
| 62 | static_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 |
| 67 | static_assert(DynamicParameterDims::kIsValid == true, |
| 68 | "Unit test of is valid for dynamic parameter dims failed."); |
| 69 | static_assert(DynamicParameterDims::kIsDynamic == true, |
| 70 | "Unit test of is dynamic for dynamic parameter dims failed."); |
| 71 | static_assert(DynamicParameterDims::kNumParameterBlocks == 0, |
| 72 | "Unit test of number if parameter blocks for dynamic parameter " |
| 73 | "dims failed."); |
| 74 | static_assert( |
| 75 | DynamicParameterDims::kNumParameters == 0, |
| 76 | "Unit test of number of parameters for dynamic parameter dims failed."); |
| 77 | |
| 78 | TEST(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 | |
| 89 | TEST(ParameterDims, GetUnpackedParameters) { |
| 90 | constexpr int N0 = 3; |
| 91 | constexpr int N1 = 4; |
| 92 | constexpr int N2 = 2; |
| 93 | |
Johannes Beck | 8eef94d | 2018-10-13 22:34:53 +0200 | [diff] [blame] | 94 | using ParameterDims = StaticParameterDims<N0, N1, N2>; |
Johannes Beck | 7edb3a6 | 2018-10-08 22:15:22 +0200 | [diff] [blame] | 95 | |
Johannes Beck | 8eef94d | 2018-10-13 22:34:53 +0200 | [diff] [blame] | 96 | std::array<double, ParameterDims::kNumParameters> packed_parameters{}; |
Johannes Beck | 7edb3a6 | 2018-10-08 22:15:22 +0200 | [diff] [blame] | 97 | std::array<double*, 3> unpacked_parameters = |
Johannes Beck | 8eef94d | 2018-10-13 22:34:53 +0200 | [diff] [blame] | 98 | ParameterDims::GetUnpackedParameters(packed_parameters.data()); |
Johannes Beck | 7edb3a6 | 2018-10-08 22:15:22 +0200 | [diff] [blame] | 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 |