From efde4c90618f13a9eeac9df05f4d4490ddc40d00 Mon Sep 17 00:00:00 2001 From: Metin Cakircali Date: Tue, 21 Nov 2023 14:21:08 +0100 Subject: [PATCH] add test for levelist --- tests/CMakeLists.txt | 2 +- tests/test_type_levelist.cc | 78 +++++++++++++++++++++++++++++++++++++ 2 files changed, 79 insertions(+), 1 deletion(-) create mode 100644 tests/test_type_levelist.cc diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 05a781a3..af06050d 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -61,7 +61,7 @@ ecbuild_add_test( TARGET metkit_test_odbsplitter NO_AS_NEEDED LIBS metkit ) -list(APPEND testFileSuffixes typesfactory expand param_axis steprange_axis time hypercube ) +list(APPEND testFileSuffixes typesfactory expand param_axis steprange_axis time hypercube type_levelist ) foreach(test IN LISTS testFileSuffixes) ecbuild_add_test( TARGET "metkit_test_${test}" diff --git a/tests/test_type_levelist.cc b/tests/test_type_levelist.cc new file mode 100644 index 00000000..c5109154 --- /dev/null +++ b/tests/test_type_levelist.cc @@ -0,0 +1,78 @@ +/* + * (C) Copyright 1996- ECMWF. + * + * This software is licensed under the terms of the Apache Licence Version 2.0 + * which can be obtained at http://www.apache.org/licenses/LICENSE-2.0. + * In applying this licence, ECMWF does not waive the privileges and immunities + * granted to it by virtue of its status as an intergovernmental organisation nor + * does it submit to any jurisdiction. + */ + +/// @file test_levelist.cc +/// @author Metin Cakircali +/// @date November 2023 + +#include "metkit/mars/TypesFactory.h" +#include "metkit/mars/TypeToByListFloat.h" +#include "metkit/mars/MarsParser.h" +#include "metkit/mars/MarsLanguage.h" + +#include "eckit/exception/Exceptions.h" +#include "eckit/testing/Test.h" + +using namespace eckit::testing; + +namespace metkit { +namespace mars { +namespace test { + +//----------------------------------------------------------------------------- + +CASE("test_metkit_exists_to-by-list-float") { + std::stringstream ss; + TypesFactory::list(ss); + const std::string str(ss.str()); + std::cout << "types=" << str << std::endl; + EXPECT(str.find_first_of("to-by-list-float")); +} + +void assertTypeExpansion(const std::string& name, std::vector values, + const std::vector& expected) { + static MarsLanguage language("retrieve"); + language.type(name)->expand(DummyContext(), values); + ASSERT(values == expected); +} + +CASE("test_metkit_expand_levelist") { + // by > 0 + assertTypeExpansion("levelist", {"-1", "to", "2", "by", "0.5"}, {"-1", "-0.5", "0", ".5", "1", "1.5", "2"}); + // by > 0 + assertTypeExpansion("levelist", {"-10.0", "to", "2.0", "by", "1"}, + {"-10", "-9", "-8", "-7", "-6", "-5", "-4", "-3", "-2", "-1", "0", "1", "2"}); + // by > 0 && from < to + assertTypeExpansion("levelist", {"4", "to", "20", "by", "4"}, {"4", "8", "12", "16", "20"}); + // by > 0 && from > to + EXPECT_THROWS(assertTypeExpansion("levelist", {"20", "to", "4", "by", "4"}, {"4", "8", "12", "16", "20"})); + // by = 0 + assertTypeExpansion("levelist", {"4", "to", "20", "by", "0"}, {"4"}); + assertTypeExpansion("levelist", {"-1", "to", "2", "by", "0"}, {"-1"}); + // by < 0 && from > to + assertTypeExpansion("levelist", {"10", "to", "4", "by", "-2"}, {"10", "8", "6", "4"}); + assertTypeExpansion("levelist", {"-2", "to", "-4", "by", "-0.5"}, {"-2", "-2.5", "-3", "-3.5", "-4"}); + assertTypeExpansion("levelist", {"0", "to", "-2", "by", "-0.5"}, {"0", "-0.5", "-1", "-1.5", "-2"}); + // by < 0 && from < to + EXPECT_THROWS(assertTypeExpansion("levelist", {"4", "to", "10", "by", "-4"}, {"4", "8", "12", "16", "20"})); + EXPECT_THROWS(assertTypeExpansion("levelist", {"-4", "to", "2", "by", "-0.5"}, {"0", "-0.5", "-1", "-1.5", "-2"})); + // by < 0 && from < to + EXPECT_THROWS(assertTypeExpansion("levelist", {"-1", "to", "2", "by", "-0.5"}, {"-1"})); +} + +} // namespace test +} // namespace mars +} // namespace metkit + +//----------------------------------------------------------------------------- + +int main(int argc, char** argv) { + return run_tests(argc, argv); +}