Back to home page

EIC code displayed by LXR

 
 

    


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

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