1 /*
2 * Copyright 2021 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include <ftl/concat.h>
18 #include <gtest/gtest.h>
19
20 namespace android::test {
21
22 // Keep in sync with example usage in header file.
TEST(Concat,Example)23 TEST(Concat, Example) {
24 std::string_view name = "Volume";
25 ftl::Concat string(ftl::truncated<3>(name), ": ", -3, " dB");
26
27 EXPECT_EQ(string.str(), "Vol: -3 dB");
28 EXPECT_EQ(string.c_str()[string.size()], '\0');
29 }
30
TEST(Concat,Characters)31 TEST(Concat, Characters) {
32 EXPECT_EQ(ftl::Concat(u'a', ' ', U'b').str(), "97 98");
33 }
34
TEST(Concat,References)35 TEST(Concat, References) {
36 int i[] = {-1, 2};
37 unsigned u = 3;
38 EXPECT_EQ(ftl::Concat(i[0], std::as_const(i[1]), u).str(), "-123");
39
40 const bool b = false;
41 const char c = 'o';
42 EXPECT_EQ(ftl::Concat(b, "tt", c).str(), "falsetto");
43 }
44
45 namespace {
46
47 static_assert(ftl::Concat{true, false, true}.str() == "truefalsetrue");
48 static_assert(ftl::Concat{':', '-', ')'}.str() == ":-)");
49
50 static_assert(ftl::Concat{"foo"}.str() == "foo");
51 static_assert(ftl::Concat{ftl::truncated<3>("foobar")}.str() == "foo");
52
53 constexpr ftl::Concat kConcat{"po", "trz", "ebie"};
54
55 static_assert(kConcat.size() == 9);
56 static_assert(kConcat.max_size() == 9);
57 static_assert(kConcat.str() == "potrzebie");
58 static_assert(kConcat.str() == std::string_view(kConcat.c_str()));
59
concat()60 constexpr auto concat() {
61 return ftl::Concat{ftl::truncated<1>("v???"), ftl::truncated<2>("ee??"),
62 ftl::truncated<3>("ble?"), ftl::truncated<4>("fetz"),
63 ftl::truncated<90>("er")};
64 }
65
66 static_assert(concat().size() == 12);
67 static_assert(concat().max_size() == 100);
68 static_assert(concat().str() == "veeblefetzer");
69 static_assert(concat().str() == std::string_view(concat().c_str()));
70
71 } // namespace
72 } // namespace android::test
73