|
| 1 | +/* |
| 2 | + * Copyright 2009- ECMWF. |
| 3 | + * |
| 4 | + * This software is licensed under the terms of the Apache Licence version 2.0 |
| 5 | + * which can be obtained at http://www.apache.org/licenses/LICENSE-2.0. |
| 6 | + * In applying this licence, ECMWF does not waive the privileges and immunities |
| 7 | + * granted to it by virtue of its status as an intergovernmental organisation |
| 8 | + * nor does it submit to any jurisdiction. |
| 9 | + */ |
| 10 | + |
| 11 | +#define BOOST_TEST_MODULE Test_Base |
| 12 | +#include <boost/test/included/unit_test.hpp> |
| 13 | + |
| 14 | +#include "MockServer.hpp" |
| 15 | +#include "ecflow/base/Algorithms.hpp" |
| 16 | +#include "ecflow/node/Family.hpp" |
| 17 | +#include "ecflow/server/BaseServer.hpp" |
| 18 | +#include "ecflow/test/scaffold/Naming.hpp" |
| 19 | + |
| 20 | +BOOST_AUTO_TEST_SUITE(U_Base) |
| 21 | + |
| 22 | +BOOST_AUTO_TEST_SUITE(T_Path) |
| 23 | + |
| 24 | +BOOST_AUTO_TEST_CASE(test_cannot_create_path_from_empty_string) { |
| 25 | + ECF_NAME_THIS_TEST(); |
| 26 | + |
| 27 | + auto result = ecf::Path::make(""); |
| 28 | + BOOST_CHECK_MESSAGE(!result.ok(), "expected !ok"); |
| 29 | +} |
| 30 | + |
| 31 | +BOOST_AUTO_TEST_CASE(test_can_create_path_from_root_only) { |
| 32 | + ECF_NAME_THIS_TEST(); |
| 33 | + |
| 34 | + auto result = ecf::Path::make("/"); |
| 35 | + BOOST_CHECK(result.ok()); |
| 36 | + auto path = result.value(); |
| 37 | + BOOST_CHECK(path.empty()); |
| 38 | + BOOST_CHECK_EQUAL(path.size(), 0ul); |
| 39 | + BOOST_CHECK_EQUAL(path.to_string(), "/"); |
| 40 | +} |
| 41 | + |
| 42 | +BOOST_AUTO_TEST_CASE(test_can_create_path_with_single_token) { |
| 43 | + ECF_NAME_THIS_TEST(); |
| 44 | + |
| 45 | + auto result = ecf::Path::make("/suite"); |
| 46 | + BOOST_CHECK(result.ok()); |
| 47 | + auto path = result.value(); |
| 48 | + BOOST_CHECK(!path.empty()); |
| 49 | + BOOST_CHECK_EQUAL(path.size(), 1ul); |
| 50 | + BOOST_CHECK_EQUAL(path[0], "suite"); |
| 51 | + BOOST_CHECK_EQUAL(path.to_string(), "/suite"); |
| 52 | +} |
| 53 | + |
| 54 | +BOOST_AUTO_TEST_CASE(test_can_create_path_with_multiple_tokens) { |
| 55 | + ECF_NAME_THIS_TEST(); |
| 56 | + |
| 57 | + auto result = ecf::Path::make("/suite/family/task"); |
| 58 | + BOOST_CHECK(result.ok()); |
| 59 | + auto path = result.value(); |
| 60 | + BOOST_CHECK(!path.empty()); |
| 61 | + BOOST_CHECK_EQUAL(path.size(), 3ul); |
| 62 | + BOOST_CHECK_EQUAL(path[0], "suite"); |
| 63 | + BOOST_CHECK_EQUAL(path[1], "family"); |
| 64 | + BOOST_CHECK_EQUAL(path[2], "task"); |
| 65 | + BOOST_CHECK_EQUAL(path.to_string(), "/suite/family/task"); |
| 66 | +} |
| 67 | + |
| 68 | +BOOST_AUTO_TEST_CASE(test_can_create_path_with_empty_tokens) { |
| 69 | + ECF_NAME_THIS_TEST(); |
| 70 | + |
| 71 | + auto result = ecf::Path::make("///suite///family///task"); |
| 72 | + BOOST_CHECK(result.ok()); |
| 73 | + auto path = result.value(); |
| 74 | + BOOST_CHECK(!path.empty()); |
| 75 | + BOOST_CHECK_EQUAL(path.size(), 3ul); |
| 76 | + BOOST_CHECK_EQUAL(path[0], "suite"); |
| 77 | + BOOST_CHECK_EQUAL(path[1], "family"); |
| 78 | + BOOST_CHECK_EQUAL(path[2], "task"); |
| 79 | + BOOST_CHECK_EQUAL(path.to_string(), "/suite/family/task"); |
| 80 | +} |
| 81 | + |
| 82 | +BOOST_AUTO_TEST_SUITE_END() // T_Path |
| 83 | + |
| 84 | +BOOST_AUTO_TEST_SUITE(T_Algorithms) |
| 85 | + |
| 86 | +BOOST_AUTO_TEST_CASE(test_can_visit_defs) { |
| 87 | + ECF_NAME_THIS_TEST(); |
| 88 | + |
| 89 | + Defs defs; |
| 90 | + suite_ptr s = defs.add_suite("suite"); |
| 91 | + family_ptr f = s->add_family("family"); |
| 92 | + task_ptr t = f->add_task("task"); |
| 93 | + |
| 94 | + struct Visitor |
| 95 | + { |
| 96 | + void operator()(const Defs& defs) { collected.push_back("defs"); } |
| 97 | + void operator()(const Node& s) { collected.push_back("node: " + s.name()); } |
| 98 | + |
| 99 | + std::vector<std::string> collected; |
| 100 | + }; |
| 101 | + |
| 102 | + auto path = ecf::Path::make("/suite/family/task").value(); |
| 103 | + Visitor visitor; |
| 104 | + ecf::visit(defs, path, visitor); |
| 105 | + |
| 106 | + BOOST_CHECK_EQUAL(visitor.collected.size(), 4ul); |
| 107 | + BOOST_CHECK_EQUAL(visitor.collected[0], "defs"); |
| 108 | + BOOST_CHECK_EQUAL(visitor.collected[1], "node: suite"); |
| 109 | + BOOST_CHECK_EQUAL(visitor.collected[2], "node: family"); |
| 110 | + BOOST_CHECK_EQUAL(visitor.collected[3], "node: task"); |
| 111 | +} |
| 112 | + |
| 113 | +BOOST_AUTO_TEST_CASE(test_can_visit_server) { |
| 114 | + ECF_NAME_THIS_TEST(); |
| 115 | + |
| 116 | + Defs defs; |
| 117 | + suite_ptr s = defs.add_suite("suite"); |
| 118 | + family_ptr f = s->add_family("family"); |
| 119 | + task_ptr t = f->add_task("task"); |
| 120 | + |
| 121 | + MockServer server(&defs); |
| 122 | + |
| 123 | + struct Visitor |
| 124 | + { |
| 125 | + void operator()(const AbstractServer& server) { collected.push_back("server"); } |
| 126 | + void operator()(const Defs& defs) { collected.push_back("defs"); } |
| 127 | + void operator()(const Node& s) { collected.push_back("node: " + s.name()); } |
| 128 | + |
| 129 | + std::vector<std::string> collected; |
| 130 | + }; |
| 131 | + |
| 132 | + auto path = ecf::Path::make("/suite/family/task").value(); |
| 133 | + Visitor visitor; |
| 134 | + ecf::visit(server, path, visitor); |
| 135 | + |
| 136 | + BOOST_CHECK_EQUAL(visitor.collected.size(), 5ul); |
| 137 | + BOOST_CHECK_EQUAL(visitor.collected[0], "server"); |
| 138 | + BOOST_CHECK_EQUAL(visitor.collected[1], "defs"); |
| 139 | + BOOST_CHECK_EQUAL(visitor.collected[2], "node: suite"); |
| 140 | + BOOST_CHECK_EQUAL(visitor.collected[3], "node: family"); |
| 141 | + BOOST_CHECK_EQUAL(visitor.collected[4], "node: task"); |
| 142 | +} |
| 143 | + |
| 144 | +BOOST_AUTO_TEST_SUITE_END() // T_Algorithms |
| 145 | + |
| 146 | +BOOST_AUTO_TEST_SUITE_END() |
0 commit comments