| 1 |
/* +------------------------------------------------------------------------+ |
| 2 |
| Mobile Robot Programming Toolkit (MRPT) | |
| 3 |
| https://www.mrpt.org/ | |
| 4 |
| | |
| 5 |
| Copyright (c) 2005-2026, Individual contributors, see AUTHORS file | |
| 6 |
| See: https://www.mrpt.org/Authors - All rights reserved. | |
| 7 |
| Released under BSD License. See: https://www.mrpt.org/License | |
| 8 |
+------------------------------------------------------------------------+ */ |
| 9 |
|
| 10 |
#include <gtest/gtest.h> |
| 11 |
#include <mrpt/typemeta/num_to_string.h> |
| 12 |
#include <mrpt/typemeta/static_string.h> |
| 13 |
|
| 14 |
using namespace mrpt::typemeta; |
| 15 |
|
| 16 |
TEST(StaticString, ctor) |
| 17 |
{ |
| 18 |
constexpr string_literal<3> a = "foo"; |
| 19 |
(void)(a); |
| 20 |
|
| 21 |
constexpr auto b = literal("foo"); |
| 22 |
(void)(b); |
| 23 |
} |
| 24 |
|
| 25 |
TEST(StaticString, concat_literals) |
| 26 |
{ |
| 27 |
constexpr auto a = literal("foo"); |
| 28 |
constexpr auto b = literal("bar"); |
| 29 |
// In GCC7 these ones can be "constexpr", but that fails in MSVC 2017 (!) |
| 30 |
auto ab = a + b; |
| 31 |
|
| 32 |
static_assert(ab.size() == 6, "***"); |
| 33 |
EXPECT_EQ(ab[0], 'f'); |
| 34 |
EXPECT_EQ(ab[5], 'r'); |
| 35 |
} |
| 36 |
|
| 37 |
TEST(StaticString, concat_multi) |
| 38 |
{ |
| 39 |
constexpr auto a = literal("foo"); |
| 40 |
constexpr auto b = literal("bar"); |
| 41 |
auto ab = a + b; |
| 42 |
auto ba = b + a; |
| 43 |
|
| 44 |
// test sstring + literal: |
| 45 |
auto abc = ab + literal("more"); |
| 46 |
static_assert(abc.size() == (6 + 4), "***"); |
| 47 |
|
| 48 |
// test sstring + sstring: |
| 49 |
auto abba = ab + ba; |
| 50 |
static_assert(abba.size() == 2 * 6, "***"); |
| 51 |
|
| 52 |
// Test c_str(): |
| 53 |
const char* s = abba.c_str(); |
| 54 |
const char* s2 = static_cast<const char*>(abba); |
| 55 |
EXPECT_EQ(s, s2); |
| 56 |
|
| 57 |
// Test cast to "const char*" () operator: |
| 58 |
const char* s3 = static_cast<const char*>(a); |
| 59 |
EXPECT_EQ(a.c_str(), s3); |
| 60 |
} |
| 61 |
|
| 62 |
TEST(num_to_string, ctor) |
| 63 |
{ |
| 64 |
constexpr auto a = num_to_string<13>::value; |
| 65 |
(void)(a); |
| 66 |
} |