1 /*
2  * Copyright (C) 2020 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/perfetto_sql/intrinsics/table_functions/experimental_counter_dur.h"
18 
19 #include <cstdint>
20 
21 #include "src/trace_processor/containers/string_pool.h"
22 #include "src/trace_processor/tables/counter_tables_py.h"
23 #include "src/trace_processor/tables/track_tables_py.h"
24 #include "test/gtest_and_gmock.h"
25 
26 namespace perfetto::trace_processor {
27 namespace {
28 
CounterRow(int64_t ts,uint32_t track_id)29 tables::CounterTable::Row CounterRow(int64_t ts, uint32_t track_id) {
30   tables::CounterTable::Row row;
31   row.ts = ts;
32   row.track_id = tables::TrackTable::Id{track_id};
33   return row;
34 }
35 
TEST(ExperimentalCounterDur,SmokeDur)36 TEST(ExperimentalCounterDur, SmokeDur) {
37   StringPool pool;
38   tables::CounterTable table(&pool);
39 
40   table.Insert(CounterRow(100 /* ts */, 1 /* track_id */));
41   table.Insert(CounterRow(102 /* ts */, 2 /* track_id */));
42   table.Insert(CounterRow(105 /* ts */, 1 /* track_id */));
43   table.Insert(CounterRow(105 /* ts */, 3 /* track_id */));
44   table.Insert(CounterRow(105 /* ts */, 2 /* track_id */));
45   table.Insert(CounterRow(110 /* ts */, 2 /* track_id */));
46 
47   auto dur = ExperimentalCounterDur::ComputeDurColumn(table);
48   ASSERT_EQ(dur.size(), table.row_count());
49 
50   ASSERT_EQ(dur.Get(0), 5);
51   ASSERT_EQ(dur.Get(1), 3);
52   ASSERT_EQ(dur.Get(2), -1);
53   ASSERT_EQ(dur.Get(3), -1);
54   ASSERT_EQ(dur.Get(4), 5);
55   ASSERT_EQ(dur.Get(5), -1);
56 }
57 
58 }  // namespace
59 }  // namespace perfetto::trace_processor
60