1 /*
2 * Copyright (C) 2020 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 <array>
18 #include <string>
19
20 #include <gtest/gtest.h>
21 #include <string.h>
22
23 #include "../ExpandableString.h"
24
25
TEST(ExpandableString,InitializeAppendRelease)26 TEST(ExpandableString, InitializeAppendRelease) {
27 const char* kAhoy = "Ahoy!";
28 struct ExpandableString s;
29 ExpandableStringInitialize(&s);
30 EXPECT_TRUE(s.data == NULL);
31 EXPECT_EQ(s.dataSize, 0u);
32 EXPECT_TRUE(ExpandableStringAppend(&s, kAhoy));
33 EXPECT_TRUE(s.data != NULL);
34 EXPECT_GE(s.dataSize, strlen(kAhoy));
35 ExpandableStringRelease(&s);
36 EXPECT_TRUE(s.data == NULL);
37 EXPECT_GE(s.dataSize, 0u);
38 }
39
TEST(ExpandableString,InitializeWriteRelease)40 TEST(ExpandableString, InitializeWriteRelease) {
41 const char* kAhoy = "Ahoy!";
42 const char* kMercy = "Mercy, Mercy, Mercy!";
43
44 struct ExpandableString s;
45 ExpandableStringInitialize(&s);
46 EXPECT_TRUE(s.data == NULL);
47 EXPECT_EQ(s.dataSize, 0u);
48 EXPECT_TRUE(ExpandableStringAssign(&s, kAhoy));
49 EXPECT_TRUE(s.data != NULL);
50 EXPECT_GE(s.dataSize, strlen(kAhoy));
51 EXPECT_TRUE(ExpandableStringAssign(&s, kMercy));
52 EXPECT_TRUE(s.data != NULL);
53 EXPECT_GE(s.dataSize, strlen(kMercy));
54 EXPECT_TRUE(ExpandableStringAssign(&s, kAhoy));
55 EXPECT_TRUE(s.data != NULL);
56 EXPECT_GE(s.dataSize, strlen(kAhoy));
57 ExpandableStringRelease(&s);
58 EXPECT_TRUE(s.data == NULL);
59 EXPECT_GE(s.dataSize, 0u);
60 }
61
62 class ExpandableStringTestFixture : public :: testing::TestWithParam<size_t> {
63 protected:
64 struct ExpandableString expandableString;
65 };
66
TEST_P(ExpandableStringTestFixture,AppendTest)67 TEST_P(ExpandableStringTestFixture, AppendTest) {
68 size_t step = GetParam();
69
70 std::array<std::string, 3> inputs = {
71 std::string(step, 'a'),
72 std::string(step, 'b'),
73 std::string(step, 'c'),
74 };
75
76 for (size_t offset = 0; offset < step; ++offset) {
77 ExpandableStringInitialize(&expandableString);
78
79 std::string pad(step - 1u, '_');
80 EXPECT_TRUE(ExpandableStringAppend(&expandableString, pad.c_str()));
81
82 for (size_t i = 0; i < 4096u; ++i) {
83 const std::string& appendee = inputs[i % inputs.size()];
84 EXPECT_TRUE(ExpandableStringAppend(&expandableString, appendee.c_str()));
85 size_t requiredSize = pad.size() + i * step + 1u;
86 EXPECT_GE(expandableString.dataSize, requiredSize);
87 }
88
89 size_t position = 0u;
90 for (char c : pad) {
91 EXPECT_EQ(c, expandableString.data[position]);
92 position++;
93 }
94 for (size_t i = 0; i < 4096; ++i) {
95 const std::string& expected = inputs[i % inputs.size()];
96 EXPECT_EQ(0, strncmp(expected.c_str(), expandableString.data + position, expected.size()));
97 position += expected.size();
98 }
99
100 ExpandableStringRelease(&expandableString);
101 }
102 }
103
104 INSTANTIATE_TEST_CASE_P(
105 AppendTest,
106 ExpandableStringTestFixture,
107 ::testing::Values(
108 1, 2, 3, 4, 5, 11, 17
109 ));