| 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 "stk_util/stk_config.h" // for STK_HAS_MPI |
| 36 |
|
| 37 |
#ifdef STK_HAS_MPI |
| 38 |
|
| 39 |
#include "gtest/gtest.h" // for Message, TestPartResult, AssertionResult, ASSERT_EQ |
| 40 |
#include "stk_util/util/Marshal.hpp" // for Marshal, operator<<, operator>>, Marshal::TYPE_CHEC... |
| 41 |
#include <list> // for list |
| 42 |
#include <memory> // for allocator_traits<>::value_type |
| 43 |
#include <stdexcept> // for runtime_error |
| 44 |
#include <string> // for operator==, string, basic_string |
| 45 |
#include <vector> // for vector |
| 46 |
|
| 47 |
struct S1 |
| 48 |
{ |
| 49 |
S1() |
| 50 |
: m_string(), |
| 51 |
m_int(0) |
| 52 |
{} |
| 53 |
|
| 54 |
S1(const std::string &s, int i) |
| 55 |
: m_string(s), |
| 56 |
m_int(i) |
| 57 |
{} |
| 58 |
|
| 59 |
std::string m_string; |
| 60 |
int m_int; |
| 61 |
}; |
| 62 |
|
| 63 |
struct S2 |
| 64 |
{ |
| 65 |
S2() |
| 66 |
: m_string(), |
| 67 |
m_int(0) |
| 68 |
{} |
| 69 |
|
| 70 |
S2(const std::string &s, int i) |
| 71 |
: m_string(s), |
| 72 |
m_int(i) |
| 73 |
{} |
| 74 |
|
| 75 |
std::string m_string; |
| 76 |
int m_int; |
| 77 |
}; |
| 78 |
|
| 79 |
// Define Marshal for S1 type in S1's namespace |
| 80 |
stk::Marshal &operator<<(stk::Marshal &mout, const S1 &s) { |
| 81 |
return mout << s.m_string << s.m_int; |
| 82 |
} |
| 83 |
|
| 84 |
stk::Marshal &operator>>(stk::Marshal &min, S1 &s) { |
| 85 |
return min >> s.m_string >> s.m_int; |
| 86 |
} |
| 87 |
|
| 88 |
|
| 89 |
// Define Marshal for S2 type in stk's namespace |
| 90 |
namespace stk { |
| 91 |
|
| 92 |
template<> |
| 93 |
stk::Marshal &operator<<(stk::Marshal &mout, const S2 &s) { |
| 94 |
return mout << s.m_string << s.m_int; |
| 95 |
} |
| 96 |
|
| 97 |
template<> |
| 98 |
stk::Marshal &operator>>(stk::Marshal &min, S2 &s) { |
| 99 |
return min >> s.m_string >> s.m_int; |
| 100 |
} |
| 101 |
|
| 102 |
} // namespace stk |
| 103 |
|
| 104 |
|
| 105 |
template <class T> |
| 106 |
void |
| 107 |
test(const T &t_in) |
| 108 |
{ |
| 109 |
stk::Marshal mout(stk::Marshal::TYPE_CHECK_ALL); |
| 110 |
mout << t_in; |
| 111 |
|
| 112 |
stk::Marshal min(mout.str()); |
| 113 |
T t_out; |
| 114 |
min >> t_out; |
| 115 |
|
| 116 |
ASSERT_EQ(t_in, t_out); |
| 117 |
} |
| 118 |
|
| 119 |
TEST(UnitTestMarshal, UnitTest) |
| 120 |
{ |
| 121 |
// Marshal/Unmarshal POD |
| 122 |
{ |
| 123 |
test<char>('a'); |
| 124 |
test<signed char>('a'); |
| 125 |
test<unsigned char>('a'); |
| 126 |
test<signed short>(1); |
| 127 |
test<unsigned short>(2); |
| 128 |
test<signed int>(3); |
| 129 |
test<unsigned int>(4); |
| 130 |
test<signed long>(5); |
| 131 |
test<unsigned long>(6); |
| 132 |
test<signed long long>(7); |
| 133 |
test<unsigned long long>(8); |
| 134 |
test<float>(9.0); |
| 135 |
test<double>(10.0); |
| 136 |
} |
| 137 |
|
| 138 |
// Marshal/Unmarshal more than one POD in a single message |
| 139 |
{ |
| 140 |
stk::Marshal mout(stk::Marshal::TYPE_CHECK_ALL); |
| 141 |
std::string s_out("this is a test"); |
| 142 |
int i_out = 7; |
| 143 |
|
| 144 |
mout << s_out << i_out; |
| 145 |
|
| 146 |
stk::Marshal min(mout.str()); |
| 147 |
std::string s_in; |
| 148 |
int i_in = 0; |
| 149 |
|
| 150 |
min >> s_in >> i_in; |
| 151 |
|
| 152 |
ASSERT_EQ(min.size(), mout.size()); |
| 153 |
ASSERT_TRUE(mout); |
| 154 |
ASSERT_TRUE(min); |
| 155 |
ASSERT_EQ((s_in == s_out), true); |
| 156 |
ASSERT_EQ(i_in, i_out); |
| 157 |
} |
| 158 |
|
| 159 |
// Marshal/Unmarshal locally defined class/struct |
| 160 |
{ |
| 161 |
stk::Marshal mout; |
| 162 |
S1 s_out("this is a test", 5); |
| 163 |
mout << s_out; |
| 164 |
|
| 165 |
stk::Marshal min(mout.str()); |
| 166 |
S1 s_in; |
| 167 |
|
| 168 |
min >> s_in; |
| 169 |
|
| 170 |
ASSERT_EQ((s_in.m_string == s_out.m_string), true); |
| 171 |
ASSERT_EQ(s_in.m_int, s_out.m_int); |
| 172 |
} |
| 173 |
|
| 174 |
// Marshal/Unmarshal std::vector of S2's (uses the stk namespace operator>> and operator<<) |
| 175 |
{ |
| 176 |
stk::Marshal mout; |
| 177 |
S2 s_out("this is a test", 5); |
| 178 |
std::vector<S2> v_out; |
| 179 |
v_out.push_back(s_out); |
| 180 |
|
| 181 |
mout << v_out; |
| 182 |
|
| 183 |
stk::Marshal min(mout.str()); |
| 184 |
std::vector<S2> v_in; |
| 185 |
|
| 186 |
min >> v_in; |
| 187 |
|
| 188 |
ASSERT_EQ((v_in[0].m_string == v_out[0].m_string), true); |
| 189 |
} |
| 190 |
|
| 191 |
// Marshal/Unmarshal error from type mismatch |
| 192 |
{ |
| 193 |
|
| 194 |
stk::Marshal mout(stk::Marshal::TYPE_CHECK_ALL); |
| 195 |
std::string s_out("this is a test"); |
| 196 |
int i_out = 7; |
| 197 |
|
| 198 |
mout << s_out << i_out; |
| 199 |
|
| 200 |
stk::Marshal min(mout.str()); |
| 201 |
std::string s_in; |
| 202 |
double x_in = 0.0; |
| 203 |
|
| 204 |
ASSERT_THROW(min >> s_in >> x_in, std::runtime_error); |
| 205 |
} |
| 206 |
|
| 207 |
// Marshal error for STL container mismatch |
| 208 |
{ |
| 209 |
stk::Marshal mout(stk::Marshal::TYPE_CHECK_ALL); |
| 210 |
S1 s_out("this is a test", 5); |
| 211 |
std::vector<S1> v_out; |
| 212 |
v_out.push_back(s_out); |
| 213 |
|
| 214 |
mout << v_out; |
| 215 |
|
| 216 |
stk::Marshal min(mout.str()); |
| 217 |
std::list<S1> v_in; |
| 218 |
|
| 219 |
ASSERT_THROW(min >> v_in, std::runtime_error); |
| 220 |
} |
| 221 |
|
| 222 |
// Marshal error for STL container mismatch |
| 223 |
{ |
| 224 |
stk::Marshal mout(stk::Marshal::TYPE_CHECK_ALL); |
| 225 |
S1 s_out("this is a test", 5); |
| 226 |
std::list<S1> v_out; |
| 227 |
v_out.push_back(s_out); |
| 228 |
|
| 229 |
mout << v_out; |
| 230 |
|
| 231 |
stk::Marshal min(mout.str()); |
| 232 |
std::vector<S1> v_in; |
| 233 |
|
| 234 |
ASSERT_THROW(min >> v_in, std::runtime_error); |
| 235 |
} |
| 236 |
|
| 237 |
// Marshal without error for STL container mismatch |
| 238 |
{ |
| 239 |
stk::Marshal mout(stk::Marshal::TYPE_CHECK_NONE); |
| 240 |
S1 s_out("this is a test", 5); |
| 241 |
std::vector<S1> v_out; |
| 242 |
v_out.push_back(s_out); |
| 243 |
|
| 244 |
mout << v_out; |
| 245 |
|
| 246 |
stk::Marshal min(mout.str()); |
| 247 |
std::list<S1> v_in; |
| 248 |
|
| 249 |
ASSERT_NO_THROW(min >> v_in); |
| 250 |
} |
| 251 |
} |
| 252 |
#endif // STK_HAS_MPI |
| 253 |
|