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 constexpr std::array<int, 2> nonchar = {1, 2};
41 static constexpr size_t nonarray = 2;
42
43 static_assert(CanCreate<nonconst>::value == false);
44 static_assert(CanCreate<nonarray>::value == false);
45 static_assert(CanCreate<nonchar>::value == false);
46
47 static constexpr std::array<char, 2> scoped = {'a', 'b'};
48 constexpr StaticStringView Ticket1 = StaticStringView::create<global>();
49 constexpr StaticStringView Ticket2 = StaticStringView::create<scoped>();
50 const StaticStringView Ticket3 = StaticStringView::create<scoped>();
51 EXPECT_EQ(Ticket3, Ticket2);
52 EXPECT_EQ(Ticket1.getStringView(), Ticket2.getStringView());
53 EXPECT_EQ(std::string_view{"ab"}, Ticket1.getStringView());
54 }
TEST(StaticStringViewTests,CompileTimeConvert)55 TEST(StaticStringViewTests, CompileTimeConvert) {
56 static constexpr std::array<char, 4> converted = StaticStringView::toStdArray("test");
57 constexpr StaticStringView ticket = StaticStringView::create<converted>();
58 EXPECT_EQ(ticket, std::string_view{"test"});
59 // Unchecked constexpr construction
60 static const std::array<char, 5> converted2 = StaticStringView::toStdArray("test2");
61 constexpr auto ticket2 = StaticStringView::create<converted2, false>();
62 EXPECT_EQ(ticket2, std::string_view{"test2"});
63 constexpr char stack_array[4] = {'a', 'b', 'c', '\0'};
64 static constexpr auto converted3 = StaticStringView::toStdArray(stack_array);
65 constexpr auto ticket3 = StaticStringView::create<converted3>();
66 EXPECT_EQ(ticket3, std::string_view{"abc"});
67 }
68
TEST(StaticStringViewTests,CompileTimeConcat)69 TEST(StaticStringViewTests, CompileTimeConcat) {
70 // temporaries should not be static to prevent odr use
71 constexpr std::array<char, 3> arr1 = {'a', 'b', 'c'};
72 constexpr std::array<char, 4> arr2 = {'d', 'e', 'f', 'g'};
73 static constexpr std::array<char, 7> res = StaticStringView::concatArray(arr1, arr2);
74 static constexpr std::array<char, 7> expected = {'a', 'b', 'c', 'd', 'e', 'f', 'g'};
75 EXPECT_EQ(res, expected);
76 }
77
TEST(StaticStringViewTests,StringViewForwarding)78 TEST(StaticStringViewTests, StringViewForwarding) {
79 static constexpr auto converted = StaticStringView::toStdArray("test");
80 constexpr auto ticket = StaticStringView::create<converted>();
81 EXPECT_EQ(ticket.length(), ticket.getStringView().length());
82 EXPECT_TRUE(ticket == ticket.getStringView());
83 EXPECT_TRUE(ticket == ticket);
84 EXPECT_TRUE(ticket.getStringView() == ticket);
85 EXPECT_TRUE(ticket > "abc");
86 EXPECT_TRUE("abc" < ticket);
87 }
88