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