| 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> // for AssertHelper, ASSERT_TRUE, etc |
| 36 |
#include <stk_mesh/baseImpl/SlideBucketContents.hpp> |
| 37 |
#include <vector> |
| 38 |
|
| 39 |
namespace { |
| 40 |
|
| 41 |
class TestBkt { |
| 42 |
public: |
| 43 |
TestBkt(int n) : m_x(n) {} |
| 44 |
size_t size() const { return 1; } |
| 45 |
const int& operator[](size_t /*i*/) const { return m_x; } |
| 46 |
private: |
| 47 |
int m_x; |
| 48 |
}; |
| 49 |
|
| 50 |
TEST(GetBucket, byValue) |
| 51 |
{ |
| 52 |
std::vector<TestBkt> vec = {TestBkt(9)}; |
| 53 |
EXPECT_EQ(9, stk::mesh::impl::get_bucket(vec,0)[0]); |
| 54 |
} |
| 55 |
|
| 56 |
TEST(GetBucket, byPtr) |
| 57 |
{ |
| 58 |
std::vector<TestBkt*> vec; |
| 59 |
TestBkt bkt(9); |
| 60 |
vec.push_back(&bkt); |
| 61 |
|
| 62 |
EXPECT_EQ(9, stk::mesh::impl::get_bucket(vec,0)[0]); |
| 63 |
} |
| 64 |
|
| 65 |
template<typename BucketType> |
| 66 |
void call_the_slide_function(unsigned holeBkt, unsigned holeOrd, |
| 67 |
std::vector<BucketType>& buckets) |
| 68 |
{ |
| 69 |
auto is_hole = [&](int item){return item == -1;}; |
| 70 |
|
| 71 |
auto overwrite = |
| 72 |
[&](unsigned destBkt, unsigned destOrd, unsigned srcBkt, unsigned srcOrd) |
| 73 |
{buckets[destBkt][destOrd] = buckets[srcBkt][srcOrd];}; |
| 74 |
|
| 75 |
auto remove_last = |
| 76 |
[&]() |
| 77 |
{ |
| 78 |
BucketType& lastBucket = buckets.back(); |
| 79 |
lastBucket.pop_back(); |
| 80 |
if (lastBucket.empty()) { |
| 81 |
buckets.pop_back(); |
| 82 |
} |
| 83 |
}; |
| 84 |
|
| 85 |
stk::mesh::impl::slide_contents_to_fill_holes( |
| 86 |
holeBkt, holeOrd, buckets, |
| 87 |
is_hole, overwrite, remove_last |
| 88 |
); |
| 89 |
} |
| 90 |
|
| 91 |
TEST(UnitTestSlide, slide_bucket_contents_nominal) |
| 92 |
{ |
| 93 |
using BucketType = std::vector<int>; |
| 94 |
std::vector<BucketType> buckets = |
| 95 |
{ {0, 1, -1, 3}, {6, -1, -1, 10}, {52, -1, 54, 55, 56, 57} }; |
| 96 |
|
| 97 |
std::vector<BucketType> finalBuckets = |
| 98 |
{ {0, 1, 3, 6}, {10, 52, 54, 55}, {56, 57} }; |
| 99 |
|
| 100 |
call_the_slide_function(0, 2, buckets); |
| 101 |
|
| 102 |
EXPECT_EQ(buckets, finalBuckets); |
| 103 |
} |
| 104 |
|
| 105 |
TEST(UnitTestSlide, slide_bucket_contents_holeAtEndOfBkt) |
| 106 |
{ |
| 107 |
using BucketType = std::vector<int>; |
| 108 |
std::vector<BucketType> buckets = |
| 109 |
{ {0, -1, 3, -1}, {6, 8, -1, 13}, {52, 53, 54, 55} }; |
| 110 |
|
| 111 |
std::vector<BucketType> finalBuckets = |
| 112 |
{ {0, 3, 6, 8}, {13, 52, 53, 54}, {55} }; |
| 113 |
|
| 114 |
call_the_slide_function(0, 1, buckets); |
| 115 |
|
| 116 |
EXPECT_EQ(buckets, finalBuckets); |
| 117 |
} |
| 118 |
|
| 119 |
TEST(UnitTestSlide, slide_bucket_contents_holeAtStartOfBkt) |
| 120 |
{ |
| 121 |
using BucketType = std::vector<int>; |
| 122 |
std::vector<BucketType> buckets = |
| 123 |
{ {-1, -1, 3, -1}, {-1, 8, -1, 13}, {52, 53, 54, 55, 56, 57} }; |
| 124 |
|
| 125 |
std::vector<BucketType> finalBuckets = |
| 126 |
{ {3, 8, 13, 52}, {53, 54, 55, 56}, {57} }; |
| 127 |
|
| 128 |
call_the_slide_function(0, 0, buckets); |
| 129 |
|
| 130 |
EXPECT_EQ(buckets, finalBuckets); |
| 131 |
} |
| 132 |
|
| 133 |
TEST(UnitTestSlide, slide_bucket_contents_removeMiddleBucket) |
| 134 |
{ |
| 135 |
using BucketType = std::vector<int>; |
| 136 |
std::vector<BucketType> buckets = |
| 137 |
{ {0, 1}, {-1, -1, -1}, {52, 53} }; |
| 138 |
|
| 139 |
std::vector<BucketType> finalBuckets = |
| 140 |
{ {0, 1}, {52, 53} }; |
| 141 |
|
| 142 |
call_the_slide_function(1, 0, buckets); |
| 143 |
|
| 144 |
EXPECT_EQ(buckets, finalBuckets); |
| 145 |
} |
| 146 |
|
| 147 |
TEST(UnitTestSlide, slide_bucket_contents_removeEndBucket) |
| 148 |
{ |
| 149 |
using BucketType = std::vector<int>; |
| 150 |
std::vector<BucketType> buckets = |
| 151 |
{ {0, 1}, {52, 53}, {-1, -1, -1} }; |
| 152 |
|
| 153 |
std::vector<BucketType> finalBuckets = |
| 154 |
{ {0, 1}, {52, 53} }; |
| 155 |
|
| 156 |
call_the_slide_function(2, 0, buckets); |
| 157 |
|
| 158 |
EXPECT_EQ(buckets, finalBuckets); |
| 159 |
} |
| 160 |
|
| 161 |
TEST(UnitTestSlide, slide_bucket_contents_removeOnlyBucket) |
| 162 |
{ |
| 163 |
using BucketType = std::vector<int>; |
| 164 |
std::vector<BucketType> buckets = |
| 165 |
{ {-1, -1, -1} }; |
| 166 |
|
| 167 |
call_the_slide_function(0, 0, buckets); |
| 168 |
|
| 169 |
EXPECT_TRUE(buckets.empty()); |
| 170 |
} |
| 171 |
|
| 172 |
} // empty namespace |