From 13534ef7852139892cdbf4682f1b7ebe367e6ffa Mon Sep 17 00:00:00 2001 From: Alexander Condello Date: Thu, 12 Dec 2024 15:01:06 -0800 Subject: [PATCH] Refactor some tests to use ArrayValidatioNode --- tests/cpp/nodes/indexing/test_advanced.cpp | 54 ++++++++-------------- tests/cpp/nodes/indexing/test_basic.cpp | 39 ++++------------ tests/cpp/nodes/test_collections.cpp | 21 +++------ tests/cpp/utils.hpp | 50 -------------------- 4 files changed, 32 insertions(+), 132 deletions(-) delete mode 100644 tests/cpp/utils.hpp diff --git a/tests/cpp/nodes/indexing/test_advanced.cpp b/tests/cpp/nodes/indexing/test_advanced.cpp index a1d3ea30..280ff73a 100644 --- a/tests/cpp/nodes/indexing/test_advanced.cpp +++ b/tests/cpp/nodes/indexing/test_advanced.cpp @@ -21,7 +21,6 @@ #include "dwave-optimization/nodes/mathematical.hpp" #include "dwave-optimization/nodes/numbers.hpp" #include "dwave-optimization/nodes/testing.hpp" -#include "../../utils.hpp" namespace dwave::optimization { @@ -80,6 +79,8 @@ TEST_CASE("AdvancedIndexingNode") { auto s_ptr = graph.emplace_node(5); auto B_ptr = graph.emplace_node(A_ptr, s_ptr); + graph.emplace_node(B_ptr); + THEN("The resulting array has the size/shape we expect") { CHECK(B_ptr->dynamic()); CHECK(std::ranges::equal(B_ptr->shape(), std::vector{-1})); @@ -120,8 +121,6 @@ TEST_CASE("AdvancedIndexingNode") { CHECK(B_ptr->size(state) == s_ptr->size(state)); CHECK(std::ranges::equal(B_ptr->view(state), std::vector{4})); - - verify_array_diff({}, {4}, B_ptr->diff(state)); } AND_WHEN("We commit") { @@ -166,8 +165,6 @@ TEST_CASE("AdvancedIndexingNode") { CHECK(std::ranges::equal(B_ptr->view(state), std::vector{4, 3})); CHECK(B_ptr->size_diff(state) == 2); // grew by two - - verify_array_diff({}, {4, 3}, B_ptr->diff(state)); } AND_WHEN("We commit") { @@ -195,8 +192,6 @@ TEST_CASE("AdvancedIndexingNode") { CHECK(std::ranges::equal(B_ptr->view(state), std::vector{4})); CHECK(B_ptr->size_diff(state) == -1); // shrank by one - - verify_array_diff({4, 3}, {4}, B_ptr->diff(state)); } AND_WHEN("We commit") { @@ -256,6 +251,8 @@ TEST_CASE("AdvancedIndexingNode") { auto B_ptr = graph.emplace_node(A_ptr, i_ptr, j_ptr); + graph.emplace_node(B_ptr); + THEN("Then the resulting matrix has the size/shape we expect") { CHECK(std::ranges::equal(B_ptr->shape(), std::vector{3})); CHECK(B_ptr->size() == 3); @@ -306,8 +303,6 @@ TEST_CASE("AdvancedIndexingNode") { CHECK(std::ranges::equal(i_ptr->view(state), std::vector{0, 1, 2})); CHECK(std::ranges::equal(j_ptr->view(state), std::vector{2, 1, 0})); CHECK(std::ranges::equal(B_ptr->view(state), std::vector{2, 4, 6})); - - verify_array_diff({2, 7, 3}, {2, 4, 6}, B_ptr->diff(state)); } AND_WHEN("We commit") { @@ -379,7 +374,6 @@ TEST_CASE("AdvancedIndexingNode") { B_ptr->propagate(state); CHECK(std::ranges::equal(B_ptr->view(state), std::vector{0})); - verify_array_diff({}, {0}, B_ptr->diff(state)); } } @@ -419,7 +413,6 @@ TEST_CASE("AdvancedIndexingNode") { CHECK(std::ranges::equal(i_ptr->view(state), std::vector{0, 2, 1})); CHECK(std::ranges::equal(j_ptr->view(state), std::vector{2, 1, 0})); CHECK(std::ranges::equal(B_ptr->view(state), std::vector{2, 7, 3})); - verify_array_diff({2, 7}, {2, 7, 3}, B_ptr->diff(state)); AND_WHEN("We revert") { dyn_ptr->revert(state); @@ -448,7 +441,6 @@ TEST_CASE("AdvancedIndexingNode") { CHECK(std::ranges::equal(i_ptr->view(state), std::vector{0})); CHECK(std::ranges::equal(j_ptr->view(state), std::vector{2})); CHECK(std::ranges::equal(B_ptr->view(state), std::vector{2})); - verify_array_diff({2, 7}, {2}, B_ptr->diff(state)); AND_WHEN("We revert") { dyn_ptr->revert(state); @@ -475,7 +467,6 @@ TEST_CASE("AdvancedIndexingNode") { CHECK(std::ranges::equal(i_ptr->view(state), std::vector{0})); CHECK(std::ranges::equal(j_ptr->view(state), std::vector{1})); CHECK(std::ranges::equal(B_ptr->view(state), std::vector{1})); - verify_array_diff({2, 7}, {1}, B_ptr->diff(state)); AND_WHEN("We revert") { dyn_ptr->revert(state); @@ -506,7 +497,6 @@ TEST_CASE("AdvancedIndexingNode") { CHECK(std::ranges::equal(i_ptr->view(state), std::vector{0, 2})); CHECK(std::ranges::equal(j_ptr->view(state), std::vector{2, 1})); CHECK(std::ranges::equal(B_ptr->view(state), std::vector{2, 7})); - verify_array_diff({2, 7}, {2, 7}, B_ptr->diff(state)); AND_WHEN("We revert") { dyn_ptr->revert(state); @@ -739,6 +729,8 @@ TEST_CASE("AdvancedIndexingNode") { auto adv = graph.emplace_node(arr_ptr, i_ptr, j_ptr, k_ptr, Slice()); + graph.emplace_node(adv); + THEN("We get the shape we expect") { CHECK(adv->dynamic()); CHECK(std::ranges::equal(adv->shape(), std::vector{-1, 4})); @@ -767,8 +759,6 @@ TEST_CASE("AdvancedIndexingNode") { CHECK(adv->size(state) == 8); CHECK(std::ranges::equal(adv->view(state), std::vector{36, 37, 38, 39, 116, 117, 118, 119})); - verify_array_diff({}, {36, 37, 38, 39, 116, 117, 118, 119}, - adv->diff(state)); } AND_WHEN("We shrink the indexing nodes and propagate") { @@ -790,8 +780,6 @@ TEST_CASE("AdvancedIndexingNode") { CHECK(adv->size(state) == 4); CHECK(std::ranges::equal(adv->view(state), std::vector{36, 37, 38, 39})); - verify_array_diff({36, 37, 38, 39, 116, 117, 118, 119}, - {36, 37, 38, 39}, adv->diff(state)); } AND_WHEN("We revert") { @@ -818,6 +806,8 @@ TEST_CASE("AdvancedIndexingNode") { auto adv = graph.emplace_node(arr_ptr, i_ptr, Slice(), j_ptr, k_ptr); + graph.emplace_node(adv); + THEN("We get the shape we expect") { CHECK(adv->dynamic()); CHECK(std::ranges::equal(adv->shape(), std::vector{-1, 3})); @@ -846,7 +836,6 @@ TEST_CASE("AdvancedIndexingNode") { CHECK(adv->size(state) == 6); CHECK(std::ranges::equal(adv->view(state), std::vector{7, 27, 47, 71, 91, 111})); - verify_array_diff({}, {7, 27, 47, 71, 91, 111}, adv->diff(state)); } AND_WHEN("We shrink the indexing nodes and propagate") { @@ -867,8 +856,6 @@ TEST_CASE("AdvancedIndexingNode") { THEN("The state has the expected values and the diff is correct") { CHECK(adv->size(state) == 3); CHECK(std::ranges::equal(adv->view(state), std::vector{7, 27, 47})); - verify_array_diff({7, 27, 47, 71, 91, 111}, {7, 27, 47}, - adv->diff(state)); } AND_WHEN("We revert") { @@ -1211,6 +1198,8 @@ TEST_CASE("AdvancedIndexingNode") { auto adv = graph.emplace_node(arr_ptr, Slice(), i_ptr, j_ptr, k_ptr); + graph.emplace_node(adv); + THEN("We get the shape we expect") { CHECK(adv->dynamic()); CHECK(std::ranges::equal(adv->shape(), std::vector{-1, 3})); @@ -1239,7 +1228,6 @@ TEST_CASE("AdvancedIndexingNode") { CHECK(adv->size(state) == 6); CHECK(std::ranges::equal(adv->view(state), std::vector{24, 8, 46, 84, 68, 106})); - verify_array_diff({}, {24, 8, 46, 84, 68, 106}, adv->diff(state)); } AND_WHEN("We mutate the main array") { @@ -1260,8 +1248,6 @@ TEST_CASE("AdvancedIndexingNode") { CHECK(adv->size(state) == 6); CHECK(std::ranges::equal(adv->view(state), std::vector{24, -4, 46, -1, -2, 106})); - verify_array_diff({24, 8, 46, 84, 68, 106}, {24, -4, 46, -1, -2, 106}, - adv->diff(state)); } } @@ -1283,8 +1269,6 @@ TEST_CASE("AdvancedIndexingNode") { CHECK(adv->size(state) == 6); CHECK(std::ranges::equal(adv->view(state), std::vector{24, -4, 46, -1, -2, 106})); - verify_array_diff({24, 8, 46, 84, 68, 106}, {24, -4, 46, -1, -2, 106}, - adv->diff(state)); } } } @@ -1296,6 +1280,8 @@ TEST_CASE("AdvancedIndexingNode") { Slice()); auto val = graph.emplace_node(adv); + graph.emplace_node(adv); + THEN("We get the shape we expect") { CHECK(adv->dynamic()); CHECK(std::ranges::equal(adv->shape(), std::vector{-1, 3, 4})); @@ -1328,7 +1314,6 @@ TEST_CASE("AdvancedIndexingNode") { CHECK(adv->size(state) == 2 * 3 * 4); CHECK(std::ranges::equal(adv->shape(state), std::vector{2, 3, 4})); CHECK(std::ranges::equal(adv->view(state), expected)); - verify_array_diff({}, expected, adv->diff(state)); } arr_ptr->commit(state); @@ -1360,7 +1345,6 @@ TEST_CASE("AdvancedIndexingNode") { CHECK(adv->size(state) == 2 * 3 * 4); CHECK(std::ranges::equal(adv->shape(state), std::vector{2, 3, 4})); CHECK(std::ranges::equal(adv->view(state), new_expected)); - verify_array_diff(expected, new_expected, adv->diff(state)); } } @@ -1381,7 +1365,6 @@ TEST_CASE("AdvancedIndexingNode") { CHECK(adv->size(state) == 2 * 3 * 4); CHECK(std::ranges::equal(adv->shape(state), std::vector{2, 3, 4})); CHECK(std::ranges::equal(adv->view(state), new_expected)); - verify_array_diff(expected, new_expected, adv->diff(state)); } AND_WHEN("We revert") { @@ -1422,7 +1405,6 @@ TEST_CASE("AdvancedIndexingNode") { CHECK(adv->size(state) == 2 * 3 * 4); CHECK(std::ranges::equal(adv->shape(state), std::vector{2, 3, 4})); CHECK(std::ranges::equal(adv->view(state), new_expected)); - verify_array_diff(expected, new_expected, adv->diff(state)); } AND_WHEN("We revert") { @@ -1459,6 +1441,8 @@ TEST_CASE("AdvancedIndexingNode") { auto adv = graph.emplace_node(arr_ptr, i_ptr, j_ptr, Slice(), Slice()); + graph.emplace_node(adv); + THEN("We get the shape we expect") { CHECK(!adv->dynamic()); CHECK(std::ranges::equal(adv->shape(), std::vector{5, 4})); @@ -1496,7 +1480,6 @@ TEST_CASE("AdvancedIndexingNode") { std::vector new_state({40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59}); CHECK(std::ranges::equal(adv->view(state), new_state)); - verify_array_diff(expected_initial_state, new_state, adv->diff(state)); } } @@ -1516,7 +1499,6 @@ TEST_CASE("AdvancedIndexingNode") { std::vector new_state({80, -81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99}); CHECK(std::ranges::equal(adv->view(state), new_state)); - verify_array_diff(expected_initial_state, new_state, adv->diff(state)); } } } @@ -1526,6 +1508,8 @@ TEST_CASE("AdvancedIndexingNode") { auto adv = graph.emplace_node(arr_ptr, Slice(), i_ptr, j_ptr, Slice()); + graph.emplace_node(adv); + THEN("We get the shape we expect") { CHECK(!adv->dynamic()); CHECK(std::ranges::equal(adv->shape(), std::vector{2, 4})); @@ -1560,7 +1544,6 @@ TEST_CASE("AdvancedIndexingNode") { std::vector new_state({8, 9, 10, 11, 68, 69, 70, 71}); CHECK(std::ranges::equal(adv->view(state), new_state)); - verify_array_diff(expected_initial_state, new_state, adv->diff(state)); } } @@ -1579,7 +1562,6 @@ TEST_CASE("AdvancedIndexingNode") { std::vector new_state({24, -25, 26, 27, 84, 85, 86, 87}); CHECK(std::ranges::equal(adv->view(state), new_state)); - verify_array_diff(expected_initial_state, new_state, adv->diff(state)); } } } @@ -1589,6 +1571,8 @@ TEST_CASE("AdvancedIndexingNode") { auto adv = graph.emplace_node(arr_ptr, i_ptr, j_ptr, Slice(), k_ptr); + graph.emplace_node(adv); + THEN("We get the shape we expect") { CHECK(!adv->dynamic()); CHECK(std::ranges::equal(adv->shape(), std::vector{5})); @@ -1625,7 +1609,6 @@ TEST_CASE("AdvancedIndexingNode") { std::vector new_state({41, 45, 49, 53, 57}); CHECK(std::ranges::equal(adv->view(state), new_state)); - verify_array_diff(expected_initial_state, new_state, adv->diff(state)); } } @@ -1644,7 +1627,6 @@ TEST_CASE("AdvancedIndexingNode") { std::vector new_state({83, -87, 91, 95, 99}); CHECK(std::ranges::equal(adv->view(state), new_state)); - verify_array_diff(expected_initial_state, new_state, adv->diff(state)); } } } diff --git a/tests/cpp/nodes/indexing/test_basic.cpp b/tests/cpp/nodes/indexing/test_basic.cpp index e1d33cd2..a4079329 100644 --- a/tests/cpp/nodes/indexing/test_basic.cpp +++ b/tests/cpp/nodes/indexing/test_basic.cpp @@ -21,7 +21,6 @@ #include "dwave-optimization/nodes/mathematical.hpp" #include "dwave-optimization/nodes/numbers.hpp" #include "dwave-optimization/nodes/testing.hpp" -#include "../../utils.hpp" namespace dwave::optimization { @@ -625,6 +624,8 @@ TEST_CASE("BasicIndexingNode") { auto x_ptr = graph.emplace_node(5); auto y_ptr = graph.emplace_node(x_ptr, Slice(1, std::nullopt, 2)); + graph.emplace_node(y_ptr); + THEN("y has the shape we expect") { CHECK(y_ptr->size() == 2); CHECK(y_ptr->ndim() == 1); @@ -646,8 +647,6 @@ TEST_CASE("BasicIndexingNode") { THEN("The states are as expected") { CHECK(std::ranges::equal(x_ptr->view(state), std::vector{0, 2, 1, 3, 4})); CHECK(std::ranges::equal(y_ptr->view(state), std::vector{2, 3})); - - verify_array_diff({1, 3}, {2, 3}, y_ptr->diff(state)); } } } @@ -657,6 +656,8 @@ TEST_CASE("BasicIndexingNode") { auto y_ptr = graph.emplace_node( x_ptr, Slice(std::nullopt, std::nullopt, 2), Slice(1, std::nullopt)); + graph.emplace_node(y_ptr); + THEN("y has the shape and strides we expect") { CHECK(std::ranges::equal(y_ptr->shape(), std::vector{2, 2})); @@ -681,8 +682,6 @@ TEST_CASE("BasicIndexingNode") { CHECK(std::ranges::equal(x_ptr->view(state), std::vector{1, 1, 0, 0, 0, 0, 0, 0, 0})); CHECK(std::ranges::equal(y_ptr->view(state), std::vector{1, 0, 0, 0})); - - verify_array_diff({0, 0, 0, 0}, {1, 0, 0, 0}, y_ptr->diff(state)); } } } @@ -738,6 +737,8 @@ TEST_CASE("BasicIndexingNode") { auto x_ptr = graph.emplace_node(5); auto y_ptr = graph.emplace_node(x_ptr, Slice(0, -2)); // x[:-2] + graph.emplace_node(y_ptr); + THEN("The resulting BasicIndexingNode has the shape we expect") { CHECK(y_ptr->size() == Array::DYNAMIC_SIZE); CHECK(y_ptr->ndim() == 1); @@ -784,8 +785,6 @@ TEST_CASE("BasicIndexingNode") { CHECK(y_ptr->shape(state)[0] == y_ptr->size(state)); CHECK(std::ranges::equal(y_ptr->view(state), std::vector{0})); - verify_array_diff({}, {0}, y_ptr->diff(state)); - x_ptr->commit(state); y_ptr->commit(state); @@ -794,8 +793,6 @@ TEST_CASE("BasicIndexingNode") { x_ptr->propagate(state); y_ptr->propagate(state); - verify_array_diff({0}, {0, 1}, y_ptr->diff(state)); - CHECK(std::ranges::equal(y_ptr->view(state), std::vector{0, 1})); } @@ -810,8 +807,6 @@ TEST_CASE("BasicIndexingNode") { CHECK(y_ptr->shape(state)[0] == y_ptr->size(state)); CHECK(std::ranges::equal(y_ptr->view(state), std::vector{0, 1})); - verify_array_diff({}, {0, 1}, y_ptr->diff(state)); - x_ptr->commit(state); y_ptr->commit(state); @@ -819,8 +814,6 @@ TEST_CASE("BasicIndexingNode") { x_ptr->propagate(state); y_ptr->propagate(state); - - verify_array_diff({0, 1}, {0, 1, 2}, y_ptr->diff(state)); } AND_WHEN("We shrink the dynamic array below the range") { @@ -840,7 +833,6 @@ TEST_CASE("BasicIndexingNode") { x_ptr->propagate(state); y_ptr->propagate(state); CHECK(std::ranges::equal(y_ptr->view(state), std::vector{0})); - verify_array_diff({0, 1}, {0}, y_ptr->diff(state)); x_ptr->commit(state); y_ptr->commit(state); @@ -850,7 +842,6 @@ TEST_CASE("BasicIndexingNode") { x_ptr->propagate(state); y_ptr->propagate(state); CHECK(std::ranges::equal(y_ptr->view(state), std::vector())); - verify_array_diff({0}, {}, y_ptr->diff(state)); x_ptr->commit(state); y_ptr->commit(state); @@ -887,7 +878,6 @@ TEST_CASE("BasicIndexingNode") { y_ptr->propagate(state); CHECK(std::ranges::equal(y_ptr->view(state), std::vector{0, 1})); - verify_array_diff({0, 1}, {0, 1}, y_ptr->diff(state)); } AND_WHEN("We shrink and grow the dynamic array") { @@ -902,7 +892,6 @@ TEST_CASE("BasicIndexingNode") { y_ptr->propagate(state); CHECK(std::ranges::equal(y_ptr->view(state), std::vector{0})); - verify_array_diff({}, {0}, y_ptr->diff(state)); x_ptr->commit(state); y_ptr->commit(state); @@ -915,7 +904,6 @@ TEST_CASE("BasicIndexingNode") { y_ptr->propagate(state); CHECK(std::ranges::equal(y_ptr->view(state), std::vector{0})); - verify_array_diff({0}, {0}, y_ptr->diff(state)); } AND_WHEN("We change the shape of the dynamic array, and then revert") { @@ -940,6 +928,8 @@ TEST_CASE("BasicIndexingNode") { auto y_ptr = graph.emplace_node(x_ptr, Slice(-2, std::nullopt)); // x[-2:] + graph.emplace_node(y_ptr); + THEN("The resulting BasicIndexingNode has the shape we expect") { CHECK(y_ptr->size() == Array::DYNAMIC_SIZE); CHECK(y_ptr->ndim() == 1); @@ -968,7 +958,6 @@ TEST_CASE("BasicIndexingNode") { CHECK(y_ptr->shape(state).size() == 1); CHECK(y_ptr->shape(state)[0] == 1); CHECK(std::ranges::equal(y_ptr->view(state), std::vector{0})); - verify_array_diff({}, {0}, y_ptr->diff(state)); } AND_WHEN("We grow the dynamic array up to the range") { @@ -983,8 +972,6 @@ TEST_CASE("BasicIndexingNode") { CHECK(y_ptr->shape(state)[0] == 2); CHECK(std::ranges::equal(y_ptr->view(state), std::vector{0, 1})); - verify_array_diff({}, {0, 1}, y_ptr->diff(state)); - x_ptr->commit(state); y_ptr->commit(state); @@ -993,8 +980,6 @@ TEST_CASE("BasicIndexingNode") { x_ptr->propagate(state); y_ptr->propagate(state); - verify_array_diff({0, 1}, {1, 2}, y_ptr->diff(state)); - CHECK(std::ranges::equal(y_ptr->view(state), std::vector{1, 2})); } @@ -1009,8 +994,6 @@ TEST_CASE("BasicIndexingNode") { CHECK(y_ptr->shape(state)[0] == y_ptr->size(state)); CHECK(std::ranges::equal(y_ptr->view(state), std::vector{2, 3})); - verify_array_diff({}, {2, 3}, y_ptr->diff(state)); - x_ptr->commit(state); y_ptr->commit(state); @@ -1018,8 +1001,6 @@ TEST_CASE("BasicIndexingNode") { x_ptr->propagate(state); y_ptr->propagate(state); - - verify_array_diff({2, 3}, {3, 4}, y_ptr->diff(state)); } AND_WHEN("We shrink the dynamic array below the range") { @@ -1039,7 +1020,6 @@ TEST_CASE("BasicIndexingNode") { x_ptr->propagate(state); y_ptr->propagate(state); CHECK(std::ranges::equal(y_ptr->view(state), std::vector{1, 2})); - verify_array_diff({2, 3}, {1, 2}, y_ptr->diff(state)); x_ptr->commit(state); y_ptr->commit(state); @@ -1050,7 +1030,6 @@ TEST_CASE("BasicIndexingNode") { x_ptr->propagate(state); y_ptr->propagate(state); CHECK(std::ranges::equal(y_ptr->view(state), std::vector{0})); - verify_array_diff({1, 2}, {0}, y_ptr->diff(state)); x_ptr->commit(state); y_ptr->commit(state); @@ -1060,7 +1039,6 @@ TEST_CASE("BasicIndexingNode") { x_ptr->propagate(state); y_ptr->propagate(state); CHECK(std::ranges::equal(y_ptr->view(state), std::vector())); - verify_array_diff({0}, {}, y_ptr->diff(state)); } AND_WHEN("We shrink and grow the dynamic array") { @@ -1075,7 +1053,6 @@ TEST_CASE("BasicIndexingNode") { y_ptr->propagate(state); // [2, 3] CHECK(std::ranges::equal(y_ptr->view(state), std::vector{2, 3})); - verify_array_diff({}, {2, 3}, y_ptr->diff(state)); } AND_WHEN("We change the shape of the dynamic array, and then revert") { diff --git a/tests/cpp/nodes/test_collections.cpp b/tests/cpp/nodes/test_collections.cpp index 8a4773cd..10f1199a 100644 --- a/tests/cpp/nodes/test_collections.cpp +++ b/tests/cpp/nodes/test_collections.cpp @@ -19,7 +19,6 @@ #include "dwave-optimization/graph.hpp" #include "dwave-optimization/nodes/collections.hpp" #include "dwave-optimization/nodes/testing.hpp" -#include "../utils.hpp" namespace dwave::optimization { @@ -40,6 +39,8 @@ TEST_CASE("DisjointBitSetsNode") { std::vector sets; for (int i = 0; i < 3; ++i) { sets.push_back(graph.emplace_node(ptr)); + + graph.emplace_node(sets.at(i)); } THEN("We shouldn't be able to add any more successors") { @@ -102,12 +103,6 @@ TEST_CASE("DisjointBitSetsNode") { CHECK(std::ranges::equal(sets[2]->view(state), std::vector{0, 0, 1, 0, 1})); } - THEN("The successor nodes' diffs have the changes") { - verify_array_diff({1, 1, 1, 1, 1}, {0, 1, 0, 1, 0}, sets[0]->diff(state)); - verify_array_diff({0, 0, 0, 0, 0}, {1, 0, 0, 0, 0}, sets[1]->diff(state)); - verify_array_diff({0, 0, 0, 0, 0}, {0, 0, 1, 0, 1}, sets[2]->diff(state)); - } - THEN("The successor nodes' size_diffs are correct") { CHECK(sets[0]->size_diff(state) == 0); CHECK(sets[1]->size_diff(state) == 0); @@ -217,6 +212,8 @@ TEST_CASE("DisjointListsNode") { std::vector lists; for (int i = 0; i < 3; ++i) { lists.push_back(graph.emplace_node(ptr)); + + graph.emplace_node(lists.at(i)); } THEN("We shouldn't be able to add any more successors") { @@ -280,12 +277,6 @@ TEST_CASE("DisjointListsNode") { CHECK(std::ranges::equal(lists[2]->view(state), std::vector{3, 1})); } - THEN("The successor nodes' diffs have the changes") { - verify_array_diff({0, 1, 2, 3, 4}, {0, 4, 2}, lists[0]->diff(state)); - CHECK(lists[1]->diff(state).size() == 0); - verify_array_diff({}, {3, 1}, lists[2]->diff(state)); - } - THEN("The successor nodes' size_diffs are correct") { CHECK(lists[0]->size_diff(state) == -2); CHECK(lists[1]->size_diff(state) == 0); @@ -463,6 +454,8 @@ TEST_CASE("SetNode") { auto ptr = graph.emplace_node(num_elements); + graph.emplace_node(ptr); + THEN("The shape is dynamic") { CHECK(ptr->ndim() == 1); CHECK(ptr->size() == Array::DYNAMIC_SIZE); @@ -495,7 +488,6 @@ TEST_CASE("SetNode") { CHECK(std::ranges::equal(ptr->view(state), std::vector{0})); CHECK(ptr->size_diff(state) == 1); - verify_array_diff({}, {0}, ptr->diff(state)); } AND_WHEN("We commit") { @@ -598,7 +590,6 @@ TEST_CASE("SetNode") { CHECK(std::ranges::equal(ptr->view(state), std::vector{2, 0})); CHECK(ptr->size_diff(state) == -1); - verify_array_diff({2, 0, 3}, {2, 0}, ptr->diff(state)); } AND_WHEN("We commit") { diff --git a/tests/cpp/utils.hpp b/tests/cpp/utils.hpp deleted file mode 100644 index fe462572..00000000 --- a/tests/cpp/utils.hpp +++ /dev/null @@ -1,50 +0,0 @@ -// Copyright 2023 D-Wave Inc. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -// Utility functions for testing - -#pragma once - -#include -#include - -#include "catch2/catch_test_macros.hpp" -#include "dwave-optimization/array.hpp" - -namespace dwave::optimization { - -inline void verify_array_diff(std::vector state, const std::vector& final_state, - const std::span& diff) { - for (const auto& update : diff) { - const ssize_t size = state.size(); - if (update.placed()) { - // Can only place at the end of an array - REQUIRE(update.index == size); - state.push_back(update.value); - } else if (update.removed()) { - // Can only remove at the end of an array - REQUIRE(update.index == size - 1); - state.pop_back(); - } else { - REQUIRE(update.index >= 0); - REQUIRE(update.index < size); - CHECK(update.old == state[update.index]); - state[update.index] = update.value; - } - } - - CHECK(std::ranges::equal(state, final_state)); -} - -} // namespace dwave::optimization