1 /*
2 * Copyright (C) 2019 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/db/compare.h"
18 #include "test/gtest_and_gmock.h"
19
20 namespace perfetto {
21 namespace trace_processor {
22 namespace {
23
TEST(CompareTest,SqlValueDifferentTypes)24 TEST(CompareTest, SqlValueDifferentTypes) {
25 ASSERT_LT(compare::SqlValue(SqlValue(), SqlValue::Long(10)), 0);
26 ASSERT_LT(compare::SqlValue(SqlValue::Double(10.0), SqlValue::String("10")),
27 0);
28
29 // Numerics should still compare equal even if they have different types.
30 ASSERT_EQ(compare::SqlValue(SqlValue::Long(10), SqlValue::Double(10.0)), 0);
31 }
32
TEST(CompareTest,SqlValueLong)33 TEST(CompareTest, SqlValueLong) {
34 SqlValue int32_min = SqlValue::Long(std::numeric_limits<int32_t>::min());
35 SqlValue minus_1 = SqlValue::Long(-1);
36 SqlValue zero = SqlValue::Long(0);
37 SqlValue uint32_max = SqlValue::Long(std::numeric_limits<uint32_t>::max());
38
39 ASSERT_LT(compare::SqlValue(int32_min, minus_1), 0);
40 ASSERT_LT(compare::SqlValue(int32_min, uint32_max), 0);
41 ASSERT_LT(compare::SqlValue(minus_1, uint32_max), 0);
42
43 ASSERT_GT(compare::SqlValue(uint32_max, zero), 0);
44
45 ASSERT_EQ(compare::SqlValue(zero, zero), 0);
46 }
47
TEST(CompareTest,SqlValueDouble)48 TEST(CompareTest, SqlValueDouble) {
49 SqlValue int32_min = SqlValue::Double(std::numeric_limits<int32_t>::min());
50 SqlValue minus_1 = SqlValue::Double(-1.0);
51 SqlValue zero = SqlValue::Double(0);
52 SqlValue uint32_max = SqlValue::Double(std::numeric_limits<uint32_t>::max());
53
54 ASSERT_LT(compare::SqlValue(int32_min, minus_1), 0);
55 ASSERT_LT(compare::SqlValue(int32_min, uint32_max), 0);
56 ASSERT_LT(compare::SqlValue(minus_1, uint32_max), 0);
57
58 ASSERT_GT(compare::SqlValue(uint32_max, zero), 0);
59
60 ASSERT_EQ(compare::SqlValue(zero, zero), 0);
61 }
62
TEST(CompareTest,SqlValueString)63 TEST(CompareTest, SqlValueString) {
64 SqlValue a = SqlValue::String("a");
65 SqlValue aa = SqlValue::String("aa");
66 SqlValue b = SqlValue::String("b");
67
68 ASSERT_LT(compare::SqlValue(a, aa), 0);
69 ASSERT_LT(compare::SqlValue(aa, b), 0);
70 ASSERT_LT(compare::SqlValue(a, b), 0);
71
72 ASSERT_GT(compare::SqlValue(aa, a), 0);
73
74 ASSERT_EQ(compare::SqlValue(a, a), 0);
75 ASSERT_EQ(compare::SqlValue(aa, SqlValue::String("aa")), 0);
76 }
77
78 } // namespace
79 } // namespace trace_processor
80 } // namespace perfetto
81