| 1 |
// Copyright 2002 - 2008, 2010, 2011 National Technology Engineering |
| 2 |
// Solutions of Sandia, LLC (NTESS). Under the terms of Contract |
| 3 |
// DE-NA0003525 with NTESS, the U.S. Government retains certain rights |
| 4 |
// in this software. |
| 5 |
// |
| 6 |
// Redistribution and use in source and binary forms, with or without |
| 7 |
// modification, are permitted provided that the following conditions are |
| 8 |
// met: |
| 9 |
// |
| 10 |
// * Redistributions of source code must retain the above copyright |
| 11 |
// notice, this list of conditions and the following disclaimer. |
| 12 |
// |
| 13 |
// * Redistributions in binary form must reproduce the above |
| 14 |
// copyright notice, this list of conditions and the following |
| 15 |
// disclaimer in the documentation and/or other materials provided |
| 16 |
// with the distribution. |
| 17 |
// |
| 18 |
// * Neither the name of NTESS nor the names of its contributors |
| 19 |
// may be used to endorse or promote products derived from this |
| 20 |
// software without specific prior written permission. |
| 21 |
// |
| 22 |
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 23 |
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 24 |
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 25 |
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 26 |
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 27 |
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 28 |
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 29 |
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 30 |
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 31 |
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 32 |
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 33 |
// |
| 34 |
#include <gtest/gtest.h> |
| 35 |
#include <stk_util/util/string_utils.hpp> |
| 36 |
#include <stk_tools/block_extractor/ParseCsv.hpp> |
| 37 |
|
| 38 |
namespace |
| 39 |
{ |
| 40 |
|
| 41 |
template<typename T> |
| 42 |
void compare_result(const std::vector<T> & result, const std::vector<T> & expected) |
| 43 |
{ |
| 44 |
ASSERT_EQ(result.size(), expected.size()); |
| 45 |
for (size_t i = 0; i < result.size(); ++i) { |
| 46 |
EXPECT_EQ(result[i], expected[i]); |
| 47 |
} |
| 48 |
} |
| 49 |
|
| 50 |
TEST(GetIdsFromStrings, empty) |
| 51 |
{ |
| 52 |
std::vector<std::string> input = {}; |
| 53 |
std::vector<int> result = stk::tools::get_ids_from_strings(input); |
| 54 |
compare_result(result, {}); |
| 55 |
} |
| 56 |
|
| 57 |
TEST(GetIdsFromStrings, singleInteger) |
| 58 |
{ |
| 59 |
std::vector<std::string> input = {"5"}; |
| 60 |
std::vector<int> result = stk::tools::get_ids_from_strings(input); |
| 61 |
compare_result(result, {5}); |
| 62 |
} |
| 63 |
|
| 64 |
TEST(GetIdsFromStrings, multipleIntegers) |
| 65 |
{ |
| 66 |
std::vector<std::string> input = {"1", "2", "3", "5", "8"}; |
| 67 |
std::vector<int> result = stk::tools::get_ids_from_strings(input); |
| 68 |
compare_result(result, {1, 2, 3, 5, 8}); |
| 69 |
} |
| 70 |
|
| 71 |
TEST(GetIdsFromStrings, nonInteger) |
| 72 |
{ |
| 73 |
std::vector<std::string> input = {"1", "block_2"}; |
| 74 |
EXPECT_THROW(stk::tools::get_ids_from_strings(input), std::runtime_error); |
| 75 |
} |
| 76 |
|
| 77 |
TEST(GetIdsFromStrings, singleRangeNonInteger) |
| 78 |
{ |
| 79 |
std::vector<std::string> input = {"1:block_5"}; |
| 80 |
EXPECT_THROW(stk::tools::get_ids_from_strings(input), std::runtime_error); |
| 81 |
} |
| 82 |
|
| 83 |
TEST(GetIdsFromStrings, singleRangeTooManyFields) |
| 84 |
{ |
| 85 |
std::vector<std::string> input = {"1:5:2:2"}; |
| 86 |
EXPECT_THROW(stk::tools::get_ids_from_strings(input), std::logic_error); |
| 87 |
} |
| 88 |
|
| 89 |
TEST(GetIdsFromStrings, singleRangeOutOfOrder) |
| 90 |
{ |
| 91 |
std::vector<std::string> input = {"5:1"}; |
| 92 |
EXPECT_THROW(stk::tools::get_ids_from_strings(input), std::logic_error); |
| 93 |
} |
| 94 |
|
| 95 |
TEST(GetIdsFromStrings, singleRangeZeroLimit) |
| 96 |
{ |
| 97 |
std::vector<std::string> input = {"0:5"}; |
| 98 |
EXPECT_THROW(stk::tools::get_ids_from_strings(input), std::logic_error); |
| 99 |
} |
| 100 |
|
| 101 |
TEST(GetIdsFromStrings, singleRangeNegativeLimit) |
| 102 |
{ |
| 103 |
std::vector<std::string> input = {"1:-5"}; |
| 104 |
EXPECT_THROW(stk::tools::get_ids_from_strings(input), std::logic_error); |
| 105 |
} |
| 106 |
|
| 107 |
TEST(GetIdsFromStrings, singleRangeNegativeStride) |
| 108 |
{ |
| 109 |
std::vector<std::string> input = {"1:5:-1"}; |
| 110 |
EXPECT_THROW(stk::tools::get_ids_from_strings(input), std::logic_error); |
| 111 |
} |
| 112 |
|
| 113 |
TEST(GetIdsFromStrings, singleRangeNoStride) |
| 114 |
{ |
| 115 |
std::vector<std::string> input = {"1:5"}; |
| 116 |
std::vector<int> result = stk::tools::get_ids_from_strings(input); |
| 117 |
compare_result(result, {1, 2, 3, 4, 5}); |
| 118 |
} |
| 119 |
|
| 120 |
TEST(GetIdsFromStrings, singleRangeStride1) |
| 121 |
{ |
| 122 |
std::vector<std::string> input = {"1:5:1"}; |
| 123 |
std::vector<int> result = stk::tools::get_ids_from_strings(input); |
| 124 |
compare_result(result, {1, 2, 3, 4, 5}); |
| 125 |
} |
| 126 |
|
| 127 |
TEST(GetIdsFromStrings, singleRangeStride2) |
| 128 |
{ |
| 129 |
std::vector<std::string> input = {"1:5:2"}; |
| 130 |
std::vector<int> result = stk::tools::get_ids_from_strings(input); |
| 131 |
compare_result(result, {1, 3, 5}); |
| 132 |
} |
| 133 |
|
| 134 |
TEST(GetIdsFromStrings, singleRangeStride3) |
| 135 |
{ |
| 136 |
std::vector<std::string> input = {"1:5:3"}; |
| 137 |
std::vector<int> result = stk::tools::get_ids_from_strings(input); |
| 138 |
compare_result(result, {1, 4}); // Not inclusive of range end |
| 139 |
} |
| 140 |
|
| 141 |
TEST(GetIdsFromStrings, singleRangeStrideTooBig) |
| 142 |
{ |
| 143 |
std::vector<std::string> input = {"1:5:5"}; |
| 144 |
std::vector<int> result = stk::tools::get_ids_from_strings(input); |
| 145 |
compare_result(result, {1}); // Only first value |
| 146 |
} |
| 147 |
|
| 148 |
TEST(GetIdsFromStrings, mixedIntsAndRanges) |
| 149 |
{ |
| 150 |
std::vector<std::string> input = {"1", "10:14:2", "20"}; |
| 151 |
std::vector<int> result = stk::tools::get_ids_from_strings(input); |
| 152 |
compare_result(result, {1, 10, 12, 14, 20}); |
| 153 |
} |
| 154 |
|
| 155 |
TEST(GetIdsFromStrings, multipleRanges) |
| 156 |
{ |
| 157 |
std::vector<std::string> input = {"1:5", "10:14:2"}; |
| 158 |
std::vector<int> result = stk::tools::get_ids_from_strings(input); |
| 159 |
compare_result(result, {1, 2, 3, 4, 5, 10, 12, 14}); |
| 160 |
} |
| 161 |
|
| 162 |
} |