OpenKalman
tests.hpp
Go to the documentation of this file.
1 /* This file is part of OpenKalman, a header-only C++ library for
2  * Kalman filters and other recursive filters.
3  *
4  * Copyright (c) 2021-2025 Christopher Lee Ogden <ogden@gatech.edu>
5  *
6  * This Source Code Form is subject to the terms of the Mozilla Public
7  * License, v. 2.0. If a copy of the MPL was not distributed with this
8  * file, You can obtain one at https://mozilla.org/MPL/2.0/.
9  */
10 
17 #ifndef OPENKALMAN_COLLECTIONS_TESTS_HPP
18 #define OPENKALMAN_COLLECTIONS_TESTS_HPP
19 
20 #include <type_traits>
21 #include <tuple>
22 #include <string>
23 #include <gtest/gtest.h>
24 #include "values/tests/tests.hpp"
26 
27 namespace OpenKalman::test
28 {
35 #ifdef __cpp_concepts
36  template<collections::tuple_like Arg1, collections::tuple_like Arg2, typename Err>
37  struct TestComparison<Arg1, Arg2, Err>
38 #else
39  template<typename Arg1, typename Arg2, typename Err>
40  struct TestComparison<Arg1, Arg2, Err, std::enable_if_t<
41  collections::tuple_like<Arg1> and collections::tuple_like<Arg2>>>
42 #endif
43  : ::testing::AssertionResult
44  {
45  private:
46 
47  template<std::size_t...Ix>
48  static ::testing::AssertionResult
49  compare(const Arg1 arg1, const Arg2 arg2, const Err& err, std::index_sequence<Ix...>)
50  {
51  static_assert(std::tuple_size_v<Arg1> == std::tuple_size_v<Arg2>, "tuple size of arguments must match");
52 
53  if constexpr (collections::tuple_like<Err>)
54  {
55  static_assert(std::tuple_size_v<Err> == std::tuple_size_v<Arg1>, "tuple size of error margins must match that of arguments");
56 
57  if ((... and OpenKalman::test::TestComparison {std::get<Ix>(arg1), std::get<Ix>(arg2), std::get<Ix>(err)}))
58  {
59  return ::testing::AssertionSuccess();
60  }
61  else
62  {
63  return (::testing::AssertionFailure() << ... << (std::string(Ix == 0 ? "" : ", ") +
64  std::string(OpenKalman::test::TestComparison {std::get<Ix>(arg1), std::get<Ix>(arg2), std::get<Ix>(err)} ? "true" : "false")));
65  }
66  }
67  else
68  {
69  if ((... and OpenKalman::test::TestComparison {std::get<Ix>(arg1), std::get<Ix>(arg2), err}))
70  {
71  return ::testing::AssertionSuccess();
72  }
73  else
74  {
75  return (::testing::AssertionFailure() << ... << (std::string(Ix == 0 ? "" : ", ") +
76  std::string(OpenKalman::test::TestComparison {std::get<Ix>(arg1), std::get<Ix>(arg2), err} ? "true" : "false")));
77  }
78  }
79  }
80 
81  public:
82 
83  TestComparison(const Arg1& arg1, const Arg2& arg2, const Err& err)
84  : ::testing::AssertionResult {compare(arg1, arg2, err, std::make_index_sequence<std::tuple_size_v<Arg1>>{})} {}
85 
86  };
87 
88 
89 } // namespace OpenKalman::test
90 
91 
92 #endif //OPENKALMAN_COLLECTIONS_TESTS_HPP
Definition for collections::tuple_like.
constexpr partial_ordering compare(const Lhs &lhs, const Rhs &rhs)
Compare two collections.
Definition: compare.hpp:106
Definition: tuple_reverse.hpp:103
Definition: tests.hpp:41
Definition: tests.hpp:27
Basic utilities for OpenKalman testing.