Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-08-28 08:13:34

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