Lagrange
All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Modules Pages
require_approx.h
1/*
2 * Copyright 2024 Adobe. All rights reserved.
3 * This file is licensed to you under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License. You may obtain a copy
5 * of the License at http://www.apache.org/licenses/LICENSE-2.0
6 *
7 * Unless required by applicable law or agreed to in writing, software distributed under
8 * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
9 * OF ANY KIND, either express or implied. See the License for the specific language
10 * governing permissions and limitations under the License.
11 */
12
13#pragma once
14
15#include <Eigen/Dense>
16
17#include <catch2/matchers/catch_matchers_floating_point.hpp>
18
19namespace lagrange::testing {
20
21template <typename DerivedA, typename DerivedB>
22void require_approx(
23 const Eigen::MatrixBase<DerivedA>& A,
24 const Eigen::MatrixBase<DerivedB>& B,
25 typename DerivedA::Scalar eps_rel,
26 typename DerivedA::Scalar eps_abs)
27{
28 REQUIRE(A.rows() == B.rows());
29 REQUIRE(A.cols() == B.cols());
30 for (Eigen::Index i = 0; i < A.size(); ++i) {
31 REQUIRE_THAT(
32 A.derived().data()[i],
33 Catch::Matchers::WithinRel(B.derived().data()[i], eps_rel) ||
34 (Catch::Matchers::WithinAbs(0, eps_abs) &&
35 Catch::Matchers::WithinAbs(B.derived().data()[i], eps_abs)));
36 }
37}
38
39} // namespace lagrange::testing