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/importers/json/json_utils.h"
18
19 #include <json/value.h>
20
21 #include "test/gtest_and_gmock.h"
22
23 namespace perfetto {
24 namespace trace_processor {
25 namespace json {
26 namespace {
27
TEST(JsonTraceUtilsTest,CoerceToUint32)28 TEST(JsonTraceUtilsTest, CoerceToUint32) {
29 ASSERT_EQ(CoerceToUint32(Json::Value(42)).value_or(0), 42u);
30 ASSERT_EQ(CoerceToUint32(Json::Value("42")).value_or(0), 42u);
31 ASSERT_EQ(CoerceToInt64(Json::Value(42.1)).value_or(-1), 42);
32 }
33
TEST(JsonTraceUtilsTest,CoerceToInt64)34 TEST(JsonTraceUtilsTest, CoerceToInt64) {
35 ASSERT_EQ(CoerceToInt64(Json::Value(42)).value_or(-1), 42);
36 ASSERT_EQ(CoerceToInt64(Json::Value("42")).value_or(-1), 42);
37 ASSERT_EQ(CoerceToInt64(Json::Value(42.1)).value_or(-1), 42);
38 ASSERT_FALSE(CoerceToInt64(Json::Value("foo")).has_value());
39 ASSERT_FALSE(CoerceToInt64(Json::Value("1234!")).has_value());
40
41 Json::UInt64 n = 18446744073709551615UL;
42 ASSERT_EQ(CoerceToInt64(Json::Value{n}).value_or(0), -1);
43 }
44
TEST(JsonTraceUtilsTest,CoerceToTs)45 TEST(JsonTraceUtilsTest, CoerceToTs) {
46 ASSERT_EQ(CoerceToTs(Json::Value(42)).value_or(-1), 42000);
47 ASSERT_EQ(CoerceToTs(Json::Value("42")).value_or(-1), 42000);
48 ASSERT_EQ(CoerceToTs(Json::Value(42.1)).value_or(-1), 42100);
49 ASSERT_EQ(CoerceToTs(Json::Value("42.1")).value_or(-1), 42100);
50 ASSERT_EQ(CoerceToTs(Json::Value(".42")).value_or(-1), 420);
51 ASSERT_EQ(CoerceToTs(Json::Value("42.")).value_or(-1), 42000);
52 ASSERT_EQ(CoerceToTs(Json::Value("42.0")).value_or(-1), 42000);
53 ASSERT_EQ(CoerceToTs(Json::Value("0.2")).value_or(-1), 200);
54 ASSERT_EQ(CoerceToTs(Json::Value("0.2e-1")).value_or(-1), 20);
55 ASSERT_EQ(CoerceToTs(Json::Value("0.2e-2")).value_or(-1), 2);
56 ASSERT_EQ(CoerceToTs(Json::Value("0.2e-3")).value_or(-1), 0);
57 ASSERT_EQ(CoerceToTs(Json::Value("1.692108548132154500e+15")).value_or(-1),
58 1'692'108'548'132'154'500);
59 ASSERT_EQ(CoerceToTs(Json::Value("1692108548132154.500")).value_or(-1),
60 1'692'108'548'132'154'500);
61 ASSERT_EQ(CoerceToTs(Json::Value("1.692108548132154501e+15")).value_or(-1),
62 1'692'108'548'132'154'501);
63 ASSERT_EQ(CoerceToTs(Json::Value("1692108548132154.501")).value_or(-1),
64 1'692'108'548'132'154'501);
65 ASSERT_EQ(CoerceToTs(Json::Value("-1.692108548132154500E+15")).value_or(-1),
66 -1'692'108'548'132'154'500);
67 ASSERT_EQ(CoerceToTs(Json::Value("-1692108548132154.500")).value_or(-1),
68 -1'692'108'548'132'154'500);
69 ASSERT_EQ(CoerceToTs(Json::Value("-1.692108548132154501E+15")).value_or(-1),
70 -1'692'108'548'132'154'501);
71 ASSERT_EQ(CoerceToTs(Json::Value("-1692108548132154.501")).value_or(-1),
72 -1'692'108'548'132'154'501);
73 ASSERT_EQ(CoerceToTs(Json::Value("-0")).value_or(-1), 0);
74 ASSERT_EQ(CoerceToTs(Json::Value("0")).value_or(-1), 0);
75 ASSERT_EQ(CoerceToTs(Json::Value(".")).value_or(-1), 0);
76 ASSERT_FALSE(CoerceToTs(Json::Value("1234!")).has_value());
77 ASSERT_FALSE(CoerceToTs(Json::Value("123e4!")).has_value());
78 }
79
80 } // namespace
81 } // namespace json
82 } // namespace trace_processor
83 } // namespace perfetto
84