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
15TEST_CASE("StringHelpersHash")
16{
17 std::string s{"test-string"};
18 char const* const cs = "test-string";
19 REQUIRE(runtime_hash(s.c_str()) == compile_time_hash("test-string"));
20 REQUIRE(runtime_hash(cs) == compile_time_hash("test-string"));
21 REQUIRE(runtime_hash(s.c_str()) == runtime_hash(cs));
22}
23
24template <typename T>
25void printString(const T& constStr)
26{
27 static_assert(is_const_str<T>::value, "This function can only print compile-time strings!");
28
29 INFO("ConstStr:");
30 INFO("str -> " << constStr.str);
31 INFO("hash -> " << constStr.hash);
32};
33
34TEST_CASE("StringHelpersConstStr")
35{
36 printString(CONST_STR("this/is/a/histogram"));
37
38 auto myConstStr = CONST_STR("helloWorld");
39 printString(myConstStr);
40 static_assert(std::same_as<decltype(myConstStr), ConstStr<'h', 'e', 'l', 'l', 'o', 'W', 'o', 'r', 'l', 'd'>>);
41 static_assert(myConstStr.hash == (uint32_t)942280617);
42 REQUIRE(myConstStr.hash == compile_time_hash("helloWorld"));
43
44 if constexpr (is_const_str_v(myConstStr)) {
45 INFO("myConstStr is a compile-time string");
46 }
47
48 auto myConstStr2 = CONST_STR("hello") + CONST_STR("Universe");
49 printString(myConstStr2);
50 static_assert(std::same_as<decltype(myConstStr2), ConstStr<'h', 'e', 'l', 'l', 'o', 'U', 'n', 'i', 'v', 'e', 'r', 's', 'e'>>);
51
52 enum ParticleSpecies {
53 kPion,
54 kKaon
55 };
56 static constexpr std::string_view hist[] = {"ptDist", "etaDist"};
57 static constexpr std::string_view particleSuffix[] = {"_pions", "_kaons"};
58
59 printString(CONST_STR(hist[0]) + CONST_STR(particleSuffix[kPion]));
60 printString(CONST_STR(hist[0]) + CONST_STR(particleSuffix[kKaon]));
61 printString(CONST_STR(hist[1]) + CONST_STR(particleSuffix[kPion]));
62 printString(CONST_STR(hist[1]) + CONST_STR(particleSuffix[kKaon]));
63
64 REQUIRE(CONST_STR(hist[0]).hash == CONST_STR("ptDist").hash);
65}
consteval bool is_const_str_v(T)
constexpr uint32_t runtime_hash(char const *str)
#define CONST_STR(literal)
consteval uint32_t compile_time_hash(char const *str)
TEST_CASE("StringHelpersHash")
void printString(const T &constStr)