Project
Loading...
Searching...
No Matches
test_StringHelpers.cxx
Go to the documentation of this file.
1// Copyright 2019-2020 CERN and copyright holders of ALICE O2.
2// See https://alice-o2.web.cern.ch/copyright for details of the copyright holders.
3// All rights not expressly granted are reserved.
4//
5// This software is distributed under the terms of the GNU General Public
6// License v3 (GPL Version 3), copied verbatim in the file "COPYING".
7//
8// In applying this license CERN does not waive the privileges and immunities
9// granted to it by virtue of its status as an Intergovernmental Organization
10// or submit itself to any jurisdiction.
11
12#include <catch_amalgamated.hpp>
14
15static constexpr std::string_view part1 = "o2::aod::track::";
16static constexpr std::string_view part2 = "pt";
17
18TEST_CASE("StringHelpersHash")
19{
20 std::string s{"test-string"};
21 char const* const cs = "test-string";
22 REQUIRE(runtime_hash(s.c_str()) == compile_time_hash("test-string"));
23 REQUIRE(runtime_hash(cs) == compile_time_hash("test-string"));
24 REQUIRE(runtime_hash(s.c_str()) == runtime_hash(cs));
25
26 REQUIRE(compile_time_hash(part1, part2) == "o2::aod::track::pt"_h);
27}
28
29template <typename T>
30void printString(const T& constStr)
31{
32 static_assert(is_const_str<T>::value, "This function can only print compile-time strings!");
33
34 INFO("ConstStr:");
35 INFO("str -> " << constStr.str);
36 INFO("hash -> " << constStr.hash);
37};
38
39TEST_CASE("StringHelpersConstStr")
40{
41 printString(CONST_STR("this/is/a/histogram"));
42
43 auto myConstStr = CONST_STR("helloWorld");
44 printString(myConstStr);
45 static_assert(std::same_as<decltype(myConstStr), ConstStr<'h', 'e', 'l', 'l', 'o', 'W', 'o', 'r', 'l', 'd'>>);
46 static_assert(myConstStr.hash == (uint32_t)942280617);
47 REQUIRE(myConstStr.hash == compile_time_hash("helloWorld"));
48
49 if constexpr (is_const_str_v(myConstStr)) {
50 INFO("myConstStr is a compile-time string");
51 }
52
53 auto myConstStr2 = CONST_STR("hello") + CONST_STR("Universe");
54 printString(myConstStr2);
55 static_assert(std::same_as<decltype(myConstStr2), ConstStr<'h', 'e', 'l', 'l', 'o', 'U', 'n', 'i', 'v', 'e', 'r', 's', 'e'>>);
56
57 enum ParticleSpecies {
58 kPion,
59 kKaon
60 };
61 static constexpr std::string_view hist[] = {"ptDist", "etaDist"};
62 static constexpr std::string_view particleSuffix[] = {"_pions", "_kaons"};
63
64 printString(CONST_STR(hist[0]) + CONST_STR(particleSuffix[kPion]));
65 printString(CONST_STR(hist[0]) + CONST_STR(particleSuffix[kKaon]));
66 printString(CONST_STR(hist[1]) + CONST_STR(particleSuffix[kPion]));
67 printString(CONST_STR(hist[1]) + CONST_STR(particleSuffix[kKaon]));
68
69 REQUIRE(CONST_STR(hist[0]).hash == CONST_STR("ptDist").hash);
70}
consteval bool is_const_str_v(T)
constexpr uint32_t runtime_hash(char const *str)
consteval uint32_t compile_time_hash(Ts... Vs)
#define CONST_STR(literal)
TEST_CASE("StringHelpersHash")
void printString(const T &constStr)