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/trace_processor/filtered_row_index.h" 18 19 #include "gmock/gmock.h" 20 #include "gtest/gtest.h" 21 22 namespace perfetto { 23 namespace trace_processor { 24 namespace { 25 26 using ::testing::ElementsAre; 27 28 TEST(FilteredRowIndexUnittest, Noop) { 29 FilteredRowIndex index(1, 4); 30 ASSERT_THAT(index.ToRowVector(), ElementsAre(1, 2, 3)); 31 } 32 33 TEST(FilteredRowIndexUnittest, FilterRows) { 34 FilteredRowIndex index(1, 5); 35 bool in_bound_indices = true; 36 index.FilterRows([&in_bound_indices](uint32_t row) { 37 in_bound_indices = in_bound_indices && row >= 1 && row < 5; 38 return row == 2 || row == 3; 39 }); 40 ASSERT_THAT(index.ToRowVector(), ElementsAre(2, 3)); 41 } 42 43 TEST(FilteredRowIndexUnittest, FilterRowsTwice) { 44 FilteredRowIndex index(1, 5); 45 index.FilterRows([](uint32_t row) { return row == 2 || row == 3; }); 46 bool in_bound_indices = true; 47 index.FilterRows([&in_bound_indices](uint32_t row) { 48 in_bound_indices = in_bound_indices && (row == 2 || row == 3); 49 return row == 2; 50 }); 51 ASSERT_TRUE(in_bound_indices); 52 ASSERT_THAT(index.ToRowVector(), ElementsAre(2)); 53 } 54 55 TEST(FilteredRowIndexUnittest, FilterThenIntersect) { 56 FilteredRowIndex index(1, 5); 57 index.FilterRows([](uint32_t row) { return row == 2 || row == 3; }); 58 index.IntersectRows({0, 2, 4, 5, 10}); 59 ASSERT_THAT(index.ToRowVector(), ElementsAre(2)); 60 } 61 62 TEST(FilteredRowIndexUnittest, IntersectThenFilter) { 63 FilteredRowIndex index(1, 5); 64 index.IntersectRows({0, 2, 4, 5, 10}); 65 index.FilterRows([](uint32_t row) { return row == 2 || row == 3; }); 66 ASSERT_THAT(index.ToRowVector(), ElementsAre(2)); 67 } 68 69 TEST(FilteredRowIndexUnittest, Intersect) { 70 FilteredRowIndex index(1, 5); 71 index.IntersectRows({0, 2, 4, 5, 10}); 72 ASSERT_THAT(index.ToRowVector(), ElementsAre(2, 4)); 73 } 74 75 TEST(FilteredRowIndexUnittest, IntersectTwice) { 76 FilteredRowIndex index(1, 5); 77 index.IntersectRows({0, 2, 4, 5, 10}); 78 index.IntersectRows({4}); 79 ASSERT_THAT(index.ToRowVector(), ElementsAre(4)); 80 } 81 82 TEST(FilteredRowIndexUnittest, ToIterator) { 83 FilteredRowIndex index(1, 5); 84 index.IntersectRows({0, 2, 4, 5, 10}); 85 auto iterator = index.ToRowIterator(false); 86 87 ASSERT_THAT(iterator->Row(), 2); 88 iterator->NextRow(); 89 ASSERT_THAT(iterator->Row(), 4); 90 iterator->NextRow(); 91 ASSERT_TRUE(iterator->IsEnd()); 92 } 93 94 } // namespace 95 } // namespace trace_processor 96 } // namespace perfetto 97