• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/sqlite/query_constraints.h"
18 
19 #include "perfetto/base/logging.h"
20 #include "test/gtest_and_gmock.h"
21 
22 using testing::ElementsAreArray;
23 using testing::Field;
24 using testing::Matcher;
25 using testing::Matches;
26 using testing::Pointwise;
27 
28 namespace perfetto {
29 namespace trace_processor {
30 namespace {
31 
32 class QueryConstraintsTest : public ::testing::Test {
33  public:
QueryConstraintsTest()34   QueryConstraintsTest() { PERFETTO_CHECK(sqlite3_initialize() == SQLITE_OK); }
35 };
36 
TEST_F(QueryConstraintsTest,ConvertToAndFromSqlString)37 TEST_F(QueryConstraintsTest, ConvertToAndFromSqlString) {
38   QueryConstraints qc(0);
39   qc.AddConstraint(12, 0, 0);
40 
41   QueryConstraints::SqliteString only_constraint = qc.ToNewSqlite3String();
42   ASSERT_TRUE(strcmp(only_constraint.get(), "C1,12,0;O0;U0") == 0);
43 
44   QueryConstraints qc_constraint =
45       QueryConstraints::FromString(only_constraint.get());
46   ASSERT_EQ(qc, qc_constraint);
47 
48   qc.AddOrderBy(1, false);
49   qc.AddOrderBy(21, true);
50 
51   QueryConstraints::SqliteString result = qc.ToNewSqlite3String();
52   ASSERT_TRUE(strcmp(result.get(), "C1,12,0;O2,1,0,21,1;U0") == 0);
53 
54   QueryConstraints qc_result = QueryConstraints::FromString(result.get());
55   ASSERT_EQ(qc, qc_result);
56 }
57 
TEST_F(QueryConstraintsTest,CheckEmptyConstraints)58 TEST_F(QueryConstraintsTest, CheckEmptyConstraints) {
59   QueryConstraints qc(0);
60 
61   QueryConstraints::SqliteString string_result = qc.ToNewSqlite3String();
62   ASSERT_TRUE(strcmp(string_result.get(), "C0;O0;U0") == 0);
63 
64   QueryConstraints qc_result =
65       QueryConstraints::FromString(string_result.get());
66   ASSERT_EQ(qc_result.constraints().size(), 0u);
67   ASSERT_EQ(qc_result.order_by().size(), 0u);
68 }
69 
TEST_F(QueryConstraintsTest,OnlyOrderBy)70 TEST_F(QueryConstraintsTest, OnlyOrderBy) {
71   QueryConstraints qc(0);
72   qc.AddOrderBy(3, true);
73 
74   QueryConstraints::SqliteString string_result = qc.ToNewSqlite3String();
75   ASSERT_TRUE(strcmp(string_result.get(), "C0;O1,3,1;U0") == 0);
76 
77   QueryConstraints qc_result =
78       QueryConstraints::FromString(string_result.get());
79   ASSERT_EQ(qc, qc_result);
80 }
81 
TEST_F(QueryConstraintsTest,ColsUsed)82 TEST_F(QueryConstraintsTest, ColsUsed) {
83   ASSERT_EQ(QueryConstraints(0), QueryConstraints::FromString("C0;O0;U0"));
84 
85   ASSERT_EQ(QueryConstraints(4), QueryConstraints::FromString("C0;O0;U4"));
86 
87   ASSERT_EQ(QueryConstraints(1ull << 63),
88             QueryConstraints::FromString("C0;O0;U9223372036854775808"));
89 
90   ASSERT_EQ(QueryConstraints(9223372036854775807ull),
91             QueryConstraints::FromString("C0;O0;U9223372036854775807"));
92 
93   ASSERT_EQ(QueryConstraints(),
94             QueryConstraints::FromString("C0;O0;U18446744073709551615"));
95 
96   auto str = QueryConstraints(0xFFFFFFFFFFFFFFFF).ToNewSqlite3String();
97   ASSERT_STREQ(str.get(), "C0;O0;U18446744073709551615");
98 }
99 
100 }  // namespace
101 }  // namespace trace_processor
102 }  // namespace perfetto
103