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 "perfetto/protozero/message_arena.h"
18
19 #include <array>
20
21 #include "test/gtest_and_gmock.h"
22
23 namespace protozero {
24 namespace {
25
26 using ::testing::NotNull;
27
TEST(MessageArenaTest,Basic)28 TEST(MessageArenaTest, Basic) {
29 MessageArena arena;
30
31 Message* msg1 = arena.NewMessage();
32 EXPECT_THAT(msg1, NotNull());
33 Message* msg2 = arena.NewMessage();
34 EXPECT_THAT(msg2, NotNull());
35 EXPECT_NE(msg1, msg2);
36 arena.DeleteLastMessage(msg2);
37 arena.DeleteLastMessage(msg1);
38
39 Message* msg3 = arena.NewMessage();
40 EXPECT_THAT(msg3, NotNull());
41 }
42
TEST(MessageArenaTest,ManyMessages)43 TEST(MessageArenaTest, ManyMessages) {
44 MessageArena arena;
45 // Ideally this should be more than MessageArena::Block::kCapacity, but that's
46 // private.
47 constexpr size_t kNumMessages = 32;
48 std::array<Message*, kNumMessages> messages;
49
50 for (size_t i = 0; i < kNumMessages; i++) {
51 Message* msg = arena.NewMessage();
52 EXPECT_THAT(msg, NotNull());
53 messages[i] = msg;
54 }
55
56 for (auto it = messages.crbegin(); it != messages.crend(); it++) {
57 Message* msg = *it;
58 arena.DeleteLastMessage(msg);
59 }
60
61 Message* msg = arena.NewMessage();
62 EXPECT_THAT(msg, NotNull());
63 }
64
65 } // namespace
66 } // namespace protozero
67