| 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/parallel/ParallelComm.hpp> |
| 37 |
|
| 38 |
namespace { |
| 39 |
|
| 40 |
class TestCommBuffer : public ::testing::Test |
| 41 |
{ |
| 42 |
public: |
| 43 |
template<typename PACK_LAMBDA> |
| 44 |
void pack(const PACK_LAMBDA& pack_stuff) |
| 45 |
{ |
| 46 |
pack_stuff(buf); |
| 47 |
allocate_buf(); |
| 48 |
pack_stuff(buf); |
| 49 |
} |
| 50 |
|
| 51 |
void allocate_buf() |
| 52 |
{ |
| 53 |
data.resize(buf.size()); |
| 54 |
buf.set_buffer_ptrs(data.data(), data.data(), data.data()+data.size()); |
| 55 |
} |
| 56 |
|
| 57 |
template<typename T> |
| 58 |
typename std::enable_if<std::is_floating_point<T>::value>::type |
| 59 |
check_unpack(const T& gold) |
| 60 |
{ |
| 61 |
T val; |
| 62 |
buf.unpack(val); |
| 63 |
EXPECT_NEAR(gold, val, 1.e-6); |
| 64 |
} |
| 65 |
|
| 66 |
template<typename T> |
| 67 |
typename std::enable_if<std::is_floating_point<T>::value>::type |
| 68 |
check_unpack(const std::vector<T>& gold) |
| 69 |
{ |
| 70 |
std::vector<T> val; |
| 71 |
buf.unpack(val); |
| 72 |
ASSERT_EQ(val.size(), gold.size()); |
| 73 |
for(unsigned i=0; i<gold.size(); ++i) { |
| 74 |
EXPECT_NEAR(gold[i], val[i], 1.e-6); |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
template<typename T> |
| 79 |
typename std::enable_if<!std::is_floating_point<T>::value>::type |
| 80 |
check_unpack(const T& gold) |
| 81 |
{ |
| 82 |
T val; |
| 83 |
buf.unpack(val); |
| 84 |
EXPECT_EQ(gold, val); |
| 85 |
} |
| 86 |
|
| 87 |
template<typename T> |
| 88 |
void check_peek_and_unpack_pair(const T& gold) |
| 89 |
{ |
| 90 |
T peekVal; |
| 91 |
buf.peek<T>(peekVal); |
| 92 |
EXPECT_EQ(gold, peekVal); |
| 93 |
T val; |
| 94 |
buf.unpack<T>(val); |
| 95 |
EXPECT_EQ(gold, val); |
| 96 |
} |
| 97 |
|
| 98 |
stk::CommBuffer buf; |
| 99 |
std::vector<unsigned char> data; |
| 100 |
}; |
| 101 |
|
| 102 |
TEST_F(TestCommBuffer, pack_unpack_primitives) |
| 103 |
{ |
| 104 |
char goldc = 's'; |
| 105 |
int goldn = 5; |
| 106 |
float goldf = 1.1; |
| 107 |
double goldd = 3.3; |
| 108 |
bool goldb = true; |
| 109 |
|
| 110 |
pack([=](stk::CommBuffer& buffer) { |
| 111 |
buffer.pack(goldc); |
| 112 |
buffer.pack(goldn); |
| 113 |
buffer.pack(goldf); |
| 114 |
buffer.pack(goldd); |
| 115 |
buffer.pack(goldb); |
| 116 |
}); |
| 117 |
|
| 118 |
buf.reset(); |
| 119 |
|
| 120 |
check_unpack(goldc); |
| 121 |
check_unpack(goldn); |
| 122 |
check_unpack(goldf); |
| 123 |
check_unpack(goldd); |
| 124 |
check_unpack(goldb); |
| 125 |
} |
| 126 |
|
| 127 |
TEST_F(TestCommBuffer, pack_unpack_pairs) |
| 128 |
{ |
| 129 |
std::pair<int,char> goldpic(5, 'c'); |
| 130 |
std::pair<double,int> goldpdi(3.14, 42); |
| 131 |
|
| 132 |
pack([=](stk::CommBuffer& buffer) { |
| 133 |
buffer.pack(goldpic); |
| 134 |
buffer.pack(goldpdi); |
| 135 |
}); |
| 136 |
|
| 137 |
buf.reset(); |
| 138 |
|
| 139 |
check_unpack(goldpic); |
| 140 |
check_unpack(goldpdi); |
| 141 |
} |
| 142 |
|
| 143 |
TEST_F(TestCommBuffer, skip_pack_pairs_explicit_template_parameter) |
| 144 |
{ |
| 145 |
std::pair<int,char> goldpic(5, 'c'); |
| 146 |
std::pair<unsigned long,int> goldpui(5, 9); |
| 147 |
std::pair<double,int> goldpdi(3.14, 42); |
| 148 |
|
| 149 |
buf.skip<std::pair<int,char>>(1); |
| 150 |
buf.skip<std::pair<unsigned long,int>>(1); |
| 151 |
buf.skip<std::pair<double,int>>(1); |
| 152 |
|
| 153 |
allocate_buf(); |
| 154 |
|
| 155 |
buf.pack<std::pair<int,char>>(goldpic); |
| 156 |
buf.pack<std::pair<unsigned long,int>>(goldpui); |
| 157 |
buf.pack<std::pair<double,int>>(goldpdi); |
| 158 |
|
| 159 |
buf.reset(); |
| 160 |
|
| 161 |
check_unpack(goldpic); |
| 162 |
check_unpack(goldpui); |
| 163 |
check_unpack(goldpdi); |
| 164 |
} |
| 165 |
|
| 166 |
TEST_F(TestCommBuffer, pack_unpack_pairs_explicit_template_parameter) |
| 167 |
{ |
| 168 |
std::pair<int,char> goldpic(5, 'c'); |
| 169 |
std::pair<unsigned long,int> goldpui(5, 9); |
| 170 |
std::pair<double,int> goldpdi(3.14, 42); |
| 171 |
|
| 172 |
pack([=](stk::CommBuffer& buffer) { |
| 173 |
buffer.pack<std::pair<int,char>>(goldpic); |
| 174 |
buffer.pack<std::pair<unsigned long,int>>(goldpui); |
| 175 |
buffer.pack<std::pair<double,int>>(goldpdi); |
| 176 |
}); |
| 177 |
|
| 178 |
buf.reset(); |
| 179 |
|
| 180 |
check_unpack(goldpic); |
| 181 |
check_unpack(goldpui); |
| 182 |
check_unpack(goldpdi); |
| 183 |
} |
| 184 |
|
| 185 |
TEST_F(TestCommBuffer, pack_unpack_pairs_explicit_template_parameter_peek_unpack) |
| 186 |
{ |
| 187 |
std::pair<int,char> goldpic(5, 'c'); |
| 188 |
std::pair<unsigned long,int> goldpui(5, 9); |
| 189 |
std::pair<double,int> goldpdi(3.14, 42); |
| 190 |
|
| 191 |
pack([=](stk::CommBuffer& buffer) { |
| 192 |
buffer.pack(goldpic); |
| 193 |
buffer.pack(goldpui); |
| 194 |
buffer.pack(goldpdi); |
| 195 |
}); |
| 196 |
|
| 197 |
buf.reset(); |
| 198 |
|
| 199 |
check_peek_and_unpack_pair(goldpic); |
| 200 |
check_peek_and_unpack_pair(goldpui); |
| 201 |
check_peek_and_unpack_pair(goldpdi); |
| 202 |
} |
| 203 |
|
| 204 |
TEST_F(TestCommBuffer, pack_unpack_pair_string) |
| 205 |
{ |
| 206 |
std::pair<std::string,int> goldpsi("short string", 5); |
| 207 |
std::pair<double,std::string> goldpds(3.14, "some string"); |
| 208 |
|
| 209 |
pack([=](stk::CommBuffer& buffer) { |
| 210 |
buffer.pack(goldpsi); |
| 211 |
buffer.pack(goldpds); |
| 212 |
}); |
| 213 |
|
| 214 |
buf.reset(); |
| 215 |
|
| 216 |
check_unpack(goldpsi); |
| 217 |
check_unpack(goldpds); |
| 218 |
} |
| 219 |
|
| 220 |
TEST_F(TestCommBuffer, pack_unpack_vector_primitives) |
| 221 |
{ |
| 222 |
std::vector<char> goldc = {'a', 'b', 'c'}; |
| 223 |
std::vector<int> goldn = {5, 6, 7}; |
| 224 |
std::vector<float> goldf = {1.1, 1.2, 1.3}; |
| 225 |
std::vector<double> goldd = {3.3, 3.4, 3.5}; |
| 226 |
std::vector<bool> goldb = {true, false, true}; |
| 227 |
|
| 228 |
pack([=](stk::CommBuffer& buffer) { |
| 229 |
buffer.pack(goldc); |
| 230 |
buffer.pack(goldn); |
| 231 |
buffer.pack(goldf); |
| 232 |
buffer.pack(goldd); |
| 233 |
buffer.pack(goldb); |
| 234 |
}); |
| 235 |
|
| 236 |
buf.reset(); |
| 237 |
|
| 238 |
check_unpack(goldc); |
| 239 |
check_unpack(goldn); |
| 240 |
check_unpack(goldf); |
| 241 |
check_unpack(goldd); |
| 242 |
check_unpack(goldb); |
| 243 |
} |
| 244 |
|
| 245 |
TEST_F(TestCommBuffer, pack_unpack_string) |
| 246 |
{ |
| 247 |
std::string goldstr("stk is awesome"); |
| 248 |
|
| 249 |
pack([=](stk::CommBuffer& buffer) { |
| 250 |
buffer.pack(goldstr); |
| 251 |
}); |
| 252 |
|
| 253 |
buf.reset(); |
| 254 |
|
| 255 |
check_unpack(goldstr); |
| 256 |
} |
| 257 |
|
| 258 |
TEST_F(TestCommBuffer, pack_unpack_string_explicit_template_parameter) |
| 259 |
{ |
| 260 |
std::string goldstr("stk is awesome"); |
| 261 |
|
| 262 |
pack([=](stk::CommBuffer& buffer) { |
| 263 |
buffer.pack<std::string>(goldstr); |
| 264 |
}); |
| 265 |
|
| 266 |
buf.reset(); |
| 267 |
|
| 268 |
std::string str; |
| 269 |
buf.unpack<std::string>(str); |
| 270 |
EXPECT_EQ(goldstr, str); |
| 271 |
} |
| 272 |
|
| 273 |
TEST_F(TestCommBuffer, pack_unpack_vector_string) |
| 274 |
{ |
| 275 |
std::vector<std::string> goldstr = {"stk", "is", "awesome"}; |
| 276 |
|
| 277 |
pack([=](stk::CommBuffer& buffer) { |
| 278 |
buffer.pack(goldstr); |
| 279 |
}); |
| 280 |
|
| 281 |
buf.reset(); |
| 282 |
|
| 283 |
check_unpack(goldstr); |
| 284 |
} |
| 285 |
|
| 286 |
} |
| 287 |
|