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/query_constraints.h"
18 #include "gmock/gmock.h"
19 #include "gtest/gtest.h"
20 #include "perfetto/base/logging.h"
21 
22 using testing::ElementsAreArray;
23 using testing::Matcher;
24 using testing::Field;
25 using testing::Pointwise;
26 using testing::Matches;
27 
28 namespace perfetto {
29 namespace trace_processor {
30 namespace {
31 
TEST(QueryConstraintsTest,ConvertToAndFromSqlString)32 TEST(QueryConstraintsTest, ConvertToAndFromSqlString) {
33   QueryConstraints qc;
34   qc.AddConstraint(12, 0);
35 
36   QueryConstraints::SqliteString only_constraint = qc.ToNewSqlite3String();
37   ASSERT_TRUE(strcmp(only_constraint.get(), "C1,12,0,O0") == 0);
38 
39   QueryConstraints qc_constraint =
40       QueryConstraints::FromString(only_constraint.get());
41   ASSERT_EQ(qc, qc_constraint);
42 
43   qc.AddOrderBy(1, false);
44   qc.AddOrderBy(21, true);
45 
46   QueryConstraints::SqliteString result = qc.ToNewSqlite3String();
47   ASSERT_TRUE(strcmp(result.get(), "C1,12,0,O2,1,0,21,1") == 0);
48 
49   QueryConstraints qc_result = QueryConstraints::FromString(result.get());
50   ASSERT_EQ(qc, qc_result);
51 }
52 
TEST(QueryConstraintsTest,CheckEmptyConstraints)53 TEST(QueryConstraintsTest, CheckEmptyConstraints) {
54   QueryConstraints qc;
55 
56   QueryConstraints::SqliteString string_result = qc.ToNewSqlite3String();
57   ASSERT_TRUE(strcmp(string_result.get(), "C0,O0") == 0);
58 
59   QueryConstraints qc_result =
60       QueryConstraints::FromString(string_result.get());
61   ASSERT_EQ(qc_result.constraints().size(), 0);
62   ASSERT_EQ(qc_result.order_by().size(), 0);
63 }
64 
TEST(QueryConstraintsTest,OnlyOrderBy)65 TEST(QueryConstraintsTest, OnlyOrderBy) {
66   QueryConstraints qc;
67   qc.AddOrderBy(3, true);
68 
69   QueryConstraints::SqliteString string_result = qc.ToNewSqlite3String();
70   ASSERT_TRUE(strcmp(string_result.get(), "C0,O1,3,1") == 0);
71 
72   QueryConstraints qc_result =
73       QueryConstraints::FromString(string_result.get());
74   ASSERT_EQ(qc, qc_result);
75 }
76 
77 }  // namespace
78 }  // namespace trace_processor
79 }  // namespace perfetto
80