From becd546608c591ec9ac76eec15d0aa0982f58d18 Mon Sep 17 00:00:00 2001 From: Arkadiusz Szczepkowicz Date: Tue, 25 Apr 2023 16:09:13 +0200 Subject: [PATCH] #2130: Add UTs for reading phases metadata --- tests/unit/collection/test_lb_data_holder.cc | 196 +++++++++++++++++++ 1 file changed, 196 insertions(+) create mode 100644 tests/unit/collection/test_lb_data_holder.cc diff --git a/tests/unit/collection/test_lb_data_holder.cc b/tests/unit/collection/test_lb_data_holder.cc new file mode 100644 index 0000000000..585be221f6 --- /dev/null +++ b/tests/unit/collection/test_lb_data_holder.cc @@ -0,0 +1,196 @@ +/* +//@HEADER +// ***************************************************************************** +// +// test_lb_data_holder.cc +// DARMA/vt => Virtual Transport +// +// Copyright 2019-2021 National Technology & Engineering Solutions of Sandia, LLC +// (NTESS). Under the terms of Contract DE-NA0003525 with NTESS, the U.S. +// Government retains certain rights in this software. +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are met: +// +// * Redistributions of source code must retain the above copyright notice, +// this list of conditions and the following disclaimer. +// +// * Redistributions in binary form must reproduce the above copyright notice, +// this list of conditions and the following disclaimer in the documentation +// and/or other materials provided with the distribution. +// +// * Neither the name of the copyright holder nor the names of its +// contributors may be used to endorse or promote products derived from this +// software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE +// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +// POSSIBILITY OF SUCH DAMAGE. +// +// Questions? Contact darma@sandia.gov +// +// ***************************************************************************** +//@HEADER +*/ + +#include + +#include "test_parallel_harness.h" +#include "test_helpers.h" +#include "test_collection_common.h" + +#include "vt/vrt/collection/manager.h" +#include "vt/vrt/collection/balance/lb_data_holder.h" + +#include + +#if vt_check_enabled(lblite) + +namespace vt { namespace tests { namespace unit { namespace lb { + +static constexpr int const num_phases = 10; + +struct TestOptionalPhasesMetadata : TestParallelHarnessParam { }; + +void addPhasesDataToJson(nlohmann::json& json, PhaseType amountOfPhasesToAdd, std::vector toSkip) { + using ElementIDStruct = vt::vrt::collection::balance::ElementIDStruct; + using LBDataHolder = vt::vrt::collection::balance::LBDataHolder; + using LoadSummary = vt::vrt::collection::balance::LoadSummary; + + std::unordered_map> ids; + for (PhaseType i = 0; i < 2; i++) { + auto id = elm::ElmIDBits::createCollectionImpl(true, i+1, 0, 0); + for(PhaseType j = 0; j < amountOfPhasesToAdd; j++) { + ids[j].push_back(id); + } + } + + LBDataHolder dh; + for (PhaseType i = 0; i < amountOfPhasesToAdd; i++) { + for (auto&& elm : ids[i]) { + dh.node_data_[i][elm] = LoadSummary{3}; + } + } + + nlohmann::json phases = nlohmann::json::array(); + for (PhaseType i = 0; i < num_phases; i++) { + if(std::find(toSkip.begin(), toSkip.end(), i) == toSkip.end()) { + phases.push_back(*dh.toJson(i)); + } + } + + json["phases"] = phases; +} + +TEST_F(TestOptionalPhasesMetadata, test_no_lb_phases_metadata) { + using LBDataHolder = vt::vrt::collection::balance::LBDataHolder; + + nlohmann::json json; + json["metadata"]["type"] = "LBDatafile"; + + addPhasesDataToJson(json, num_phases, {}); + + LBDataHolder testObj(json); + ASSERT_EQ(testObj.count_, 0); + std::set expectedSkipped = {}; + ASSERT_EQ(testObj.skipped_phases_, expectedSkipped); + std::set expectedIdentical = {}; + ASSERT_EQ(testObj.identical_phases_, expectedIdentical); + + auto outJsonPtr = testObj.metadataToJson(); + ASSERT_TRUE(outJsonPtr != nullptr); + + ASSERT_EQ((*outJsonPtr)["count"], 0); + std::vector expectedSkippedList = {}; + ASSERT_EQ((*outJsonPtr)["skipped"]["list"], expectedSkippedList); + std::vector> expectedSkippedRanges = {}; + ASSERT_EQ((*outJsonPtr)["skipped"]["range"], expectedSkippedRanges); + std::vector expectedIdenticalList = {}; + ASSERT_EQ((*outJsonPtr)["identical_to_previous"]["list"], expectedIdenticalList); + std::vector> expectedIdenticalRanges = {}; + ASSERT_EQ((*outJsonPtr)["identical_to_previous"]["range"], expectedIdenticalRanges); +} + +TEST_F(TestOptionalPhasesMetadata, test_lb_phases_metadata_empty) { + using LBDataHolder = vt::vrt::collection::balance::LBDataHolder; + + nlohmann::json metadata, phasesMetadata, json; + phasesMetadata["count"] = num_phases; + phasesMetadata["skipped"]["list"] = {}; + phasesMetadata["skipped"]["range"] = {}; + phasesMetadata["identical_to_previous"]["list"] = {}; + phasesMetadata["identical_to_previous"]["range"] = {}; + metadata["type"] = "LBDatafile"; + metadata["phases"] = phasesMetadata; + json["metadata"] = metadata; + + addPhasesDataToJson(json, num_phases, {}); + + LBDataHolder testObj(json); + ASSERT_EQ(testObj.count_, num_phases); + std::set expectedSkipped = {}; + ASSERT_EQ(testObj.skipped_phases_, expectedSkipped); + std::set expectedIdentical = {}; + ASSERT_EQ(testObj.identical_phases_, expectedIdentical); + + auto outJsonPtr = testObj.metadataToJson(); + ASSERT_TRUE(outJsonPtr != nullptr); + + ASSERT_EQ((*outJsonPtr)["count"], num_phases); + std::vector expectedSkippedList = {}; + ASSERT_EQ((*outJsonPtr)["skipped"]["list"], expectedSkippedList); + std::vector> expectedSkippedRanges = {}; + ASSERT_EQ((*outJsonPtr)["skipped"]["range"], expectedSkippedRanges); + std::vector expectedIdenticalList = {}; + ASSERT_EQ((*outJsonPtr)["identical_to_previous"]["list"], expectedIdenticalList); + std::vector> expectedIdenticalRanges = {}; + ASSERT_EQ((*outJsonPtr)["identical_to_previous"]["range"], expectedIdenticalRanges); +} + +TEST_F(TestOptionalPhasesMetadata, test_lb_phases_metadata_filled) { + using LBDataHolder = vt::vrt::collection::balance::LBDataHolder; + + nlohmann::json metadata, phasesMetadata, json; + phasesMetadata["count"] = num_phases; + phasesMetadata["skipped"]["list"] = {2}; + phasesMetadata["skipped"]["range"] = {{3,4}}; + phasesMetadata["identical_to_previous"]["list"] = {1}; + phasesMetadata["identical_to_previous"]["range"] = {{8,9}}; + metadata["type"] = "LBDatafile"; + metadata["phases"] = phasesMetadata; + json["metadata"] = metadata; + + addPhasesDataToJson(json, num_phases, {1,2,3,4,8,9}); + + LBDataHolder testObj(json); + ASSERT_EQ(testObj.count_, num_phases); + std::set expectedSkipped = {2, 3, 4}; + ASSERT_EQ(testObj.skipped_phases_, expectedSkipped); + std::set expectedIdentical = {1, 8, 9}; + ASSERT_EQ(testObj.identical_phases_, expectedIdentical); + + auto outJsonPtr = testObj.metadataToJson(); + ASSERT_TRUE(outJsonPtr != nullptr); + + ASSERT_EQ((*outJsonPtr)["count"], num_phases); + std::vector expectedSkippedList = {}; + ASSERT_EQ((*outJsonPtr)["skipped"]["list"], expectedSkippedList); + std::vector> expectedSkippedRanges = {{2,4}}; + ASSERT_EQ((*outJsonPtr)["skipped"]["range"], expectedSkippedRanges); + std::vector expectedIdenticalList = {1}; + ASSERT_EQ((*outJsonPtr)["identical_to_previous"]["list"], expectedIdenticalList); + std::vector> expectedIdenticalRanges = {{8,9}}; + ASSERT_EQ((*outJsonPtr)["identical_to_previous"]["range"], expectedIdenticalRanges); +} + +}}}} // end namespace vt::tests::unit::lb + +#endif /*vt_check_enabled(lblite)*/