| 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/PrintTable.hpp" // for operator<<, PrintTable, end_col, end_row, end_he... |
| 37 |
#include <iostream> // for ostringstream, endl, basic_ostream, ostream, hex |
| 38 |
#include <string> // for operator==, string |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
TEST( UnitTestPrintTable, UnitTest) |
| 43 |
{ |
| 44 |
std::ostringstream oss; |
| 45 |
|
| 46 |
{ |
| 47 |
oss.str(""); |
| 48 |
oss << std::endl; |
| 49 |
|
| 50 |
stk::PrintTable table; |
| 51 |
table.setAutoEndCol(false); |
| 52 |
|
| 53 |
table.setTitle("Test"); |
| 54 |
|
| 55 |
table << "x" << stk::end_col |
| 56 |
<< "y" << stk::end_col << stk::end_header |
| 57 |
<< "2" << stk::end_row |
| 58 |
<< "" << stk::end_col |
| 59 |
<< "3" << stk::end_row |
| 60 |
<< "" << stk::end_col |
| 61 |
<< "" << stk::end_col |
| 62 |
<< "4" << stk::end_row; |
| 63 |
oss << table; |
| 64 |
} |
| 65 |
ASSERT_EQ((std::string("\n Test\nx y\n- - -\n2\n 3\n 4\n\n") == oss.str()), true); |
| 66 |
|
| 67 |
{ |
| 68 |
oss.str(""); |
| 69 |
oss << std::endl; |
| 70 |
|
| 71 |
stk::PrintTable table; |
| 72 |
|
| 73 |
table.setTitle("A multiplication table, auto end column"); |
| 74 |
|
| 75 |
table << "x" << "|"; |
| 76 |
for (int i = 0; i < 10; ++i) |
| 77 |
table << i << "|"; |
| 78 |
table << stk::end_row; |
| 79 |
for (int i = 0; i < 10; ++i) { |
| 80 |
table << i << "|"; |
| 81 |
for (int j = 0; j < 10; ++j) |
| 82 |
table << i*j << "|"; |
| 83 |
table << stk::end_row; |
| 84 |
} |
| 85 |
|
| 86 |
oss << table; |
| 87 |
} |
| 88 |
ASSERT_EQ((std::string("\n A multiplication table, auto end column\nx | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |\n0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |\n1 | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |\n2 | 0 | 2 | 4 | 6 | 8 | 10 | 12 | 14 | 16 | 18 |\n3 | 0 | 3 | 6 | 9 | 12 | 15 | 18 | 21 | 24 | 27 |\n4 | 0 | 4 | 8 | 12 | 16 | 20 | 24 | 28 | 32 | 36 |\n5 | 0 | 5 | 10 | 15 | 20 | 25 | 30 | 35 | 40 | 45 |\n6 | 0 | 6 | 12 | 18 | 24 | 30 | 36 | 42 | 48 | 54 |\n7 | 0 | 7 | 14 | 21 | 28 | 35 | 42 | 49 | 56 | 63 |\n8 | 0 | 8 | 16 | 24 | 32 | 40 | 48 | 56 | 64 | 72 |\n9 | 0 | 9 | 18 | 27 | 36 | 45 | 54 | 63 | 72 | 81 |\n\n") == oss.str()), true); |
| 89 |
|
| 90 |
{ |
| 91 |
oss.str(""); |
| 92 |
oss << std::endl; |
| 93 |
|
| 94 |
stk::PrintTable table; |
| 95 |
|
| 96 |
table.setTitle("A multiplication table, auto end column"); |
| 97 |
|
| 98 |
table.setAutoEndCol(true); |
| 99 |
table.setCommaSeparatedValues(false); |
| 100 |
|
| 101 |
table << "x" << "|"; |
| 102 |
for (int i = 0; i < 10; ++i) |
| 103 |
table << i << "|"; |
| 104 |
table << stk::end_row; |
| 105 |
for (int i = 0; i < 10; ++i) { |
| 106 |
table << i << "|"; |
| 107 |
for (int j = 0; j < 10; ++j) |
| 108 |
table << i*j << "|"; |
| 109 |
table << stk::end_row; |
| 110 |
} |
| 111 |
|
| 112 |
oss << table; |
| 113 |
} |
| 114 |
ASSERT_EQ((std::string("\n A multiplication table, auto end column\nx | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |\n0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |\n1 | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |\n2 | 0 | 2 | 4 | 6 | 8 | 10 | 12 | 14 | 16 | 18 |\n3 | 0 | 3 | 6 | 9 | 12 | 15 | 18 | 21 | 24 | 27 |\n4 | 0 | 4 | 8 | 12 | 16 | 20 | 24 | 28 | 32 | 36 |\n5 | 0 | 5 | 10 | 15 | 20 | 25 | 30 | 35 | 40 | 45 |\n6 | 0 | 6 | 12 | 18 | 24 | 30 | 36 | 42 | 48 | 54 |\n7 | 0 | 7 | 14 | 21 | 28 | 35 | 42 | 49 | 56 | 63 |\n8 | 0 | 8 | 16 | 24 | 32 | 40 | 48 | 56 | 64 | 72 |\n9 | 0 | 9 | 18 | 27 | 36 | 45 | 54 | 63 | 72 | 81 |\n\n") == oss.str()), true); |
| 115 |
|
| 116 |
{ |
| 117 |
oss.str(""); |
| 118 |
oss << std::endl; |
| 119 |
|
| 120 |
stk::PrintTable table; |
| 121 |
|
| 122 |
table.setTitle("A multiplication table (in hex), no auto end column"); |
| 123 |
|
| 124 |
table.setAutoEndCol(false); |
| 125 |
table.setCommentPrefix("# "); |
| 126 |
|
| 127 |
table << "x" << stk::end_col << "|" << stk::end_col; |
| 128 |
for (int i = 0; i < 10; ++i) |
| 129 |
table << i << stk::end_col << "|" << stk::end_col; |
| 130 |
table << stk::end_header; |
| 131 |
for (int i = 0; i < 10; ++i) { |
| 132 |
table << i << stk::end_col << "|" << stk::end_col; |
| 133 |
for (int j = 0; j < 10; ++j) |
| 134 |
table << std::hex << i*j << stk::end_col << "|" << stk::end_col; |
| 135 |
table << stk::end_row; |
| 136 |
} |
| 137 |
|
| 138 |
oss << table; |
| 139 |
} |
| 140 |
ASSERT_EQ((std::string("\n# A multiplication table (in hex), no auto end column\n# x | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |\n# - - - - - - -- - -- - -- - -- - -- - -- - -- - -- -\n 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |\n 1 | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |\n 2 | 0 | 2 | 4 | 6 | 8 | a | c | e | 10 | 12 |\n 3 | 0 | 3 | 6 | 9 | c | f | 12 | 15 | 18 | 1b |\n 4 | 0 | 4 | 8 | c | 10 | 14 | 18 | 1c | 20 | 24 |\n 5 | 0 | 5 | a | f | 14 | 19 | 1e | 23 | 28 | 2d |\n 6 | 0 | 6 | c | 12 | 18 | 1e | 24 | 2a | 30 | 36 |\n 7 | 0 | 7 | e | 15 | 1c | 23 | 2a | 31 | 38 | 3f |\n 8 | 0 | 8 | 10 | 18 | 20 | 28 | 30 | 38 | 40 | 48 |\n 9 | 0 | 9 | 12 | 1b | 24 | 2d | 36 | 3f | 48 | 51 |\n \n") == oss.str()), true); |
| 141 |
|
| 142 |
{ |
| 143 |
oss.str(""); |
| 144 |
oss << std::endl; |
| 145 |
|
| 146 |
stk::PrintTable table; |
| 147 |
|
| 148 |
table.setTitle("A multiplication table, comma separated values"); |
| 149 |
|
| 150 |
table.setAutoEndCol(true); |
| 151 |
table.setCommaSeparatedValues(true); |
| 152 |
|
| 153 |
table << "x"; |
| 154 |
for (int i = 0; i < 10; ++i) |
| 155 |
table << i; |
| 156 |
table << stk::end_header; |
| 157 |
|
| 158 |
for (int i = 0; i < 10; ++i) { |
| 159 |
table << i; |
| 160 |
for (int j = 0; j < 10; ++j) |
| 161 |
table << i*j; |
| 162 |
table << stk::end_row; |
| 163 |
} |
| 164 |
|
| 165 |
oss << table; |
| 166 |
} |
| 167 |
ASSERT_EQ((std::string("\nA multiplication table, comma separated values\nx,0,1,2,3,4,5,6,7,8,9\n0,0,0,0,0,0,0,0,0,0,0\n1,0,1,2,3,4,5,6,7,8,9\n2,0,2,4,6,8,10,12,14,16,18\n3,0,3,6,9,12,15,18,21,24,27\n4,0,4,8,12,16,20,24,28,32,36\n5,0,5,10,15,20,25,30,35,40,45\n6,0,6,12,18,24,30,36,42,48,54\n7,0,7,14,21,28,35,42,49,56,63\n8,0,8,16,24,32,40,48,56,64,72\n9,0,9,18,27,36,45,54,63,72,81\n\n") == oss.str()), true); |
| 168 |
} |