blob: ebef34076a44239e4aae03517ec65d83a377f359 [file] [log] [blame]
Andrew Topa953d4e2016-11-22 22:38:45 -08001/*
2 * Copyright 2016 Google Inc. All Rights Reserved.
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 "nb/analytics/memory_tracker_helpers.h"
18#include "testing/gtest/include/gtest/gtest.h"
19
20namespace nb {
21namespace analytics {
22namespace {
23
24///////////////////////////////////////////////////////////////////////////////
25TEST(AtomicStringAllocationGroupMap, Use) {
26 AtomicStringAllocationGroupMap map;
27 AllocationGroup* tag = map.Ensure("MemoryRegion");
28 EXPECT_TRUE(tag != NULL);
29 EXPECT_EQ(std::string("MemoryRegion"), tag->name());
30 AllocationGroup* tag2 = map.Ensure("MemoryRegion");
31 EXPECT_EQ(tag, tag2);
32}
33
34TEST(AtomicAllocationMap, AddHasRemove) {
35 AtomicAllocationMap atomic_pointer_map;
36 int32_t int_a = 0;
37 int32_t int_b = 1;
38
39 // Initially empty.
40 EXPECT_TRUE(atomic_pointer_map.Empty());
41
42 const AllocationRecord int32_alloc_record =
43 AllocationRecord(sizeof(int32_t), NULL);
44 AllocationRecord alloc_output;
45
46 EXPECT_TRUE(atomic_pointer_map.Add(&int_a, int32_alloc_record));
47 EXPECT_FALSE(atomic_pointer_map.Add(&int_a, int32_alloc_record));
48 EXPECT_TRUE(atomic_pointer_map.Get(&int_a, &alloc_output));
49 EXPECT_EQ(alloc_output.size, sizeof(int32_t));
50
51 EXPECT_FALSE(atomic_pointer_map.Get(&int_b, &alloc_output));
52 EXPECT_EQ(0, alloc_output.size);
53 // Adding pointer to int_a increases set to 1 element.
54 EXPECT_EQ(atomic_pointer_map.Size(), 1);
55 EXPECT_FALSE(atomic_pointer_map.Empty());
56
57 // Adds pointer to int_b.
58 EXPECT_TRUE(atomic_pointer_map.Add(&int_b, int32_alloc_record));
59 EXPECT_TRUE(atomic_pointer_map.Get(&int_b, &alloc_output));
60 EXPECT_EQ(sizeof(int32_t), alloc_output.size);
61 // Expect that the second pointer added will increase the number of elements.
62 EXPECT_EQ(atomic_pointer_map.Size(), 2);
63 EXPECT_FALSE(atomic_pointer_map.Empty());
64
65 // Now remove the elements and ensure that they no longer found and that
66 // the size of the table shrinks to empty.
67 EXPECT_TRUE(atomic_pointer_map.Remove(&int_a, &alloc_output));
68 EXPECT_EQ(sizeof(int32_t), alloc_output.size);
69 EXPECT_EQ(atomic_pointer_map.Size(), 1);
70 EXPECT_FALSE(atomic_pointer_map.Remove(&int_a, &alloc_output));
71 EXPECT_EQ(0, alloc_output.size);
72 EXPECT_TRUE(atomic_pointer_map.Remove(&int_b, &alloc_output));
73 EXPECT_EQ(atomic_pointer_map.Size(), 0);
74
75 EXPECT_TRUE(atomic_pointer_map.Empty());
76}
77
78TEST(ConcurrentAllocationMap, AddHasRemove) {
79 ConcurrentAllocationMap alloc_map;
80 int32_t int_a = 0;
81 int32_t int_b = 1;
82
83 // Initially empty.
84 EXPECT_TRUE(alloc_map.Empty());
85
86 const AllocationRecord int32_alloc_record =
87 AllocationRecord(sizeof(int32_t), NULL);
88 AllocationRecord alloc_output;
89
90 EXPECT_TRUE(alloc_map.Add(&int_a, int32_alloc_record));
91 EXPECT_FALSE(alloc_map.Add(&int_a, int32_alloc_record));
92 EXPECT_TRUE(alloc_map.Get(&int_a, &alloc_output));
93 EXPECT_EQ(alloc_output.size, sizeof(int32_t));
94
95 EXPECT_FALSE(alloc_map.Get(&int_b, &alloc_output));
96 EXPECT_EQ(0, alloc_output.size);
97 // Adding pointer to int_a increases set to 1 element.
98 EXPECT_EQ(alloc_map.Size(), 1);
99 EXPECT_FALSE(alloc_map.Empty());
100
101 // Adds pointer to int_b.
102 EXPECT_TRUE(alloc_map.Add(&int_b, int32_alloc_record));
103 EXPECT_TRUE(alloc_map.Get(&int_b, &alloc_output));
104 EXPECT_EQ(sizeof(int32_t), alloc_output.size);
105 // Expect that the second pointer added will increase the number of elements.
106 EXPECT_EQ(alloc_map.Size(), 2);
107 EXPECT_FALSE(alloc_map.Empty());
108
109 // Now remove the elements and ensure that they no longer found and that
110 // the size of the table shrinks to empty.
111 EXPECT_TRUE(alloc_map.Remove(&int_a, &alloc_output));
112 EXPECT_EQ(sizeof(int32_t), alloc_output.size);
113 EXPECT_EQ(alloc_map.Size(), 1);
114 EXPECT_FALSE(alloc_map.Remove(&int_a, &alloc_output));
115 EXPECT_EQ(0, alloc_output.size);
116 EXPECT_TRUE(alloc_map.Remove(&int_b, &alloc_output));
117 EXPECT_EQ(alloc_map.Size(), 0);
118
119 EXPECT_TRUE(alloc_map.Empty());
120}
121
122} // namespace
123} // namespace analytics
124} // namespace nb