Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-09-17 08:04:22

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/JoinStrings.hpp"
0012 
0013 #include <array>
0014 #include <list>
0015 #include <numeric>
0016 #include <ranges>
0017 #include <string>
0018 #include <vector>
0019 
0020 using namespace Acts;
0021 
0022 BOOST_AUTO_TEST_SUITE(JoinStrings)
0023 
0024 BOOST_AUTO_TEST_CASE(EmptyRange) {
0025   std::vector<std::string> empty;
0026   BOOST_CHECK_EQUAL(joinStrings(empty, ","), "");
0027 
0028   std::vector<int> emptyInts;
0029   BOOST_CHECK_EQUAL(joinStrings(emptyInts, ","), "");
0030 
0031   // Test with custom format
0032   BOOST_CHECK_EQUAL(joinStrings(emptyInts, ",", "{:02d}"), "");
0033 }
0034 
0035 BOOST_AUTO_TEST_CASE(SingleElement) {
0036   std::vector<std::string> single = {"hello"};
0037   BOOST_CHECK_EQUAL(joinStrings(single, ","), "hello");
0038 
0039   std::vector<int> singleInt = {42};
0040   BOOST_CHECK_EQUAL(joinStrings(singleInt, ","), "42");
0041 
0042   // Test with custom format
0043   BOOST_CHECK_EQUAL(joinStrings(singleInt, ",", "{:04d}"), "0042");
0044 }
0045 
0046 BOOST_AUTO_TEST_CASE(MultipleElements) {
0047   std::vector<std::string> strings = {"apple", "banana", "cherry"};
0048   BOOST_CHECK_EQUAL(joinStrings(strings, ","), "apple,banana,cherry");
0049   BOOST_CHECK_EQUAL(joinStrings(strings, ", "), "apple, banana, cherry");
0050   BOOST_CHECK_EQUAL(joinStrings(strings, " | "), "apple | banana | cherry");
0051 }
0052 
0053 BOOST_AUTO_TEST_CASE(IntegerTypes) {
0054   std::vector<int> ints = {1, 2, 3, 4, 5};
0055   BOOST_CHECK_EQUAL(joinStrings(ints, ","), "1,2,3,4,5");
0056   BOOST_CHECK_EQUAL(joinStrings(ints, " - "), "1 - 2 - 3 - 4 - 5");
0057 
0058   // Test with custom format
0059   BOOST_CHECK_EQUAL(joinStrings(ints, ",", "{:02d}"), "01,02,03,04,05");
0060   BOOST_CHECK_EQUAL(joinStrings(ints, ",", "{:+d}"), "+1,+2,+3,+4,+5");
0061 }
0062 
0063 BOOST_AUTO_TEST_CASE(FloatingPointTypes) {
0064   std::vector<double> doubles = {1.5, 2.7, 3.14};
0065   BOOST_CHECK_EQUAL(joinStrings(doubles, ","), "1.5,2.7,3.14");
0066 
0067   // Test with custom format
0068   BOOST_CHECK_EQUAL(joinStrings(doubles, ",", "{:.1f}"), "1.5,2.7,3.1");
0069   BOOST_CHECK_EQUAL(joinStrings(doubles, ",", "{:.2f}"), "1.50,2.70,3.14");
0070 }
0071 
0072 BOOST_AUTO_TEST_CASE(MixedNumericTypes) {
0073   std::vector<long> longs = {1000L, 2000L, 3000L};
0074   BOOST_CHECK_EQUAL(joinStrings(longs, " | "), "1000 | 2000 | 3000");
0075 
0076   std::vector<unsigned int> uints = {10u, 20u, 30u};
0077   BOOST_CHECK_EQUAL(joinStrings(uints, " -> "), "10 -> 20 -> 30");
0078 }
0079 
0080 BOOST_AUTO_TEST_CASE(DifferentContainerTypes) {
0081   // std::array
0082   std::array<std::string, 3> arr = {"x", "y", "z"};
0083   BOOST_CHECK_EQUAL(joinStrings(arr, "-"), "x-y-z");
0084 
0085   // std::list
0086   std::list<int> lst = {7, 8, 9};
0087   BOOST_CHECK_EQUAL(joinStrings(lst, " "), "7 8 9");
0088 
0089   // C-style array via span-like interface
0090   int cArray[] = {10, 11, 12};
0091   std::vector<int> vecFromArray(std::begin(cArray), std::end(cArray));
0092   BOOST_CHECK_EQUAL(joinStrings(vecFromArray, "/"), "10/11/12");
0093 }
0094 
0095 BOOST_AUTO_TEST_CASE(StringViewCompatible) {
0096   // Test with string literals and const char*
0097   std::vector<const char*> cstrings = {"hello", "world", "test"};
0098   BOOST_CHECK_EQUAL(joinStrings(cstrings, " "), "hello world test");
0099 
0100   // Test with std::string_view
0101   std::vector<std::string_view> stringViews = {"foo", "bar", "baz"};
0102   BOOST_CHECK_EQUAL(joinStrings(stringViews, "::"), "foo::bar::baz");
0103 }
0104 
0105 BOOST_AUTO_TEST_CASE(SpecialDelimiters) {
0106   std::vector<std::string> strings = {"a", "b", "c"};
0107 
0108   // Empty delimiter
0109   BOOST_CHECK_EQUAL(joinStrings(strings, ""), "abc");
0110 
0111   // Multi-character delimiters
0112   BOOST_CHECK_EQUAL(joinStrings(strings, " and "), "a and b and c");
0113   BOOST_CHECK_EQUAL(joinStrings(strings, " -> "), "a -> b -> c");
0114 
0115   // Special characters
0116   BOOST_CHECK_EQUAL(joinStrings(strings, "\n"), "a\nb\nc");
0117   BOOST_CHECK_EQUAL(joinStrings(strings, "\t"), "a\tb\tc");
0118 }
0119 
0120 BOOST_AUTO_TEST_CASE(EmptyStrings) {
0121   std::vector<std::string> withEmpty = {"", "middle", ""};
0122   BOOST_CHECK_EQUAL(joinStrings(withEmpty, ","), ",middle,");
0123 
0124   std::vector<std::string> allEmpty = {"", "", ""};
0125   BOOST_CHECK_EQUAL(joinStrings(allEmpty, "-"), "--");
0126 }
0127 
0128 BOOST_AUTO_TEST_CASE(CustomFormatStrings) {
0129   std::vector<int> numbers = {1, 2, 3};
0130 
0131   // Hexadecimal
0132   BOOST_CHECK_EQUAL(joinStrings(numbers, ",", "{:x}"), "1,2,3");
0133   BOOST_CHECK_EQUAL(joinStrings(numbers, ",", "{:X}"), "1,2,3");
0134 
0135   // With prefix
0136   BOOST_CHECK_EQUAL(joinStrings(numbers, ",", "#{:02d}"), "#01,#02,#03");
0137 
0138   std::vector<double> values = {1.234, 5.678};
0139   BOOST_CHECK_EQUAL(joinStrings(values, " | ", "{:.1e}"), "1.2e+00 | 5.7e+00");
0140 }
0141 
0142 BOOST_AUTO_TEST_CASE(LargerRanges) {
0143   // Test with larger ranges to ensure performance is reasonable
0144   std::vector<int> large(100);
0145   std::iota(large.begin(), large.end(), 1);
0146 
0147   std::string result = joinStrings(large, ",");
0148 
0149   // Check start and end of result
0150   BOOST_CHECK(result.starts_with("1,2,3"));
0151   BOOST_CHECK(result.ends_with("98,99,100"));
0152 
0153   // Check total length is reasonable (should contain all numbers and
0154   // delimiters)
0155   BOOST_CHECK(result.length() > 100);  // At least 100 digits + 99 commas
0156 }
0157 
0158 BOOST_AUTO_TEST_CASE(BooleanTypes) {
0159   // Use std::array instead of std::vector<bool> due to proxy reference issues
0160   std::array<bool, 3> bools = {true, false, true};
0161   BOOST_CHECK_EQUAL(joinStrings(bools, ","), "true,false,true");
0162 
0163   // With custom format to get numeric representation
0164   BOOST_CHECK_EQUAL(joinStrings(bools, " | ", "{:d}"), "1 | 0 | 1");
0165 }
0166 
0167 BOOST_AUTO_TEST_CASE(CharTypes) {
0168   std::vector<char> chars = {'a', 'b', 'c'};
0169   BOOST_CHECK_EQUAL(joinStrings(chars, "-"), "a-b-c");
0170 
0171   // With custom format for ASCII values
0172   BOOST_CHECK_EQUAL(joinStrings(chars, ",", "{:d}"), "97,98,99");
0173 }
0174 
0175 BOOST_AUTO_TEST_CASE(ViewsTransform) {
0176   // Test with std::views::transform - similar to real usage in GraphViz.cpp
0177   std::vector<int> numbers = {1, 2, 3, 4};
0178 
0179   auto squared = std::views::transform(numbers, [](int n) { return n * n; });
0180   BOOST_CHECK_EQUAL(joinStrings(squared, ","), "1,4,9,16");
0181 
0182   // Test with string transformation
0183   std::vector<std::string> words = {"hello", "world", "test"};
0184   auto uppercased = std::views::transform(words, [](const std::string& s) {
0185     std::string result;
0186     std::transform(s.begin(), s.end(), std::back_inserter(result),
0187                    [](char c) { return std::toupper(c); });
0188     return result;
0189   });
0190   BOOST_CHECK_EQUAL(joinStrings(uppercased, " "), "HELLO WORLD TEST");
0191 
0192   // Test with custom format on transformed view
0193   auto prefixed = std::views::transform(numbers, [](int n) { return n * 10; });
0194   BOOST_CHECK_EQUAL(joinStrings(prefixed, " | ", "#{:02d}"),
0195                     "#10 | #20 | #30 | #40");
0196 }
0197 
0198 BOOST_AUTO_TEST_SUITE_END()