Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-12-16 09:25:21

0001 // This file is part of the ACTS project.
0002 //
0003 // Copyright (C) 2016 CERN for the benefit of the ACTS project
0004 //
0005 // This Source Code Form is subject to the terms of the Mozilla Public
0006 // License, v. 2.0. If a copy of the MPL was not distributed with this
0007 // file, You can obtain one at https://mozilla.org/MPL/2.0/.
0008 
0009 #include <boost/test/unit_test.hpp>
0010 
0011 #include "Acts/Utilities/Table.hpp"
0012 
0013 #include <sstream>
0014 #include <stdexcept>
0015 #include <string>
0016 
0017 using namespace Acts;
0018 
0019 namespace ActsTests {
0020 
0021 BOOST_AUTO_TEST_SUITE(UtilitiesSuite)
0022 
0023 BOOST_AUTO_TEST_CASE(TableBasicUsage) {
0024   Table table;
0025 
0026   // Add columns with different alignments and formats
0027   table.addColumn("Name", "{}", Table::Alignment::Left);
0028   table.addColumn("Value", "{:.2f}", Table::Alignment::Right);
0029   table.addColumn("Count", "{}", Table::Alignment::Center);
0030 
0031   // Add rows
0032   table.addRow("Item1", 1.234, 42);
0033   table.addRow("LongerItemName", 5.678, 7);
0034 
0035   std::string result = table.toString();
0036 
0037   // Expected output with exact formatting and alignment
0038   std::string expected =
0039       "| Name           | Value | Count |\n"
0040       "|:---------------|------:|:-----:|\n"
0041       "| Item1          |  1.23 |  42   |\n"
0042       "| LongerItemName |  5.68 |   7   |\n";
0043 
0044   BOOST_CHECK_EQUAL(result, expected);
0045 }
0046 
0047 BOOST_AUTO_TEST_CASE(TableMarkdownFormat) {
0048   Table table;
0049   table.addColumn("Algorithm", "{}", Table::Alignment::Left);
0050   table.addColumn("Time", "{}", Table::Alignment::Right);
0051   table.addColumn("Percentage", "{:.1f}%", Table::Alignment::Center);
0052 
0053   table.addRow("Reader", "1.2ms", 45.6);
0054   table.addRow("Processor", "800us", 30.2);
0055 
0056   std::string result = table.toString();
0057 
0058   // Expected output with exact alignment indicators
0059   std::string expected =
0060       "| Algorithm |  Time | Percentage |\n"
0061       "|:----------|------:|:----------:|\n"
0062       "| Reader    | 1.2ms |   45.6%    |\n"
0063       "| Processor | 800us |   30.2%    |\n";
0064 
0065   BOOST_CHECK_EQUAL(result, expected);
0066 }
0067 
0068 BOOST_AUTO_TEST_CASE(TableEmptyTable) {
0069   Table table;
0070 
0071   std::string result = table.toString();
0072   BOOST_CHECK(result.empty());
0073 }
0074 
0075 BOOST_AUTO_TEST_CASE(TableArgumentCountMismatch) {
0076   Table table;
0077   table.addColumn("Name", "{}", Table::Alignment::Left);
0078   table.addColumn("Value", "{}", Table::Alignment::Right);
0079 
0080   // Too many arguments
0081   BOOST_CHECK_THROW(table.addRow("Test", 123, 456), std::runtime_error);
0082 
0083   // Too few arguments
0084   BOOST_CHECK_THROW(table.addRow("Test"), std::runtime_error);
0085 }
0086 
0087 BOOST_AUTO_TEST_CASE(TableFormatString) {
0088   Table table;
0089   table.addColumn("Integer", "{:04d}", Table::Alignment::Right);
0090   table.addColumn("Float", "{:.3f}", Table::Alignment::Left);
0091   table.addColumn("Scientific", "{:.2e}", Table::Alignment::Center);
0092 
0093   table.addRow(42, 3.14159, 1234567.89);
0094 
0095   std::string result = table.toString();
0096 
0097   // Expected output with exact formatting
0098   std::string expected =
0099       "| Integer | Float | Scientific |\n"
0100       "|--------:|:------|:----------:|\n"
0101       "|    0042 | 3.142 |  1.23e+06  |\n";
0102 
0103   BOOST_CHECK_EQUAL(result, expected);
0104 }
0105 
0106 BOOST_AUTO_TEST_CASE(TableSingleColumn) {
0107   Table table;
0108   table.addColumn("Items", "{}", Table::Alignment::Center);
0109 
0110   table.addRow("First");
0111   table.addRow("Second");
0112   table.addRow("Third");
0113 
0114   std::string result = table.toString();
0115 
0116   // Expected output for single column with center alignment
0117   std::string expected =
0118       "| Items  |\n"
0119       "|:------:|\n"
0120       "| First  |\n"
0121       "| Second |\n"
0122       "| Third  |\n";
0123 
0124   BOOST_CHECK_EQUAL(result, expected);
0125 }
0126 
0127 BOOST_AUTO_TEST_CASE(TableMixedTypes) {
0128   Table table;
0129   table.addColumn("String", "{}", Table::Alignment::Left);
0130   table.addColumn("Integer", "{}", Table::Alignment::Right);
0131   table.addColumn("Double", "{:.2f}", Table::Alignment::Right);
0132   table.addColumn("Boolean", "{}", Table::Alignment::Center);
0133 
0134   table.addRow("Test", 42, 3.14159, true);
0135   table.addRow("Another", -17, -2.718, false);
0136 
0137   std::string result = table.toString();
0138 
0139   // Expected output with mixed types
0140   std::string expected =
0141       "| String  | Integer | Double | Boolean |\n"
0142       "|:--------|--------:|-------:|:-------:|\n"
0143       "| Test    |      42 |   3.14 |  true   |\n"
0144       "| Another |     -17 |  -2.72 |  false  |\n";
0145 
0146   BOOST_CHECK_EQUAL(result, expected);
0147 }
0148 
0149 BOOST_AUTO_TEST_CASE(TableStringAlignment) {
0150   Table table;
0151 
0152   // Test string alignment overload with full names
0153   table.addColumn("Left", "{}", "left");
0154   table.addColumn("Right", "{}", "right");
0155   table.addColumn("Center", "{}", "center");
0156 
0157   table.addRow("Item1", "Value1", "Data1");
0158   table.addRow("LongerItem", "V2", "D2");
0159 
0160   std::string result = table.toString();
0161 
0162   // Expected output with string-based alignments
0163   std::string expected =
0164       "| Left       |  Right | Center |\n"
0165       "|:-----------|-------:|:------:|\n"
0166       "| Item1      | Value1 | Data1  |\n"
0167       "| LongerItem |     V2 |   D2   |\n";
0168 
0169   BOOST_CHECK_EQUAL(result, expected);
0170 }
0171 
0172 BOOST_AUTO_TEST_CASE(TableStringAlignmentShort) {
0173   Table table;
0174 
0175   // Test string alignment overload with short names (case insensitive)
0176   table.addColumn("Col1", "{}", "L");
0177   table.addColumn("Col2", "{}", "r");
0178   table.addColumn("Col3", "{}", "C");
0179 
0180   table.addRow("A", "B", "C");
0181   table.addRow("XX", "YY", "ZZ");
0182 
0183   std::string result = table.toString();
0184 
0185   // Expected output with short alignment strings
0186   std::string expected =
0187       "| Col1 | Col2 | Col3 |\n"
0188       "|:-----|-----:|:----:|\n"
0189       "| A    |    B |  C   |\n"
0190       "| XX   |   YY |  ZZ  |\n";
0191 
0192   BOOST_CHECK_EQUAL(result, expected);
0193 }
0194 
0195 BOOST_AUTO_TEST_CASE(TableInvalidAlignment) {
0196   Table table;
0197 
0198   // Test invalid alignment string
0199   BOOST_CHECK_THROW(table.addColumn("Test", "{}", "invalid"),
0200                     std::invalid_argument);
0201   BOOST_CHECK_THROW(table.addColumn("Test", "{}", "middle"),
0202                     std::invalid_argument);
0203   BOOST_CHECK_THROW(table.addColumn("Test", "{}", ""), std::invalid_argument);
0204 }
0205 
0206 BOOST_AUTO_TEST_CASE(TableMarkdownMode) {
0207   Table table;
0208   table.addColumn("Name", "{}", "left");
0209   table.addColumn("Value", "{}", "right");
0210 
0211   table.addRow("Item1", "A");
0212   table.addRow("Item2", "BB");
0213 
0214   // Test with markdown markers (default)
0215   std::string withMarkers = table.toString();
0216   std::string expectedWithMarkers =
0217       "| Name  | Value |\n"
0218       "|:------|------:|\n"
0219       "| Item1 |     A |\n"
0220       "| Item2 |    BB |\n";
0221 
0222   BOOST_CHECK_EQUAL(withMarkers, expectedWithMarkers);
0223 
0224   // Test without markdown markers
0225   table.setMarkdownMode(false);
0226   std::string withoutMarkers = table.toString();
0227   std::string expectedWithoutMarkers =
0228       "| Name  | Value |\n"
0229       "|-------|-------|\n"
0230       "| Item1 |     A |\n"
0231       "| Item2 |    BB |\n";
0232 
0233   BOOST_CHECK_EQUAL(withoutMarkers, expectedWithoutMarkers);
0234 }
0235 
0236 BOOST_AUTO_TEST_CASE(TableOstreamOperator) {
0237   Table table;
0238   table.addColumn("Col1", "{}", "left");
0239   table.addColumn("Col2", "{}", "center");
0240 
0241   table.addRow("A", "B");
0242   table.addRow("C", "D");
0243 
0244   // Test ostream operator
0245   std::ostringstream oss;
0246   oss << table;
0247 
0248   std::string expected =
0249       "| Col1 | Col2 |\n"
0250       "|:-----|:----:|\n"
0251       "| A    |  B   |\n"
0252       "| C    |  D   |\n";
0253 
0254   BOOST_CHECK_EQUAL(oss.str(), expected);
0255 }
0256 
0257 BOOST_AUTO_TEST_SUITE_END()
0258 
0259 }  // namespace ActsTests