| 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/Parallel.hpp" // for parallel_machine_size, MPI_COMM_WORLD, Par... |
| 37 |
#include "stk_util/parallel/ParallelComm.hpp" // for CommBroadcast, CommBuffer |
| 38 |
#include <cstdlib> // for rand, RAND_MAX |
| 39 |
#include <functional> // for function |
| 40 |
#include <map> // for map |
| 41 |
#include <string> // for string, basic_string |
| 42 |
#include <vector> // for vector |
| 43 |
|
| 44 |
namespace |
| 45 |
{ |
| 46 |
std::string random_string(const int len) |
| 47 |
{ |
| 48 |
static const char alphanum[] = "0123456789" |
| 49 |
"ABCDEFGHIJKLMNOPQRSTUVWXYZ" |
| 50 |
"abcdefghijklmnopqrstuvwxyz"; |
| 51 |
|
| 52 |
std::vector<char> s(len); |
| 53 |
for (int i = 0; i < len; ++i) |
| 54 |
{ |
| 55 |
s[i] = alphanum[rand() % (sizeof(alphanum) - 1)]; |
| 56 |
} |
| 57 |
|
| 58 |
std::string str; |
| 59 |
str.assign(s.data(), len); |
| 60 |
return str; |
| 61 |
} |
| 62 |
double random_double(double fMin, double fMax) |
| 63 |
{ |
| 64 |
double f = (double)rand() / RAND_MAX; |
| 65 |
return fMin + f * (fMax - fMin); |
| 66 |
} |
| 67 |
int random_int(int imin, int imax) { return rand() % (imax - imin) + imin; } |
| 68 |
|
| 69 |
template<class T> |
| 70 |
void test_map(std::function<T()> generator) { |
| 71 |
stk::ParallelMachine global = MPI_COMM_WORLD; |
| 72 |
if (stk::parallel_machine_size(global) > 1) return; |
| 73 |
|
| 74 |
for (int i = 0; i < 100; ++i) |
| 75 |
{ |
| 76 |
int num_entries = random_int(1,4); |
| 77 |
std::map<std::string, T> smap; |
| 78 |
for(int j = 0; j < num_entries; ++j) { |
| 79 |
std::string key = random_string(random_int(1, 15)); |
| 80 |
T val = generator(); |
| 81 |
smap[key] = val; |
| 82 |
} |
| 83 |
|
| 84 |
stk::CommBroadcast bcast(MPI_COMM_WORLD, 0); |
| 85 |
auto& b = bcast.send_buffer(); |
| 86 |
b.pack(smap); |
| 87 |
bcast.allocate_buffer(); |
| 88 |
b.pack(smap); |
| 89 |
|
| 90 |
std::map<std::string, T> rmap; |
| 91 |
b.reset(); |
| 92 |
EXPECT_ANY_THROW(b.peek(rmap)); |
| 93 |
b.unpack(rmap); |
| 94 |
|
| 95 |
for(auto&& vp : smap) { |
| 96 |
EXPECT_EQ(vp.second, rmap[vp.first]); |
| 97 |
} |
| 98 |
} |
| 99 |
} |
| 100 |
|
| 101 |
} // namespace |
| 102 |
|
| 103 |
TEST(StringPacking, packSendAndPeek) |
| 104 |
{ |
| 105 |
stk::ParallelMachine global = MPI_COMM_WORLD; |
| 106 |
if (stk::parallel_machine_size(global) > 1) return; |
| 107 |
|
| 108 |
std::string key = "this is a test"; |
| 109 |
|
| 110 |
stk::CommBroadcast bcast(MPI_COMM_WORLD, 0); |
| 111 |
auto& b = bcast.send_buffer(); |
| 112 |
b.pack(key); |
| 113 |
bcast.allocate_buffer(); |
| 114 |
b.pack(key); |
| 115 |
|
| 116 |
b.reset(); |
| 117 |
|
| 118 |
std::string rpeek; |
| 119 |
b.peek(rpeek); |
| 120 |
std::string recv; |
| 121 |
b.unpack(recv); |
| 122 |
|
| 123 |
EXPECT_EQ(key, rpeek); |
| 124 |
EXPECT_EQ(key, recv); |
| 125 |
} |
| 126 |
|
| 127 |
TEST(MapPacking, PackingStringMap) |
| 128 |
{ |
| 129 |
test_map<std::string>([](){ return random_string(random_int(1, 20));}); |
| 130 |
} |
| 131 |
|
| 132 |
TEST(MapPacking, PackingDoubleMap) |
| 133 |
{ |
| 134 |
test_map<double>([](){ return random_double(-1e12, 1e12);}); |
| 135 |
} |
| 136 |
|
| 137 |
TEST(MapPacking, PackingIntMap) |
| 138 |
{ |
| 139 |
test_map<int>([](){ return rand();}); |
| 140 |
} |