1 /*
2 * Copyright (C) 2023 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/perf/reader.h"
18
19 #include <stddef.h>
20 #include <cstdint>
21
22 #include "perfetto/trace_processor/trace_blob.h"
23 #include "perfetto/trace_processor/trace_blob_view.h"
24 #include "test/gtest_and_gmock.h"
25
26 namespace perfetto::trace_processor::perf_importer {
27 namespace {
28
29 using ::testing::ElementsAre;
30 using ::testing::Eq;
31 using ::testing::SizeIs;
32
33 template <typename T>
TraceBlobViewFromVector(std::vector<T> nums)34 TraceBlobView TraceBlobViewFromVector(std::vector<T> nums) {
35 size_t data_size = sizeof(T) * nums.size();
36 auto blob = TraceBlob::Allocate(data_size);
37 memcpy(blob.data(), nums.data(), data_size);
38 return TraceBlobView(std::move(blob));
39 }
40
TEST(ReaderUnittest,Read)41 TEST(ReaderUnittest, Read) {
42 TraceBlobView tbv = TraceBlobViewFromVector(std::vector<uint64_t>{2, 4, 8});
43 Reader reader(std::move(tbv));
44 uint64_t val;
45 reader.Read(val);
46 EXPECT_EQ(val, 2u);
47 }
48
TEST(ReaderUnittest,ReadOptional)49 TEST(ReaderUnittest, ReadOptional) {
50 TraceBlobView tbv = TraceBlobViewFromVector(std::vector<uint64_t>{2, 4, 8});
51 Reader reader(std::move(tbv));
52 std::optional<uint64_t> val;
53 reader.ReadOptional(val);
54 EXPECT_EQ(val, 2u);
55 }
56
TEST(ReaderUnittest,ReadVector)57 TEST(ReaderUnittest, ReadVector) {
58 TraceBlobView tbv =
59 TraceBlobViewFromVector(std::vector<uint64_t>{2, 4, 8, 16, 32});
60 Reader reader(std::move(tbv));
61
62 std::vector<uint64_t> res(3);
63 reader.ReadVector(res);
64
65 std::vector<uint64_t> valid{2, 4, 8};
66 EXPECT_EQ(res, valid);
67 }
68
TEST(ReaderUnittest,Skip)69 TEST(ReaderUnittest, Skip) {
70 TraceBlobView tbv = TraceBlobViewFromVector(std::vector<uint64_t>{2, 4, 8});
71 Reader reader(std::move(tbv));
72
73 reader.Skip<uint64_t>();
74
75 uint64_t val;
76 reader.Read(val);
77 EXPECT_EQ(val, 4u);
78 }
79
80 } // namespace
81 } // namespace perfetto::trace_processor::perf_importer
82