diff options
Diffstat (limited to 'Tests/CMakeLib/testCTestResourceSpec.cxx')
-rw-r--r-- | Tests/CMakeLib/testCTestResourceSpec.cxx | 84 |
1 files changed, 84 insertions, 0 deletions
diff --git a/Tests/CMakeLib/testCTestResourceSpec.cxx b/Tests/CMakeLib/testCTestResourceSpec.cxx new file mode 100644 index 0000000..e4bc770 --- /dev/null +++ b/Tests/CMakeLib/testCTestResourceSpec.cxx @@ -0,0 +1,84 @@ +#include <iostream> +#include <string> +#include <vector> + +#include "cmCTestResourceSpec.h" + +struct ExpectedSpec +{ + std::string Path; + bool ParseResult; + cmCTestResourceSpec Expected; +}; + +static const std::vector<ExpectedSpec> expectedResourceSpecs = { + /* clang-format off */ + {"spec1.json", true, {{{ + {"gpus", { + {"2", 4}, + {"e", 1}, + }}, + {"threads", { + }}, + }}}}, + {"spec2.json", true, {{{ + }}}}, + {"spec3.json", false, {{{}}}}, + {"spec4.json", false, {{{}}}}, + {"spec5.json", false, {{{}}}}, + {"spec6.json", false, {{{}}}}, + {"spec7.json", false, {{{}}}}, + {"spec8.json", false, {{{}}}}, + {"spec9.json", false, {{{}}}}, + {"spec10.json", false, {{{}}}}, + {"spec11.json", false, {{{}}}}, + {"spec12.json", false, {{{}}}}, + {"spec13.json", false, {{{}}}}, + {"spec14.json", true, {{{}}}}, + {"spec15.json", true, {{{}}}}, + {"spec16.json", true, {{{}}}}, + {"spec17.json", false, {{{}}}}, + {"spec18.json", false, {{{}}}}, + {"noexist.json", false, {{{}}}}, + /* clang-format on */ +}; + +static bool testSpec(const std::string& path, bool expectedResult, + const cmCTestResourceSpec& expected) +{ + cmCTestResourceSpec actual; + bool result = actual.ReadFromJSONFile(path); + if (result != expectedResult) { + std::cout << "ReadFromJSONFile(\"" << path << "\") returned " << result + << ", should be " << expectedResult << std::endl; + return false; + } + + if (result && actual != expected) { + std::cout << "ReadFromJSONFile(\"" << path + << "\") did not give expected spec" << std::endl; + return false; + } + + return true; +} + +int testCTestResourceSpec(int argc, char** const argv) +{ + if (argc < 2) { + std::cout << "Invalid arguments.\n"; + return -1; + } + + int retval = 0; + for (auto const& spec : expectedResourceSpecs) { + std::string path = argv[1]; + path += "/testCTestResourceSpec_data/"; + path += spec.Path; + if (!testSpec(path, spec.ParseResult, spec.Expected)) { + retval = -1; + } + } + + return retval; +} |