1 /*
2 * Copyright (C) 2018 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 "src/tracing/core/patch_list.h"
18
19 #include <ostream>
20
21 #include "test/gtest_and_gmock.h"
22
23 using testing::ElementsAre;
24
25 namespace perfetto {
26
27 std::ostream& operator<<(std::ostream& o, const Patch& p);
operator <<(std::ostream & o,const Patch & p)28 std::ostream& operator<<(std::ostream& o, const Patch& p) {
29 o << p.chunk_id << "@" << p.offset << " : {" << std::hex << p.size_field[0]
30 << "," << p.size_field[1] << "," << p.size_field[2] << ","
31 << p.size_field[3] << "}";
32 return o;
33 }
34
35 namespace {
36
TEST(PatchListTest,InsertAndRemove)37 TEST(PatchListTest, InsertAndRemove) {
38 PatchList pl;
39
40 ASSERT_TRUE(pl.empty());
41
42 pl.emplace_back(ChunkID(5), 50);
43 ASSERT_THAT(pl, ElementsAre(Patch(ChunkID(5), 50)));
44
45 pl.emplace_back(ChunkID(6), 60);
46 ASSERT_THAT(pl, ElementsAre(Patch(ChunkID(5), 50), Patch(ChunkID(6), 60)));
47
48 ASSERT_EQ(pl.front(), Patch(ChunkID(5), 50));
49 ASSERT_EQ(pl.back(), Patch(ChunkID(6), 60));
50
51 pl.pop_front();
52 ASSERT_EQ(pl.front(), Patch(ChunkID(6), 60));
53 pl.emplace_back(ChunkID(7), 70);
54
55 pl.pop_front();
56 ASSERT_EQ(pl.front(), Patch(ChunkID(7), 70));
57 ASSERT_EQ(pl.back(), Patch(ChunkID(7), 70));
58
59 pl.pop_front();
60
61 for (int i = 0; i < 3; i++) {
62 ASSERT_TRUE(pl.empty());
63
64 pl.emplace_back(ChunkID(8), 80);
65 pl.emplace_back(ChunkID(9), 90);
66 ASSERT_THAT(pl, ElementsAre(Patch(ChunkID(8), 80), Patch(ChunkID(9), 90)));
67
68 pl.pop_front();
69 pl.pop_front();
70 }
71 }
72
TEST(PatchListTest,PointerStability)73 TEST(PatchListTest, PointerStability) {
74 PatchList pl;
75 const uint8_t* ptrs[10]{};
76 for (uint16_t i = 0; i < 1000; i++) {
77 pl.emplace_back(ChunkID(i), i);
78 if (i >= 1000 - 10)
79 ptrs[i - (1000 - 10)] = &pl.back().size_field[0];
80 }
81
82 for (uint16_t i = 0; i < 1000 - 10; i++)
83 pl.pop_front();
84
85 auto it = pl.begin();
86 for (uint16_t i = 0; it != pl.end(); it++, i++) {
87 EXPECT_EQ(ptrs[i], &it->size_field[0]);
88 }
89 }
90
91 } // namespace
92 } // namespace perfetto
93