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/traced/probes/filesystem/range_tree.h"
18 #include "src/traced/probes/filesystem/prefix_finder.h"
19
20 #include <fcntl.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <sys/stat.h>
24
25 #include "perfetto/base/build_config.h"
26 #include "perfetto/ext/base/scoped_file.h"
27 #include "perfetto/ext/base/temp_file.h"
28 #include "perfetto/ext/base/utils.h"
29 #include "test/gtest_and_gmock.h"
30
31 namespace perfetto {
32 namespace {
33
34 using ::testing::Contains;
35 using ::testing::Not;
36
TEST(RangeTreeTest,Basic)37 TEST(RangeTreeTest, Basic) {
38 PrefixFinder pr(1);
39 pr.AddPath("/a/foo");
40 pr.AddPath("/b/foo");
41 pr.AddPath("/c/foo");
42 pr.AddPath("/d/foo");
43 pr.Finalize();
44
45 auto a = pr.GetPrefix("/a/foo");
46 auto b = pr.GetPrefix("/b/foo");
47 auto c = pr.GetPrefix("/c/foo");
48 auto d = pr.GetPrefix("/d/foo");
49
50 ASSERT_EQ(a->ToString(), "/a");
51 ASSERT_EQ(b->ToString(), "/b");
52 ASSERT_EQ(c->ToString(), "/c");
53 ASSERT_EQ(d->ToString(), "/d");
54
55 // This test needs to be changed for other kSetSize.
56 ASSERT_EQ(kSetSize, 3u);
57
58 RangeTree t;
59 t.Insert(1, a);
60 t.Insert(2, a);
61 t.Insert(20, b);
62 t.Insert(24, a);
63 t.Insert(25, c);
64 t.Insert(27, d);
65
66 EXPECT_THAT(t.Get(1), Contains("/a"));
67 EXPECT_THAT(t.Get(2), Contains("/a"));
68 EXPECT_THAT(t.Get(20), Contains("/b"));
69 EXPECT_THAT(t.Get(24), Contains("/a"));
70 EXPECT_THAT(t.Get(25), Contains("/c"));
71 EXPECT_THAT(t.Get(27), Contains("/d"));
72 // 27 will have overflowed kSetSize = 3;
73 EXPECT_THAT(t.Get(27), Not(Contains("/a")));
74 EXPECT_THAT(t.Get(27), Not(Contains("/b")));
75 EXPECT_THAT(t.Get(27), Not(Contains("/c")));
76 }
77
78 } // namespace
79 } // namespace perfetto
80