Skip to content

Commit 238f924

Browse files
committed
Apply CPPLint to CRT Tests
This one was a bit trickier as there was more usage of dynamic arrays and less safe casts. I've tried to minimise the changes to just those required to passing linting.
1 parent b819364 commit 238f924

File tree

7 files changed

+39
-31
lines changed

7 files changed

+39
-31
lines changed

tests/crt/buffer_write_stream.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
#include <tvm/runtime/crt/rpc_common/frame_buffer.h>
2525
#include <tvm/runtime/crt/rpc_common/write_stream.h>
2626

27+
#include <string>
28+
2729
using ::tvm::runtime::micro_rpc::FrameBuffer;
2830
using ::tvm::runtime::micro_rpc::WriteStream;
2931

@@ -51,7 +53,7 @@ class BufferWriteStream : public WriteStream {
5153

5254
std::string BufferContents() { return std::string((const char*)buffer_data_, buffer_.Size()); }
5355

54-
static constexpr unsigned int capacity() { return N; };
56+
static constexpr unsigned int capacity() { return N; }
5557

5658
private:
5759
bool packet_done_{false};

tests/crt/framing_test.cc

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -150,23 +150,25 @@ TEST_F(UnframerTest, PacketTooLong) {
150150
unframer_.Write(packet_length_bytes, sizeof(packet_length), &bytes_consumed));
151151
EXPECT_EQ(sizeof(packet_length), bytes_consumed);
152152

153-
uint8_t long_payload[decltype(write_stream_)::capacity() + 1];
154-
for (size_t i = 0; i < sizeof(long_payload); i++) {
153+
unsigned int long_payload_len = decltype(write_stream_)::capacity() + 1;
154+
auto long_payload = std::make_unique<uint8_t[]>(long_payload_len);
155+
for (size_t i = 0; i < long_payload_len; i++) {
155156
long_payload[i] = i & 0xff;
156157
if (long_payload[i] == uint8_t(Escape::kEscapeStart)) {
157158
long_payload[i] = 0;
158159
}
159160
}
160-
crc = tvm::runtime::micro_rpc::crc16_compute(long_payload, sizeof(long_payload), &crc);
161+
crc = tvm::runtime::micro_rpc::crc16_compute(long_payload.get(), long_payload_len, &crc);
161162
EXPECT_EQ(kTvmErrorWriteStreamShortWrite,
162-
unframer_.Write(long_payload, sizeof(long_payload), &bytes_consumed));
163+
unframer_.Write(long_payload.get(), long_payload_len, &bytes_consumed));
163164
EXPECT_EQ(write_stream_.capacity(), bytes_consumed);
164165

165-
EXPECT_EQ(kTvmErrorNoError, unframer_.Write((uint8_t*)&crc, sizeof(crc), &bytes_consumed));
166+
EXPECT_EQ(kTvmErrorNoError,
167+
unframer_.Write(reinterpret_cast<uint8_t*>(&crc), sizeof(crc), &bytes_consumed));
166168
EXPECT_EQ(2UL, bytes_consumed); // 2, because framer is now in kFindPacketStart.
167169
EXPECT_FALSE(write_stream_.packet_done());
168170
EXPECT_FALSE(write_stream_.is_valid());
169-
EXPECT_EQ(std::string((char*)long_payload, write_stream_.capacity()),
171+
EXPECT_EQ(std::string(reinterpret_cast<char*>(long_payload.get()), write_stream_.capacity()),
170172
write_stream_.BufferContents());
171173

172174
// Writing a smaller packet directly afterward should work.
@@ -177,7 +179,7 @@ TEST_F(UnframerTest, PacketTooLong) {
177179
EXPECT_TRUE(write_stream_.packet_done());
178180
EXPECT_TRUE(write_stream_.is_valid());
179181
EXPECT_EQ(kPacket1.payload, write_stream_.BufferContents());
180-
};
182+
}
181183

182184
class UnframerTestParameterized : public UnframerTest,
183185
public ::testing::WithParamInterface<const TestPacket*> {};
@@ -297,4 +299,4 @@ TEST_P(UnframerTestParameterized, TestArbitraryPacketReset) {
297299
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
298300
INSTANTIATE_TEST_CASE_P(UnframerTests, UnframerTestParameterized,
299301
::testing::ValuesIn(TestPacket::instances));
300-
#pragma GCC diagnostic pop
302+
#pragma GCC diagnostic pop

tests/crt/func_registry_test.cc

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -178,45 +178,47 @@ TEST(MutableFuncRegistry, Create) {
178178

179179
for (unsigned int rem = 0; rem < kTvmAverageFuncEntrySizeBytes; rem++) {
180180
// test_function name will be used to test overfilling.
181-
char test_function_name[kTvmAverageFunctionNameStrlenBytes + 2 + rem];
181+
auto test_function_name =
182+
std::make_unique<char[]>(kTvmAverageFunctionNameStrlenBytes + 2 + rem);
182183
TVMMutableFuncRegistry reg;
183184
memset(mem_buffer, 0, sizeof(mem_buffer));
184185
EXPECT_EQ(kTvmErrorNoError, TVMMutableFuncRegistry_Create(
185186
&reg, mem_buffer, kTvmAverageFuncEntrySizeBytes * 2 + rem));
186187

187-
snprintf_truncate(test_function_name, kTvmAverageFunctionNameStrlenBytes + 1,
188+
snprintf_truncate(test_function_name.get(), kTvmAverageFunctionNameStrlenBytes + 1,
188189
function_name_chars);
189190

190191
// Add function #1, and verify it can be retrieved.
191-
EXPECT_EQ(kTvmErrorNoError,
192-
TVMMutableFuncRegistry_Set(&reg, test_function_name, TestFunctionHandle(0x01), 0));
192+
EXPECT_EQ(kTvmErrorNoError, TVMMutableFuncRegistry_Set(&reg, test_function_name.get(),
193+
TestFunctionHandle(0x01), 0));
193194

194195
tvm_function_index_t func_index = 100;
195196
EXPECT_EQ(kTvmErrorNoError,
196-
TVMFuncRegistry_Lookup(&reg.registry, test_function_name, &func_index));
197+
TVMFuncRegistry_Lookup(&reg.registry, test_function_name.get(), &func_index));
197198
EXPECT_EQ(func_index, 0);
198199

199200
TVMBackendPackedCFunc func = NULL;
200201
EXPECT_EQ(kTvmErrorNoError, TVMFuncRegistry_GetByIndex(&reg.registry, func_index, &func));
201202
EXPECT_EQ(func, TestFunctionHandle(0x01));
202203

203204
// Ensure that overfilling `names` by 1 char is not allowed.
204-
snprintf_truncate(test_function_name, kTvmAverageFunctionNameStrlenBytes + rem + 2,
205+
snprintf_truncate(test_function_name.get(), kTvmAverageFunctionNameStrlenBytes + rem + 2,
205206
function_name_chars + 1);
206207

207-
EXPECT_EQ(kTvmErrorFunctionRegistryFull,
208-
TVMMutableFuncRegistry_Set(&reg, test_function_name, TestFunctionHandle(0x02), 0));
208+
EXPECT_EQ(
209+
kTvmErrorFunctionRegistryFull,
210+
TVMMutableFuncRegistry_Set(&reg, test_function_name.get(), TestFunctionHandle(0x02), 0));
209211
EXPECT_EQ(kTvmErrorFunctionNameNotFound,
210-
TVMFuncRegistry_Lookup(&reg.registry, test_function_name, &func_index));
212+
TVMFuncRegistry_Lookup(&reg.registry, test_function_name.get(), &func_index));
211213

212214
// Add function #2, with intentionally short (by 2 char) name. Verify it can be retrieved.
213-
snprintf_truncate(test_function_name, kTvmAverageFunctionNameStrlenBytes - 2 + 1,
215+
snprintf_truncate(test_function_name.get(), kTvmAverageFunctionNameStrlenBytes - 2 + 1,
214216
function_name_chars + 1);
215-
EXPECT_EQ(kTvmErrorNoError,
216-
TVMMutableFuncRegistry_Set(&reg, test_function_name, TestFunctionHandle(0x02), 0));
217+
EXPECT_EQ(kTvmErrorNoError, TVMMutableFuncRegistry_Set(&reg, test_function_name.get(),
218+
TestFunctionHandle(0x02), 0));
217219

218220
EXPECT_EQ(kTvmErrorNoError,
219-
TVMFuncRegistry_Lookup(&reg.registry, test_function_name, &func_index));
221+
TVMFuncRegistry_Lookup(&reg.registry, test_function_name.get(), &func_index));
220222
EXPECT_EQ(func_index, 1);
221223

222224
func = NULL;
@@ -226,7 +228,8 @@ TEST(MutableFuncRegistry, Create) {
226228
// Try adding another function, which should fail due to lack of function pointers.
227229
test_function_name[0] = 'a';
228230
test_function_name[1] = 0;
229-
EXPECT_EQ(kTvmErrorFunctionRegistryFull,
230-
TVMMutableFuncRegistry_Set(&reg, test_function_name, TestFunctionHandle(0x03), 0));
231+
EXPECT_EQ(
232+
kTvmErrorFunctionRegistryFull,
233+
TVMMutableFuncRegistry_Set(&reg, test_function_name.get(), TestFunctionHandle(0x03), 0));
231234
}
232-
}
235+
}

tests/crt/page_allocator_test.cc

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,10 @@ class PageAllocatorTest : public ::testing::Test {
3636
protected:
3737
void SetUp() override {
3838
memset(raw_memory_pool, 0, sizeof(raw_memory_pool));
39-
memory_pool = (uint8_t*)(ROUND_UP(((uintptr_t)raw_memory_pool), (1 << kPageSizeBytesLog)));
39+
memory_pool = reinterpret_cast<uint8_t*>(
40+
ROUND_UP(((uintptr_t)raw_memory_pool), (1 << kPageSizeBytesLog)));
4041
PageMemoryManagerCreate(&interface, memory_pool, kMemoryPoolSizeBytes, kPageSizeBytesLog);
41-
mgr = (MemoryManager*)interface;
42+
mgr = reinterpret_cast<MemoryManager*>(interface);
4243
ASSERT_EQ(kNumUsablePages, mgr->ptable.max_pages);
4344
dev_ = {kDLCPU, 0};
4445
}

tests/crt/session_test.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ class ReceivedMessage {
5151

5252
class TestSession {
5353
public:
54-
TestSession(uint8_t initial_nonce)
54+
explicit TestSession(uint8_t initial_nonce)
5555
: framer{&framer_write_stream},
5656
receive_buffer{receive_buffer_array, sizeof(receive_buffer_array)},
5757
sess{&framer, &receive_buffer, TestSessionMessageReceivedThunk, this},
@@ -247,4 +247,4 @@ TEST_F(SessionTest, DoubleStart) {
247247
bob_.ClearBuffers();
248248
alice_.WriteTo(&bob_);
249249
EXPECT_TRUE(bob_.sess.IsEstablished());
250-
}
250+
}

tests/crt/stack_allocator_test.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,4 +198,4 @@ TEST(StackAllocatorTest, InitialMemoryMisAlignment) {
198198

199199
ASSERT_EQ(tvm_runtime_workspace.next_alloc, &model_memory_ptr[alignment_offset]);
200200
ASSERT_EQ(tvm_runtime_workspace.workspace_size, sizeof(model_memory) - offset - alignment_offset);
201-
}
201+
}

tests/lint/cpplint.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,4 @@ python3 3rdparty/dmlc-core/scripts/lint.py vta cpp vta/include vta/src
2121
python3 3rdparty/dmlc-core/scripts/lint.py tvm cpp \
2222
include src \
2323
examples/extension/src examples/graph_executor/src \
24-
tests/cpp
24+
tests/cpp tests/crt

0 commit comments

Comments
 (0)