1 /*
2 * Copyright (C) 2022 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 #define LOG_TAG "StaticStringViewTests"
18
19 #include <mediautils/StaticStringView.h>
20
21 #include <gtest/gtest.h>
22 #include <log/log.h>
23
24 using namespace android::mediautils;
25
26 template <auto& T, class = void>
27 struct CanCreate : std::false_type {};
28
29 template <auto& T>
30 struct CanCreate<T, typename std::void_t<decltype(StaticStringView::create<T>)>> : std::true_type {
31 };
32
33 static constexpr std::array<char, 2> global = {'a', 'b'};
34
TEST(StaticStringViewTests,CreateTicket)35 TEST(StaticStringViewTests, CreateTicket) {
36 // This will always fail due to template param binding rules
37 // const std::array<char,2> nonstatic = {'a', 'b'};
38 // static_assert(can_assign<nonstatic>::value == false);
39 static std::array<char, 2> nonconst = {'a', 'b'};
40 static const std::array<char, 2> nonconstexpr = {'a', 'b'};
41 static constexpr std::array<int, 2> nonchar = {1, 2};
42 static constexpr size_t nonarray = 2;
43
44 static_assert(CanCreate<nonconst>::value == false);
45 static_assert(CanCreate<nonarray>::value == false);
46 static_assert(CanCreate<nonchar>::value == false);
47 static_assert(CanCreate<nonconstexpr>::value == false);
48
49 static constexpr std::array<char, 2> scoped = {'a', 'b'};
50 constexpr StaticStringView Ticket1 = StaticStringView::create<global>();
51 constexpr StaticStringView Ticket2 = StaticStringView::create<scoped>();
52 const StaticStringView Ticket3 = StaticStringView::create<scoped>();
53 EXPECT_EQ(Ticket3, Ticket2);
54 EXPECT_EQ(Ticket1.getStringView(), Ticket2.getStringView());
55 EXPECT_EQ(std::string_view{"ab"}, Ticket1.getStringView());
56 }
TEST(StaticStringViewTests,CompileTimeConvert)57 TEST(StaticStringViewTests, CompileTimeConvert) {
58 static constexpr std::array<char, 4> converted = StaticStringView::toStdArray("test");
59 constexpr StaticStringView ticket = StaticStringView::create<converted>();
60 EXPECT_EQ(ticket, std::string_view{"test"});
61 // Unchecked constexpr construction
62 static const std::array<char, 5> converted2 = StaticStringView::toStdArray("test2");
63 constexpr auto ticket2 = StaticStringView::create<converted2, false>();
64 EXPECT_EQ(ticket2, std::string_view{"test2"});
65 constexpr char stack_array[4] = {'a', 'b', 'c', '\0'};
66 static constexpr auto converted3 = StaticStringView::toStdArray(stack_array);
67 constexpr auto ticket3 = StaticStringView::create<converted3>();
68 EXPECT_EQ(ticket3, std::string_view{"abc"});
69 }
70
TEST(StaticStringViewTests,CompileTimeConcat)71 TEST(StaticStringViewTests, CompileTimeConcat) {
72 // temporaries should not be static to prevent odr use
73 constexpr std::array<char, 3> arr1 = {'a', 'b', 'c'};
74 constexpr std::array<char, 4> arr2 = {'d', 'e', 'f', 'g'};
75 static constexpr std::array<char, 7> res = StaticStringView::concatArray(arr1, arr2);
76 static constexpr std::array<char, 7> expected = {'a', 'b', 'c', 'd', 'e', 'f', 'g'};
77 EXPECT_EQ(res, expected);
78 }
79
TEST(StaticStringViewTests,StringViewForwarding)80 TEST(StaticStringViewTests, StringViewForwarding) {
81 static constexpr auto converted = StaticStringView::toStdArray("test");
82 constexpr auto ticket = StaticStringView::create<converted>();
83 EXPECT_EQ(ticket.length(), ticket.getStringView().length());
84 EXPECT_TRUE(ticket == ticket.getStringView());
85 EXPECT_TRUE(ticket == ticket);
86 EXPECT_TRUE(ticket.getStringView() == ticket);
87 EXPECT_TRUE(ticket > "abc");
88 EXPECT_TRUE("abc" < ticket);
89 }
90