| 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/diag/SlibDiagWriter.hpp" |
| 37 |
#include "stk_util/diag/WriterExt.hpp" // for operator<< |
| 38 |
#include "stk_util/diag/WriterOStream.hpp" // for operator<< |
| 39 |
#include "stk_util/environment/EnvData.hpp" |
| 40 |
#include "stk_util/environment/OutputLog.hpp" |
| 41 |
#include "stk_util/util/IndentStreambuf.hpp" // for indent_streambuf |
| 42 |
#include "stk_util/util/Writer.hpp" // for Writer, operator<<, dendl, push, pop, dflush |
| 43 |
#include "stk_util/util/WriterManip.hpp" // for operator<<, resetiosflags, setw, setfill |
| 44 |
#include "stk_util/util/Writer_fwd.hpp" // for LOG_ALWAYS, LOG_MEMBERS, LOG_TRACE, LOG_TRA... |
| 45 |
#include <bitset> // for bitset, bitset<>::reference |
| 46 |
#include <list> // for list, _List_iterator, list<>::iterator |
| 47 |
#include <map> // for multimap<>::value_type, multimap, map, _Rb_... |
| 48 |
#include <set> // for set |
| 49 |
#include <sstream> // for ostringstream, operator<<, ostream, operator& |
| 50 |
#include <string> // for operator==, string |
| 51 |
#include <utility> // for pair |
| 52 |
#include <vector> // for vector, vector<>::iterator |
| 53 |
|
| 54 |
using namespace stk::diag; |
| 55 |
|
| 56 |
enum LogMask { |
| 57 |
LOG_ALWAYS = stk::LOG_ALWAYS, |
| 58 |
LOG_TRACE = stk::LOG_TRACE, |
| 59 |
LOG_TRACE_STATS = stk::LOG_TRACE_STATS, |
| 60 |
LOG_TRACE_SUB_CALLS = stk::LOG_TRACE_SUB_CALLS, |
| 61 |
LOG_MEMBERS = stk::LOG_MEMBERS, |
| 62 |
LOG_TEST1 = 0x0000010, |
| 63 |
LOG_TEST2 = 0x0000020 |
| 64 |
}; |
| 65 |
|
| 66 |
TEST(UnitTestWriter, diag_writer_with_output_stream_specified) |
| 67 |
{ |
| 68 |
stk::EnvData::instance(); |
| 69 |
stk::bind_output_streams("dout>null"); |
| 70 |
EXPECT_NO_THROW(slibout << "pass"); |
| 71 |
} |
| 72 |
|
| 73 |
TEST(UnitTestWriter, diag_writer_with_default_output_stream) |
| 74 |
{ |
| 75 |
stk::EnvData::instance(); |
| 76 |
EXPECT_NO_THROW(slibout << "pass"); |
| 77 |
} |
| 78 |
|
| 79 |
TEST(UnitTestWriter, diagWriter_NoBlankLines) |
| 80 |
{ |
| 81 |
std::cout.flush(); |
| 82 |
std::ostringstream mystream; |
| 83 |
std::streambuf* oldbuf = std::cout.rdbuf(mystream.rdbuf()); |
| 84 |
sierra::configure_dwout(); |
| 85 |
|
| 86 |
sierra::dwout()<<"a\n\n\n\nb"<<std::endl; |
| 87 |
const std::string diagStr = mystream.str(); |
| 88 |
const std::string expectedStr = "a\nb\n"; |
| 89 |
EXPECT_EQ(expectedStr, diagStr); |
| 90 |
|
| 91 |
std::cout.rdbuf(oldbuf); |
| 92 |
} |
| 93 |
|
| 94 |
TEST(UnitTestWriter, diagWriter_BlankLines) |
| 95 |
{ |
| 96 |
const size_t indentSize = 2; |
| 97 |
const unsigned flags = stk::indent_streambuf::BRACES | stk::indent_streambuf::BLANK_LINES; |
| 98 |
std::cout.flush(); |
| 99 |
std::ostringstream mystream; |
| 100 |
std::streambuf* oldbuf = std::cout.rdbuf(mystream.rdbuf()); |
| 101 |
sierra::configure_dwout(indentSize, flags); |
| 102 |
|
| 103 |
sierra::dwout()<<"a\n\n\n\nb"<<std::endl; |
| 104 |
const std::string diagStr = mystream.str(); |
| 105 |
const std::string expectedStr = "a\n\n\n\nb\n"; |
| 106 |
EXPECT_EQ(expectedStr, diagStr); |
| 107 |
|
| 108 |
std::cout.rdbuf(oldbuf); |
| 109 |
} |
| 110 |
|
| 111 |
std::ostringstream & |
| 112 |
oss() |
| 113 |
{ |
| 114 |
static std::ostringstream s_oss; |
| 115 |
return s_oss; |
| 116 |
} |
| 117 |
|
| 118 |
std::ostream & |
| 119 |
dwout() |
| 120 |
{ |
| 121 |
static stk::indent_streambuf s_dwoutStreambuf(oss().rdbuf()); |
| 122 |
static std::ostream s_dwout(&s_dwoutStreambuf); |
| 123 |
|
| 124 |
return s_dwout; |
| 125 |
} |
| 126 |
|
| 127 |
// Diagnostic writer |
| 128 |
stk::diag::Writer & |
| 129 |
dw() |
| 130 |
{ |
| 131 |
static stk::diag::Writer s_diagWriter(dwout().rdbuf(), 0); |
| 132 |
|
| 133 |
return s_diagWriter; |
| 134 |
} |
| 135 |
|
| 136 |
TEST(UnitTestWriter, UnitTest) |
| 137 |
{ |
| 138 |
oss().str(std::string()); |
| 139 |
|
| 140 |
// #define SHOW_IOS_BASE_FLOATFIELD_BEHAVIORS |
| 141 |
#ifdef SHOW_IOS_BASE_FLOATFIELD_BEHAVIORS |
| 142 |
// Show libstdc++ std:ios_base floatfield bit behaviors, which have been inconsistent across platforms. |
| 143 |
// Once they are consistent, the automatic hexfloat-to-defaultfloat transition in WriterManip.{hpp,cpp} |
| 144 |
// can be removed, and "proper C++ 11" behavior can be supported. (With changes to this unit test.) |
| 145 |
const float ten_pi = M_PI * 10.0; |
| 146 |
std::cout << "const float ten_pi = M_PI * 10.0;" << std::endl; |
| 147 |
|
| 148 |
std::cout << "With default floatfieldflags (" << (std::cout.flags() & std::ios_base::floatfield) |
| 149 |
<< ") and default precision (" << std::cout.precision() << ") " << ten_pi << std::endl; |
| 150 |
|
| 151 |
std::cout.unsetf(std::ios_base::floatfield); |
| 152 |
std::cout << "With unsetf(std::ios_base::floatfield) " << ten_pi << std::endl; |
| 153 |
|
| 154 |
std::cout.setf(std::ios_base::fixed, std::ios_base::floatfield); |
| 155 |
std::cout << "With setf(std::ios_base::fixed, std::ios_base::floatfield) " << ten_pi << std::endl; |
| 156 |
|
| 157 |
std::cout.setf(std::ios_base::scientific, std::ios_base::floatfield); |
| 158 |
std::cout << "With setf(std::ios_base::scientific, std::ios_base::floatfield) " << ten_pi << std::endl; |
| 159 |
|
| 160 |
std::cout.setf((std::ios_base::fixed | std::ios_base::scientific), std::ios_base::floatfield); |
| 161 |
std::cout << "With setf((std::ios_base::fixed | std::ios_base::scientific), std::ios_base::floatfield) " << ten_pi << std::endl; |
| 162 |
#endif |
| 163 |
|
| 164 |
dw() << "This is a test" << dendl << dflush; |
| 165 |
|
| 166 |
ASSERT_EQ((std::string("This is a test\n") == oss().str()), true); |
| 167 |
|
| 168 |
oss().str(""); |
| 169 |
dw() << "Level 0" << push << dendl |
| 170 |
<< "This is a test" << dendl |
| 171 |
<< pop << dendl; |
| 172 |
|
| 173 |
ASSERT_EQ((std::string("Level 0 {\n This is a test\n}\n") == oss().str()), true); |
| 174 |
|
| 175 |
oss().str(""); |
| 176 |
{ |
| 177 |
stk::diag::WriterThrowSafe throw_safe__(dw()); |
| 178 |
|
| 179 |
dw() << "Level 0" << push << dendl |
| 180 |
<< "Level 1" << push << dendl |
| 181 |
<< "Level 2" << push << dendl |
| 182 |
<< "Level 3" << push << dendl |
| 183 |
<< "This is a test" << dendl |
| 184 |
<< pop << dendl; |
| 185 |
} |
| 186 |
dw() << dendl; |
| 187 |
ASSERT_EQ((std::string("Level 0 {\n Level 1 {\n Level 2 {\n Level 3 {\n This is a test\n }\n }\n }\n}\n") == oss().str()), true); |
| 188 |
|
| 189 |
oss().str(""); |
| 190 |
{ |
| 191 |
int x1 = 7; |
| 192 |
unsigned x2 = 7; |
| 193 |
short x3 = 7; |
| 194 |
unsigned short x4 = 7; |
| 195 |
long x5 = 7; |
| 196 |
unsigned long x6 = 7; |
| 197 |
long long x7 = 7; |
| 198 |
unsigned long long x8 = 7; |
| 199 |
char x9 = '7'; |
| 200 |
signed char x10 = '7'; |
| 201 |
unsigned char x11 = '7'; |
| 202 |
|
| 203 |
dw() << x1 << dendl; |
| 204 |
dw() << x2 << dendl; |
| 205 |
dw() << x3 << dendl; |
| 206 |
dw() << x4 << dendl; |
| 207 |
dw() << x5 << dendl; |
| 208 |
dw() << x6 << dendl; |
| 209 |
dw() << x7 << dendl; |
| 210 |
dw() << x8 << dendl; |
| 211 |
dw() << x9 << dendl; |
| 212 |
dw() << x10 << dendl; |
| 213 |
dw() << x11 << dendl; |
| 214 |
dw() << 7.0 << dendl; |
| 215 |
dw() << 7.0f << dendl; |
| 216 |
dw() << 7.0l << dendl; |
| 217 |
dw() << "This is a test" << dendl; |
| 218 |
dw() << std::string("This is a test") << dendl; |
| 219 |
} |
| 220 |
std::string actual = oss().str(); |
| 221 |
ASSERT_EQ((std::string("7\n7\n7\n7\n7\n7\n7\n7\n7\n7\n7\n7\n7\n7\nThis is a test\nThis is a test\n") == oss().str()), true); |
| 222 |
|
| 223 |
oss().str(""); |
| 224 |
dw() << std::hex << 16 << dendl; |
| 225 |
dw() << std::oct << 16 << dendl; |
| 226 |
dw() << std::dec << 16 << dendl; |
| 227 |
dw() << std::fixed << 3.14159265 << dendl; |
| 228 |
dw() << std::scientific << 3.14159265 << dendl; |
| 229 |
dw() << stk::diag::hex << 16 << dendl; |
| 230 |
dw() << stk::diag::oct << 16 << dendl; |
| 231 |
dw() << stk::diag::dec << 16 << dendl; |
| 232 |
dw() << stk::diag::fixed << 3.14159265 << dendl; |
| 233 |
dw() << stk::diag::scientific << 3.14159265 << dendl; |
| 234 |
dw() << stk::diag::setw(5) << 3.14159265 << dendl; |
| 235 |
dw() << stk::diag::setprecision(5) << 3.14159265 << dendl; |
| 236 |
dw() << stk::diag::setiosflags(std::ios::fixed) << 3.14159265 << dendl; |
| 237 |
// Check internal workarounds for inconsistent C++11 iostream manipulator support in libstdc++ and compilers. |
| 238 |
ASSERT_EQ(dw().getStream().flags() & std::ios_base::floatfield, std::ios_base::fmtflags(0)); |
| 239 |
dw() << stk::diag::resetiosflags(std::ios::fixed) << 3.14159265 << dendl; |
| 240 |
dw() << stk::diag::resetiosflags(std::ios::scientific) << 3.14159265 << dendl; |
| 241 |
ASSERT_EQ(dw().getStream().flags() & std::ios_base::floatfield, std::ios_base::fmtflags(0)); |
| 242 |
dw() << stk::diag::resetiosflags(std::ios::scientific) << 3.14159265 << dendl; |
| 243 |
dw() << stk::diag::resetiosflags(std::ios::fixed); |
| 244 |
ASSERT_EQ(dw().getStream().flags() & std::ios_base::floatfield, std::ios_base::fmtflags(0)); |
| 245 |
dw() << stk::diag::setfill('#') << stk::diag::setw(10) << "x" << dendl; |
| 246 |
ASSERT_EQ((std::string("10\n20\n16\n3.141593\n3.141593e+00\n10\n20\n16\n3.141593\n3.141593e+00\n3.141593e+00\n3.14159e+00\n3.1416\n3.14159e+00\n3.1416\n3.14159\n#########x\n") == oss().str()), true); |
| 247 |
|
| 248 |
oss().str(""); |
| 249 |
dw() << std::fixed << 3.25 << dendl; |
| 250 |
dw() << std::scientific << 4.25 << dendl; |
| 251 |
dw() << std::fixed << 5.25 << dendl; |
| 252 |
ASSERT_EQ((std::string("3.25000\n4.25000e+00\n5.25000\n") == oss().str()), true); |
| 253 |
|
| 254 |
oss().str(""); |
| 255 |
{ |
| 256 |
std::vector<int> v = { 1, 2, 3}; |
| 257 |
|
| 258 |
std::vector<int> vl; |
| 259 |
for (int i = 0; i < 20; ++i) |
| 260 |
vl.push_back(i); |
| 261 |
|
| 262 |
std::vector<int *> vp; |
| 263 |
vp.push_back(new int(1)); |
| 264 |
vp.push_back(new int(2)); |
| 265 |
vp.push_back(new int(3)); |
| 266 |
vp.push_back(nullptr); |
| 267 |
|
| 268 |
std::list<int> l = { 1, 2, 3 }; |
| 269 |
|
| 270 |
std::list<int *> lp; |
| 271 |
lp.push_back(new int(1)); |
| 272 |
lp.push_back(new int(2)); |
| 273 |
lp.push_back(new int(3)); |
| 274 |
|
| 275 |
std::map<int, int> m; |
| 276 |
m[1] = 2; |
| 277 |
m[2] = 3; |
| 278 |
m[3] = 4; |
| 279 |
|
| 280 |
std::map<int, int *> mp; |
| 281 |
mp[1] = new int(2); |
| 282 |
mp[2] = new int(3); |
| 283 |
mp[3] = new int(4); |
| 284 |
|
| 285 |
std::multimap<int, int> mm; |
| 286 |
mm.insert(std::multimap<int, int>::value_type(1, 2)); |
| 287 |
mm.insert(std::multimap<int, int>::value_type(1, 3)); |
| 288 |
mm.insert(std::multimap<int, int>::value_type(2, 4)); |
| 289 |
|
| 290 |
std::multimap<int, int *> mmp; |
| 291 |
mmp.insert(std::multimap<int, int *>::value_type(1, new int(2))); |
| 292 |
mmp.insert(std::multimap<int, int *>::value_type(1, new int(3))); |
| 293 |
mmp.insert(std::multimap<int, int *>::value_type(2, new int(4))); |
| 294 |
|
| 295 |
std::set<int> s = { 2, 3, 4}; |
| 296 |
|
| 297 |
std::bitset<8> b; |
| 298 |
b[1] = 1; |
| 299 |
b[3] = 1; |
| 300 |
|
| 301 |
dw() << typeid(int) << dendl; |
| 302 |
dw() << std::pair<int, int>(5, 7) << dendl; |
| 303 |
dw() << v << dendl; |
| 304 |
dw() << vl << dendl; |
| 305 |
dw() << vp << dendl; |
| 306 |
dw() << l << dendl; |
| 307 |
dw() << lp << dendl; |
| 308 |
dw() << m << dendl; |
| 309 |
dw() << mp << dendl; |
| 310 |
dw() << mm << dendl; |
| 311 |
dw() << mmp << dendl; |
| 312 |
dw() << s << dendl; |
| 313 |
dw() << b << dendl; |
| 314 |
|
| 315 |
for(std::vector<int*>::iterator it=vp.begin(); it!=vp.end(); ++it) |
| 316 |
delete *it; |
| 317 |
|
| 318 |
for ( std::multimap<int , int *>::iterator curmmp = mmp.begin() ; curmmp != mmp.end() ; curmmp++ ) |
| 319 |
delete curmmp->second; |
| 320 |
for ( std::map<int,int *>::iterator curmp = mp.begin() ; curmp != mp.end() ; curmp++ ) |
| 321 |
delete curmp->second; |
| 322 |
for ( std::list<int *>::iterator curp = lp.begin() ; curp != lp.end() ; curp++ ) |
| 323 |
delete *curp; |
| 324 |
|
| 325 |
} |
| 326 |
} |