Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use dfs base for face descendant test #860

Merged
merged 5 commits into from
Jan 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions test/t8_schemes/t8_gtest_dfs_base.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,12 @@
#include <t8_eclass.h>
#include <t8_schemes/t8_default/t8_default_cxx.hxx>

class TestDFS : public testing::TestWithParam<t8_eclass_t> {
class TestDFS: public testing::TestWithParam<t8_eclass_t> {
public:
/** recursive tests check something for all descendants of a starting element (currently only root) upto maxlevel
/** recursive tests check something for all descendants of a starting element (currently only root) upto maxlevel
*/
virtual void
check_element (){};
check_element () {};

/** recursive depth first search to iterate over all descendants of elem up to max_dfs_recursion_level */
void
Expand All @@ -57,16 +57,16 @@ class TestDFS : public testing::TestWithParam<t8_eclass_t> {
}

void
dfs_test_setup()
dfs_test_setup ()
{
scheme = t8_scheme_new_default_cxx();
scheme = t8_scheme_new_default_cxx ();
eclass = GetParam ();
ts = scheme->eclass_schemes[eclass];
ts->t8_element_new (1, &element);
ts->t8_element_set_linear_id (element, 0, 0);
}
void
dfs_test_teardown()
dfs_test_teardown ()
{
ts->t8_element_destroy (1, &element);
t8_scheme_cxx_unref (&scheme);
Expand All @@ -75,12 +75,12 @@ class TestDFS : public testing::TestWithParam<t8_eclass_t> {
void
SetUp () override
{
dfs_test_setup();
dfs_test_setup ();
}
void
TearDown () override
{
dfs_test_teardown();
dfs_test_teardown ();
}

t8_scheme_cxx *scheme;
Expand Down
18 changes: 9 additions & 9 deletions test/t8_schemes/t8_gtest_equal.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -27,38 +27,38 @@
#include <test/t8_gtest_macros.hxx>
#include "t8_gtest_dfs_base.hxx"

class class_test_equal: public TestDFS{
class class_test_equal: public TestDFS {
virtual void
check_element ()
{
const int num_children = ts->t8_element_num_children (element);
for (int ichild1 = 0; ichild1 < num_children; ichild1++) {
ts->t8_element_child (element, ichild1, child1);
/* the child must be different than its parent */
EXPECT_FALSE(ts->t8_element_equal(element, child1));
EXPECT_FALSE (ts->t8_element_equal (element, child1));
for (int ichild2 = 0; ichild2 < num_children; ichild2++) {
ts->t8_element_child (element, ichild2, child2);
/* the child must be different than its parent */
EXPECT_FALSE(ts->t8_element_equal(element, child2));
const int equal = ts->t8_element_equal(child1, child2);
/* The children must be equal if and only if their indices are equal. */
EXPECT_EQ(equal, ichild1 == ichild2);
EXPECT_EQ (equal, ichild1 == ichild2);
/* t8_element_equal should compute the same as t8_element_compare,
* when we only check if compare has 0 as result. */
const int compare_equal = !ts->t8_element_compare(child1, child2);
EXPECT_EQ(equal, compare_equal);
const int compare_equal = !ts->t8_element_compare (child1, child2);
EXPECT_EQ (equal, compare_equal);
}
}
}

protected:
void
SetUp () override
{
dfs_test_setup();
dfs_test_setup ();
/* Get element and initialize it */
ts->t8_element_new (1, &child1);
ts->t8_element_new (1, &child2);

}
void
TearDown () override
Expand All @@ -68,7 +68,7 @@ class class_test_equal: public TestDFS{
ts->t8_element_destroy (1, &child2);

/* Destroy DFS test */
dfs_test_teardown();
dfs_test_teardown ();
}
t8_element_t *child1;
t8_element_t *child2;
Expand All @@ -81,7 +81,7 @@ TEST_P (class_test_equal, test_equal_dfs)
#else
const int maxlvl = 6;
#endif
check_recursive_dfs_to_max_lvl(maxlvl);
check_recursive_dfs_to_max_lvl (maxlvl);
}

INSTANTIATE_TEST_SUITE_P (t8_gtest_test_all_imps, class_test_equal, AllEclasses);
187 changes: 68 additions & 119 deletions test/t8_schemes/t8_gtest_face_descendant.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -26,149 +26,98 @@
#include <t8_schemes/t8_default/t8_default_cxx.hxx>
#include <test/t8_gtest_custom_assertion.hxx>
#include <test/t8_gtest_macros.hxx>
#include "t8_gtest_dfs_base.hxx"

class class_descendant: public testing::TestWithParam<t8_eclass_t> {
protected:
void
SetUp () override
{
eclass = GetParam ();

scheme = t8_scheme_new_default_cxx ();
/* Get scheme for eclass */
ts = scheme->eclass_schemes[eclass];
}
void
TearDown () override
{
/* Destroy scheme */
t8_scheme_cxx_unref (&scheme);
}
t8_eclass_t eclass;
t8_scheme_cxx *scheme;
t8_eclass_scheme_c *ts;
};

/* Computes manually the test_child_id to calculate the descendant with this id on level ilevel. */
void
t8_face_descendant_test_child (t8_element_t *face_desc, const t8_element_t *elem, t8_eclass_scheme_c *ts, int face,
int level_elem, int ilevel, int num_children, int test_child_id)
/* compute the first/last descendant by iteratively taking the first/last child at each level*/
static void
Davknapp marked this conversation as resolved.
Show resolved Hide resolved
t8_test_manual_first_last_face_descendant (const t8_eclass_scheme_c *ts, const t8_element_t *element, const int iface,
const int desc_level, const int last, t8_element_t *face_desc)
{
const int num_children_at_face = ts->t8_element_num_face_children (element, iface);

int *child_indices = T8_ALLOC (int, num_children);

t8_element_t **children = T8_ALLOC (t8_element_t *, num_children);
ts->t8_element_new (num_children, children);
int *child_indices = T8_ALLOC (int, num_children_at_face);
t8_element_t **children = T8_ALLOC (t8_element_t *, num_children_at_face);
ts->t8_element_new (num_children_at_face, children);

ts->t8_element_copy (elem, face_desc);
for (int klevel = level_elem; klevel < ilevel; klevel++) {
ts->t8_element_copy (element, face_desc);
const int level = ts->t8_element_level (element);
for (int ilevel = level; ilevel < desc_level; ilevel++) {
EXPECT_EQ (ts->t8_element_num_face_children (element, iface), num_children_at_face);
/* Compute child_id of the test_child_id-th child. */
ts->t8_element_children_at_face (face_desc, face, children, num_children, child_indices);
int child_id = child_indices[test_child_id];
ts->t8_element_children_at_face (face_desc, iface, children, num_children_at_face, child_indices);

/* chose correct face_id dependent on if we want first or last face desc.*/
const int face_child_id = last ? num_children_at_face - 1 : 0;

const int child_id = child_indices[face_child_id];

ts->t8_element_child (face_desc, child_id, face_desc);
}
ts->t8_element_destroy (num_children, children);
ts->t8_element_destroy (num_children_at_face, children);
T8_FREE (children);
T8_FREE (child_indices);
}

/* Check the linear first and last descendants of an element along all faces.
For the test the descendants are computed manually by t8_face_descendant_test_child and
by the scheme implementation t8_element_first_descendant for the first descendants over the levels.
Next to the t8_element_first_descendant check, the function t8_element_last_descendant is checked the same way. */
void
t8_linear_face_descendant (const t8_element_t *elem, t8_element_t *manual_face_desc, t8_element_t *face_desc,
t8_eclass_scheme_c *ts, int maxlvl)
{

int level_elem = ts->t8_element_level (elem);
int num_faces;

num_faces = ts->t8_element_num_faces (elem);

/* Testing the linear first descendant. */
ts->t8_element_copy (elem, manual_face_desc);
for (int ilevel = level_elem + 1; ilevel < maxlvl; ilevel++) {
for (int jface = 0; jface < num_faces; jface++) {

/* Number of children of the first descendant of the previous level. */
int num_children = ts->t8_element_num_face_children (manual_face_desc, jface);
t8_face_descendant_test_child (manual_face_desc, elem, ts, jface, level_elem, ilevel, num_children, 0);

ts->t8_element_first_descendant_face (elem, jface, face_desc, ilevel);
/* Compare the manually computed child with the result of t8_element_first_descendant_face. */
EXPECT_ELEM_EQ (ts, face_desc, manual_face_desc);
class class_descendant: public TestDFS {
virtual void
check_element ()
{
/* Check the linear first and last descendants of an element along all faces.
* For the test the descendants are computed manually by t8_test_manual_first_last_face_descendant and
* by the scheme implementation t8_element_first_descendant for the first descendants over the levels.
*/

const int level = ts->t8_element_level (element);
const int num_faces = ts->t8_element_num_faces (element);

/* Testing the linear first descendant. */
for (int ilevel = level + 1; ilevel < max_test_lvl; ilevel++) {
for (int jface = 0; jface < num_faces; jface++) {

t8_test_manual_first_last_face_descendant (ts, element, jface, ilevel, 0, manual_face_desc);
ts->t8_element_first_descendant_face (element, jface, scheme_face_desc, ilevel);
/* Compare the manually computed child with the result of t8_element_first_descendant_face. */
EXPECT_ELEM_EQ (ts, scheme_face_desc, manual_face_desc);

t8_test_manual_first_last_face_descendant (ts, element, jface, ilevel, 1, manual_face_desc);
ts->t8_element_last_descendant_face (element, jface, scheme_face_desc, ilevel);
/* Compare the manually computed child with the result of t8_element_last_descendant_face. */
EXPECT_ELEM_EQ (ts, scheme_face_desc, manual_face_desc);
}
}
}

/* Testing the linear last descendant. */
ts->t8_element_copy (elem, manual_face_desc);
for (int ilevel = level_elem + 1; ilevel < maxlvl; ilevel++) {
for (int jface = 0; jface < num_faces; jface++) {

/* Number of children of the last descendant of the previous level. */
int num_children = ts->t8_element_num_face_children (manual_face_desc, jface);
t8_face_descendant_test_child (manual_face_desc, elem, ts, jface, level_elem, ilevel, num_children,
num_children - 1);

/* Compare the manuall computed child with the result of t8_element_last_descendant_face. */
ts->t8_element_last_descendant_face (elem, jface, face_desc, ilevel);
EXPECT_ELEM_EQ (ts, face_desc, manual_face_desc);
}
protected:
void
SetUp () override
{
dfs_test_setup ();
max_test_lvl = ts->t8_element_maxlevel ();
ts->t8_element_new (1, &manual_face_desc);
ts->t8_element_new (1, &scheme_face_desc);
}
}

/*Recursively check the first and last descendant along a face for all children with the help of the previous test t8_linear_face_descendant. */
void
t8_recursive_face_descendant (t8_element_t *elem, t8_element_t *face_desc, t8_element_t *manual_face_desc,
t8_element_t *child, t8_eclass_scheme_c *ts, int maxlvl)
{
int level = ts->t8_element_level (elem);
T8_ASSERT (level <= maxlvl && maxlvl <= ts->t8_element_maxlevel () - 1);
if (level == maxlvl)
return;

t8_linear_face_descendant (elem, manual_face_desc, face_desc, ts, maxlvl);

int num_children = ts->t8_element_num_children (elem);
for (int ichild = 0; ichild < num_children; ichild++) {
ts->t8_element_child (elem, ichild, child);
t8_recursive_face_descendant (child, face_desc, manual_face_desc, elem, ts, maxlvl);
ts->t8_element_parent (child, elem);
void
TearDown () override
{
ts->t8_element_destroy (1, &manual_face_desc);
ts->t8_element_destroy (1, &scheme_face_desc);
dfs_test_teardown ();
}
}
int max_test_lvl;
t8_element_t *manual_face_desc;
t8_element_t *scheme_face_desc;
};

TEST_P (class_descendant, t8_check_face_desc)
{

#ifdef T8_ENABLE_DEBUG
const int maxlvl = 3;
#else
const int maxlvl = 4;
#else
const int maxlvl = 6;
#endif
t8_element_t *element;
t8_element_t *child;
t8_element_t *test;
t8_element_t *tmp;

/* Get element and initialize it */
ts->t8_element_new (1, &element);
ts->t8_element_new (1, &child);
ts->t8_element_new (1, &test);
ts->t8_element_new (1, &tmp);

ts->t8_element_set_linear_id (element, 0, 0);

/* Check for correct parent-child relation */
t8_linear_face_descendant (element, child, test, ts, maxlvl);
t8_recursive_face_descendant (element, test, tmp, child, ts, maxlvl);

/* Destroy element */
ts->t8_element_destroy (1, &element);
ts->t8_element_destroy (1, &child);
ts->t8_element_destroy (1, &test);
ts->t8_element_destroy (1, &tmp);

check_recursive_dfs_to_max_lvl (maxlvl);
}

INSTANTIATE_TEST_SUITE_P (t8_gtest_element_face_descendant, class_descendant, AllEclasses);
Loading