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(TimeUnit::kUs, Json::Value(42)).value_or(-1), 42000);
47 ASSERT_EQ(CoerceToTs(TimeUnit::kUs, Json::Value("42")).value_or(-1), 42000);
48 ASSERT_EQ(CoerceToTs(TimeUnit::kUs, Json::Value(42.1)).value_or(-1), 42100);
49 ASSERT_EQ(CoerceToTs(TimeUnit::kUs, Json::Value("42.1")).value_or(-1), 42100);
50 ASSERT_EQ(CoerceToTs(TimeUnit::kUs, Json::Value(".42")).value_or(-1), 420);
51 ASSERT_EQ(CoerceToTs(TimeUnit::kUs, Json::Value("42.")).value_or(-1), 42000);
52 ASSERT_EQ(CoerceToTs(TimeUnit::kUs, Json::Value("42.0")).value_or(-1), 42000);
53 ASSERT_EQ(CoerceToTs(TimeUnit::kUs, Json::Value("0.2")).value_or(-1), 200);
54 ASSERT_EQ(CoerceToTs(TimeUnit::kUs, Json::Value("0.2e-1")).value_or(-1), 20);
55 ASSERT_EQ(CoerceToTs(TimeUnit::kNs, Json::Value(42)).value_or(-1), 42);
56 ASSERT_EQ(CoerceToTs(TimeUnit::kMs, Json::Value(42)).value_or(-1), 42000000);
57 ASSERT_EQ(CoerceToTs(TimeUnit::kUs, Json::Value(".")).value_or(-1), 0);
58 ASSERT_FALSE(CoerceToTs(TimeUnit::kNs, Json::Value("foo")).has_value());
59 ASSERT_FALSE(CoerceToTs(TimeUnit::kNs, Json::Value(".foo")).has_value());
60 ASSERT_FALSE(CoerceToTs(TimeUnit::kNs, Json::Value("0.foo")).has_value());
61 ASSERT_FALSE(CoerceToTs(TimeUnit::kNs, Json::Value("foo0.23")).has_value());
62 ASSERT_FALSE(CoerceToTs(TimeUnit::kNs, Json::Value("23.12foo")).has_value());
63 ASSERT_FALSE(CoerceToTs(TimeUnit::kNs, Json::Value("1234!")).has_value());
64 ASSERT_FALSE(CoerceToTs(TimeUnit::kUs, Json::Value("1234!")).has_value());
65 ASSERT_FALSE(CoerceToTs(TimeUnit::kMs, Json::Value("1234!")).has_value());
66 }
67
68 } // namespace
69 } // namespace json
70 } // namespace trace_processor
71 } // namespace perfetto
72