From 485ece06600a2946f5a122a4e55569483b6789f8 Mon Sep 17 00:00:00 2001 From: Chris Sidebottom Date: Wed, 25 Aug 2021 10:02:28 +0000 Subject: [PATCH] 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. --- tests/crt/buffer_write_stream.h | 4 +++- tests/crt/framing_test.cc | 18 +++++++++------- tests/crt/func_registry_test.cc | 35 +++++++++++++++++-------------- tests/crt/page_allocator_test.cc | 5 +++-- tests/crt/session_test.cc | 4 ++-- tests/crt/stack_allocator_test.cc | 2 +- tests/lint/cpplint.sh | 2 +- 7 files changed, 39 insertions(+), 31 deletions(-) diff --git a/tests/crt/buffer_write_stream.h b/tests/crt/buffer_write_stream.h index 66ef044e6ba1..48a30ac4b273 100644 --- a/tests/crt/buffer_write_stream.h +++ b/tests/crt/buffer_write_stream.h @@ -24,6 +24,8 @@ #include #include +#include + using ::tvm::runtime::micro_rpc::FrameBuffer; using ::tvm::runtime::micro_rpc::WriteStream; @@ -51,7 +53,7 @@ class BufferWriteStream : public WriteStream { std::string BufferContents() { return std::string((const char*)buffer_data_, buffer_.Size()); } - static constexpr unsigned int capacity() { return N; }; + static constexpr unsigned int capacity() { return N; } private: bool packet_done_{false}; diff --git a/tests/crt/framing_test.cc b/tests/crt/framing_test.cc index c6dd2c098dd0..e257dfc641ab 100644 --- a/tests/crt/framing_test.cc +++ b/tests/crt/framing_test.cc @@ -150,23 +150,25 @@ TEST_F(UnframerTest, PacketTooLong) { unframer_.Write(packet_length_bytes, sizeof(packet_length), &bytes_consumed)); EXPECT_EQ(sizeof(packet_length), bytes_consumed); - uint8_t long_payload[decltype(write_stream_)::capacity() + 1]; - for (size_t i = 0; i < sizeof(long_payload); i++) { + unsigned int long_payload_len = decltype(write_stream_)::capacity() + 1; + auto long_payload = std::make_unique(long_payload_len); + for (size_t i = 0; i < long_payload_len; i++) { long_payload[i] = i & 0xff; if (long_payload[i] == uint8_t(Escape::kEscapeStart)) { long_payload[i] = 0; } } - crc = tvm::runtime::micro_rpc::crc16_compute(long_payload, sizeof(long_payload), &crc); + crc = tvm::runtime::micro_rpc::crc16_compute(long_payload.get(), long_payload_len, &crc); EXPECT_EQ(kTvmErrorWriteStreamShortWrite, - unframer_.Write(long_payload, sizeof(long_payload), &bytes_consumed)); + unframer_.Write(long_payload.get(), long_payload_len, &bytes_consumed)); EXPECT_EQ(write_stream_.capacity(), bytes_consumed); - EXPECT_EQ(kTvmErrorNoError, unframer_.Write((uint8_t*)&crc, sizeof(crc), &bytes_consumed)); + EXPECT_EQ(kTvmErrorNoError, + unframer_.Write(reinterpret_cast(&crc), sizeof(crc), &bytes_consumed)); EXPECT_EQ(2UL, bytes_consumed); // 2, because framer is now in kFindPacketStart. EXPECT_FALSE(write_stream_.packet_done()); EXPECT_FALSE(write_stream_.is_valid()); - EXPECT_EQ(std::string((char*)long_payload, write_stream_.capacity()), + EXPECT_EQ(std::string(reinterpret_cast(long_payload.get()), write_stream_.capacity()), write_stream_.BufferContents()); // Writing a smaller packet directly afterward should work. @@ -177,7 +179,7 @@ TEST_F(UnframerTest, PacketTooLong) { EXPECT_TRUE(write_stream_.packet_done()); EXPECT_TRUE(write_stream_.is_valid()); EXPECT_EQ(kPacket1.payload, write_stream_.BufferContents()); -}; +} class UnframerTestParameterized : public UnframerTest, public ::testing::WithParamInterface {}; @@ -297,4 +299,4 @@ TEST_P(UnframerTestParameterized, TestArbitraryPacketReset) { #pragma GCC diagnostic ignored "-Wdeprecated-declarations" INSTANTIATE_TEST_CASE_P(UnframerTests, UnframerTestParameterized, ::testing::ValuesIn(TestPacket::instances)); -#pragma GCC diagnostic pop \ No newline at end of file +#pragma GCC diagnostic pop diff --git a/tests/crt/func_registry_test.cc b/tests/crt/func_registry_test.cc index 7a40c27f6765..9f0e7f8d1a5a 100644 --- a/tests/crt/func_registry_test.cc +++ b/tests/crt/func_registry_test.cc @@ -178,22 +178,23 @@ TEST(MutableFuncRegistry, Create) { for (unsigned int rem = 0; rem < kTvmAverageFuncEntrySizeBytes; rem++) { // test_function name will be used to test overfilling. - char test_function_name[kTvmAverageFunctionNameStrlenBytes + 2 + rem]; + auto test_function_name = + std::make_unique(kTvmAverageFunctionNameStrlenBytes + 2 + rem); TVMMutableFuncRegistry reg; memset(mem_buffer, 0, sizeof(mem_buffer)); EXPECT_EQ(kTvmErrorNoError, TVMMutableFuncRegistry_Create( ®, mem_buffer, kTvmAverageFuncEntrySizeBytes * 2 + rem)); - snprintf_truncate(test_function_name, kTvmAverageFunctionNameStrlenBytes + 1, + snprintf_truncate(test_function_name.get(), kTvmAverageFunctionNameStrlenBytes + 1, function_name_chars); // Add function #1, and verify it can be retrieved. - EXPECT_EQ(kTvmErrorNoError, - TVMMutableFuncRegistry_Set(®, test_function_name, TestFunctionHandle(0x01), 0)); + EXPECT_EQ(kTvmErrorNoError, TVMMutableFuncRegistry_Set(®, test_function_name.get(), + TestFunctionHandle(0x01), 0)); tvm_function_index_t func_index = 100; EXPECT_EQ(kTvmErrorNoError, - TVMFuncRegistry_Lookup(®.registry, test_function_name, &func_index)); + TVMFuncRegistry_Lookup(®.registry, test_function_name.get(), &func_index)); EXPECT_EQ(func_index, 0); TVMBackendPackedCFunc func = NULL; @@ -201,22 +202,23 @@ TEST(MutableFuncRegistry, Create) { EXPECT_EQ(func, TestFunctionHandle(0x01)); // Ensure that overfilling `names` by 1 char is not allowed. - snprintf_truncate(test_function_name, kTvmAverageFunctionNameStrlenBytes + rem + 2, + snprintf_truncate(test_function_name.get(), kTvmAverageFunctionNameStrlenBytes + rem + 2, function_name_chars + 1); - EXPECT_EQ(kTvmErrorFunctionRegistryFull, - TVMMutableFuncRegistry_Set(®, test_function_name, TestFunctionHandle(0x02), 0)); + EXPECT_EQ( + kTvmErrorFunctionRegistryFull, + TVMMutableFuncRegistry_Set(®, test_function_name.get(), TestFunctionHandle(0x02), 0)); EXPECT_EQ(kTvmErrorFunctionNameNotFound, - TVMFuncRegistry_Lookup(®.registry, test_function_name, &func_index)); + TVMFuncRegistry_Lookup(®.registry, test_function_name.get(), &func_index)); // Add function #2, with intentionally short (by 2 char) name. Verify it can be retrieved. - snprintf_truncate(test_function_name, kTvmAverageFunctionNameStrlenBytes - 2 + 1, + snprintf_truncate(test_function_name.get(), kTvmAverageFunctionNameStrlenBytes - 2 + 1, function_name_chars + 1); - EXPECT_EQ(kTvmErrorNoError, - TVMMutableFuncRegistry_Set(®, test_function_name, TestFunctionHandle(0x02), 0)); + EXPECT_EQ(kTvmErrorNoError, TVMMutableFuncRegistry_Set(®, test_function_name.get(), + TestFunctionHandle(0x02), 0)); EXPECT_EQ(kTvmErrorNoError, - TVMFuncRegistry_Lookup(®.registry, test_function_name, &func_index)); + TVMFuncRegistry_Lookup(®.registry, test_function_name.get(), &func_index)); EXPECT_EQ(func_index, 1); func = NULL; @@ -226,7 +228,8 @@ TEST(MutableFuncRegistry, Create) { // Try adding another function, which should fail due to lack of function pointers. test_function_name[0] = 'a'; test_function_name[1] = 0; - EXPECT_EQ(kTvmErrorFunctionRegistryFull, - TVMMutableFuncRegistry_Set(®, test_function_name, TestFunctionHandle(0x03), 0)); + EXPECT_EQ( + kTvmErrorFunctionRegistryFull, + TVMMutableFuncRegistry_Set(®, test_function_name.get(), TestFunctionHandle(0x03), 0)); } -} \ No newline at end of file +} diff --git a/tests/crt/page_allocator_test.cc b/tests/crt/page_allocator_test.cc index 3963569c5972..924bf295ffd2 100644 --- a/tests/crt/page_allocator_test.cc +++ b/tests/crt/page_allocator_test.cc @@ -36,9 +36,10 @@ class PageAllocatorTest : public ::testing::Test { protected: void SetUp() override { memset(raw_memory_pool, 0, sizeof(raw_memory_pool)); - memory_pool = (uint8_t*)(ROUND_UP(((uintptr_t)raw_memory_pool), (1 << kPageSizeBytesLog))); + memory_pool = reinterpret_cast( + ROUND_UP(((uintptr_t)raw_memory_pool), (1 << kPageSizeBytesLog))); PageMemoryManagerCreate(&interface, memory_pool, kMemoryPoolSizeBytes, kPageSizeBytesLog); - mgr = (MemoryManager*)interface; + mgr = reinterpret_cast(interface); ASSERT_EQ(kNumUsablePages, mgr->ptable.max_pages); dev_ = {kDLCPU, 0}; } diff --git a/tests/crt/session_test.cc b/tests/crt/session_test.cc index 48d7475334c2..b6b58e819700 100644 --- a/tests/crt/session_test.cc +++ b/tests/crt/session_test.cc @@ -51,7 +51,7 @@ class ReceivedMessage { class TestSession { public: - TestSession(uint8_t initial_nonce) + explicit TestSession(uint8_t initial_nonce) : framer{&framer_write_stream}, receive_buffer{receive_buffer_array, sizeof(receive_buffer_array)}, sess{&framer, &receive_buffer, TestSessionMessageReceivedThunk, this}, @@ -247,4 +247,4 @@ TEST_F(SessionTest, DoubleStart) { bob_.ClearBuffers(); alice_.WriteTo(&bob_); EXPECT_TRUE(bob_.sess.IsEstablished()); -} \ No newline at end of file +} diff --git a/tests/crt/stack_allocator_test.cc b/tests/crt/stack_allocator_test.cc index 0eae62e92aea..cd0c4a8b65e2 100644 --- a/tests/crt/stack_allocator_test.cc +++ b/tests/crt/stack_allocator_test.cc @@ -198,4 +198,4 @@ TEST(StackAllocatorTest, InitialMemoryMisAlignment) { ASSERT_EQ(tvm_runtime_workspace.next_alloc, &model_memory_ptr[alignment_offset]); ASSERT_EQ(tvm_runtime_workspace.workspace_size, sizeof(model_memory) - offset - alignment_offset); -} \ No newline at end of file +} diff --git a/tests/lint/cpplint.sh b/tests/lint/cpplint.sh index fccd0a23bd63..31eb1d94a347 100755 --- a/tests/lint/cpplint.sh +++ b/tests/lint/cpplint.sh @@ -21,4 +21,4 @@ python3 3rdparty/dmlc-core/scripts/lint.py vta cpp vta/include vta/src python3 3rdparty/dmlc-core/scripts/lint.py tvm cpp \ include src \ examples/extension/src examples/graph_executor/src \ - tests/cpp + tests/cpp tests/crt