| 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 |
|
| 35 |
#include "gtest/gtest.h" |
| 36 |
#include "stk_util/util/SortAndUnique.hpp" // for insert_keep_sorted_and_unique |
| 37 |
#include <vector> // for vector |
| 38 |
|
| 39 |
|
| 40 |
TEST( SortAndUnique, insert_keep_sorted_and_unique_overlapping_vecs) |
| 41 |
{ |
| 42 |
std::vector<int> sortedVec = {0, 2, 4, 6}; |
| 43 |
std::vector<int> itemsToInsert = {5, 7, 9}; |
| 44 |
|
| 45 |
std::vector<int> expectedResult = {0, 2, 4, 5, 6, 7, 9}; |
| 46 |
|
| 47 |
stk::util::insert_keep_sorted_and_unique(itemsToInsert, sortedVec); |
| 48 |
|
| 49 |
EXPECT_EQ(expectedResult, sortedVec); |
| 50 |
} |
| 51 |
|
| 52 |
TEST( SortAndUnique, insert_keep_sorted_and_unique_overlapping_vecs_high_capacity) |
| 53 |
{ |
| 54 |
std::vector<int> sortedVec = {0, 2, 4, 6}; |
| 55 |
sortedVec.reserve(16); |
| 56 |
std::vector<int> itemsToInsert = {5, 7, 9}; |
| 57 |
|
| 58 |
std::vector<int> expectedResult = {0, 2, 4, 5, 6, 7, 9}; |
| 59 |
|
| 60 |
stk::util::insert_keep_sorted_and_unique(itemsToInsert, sortedVec); |
| 61 |
|
| 62 |
EXPECT_EQ(expectedResult, sortedVec); |
| 63 |
} |
| 64 |
|
| 65 |
TEST( SortAndUnique, insert_keep_sorted_and_unique_vec_items_at_beginning) |
| 66 |
{ |
| 67 |
std::vector<int> sortedVec = {2, 4, 6}; |
| 68 |
std::vector<int> itemsToInsert = {0, 1}; |
| 69 |
|
| 70 |
std::vector<int> expectedResult = {0, 1, 2, 4, 6}; |
| 71 |
|
| 72 |
stk::util::insert_keep_sorted_and_unique(itemsToInsert, sortedVec); |
| 73 |
|
| 74 |
EXPECT_EQ(expectedResult, sortedVec); |
| 75 |
} |
| 76 |
|
| 77 |
TEST( SortAndUnique, insert_keep_sorted_and_unique_vec_items_at_beginning_high_capacity) |
| 78 |
{ |
| 79 |
std::vector<int> sortedVec = {2, 4, 6}; |
| 80 |
sortedVec.reserve(16); |
| 81 |
std::vector<int> itemsToInsert = {0, 1}; |
| 82 |
|
| 83 |
std::vector<int> expectedResult = {0, 1, 2, 4, 6}; |
| 84 |
|
| 85 |
stk::util::insert_keep_sorted_and_unique(itemsToInsert, sortedVec); |
| 86 |
|
| 87 |
EXPECT_EQ(expectedResult, sortedVec); |
| 88 |
} |
| 89 |
|
| 90 |
TEST( SortAndUnique, insert_keep_sorted_and_unique_vec_items_already_present) |
| 91 |
{ |
| 92 |
std::vector<int> sortedVec = {0, 2, 4, 6}; |
| 93 |
std::vector<int> itemsToInsert = {4, 6}; |
| 94 |
|
| 95 |
std::vector<int> expectedResult = {0, 2, 4, 6}; |
| 96 |
|
| 97 |
stk::util::insert_keep_sorted_and_unique(itemsToInsert, sortedVec); |
| 98 |
|
| 99 |
EXPECT_EQ(expectedResult, sortedVec); |
| 100 |
} |
| 101 |
|
| 102 |
TEST( SortAndUnique, insert_keep_sorted_and_unique_vec_items_already_present_high_capacity) |
| 103 |
{ |
| 104 |
std::vector<int> sortedVec = {0, 2, 4, 6}; |
| 105 |
sortedVec.reserve(16); |
| 106 |
std::vector<int> itemsToInsert = {4, 6}; |
| 107 |
|
| 108 |
std::vector<int> expectedResult = {0, 2, 4, 6}; |
| 109 |
|
| 110 |
stk::util::insert_keep_sorted_and_unique(itemsToInsert, sortedVec); |
| 111 |
|
| 112 |
EXPECT_EQ(expectedResult, sortedVec); |
| 113 |
} |
| 114 |
|
| 115 |
TEST( SortAndUnique, insert_keep_sorted_and_unique_vec_items_empty) |
| 116 |
{ |
| 117 |
std::vector<int> sortedVec = {0, 2, 4, 6}; |
| 118 |
std::vector<int> itemsToInsert; |
| 119 |
|
| 120 |
std::vector<int> expectedResult = {0, 2, 4, 6}; |
| 121 |
|
| 122 |
stk::util::insert_keep_sorted_and_unique(itemsToInsert, sortedVec); |
| 123 |
|
| 124 |
EXPECT_EQ(expectedResult, sortedVec); |
| 125 |
} |
| 126 |
|
| 127 |
TEST( SortAndUnique, insert_keep_sorted_and_unique_vec_empty) |
| 128 |
{ |
| 129 |
std::vector<int> sortedVec; |
| 130 |
std::vector<int> itemsToInsert = {0, 2, 4, 6}; |
| 131 |
|
| 132 |
std::vector<int> expectedResult = {0, 2, 4, 6}; |
| 133 |
|
| 134 |
stk::util::insert_keep_sorted_and_unique(itemsToInsert, sortedVec); |
| 135 |
|
| 136 |
EXPECT_EQ(expectedResult, sortedVec); |
| 137 |
} |
| 138 |
|
| 139 |
struct MyTestLess { |
| 140 |
bool operator()(int lhs, int rhs) const { return lhs < rhs; } |
| 141 |
}; |
| 142 |
|
| 143 |
TEST( SortAndUnique, insert_keep_sorted_and_unique_overlapping_vecs_myless) |
| 144 |
{ |
| 145 |
std::vector<int> sortedVec = {0, 2, 4, 6}; |
| 146 |
std::vector<int> itemsToInsert = {5, 7, 9}; |
| 147 |
|
| 148 |
std::vector<int> expectedResult = {0, 2, 4, 5, 6, 7, 9}; |
| 149 |
|
| 150 |
stk::util::insert_keep_sorted_and_unique(itemsToInsert, sortedVec, MyTestLess()); |
| 151 |
|
| 152 |
EXPECT_EQ(expectedResult, sortedVec); |
| 153 |
} |
| 154 |
|
| 155 |
TEST( SortAndUnique, insert_keep_sorted_and_unique_overlapping_vecs_myless_high_capacity) |
| 156 |
{ |
| 157 |
std::vector<int> sortedVec = {0, 2, 4, 6}; |
| 158 |
sortedVec.reserve(16); |
| 159 |
std::vector<int> itemsToInsert = {5, 7, 9}; |
| 160 |
|
| 161 |
std::vector<int> expectedResult = {0, 2, 4, 5, 6, 7, 9}; |
| 162 |
|
| 163 |
stk::util::insert_keep_sorted_and_unique(itemsToInsert, sortedVec, MyTestLess()); |
| 164 |
|
| 165 |
EXPECT_EQ(expectedResult, sortedVec); |
| 166 |
} |
| 167 |
|
| 168 |
TEST( SortAndUnique, insert_keep_sorted_and_unique_overlapping_vecs_items_at_end_myless_high_capacity) |
| 169 |
{ |
| 170 |
std::vector<int> sortedVec = {0, 2, 4, 6}; |
| 171 |
sortedVec.reserve(16); |
| 172 |
std::vector<int> itemsToInsert = {8, 9, 10}; |
| 173 |
|
| 174 |
std::vector<int> expectedResult = {0, 2, 4, 6, 8, 9, 10}; |
| 175 |
|
| 176 |
stk::util::insert_keep_sorted_and_unique(itemsToInsert, sortedVec, MyTestLess()); |
| 177 |
|
| 178 |
EXPECT_EQ(expectedResult, sortedVec); |
| 179 |
} |
| 180 |
|
| 181 |
#ifndef NDEBUG |
| 182 |
TEST( SortAndUnique, insert_keep_sorted_and_unique_items_not_sorted) |
| 183 |
{ |
| 184 |
std::vector<int> sortedVec = {0, 4, 2, 6}; |
| 185 |
std::vector<int> itemsToInsert = {4, 6}; |
| 186 |
|
| 187 |
std::vector<int> expectedResult = {0, 2, 4, 6}; |
| 188 |
|
| 189 |
EXPECT_THROW(stk::util::insert_keep_sorted_and_unique(itemsToInsert, sortedVec), std::logic_error); |
| 190 |
} |
| 191 |
#endif |
| 192 |
|