1 //===-- ProcessorTraceMonitorTest.cpp ------------------------- -*- C++ -*-===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 #include "gtest/gtest.h"
11 #include "ProcessorTrace.h"
12 #include "llvm/ADT/ArrayRef.h"
15 using namespace lldb_private
;
16 using namespace process_linux
;
18 size_t ReadCylicBufferWrapper(void *buf
, size_t buf_size
, void *cyc_buf
,
19 size_t cyc_buf_size
, size_t cyc_start
,
21 llvm::MutableArrayRef
<uint8_t> dst(reinterpret_cast<uint8_t *>(buf
),
23 llvm::MutableArrayRef
<uint8_t> src(reinterpret_cast<uint8_t *>(cyc_buf
),
25 ProcessorTraceMonitor::ReadCyclicBuffer(dst
, src
, cyc_start
, offset
);
29 TEST(CyclicBuffer
, EdgeCases
) {
31 uint8_t cyclic_buffer
[6] = {'l', 'i', 'c', 'c', 'y', 'c'};
33 // We will always leave the last bytes untouched
34 // so that string comparisions work.
35 char smaller_buffer
[4] = {};
37 // empty buffer to read into
38 bytes_read
= ReadCylicBufferWrapper(smaller_buffer
, 0, cyclic_buffer
,
39 sizeof(cyclic_buffer
), 3, 0);
40 ASSERT_EQ(0u, bytes_read
);
42 // empty cyclic buffer
43 bytes_read
= ReadCylicBufferWrapper(smaller_buffer
, sizeof(smaller_buffer
),
44 cyclic_buffer
, 0, 3, 0);
45 ASSERT_EQ(0u, bytes_read
);
49 ReadCylicBufferWrapper(smaller_buffer
, sizeof(smaller_buffer
),
50 cyclic_buffer
, sizeof(cyclic_buffer
), 3, 6);
51 ASSERT_EQ(0u, bytes_read
);
55 ReadCylicBufferWrapper(smaller_buffer
, sizeof(smaller_buffer
),
56 cyclic_buffer
, sizeof(cyclic_buffer
), 3, 7);
57 ASSERT_EQ(0u, bytes_read
);
61 ReadCylicBufferWrapper(smaller_buffer
, sizeof(smaller_buffer
),
62 cyclic_buffer
, sizeof(cyclic_buffer
), 3, 7);
63 ASSERT_EQ(0u, bytes_read
);
66 TEST(CyclicBuffer
, EqualSizeBuffer
) {
67 size_t bytes_read
= 0;
68 uint8_t cyclic_buffer
[6] = {'l', 'i', 'c', 'c', 'y', 'c'};
70 char cyclic
[] = "cyclic";
71 for (size_t i
= 0; i
< sizeof(cyclic
); i
++) {
72 // We will always leave the last bytes untouched
73 // so that string comparisions work.
74 char equal_size_buffer
[7] = {};
76 ReadCylicBufferWrapper(equal_size_buffer
, sizeof(cyclic_buffer
),
77 cyclic_buffer
, sizeof(cyclic_buffer
), 3, i
);
78 ASSERT_EQ((sizeof(cyclic
) - i
- 1), bytes_read
);
79 ASSERT_STREQ(equal_size_buffer
, (cyclic
+ i
));
83 TEST(CyclicBuffer
, SmallerSizeBuffer
) {
85 uint8_t cyclic_buffer
[6] = {'l', 'i', 'c', 'c', 'y', 'c'};
87 // We will always leave the last bytes untouched
88 // so that string comparisions work.
89 char smaller_buffer
[4] = {};
91 ReadCylicBufferWrapper(smaller_buffer
, (sizeof(smaller_buffer
) - 1),
92 cyclic_buffer
, sizeof(cyclic_buffer
), 3, 0);
93 ASSERT_EQ(3u, bytes_read
);
94 ASSERT_STREQ(smaller_buffer
, "cyc");
97 ReadCylicBufferWrapper(smaller_buffer
, (sizeof(smaller_buffer
) - 1),
98 cyclic_buffer
, sizeof(cyclic_buffer
), 3, 1);
99 ASSERT_EQ(3u, bytes_read
);
100 ASSERT_STREQ(smaller_buffer
, "ycl");
103 ReadCylicBufferWrapper(smaller_buffer
, (sizeof(smaller_buffer
) - 1),
104 cyclic_buffer
, sizeof(cyclic_buffer
), 3, 2);
105 ASSERT_EQ(3u, bytes_read
);
106 ASSERT_STREQ(smaller_buffer
, "cli");
109 ReadCylicBufferWrapper(smaller_buffer
, (sizeof(smaller_buffer
) - 1),
110 cyclic_buffer
, sizeof(cyclic_buffer
), 3, 3);
111 ASSERT_EQ(3u, bytes_read
);
112 ASSERT_STREQ(smaller_buffer
, "lic");
115 char smaller_buffer
[4] = {};
117 ReadCylicBufferWrapper(smaller_buffer
, (sizeof(smaller_buffer
) - 1),
118 cyclic_buffer
, sizeof(cyclic_buffer
), 3, 4);
119 ASSERT_EQ(2u, bytes_read
);
120 ASSERT_STREQ(smaller_buffer
, "ic");
123 char smaller_buffer
[4] = {};
125 ReadCylicBufferWrapper(smaller_buffer
, (sizeof(smaller_buffer
) - 1),
126 cyclic_buffer
, sizeof(cyclic_buffer
), 3, 5);
127 ASSERT_EQ(1u, bytes_read
);
128 ASSERT_STREQ(smaller_buffer
, "c");
132 TEST(CyclicBuffer
, BiggerSizeBuffer
) {
133 size_t bytes_read
= 0;
134 uint8_t cyclic_buffer
[6] = {'l', 'i', 'c', 'c', 'y', 'c'};
136 char cyclic
[] = "cyclic";
137 for (size_t i
= 0; i
< sizeof(cyclic
); i
++) {
138 // We will always leave the last bytes untouched
139 // so that string comparisions work.
140 char bigger_buffer
[10] = {};
142 ReadCylicBufferWrapper(bigger_buffer
, (sizeof(bigger_buffer
) - 1),
143 cyclic_buffer
, sizeof(cyclic_buffer
), 3, i
);
144 ASSERT_EQ((sizeof(cyclic
) - i
- 1), bytes_read
);
145 ASSERT_STREQ(bigger_buffer
, (cyclic
+ i
));