Skip to content

Commit

Permalink
add test for levelist
Browse files Browse the repository at this point in the history
  • Loading branch information
mcakircali committed Nov 21, 2023
1 parent 98440cb commit efde4c9
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 1 deletion.
2 changes: 1 addition & 1 deletion tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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}"
Expand Down
78 changes: 78 additions & 0 deletions tests/test_type_levelist.cc
Original file line number Diff line number Diff line change
@@ -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<std::string> values,
const std::vector<std::string>& 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);
}

0 comments on commit efde4c9

Please sign in to comment.