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 "mediautils_fixedstring_tests"
18
19 #include <mediautils/FixedString.h>
20
21 #include <gtest/gtest.h>
22 #include <utils/Log.h>
23
24 using namespace android::mediautils;
25
TEST(mediautils_fixedstring_tests,ctor)26 TEST(mediautils_fixedstring_tests, ctor) {
27 FixedString<8> s0("abcde");
28
29 ASSERT_FALSE(s0.empty());
30 ASSERT_EQ(8U, s0.capacity());
31
32 ASSERT_EQ(5U, s0.size());
33 ASSERT_EQ(3U, s0.remaining());
34 ASSERT_EQ(0, strcmp(s0.c_str(), "abcde"));
35
36 ASSERT_EQ(0, strcmp(s0.data(), "abcde"));
37
38 // overflow
39 FixedString<8> s1("abcdefghijk");
40 ASSERT_EQ(8U, s1.size());
41 ASSERT_TRUE(s1.full());
42 ASSERT_EQ(0U, s1.remaining());
43 ASSERT_EQ(0, strcmp(s1.c_str(), "abcdefgh"));
44
45 // overflow
46 FixedString<8> s2(std::string("abcdefghijk"));
47 ASSERT_TRUE(s2.full());
48
49 ASSERT_EQ(8U, s2.size());
50 ASSERT_EQ(0, strcmp(s2.c_str(), "abcdefgh"));
51
52 // complex
53 ASSERT_EQ(s1, s2);
54 ASSERT_EQ(FixedString<12>().append(s1), s2);
55 ASSERT_NE(s1, "bcd");
56
57 // string and stringview
58 ASSERT_EQ(s1.asString(), s1.asStringView());
59
60 FixedString30 s3;
61 s3 = std::string("abcd");
62 ASSERT_EQ(s3, "abcd");
63
64 s3.clear();
65 ASSERT_EQ(s3, "");
66 ASSERT_NE(s3, "abcd");
67 ASSERT_EQ(0U, s3.size());
68 }
69
TEST(mediautils_fixedstring_tests,append)70 TEST(mediautils_fixedstring_tests, append) {
71 FixedString<8> s0;
72 ASSERT_EQ(0U, s0.size());
73 ASSERT_EQ(0, strcmp(s0.c_str(), ""));
74 ASSERT_TRUE(s0.empty());
75 ASSERT_FALSE(s0.full());
76
77 s0.append("abc");
78 ASSERT_EQ(3U, s0.size());
79 ASSERT_EQ(0, strcmp(s0.c_str(), "abc"));
80
81 s0.append(std::string("d"));
82 ASSERT_EQ(4U, s0.size());
83 ASSERT_EQ(0, strcmp(s0.c_str(), "abcd"));
84
85 // overflow
86 s0.append("efghijk");
87 ASSERT_EQ(8U, s0.size());
88 ASSERT_EQ(0, strcmp(s0.c_str(), "abcdefgh"));
89 ASSERT_TRUE(s0.full());
90
91 // concatenated
92 ASSERT_EQ(FixedString62("abcd"),
93 FixedString<8>("ab").append("c").append(FixedString<8>("d")));
94 ASSERT_EQ(FixedString<12>("a").append(FixedString<12>("b")), "ab");
95 }
96
TEST(mediautils_fixedstring_tests,plus_equals)97 TEST(mediautils_fixedstring_tests, plus_equals) {
98 FixedString<8> s0;
99 ASSERT_EQ(0U, s0.size());
100 ASSERT_EQ(0, strcmp(s0.c_str(), ""));
101
102 s0 += "abc";
103 s0 += "def";
104 ASSERT_EQ(s0, "abcdef");
105 }
106
TEST(mediautils_fixedstring_tests,stream_operator)107 TEST(mediautils_fixedstring_tests, stream_operator) {
108 FixedString<8> s0('a');
109
110 s0 << 'b' << "c" << "d" << '\n';
111 ASSERT_EQ(s0, "abcd\n");
112 }
113
TEST(mediautils_fixedstring_tests,examples)114 TEST(mediautils_fixedstring_tests, examples) {
115 FixedString30 s1(std::string("a"));
116 s1 << "bc" << 'd' << '\n';
117 s1 += "hello";
118
119 ASSERT_EQ(s1, "abcd\nhello");
120
121 FixedString30 s2;
122 for (const auto &c : s1.asStringView()) {
123 s2.append(c);
124 };
125 ASSERT_EQ(s1, s2);
126
127 FixedString30 s3(std::move(s1));
128 }
129
130 // Ensure type alias works fine as well.
131 using FixedString1024 = FixedString<1024>;
132
TEST(mediautils_fixedstring_tests,copy)133 TEST(mediautils_fixedstring_tests, copy) {
134 FixedString1024 s0("abc");
135 FixedString62 s1(s0);
136
137 ASSERT_EQ(3U, s1.size());
138 ASSERT_EQ(0, strcmp(s1.c_str(), "abc"));
139 ASSERT_EQ(s0, s1);
140
141 FixedString<1024> s2(s1);
142 ASSERT_EQ(3U, s2.size());
143 ASSERT_EQ(0, strcmp(s2.c_str(), "abc"));
144 ASSERT_EQ(s2, "abc");
145 ASSERT_NE(s2, "def");
146 ASSERT_EQ(s2, std::string("abc"));
147 ASSERT_NE(s2, std::string("def"));
148 ASSERT_EQ(s1, s2);
149 ASSERT_EQ(s0, s2);
150 ASSERT_EQ(s2, FixedString62(FixedString1024("abc")));
151 }
152