Program Listing for File t8_gtest_custom_assertion.hxx

Return to documentation for file (test/t8_gtest_custom_assertion.hxx)

/*
  This file is part of t8code.
  t8code is a C library to manage a collection (a forest) of multiple
  connected adaptive space-trees of general element classes in parallel.

  Copyright (C) 2023 the developers

  t8code is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation; either version 2 of the License, or
  (at your option) any later version.

  t8code is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with t8code; if not, write to the Free Software Foundation, Inc.,
  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/

#ifndef T8_GTEST_CUSTOM_ASSERTION_HXX
#define T8_GTEST_CUSTOM_ASSERTION_HXX

#include <gtest/gtest.h>
#include <t8_schemes/t8_default/t8_default.hxx>
#include <t8_types/t8_vec.hxx>
#include <t8_forest/t8_forest_general.h>

testing::AssertionResult
element_equality (const char *ts_expr, const char *tree_class_expr, const char *elem_1_expr, const char *elem_2_expr,
                  const t8_scheme *scheme, const t8_eclass_t eclass, const t8_element_t *elem_1,
                  const t8_element_t *elem_2)
{
  if (scheme->element_is_equal (eclass, elem_1, elem_2)) {
    return testing::AssertionSuccess (false);
  }
  else {
#if T8_ENABLE_DEBUG
    char elem_1_string[BUFSIZ];
    char elem_2_string[BUFSIZ];
    const t8_eclass_t tree_class = scheme->get_eclass_scheme_eclass (eclass);
    scheme->element_to_string (eclass, elem_1, elem_1_string, BUFSIZ);
    scheme->element_to_string (eclass, elem_2, elem_2_string, BUFSIZ);
    return testing::AssertionFailure (false)
           << elem_1_expr << " " << elem_1_string << " is not equal to \n"
           << elem_2_expr << " " << elem_2_string << " given scheme " << ts_expr << " and tree class "
           << tree_class_expr << " " << t8_eclass_to_string[tree_class];
#else
    return testing::AssertionFailure (false)
           << elem_1_expr << " is not equal to \n"
           << elem_2_expr << " given scheme " << ts_expr << " and tree class " << tree_class_expr;
#endif
  }
}

#define EXPECT_ELEM_EQ(scheme, eclass, elem1, elem2) \
  EXPECT_PRED_FORMAT4 (element_equality, (scheme), (eclass), (elem1), (elem2))

#define ASSERT_ELEM_EQ(scheme, eclass, elem1, elem2) \
  ASSERT_PRED_FORMAT4 (element_equality, (scheme), (eclass), (elem1), (elem2))

template <T8InputRange TContainer1, T8InputRange TContainer2>
static inline testing::AssertionResult
container_equality (const char *container_1_expr, const char *container_2_expr, const char *precision_expr,
                    const TContainer1 &container_1, const TContainer2 &container_2, const double precision)
{
  if (t8_eq (container_1, container_2, precision)) {
    return testing::AssertionSuccess ();
  }
  else {
    return testing::AssertionFailure () << container_1_expr << " is not equal to " << container_2_expr << " \n"
                                        << "Precision given by " << precision_expr << " " << precision;
  }
}

#define EXPECT_VEC_EQ(container_1, container_2, precision) \
  EXPECT_PRED_FORMAT3 (container_equality, (container_1), (container_2), (precision))

testing::AssertionResult
forest_equality (const char *forest_A_expr, const char *forest_B_expr, const t8_forest_t forest_A,
                 const t8_forest_t forest_B)
{
  if (t8_forest_is_equal (forest_A, forest_B)) {
    return testing::AssertionSuccess ();
  }
  else {
    return testing::AssertionFailure () << forest_A_expr << " is not equal to " << forest_B_expr;
  }
}

#define EXPECT_FOREST_EQ(forest_A, forest_B) EXPECT_PRED_FORMAT2 (forest_equality, (forest_A), (forest_B))

#endif /* T8_GTEST_CUSTOM_ASSERTION_HXX */