From 77b67a8137b3e29f6aefb1e341a1c07ccaea78c8 Mon Sep 17 00:00:00 2001 From: Sebastian Messmer Date: Sat, 25 Apr 2015 17:17:06 +0200 Subject: [PATCH] Reduce use of memcmp and use operator==(Data, Data) instead --- test/implementations/encrypted/CipherTest.cpp | 5 ++--- .../ondisk/OnDiskBlockTest/OnDiskBlockCreateTest.cpp | 2 +- .../ondisk/OnDiskBlockTest/OnDiskBlockFlushTest.cpp | 11 +++-------- test/testutils/BlockStoreTest_Data.h | 7 +------ 4 files changed, 7 insertions(+), 18 deletions(-) diff --git a/test/implementations/encrypted/CipherTest.cpp b/test/implementations/encrypted/CipherTest.cpp index 620e60db..609a2717 100644 --- a/test/implementations/encrypted/CipherTest.cpp +++ b/test/implementations/encrypted/CipherTest.cpp @@ -26,14 +26,13 @@ public: void CheckEncryptThenDecryptIsIdentity(const Data &plaintext) { Data ciphertext = Encrypt(plaintext); Data decrypted = Decrypt(ciphertext); - EXPECT_EQ(plaintext.size(), decrypted.size()); - EXPECT_EQ(0, std::memcmp(plaintext.data(), decrypted.data(), plaintext.size())); + EXPECT_EQ(plaintext, decrypted); } void CheckEncryptIsIndeterministic(const Data &plaintext) { Data ciphertext = Encrypt(plaintext); Data ciphertext2 = Encrypt(plaintext); - EXPECT_NE(0, std::memcmp(ciphertext.data(), ciphertext2.data(), ciphertext.size())); + EXPECT_NE(ciphertext, ciphertext2); } void CheckEncryptedSize(const Data &plaintext) { diff --git a/test/implementations/ondisk/OnDiskBlockTest/OnDiskBlockCreateTest.cpp b/test/implementations/ondisk/OnDiskBlockTest/OnDiskBlockCreateTest.cpp index 0a36ebc4..5f1fe07d 100644 --- a/test/implementations/ondisk/OnDiskBlockTest/OnDiskBlockCreateTest.cpp +++ b/test/implementations/ondisk/OnDiskBlockTest/OnDiskBlockCreateTest.cpp @@ -69,7 +69,7 @@ TEST_P(OnDiskBlockCreateSizeTest, OnDiskSizeIsCorrect) { TEST_P(OnDiskBlockCreateSizeTest, OnDiskBlockIsZeroedOut) { Data fileContent = Data::LoadFromFile(file.path()).value(); - EXPECT_EQ(0, std::memcmp(ZEROES.data(), fileContent.data(), fileContent.size())); + EXPECT_EQ(ZEROES, fileContent); } // This test is also tested by OnDiskBlockStoreTest, but there the block is created using the BlockStore interface. diff --git a/test/implementations/ondisk/OnDiskBlockTest/OnDiskBlockFlushTest.cpp b/test/implementations/ondisk/OnDiskBlockTest/OnDiskBlockFlushTest.cpp index 2ef0b2a2..f0638639 100644 --- a/test/implementations/ondisk/OnDiskBlockTest/OnDiskBlockFlushTest.cpp +++ b/test/implementations/ondisk/OnDiskBlockTest/OnDiskBlockFlushTest.cpp @@ -38,17 +38,13 @@ public: unique_ptr CreateBlockAndLoadItFromDisk() { { - Data data(randomData.size()); - std::memcpy(data.data(), randomData.data(), randomData.size()); - auto block = OnDiskBlock::CreateOnDisk(dir.path(), key, std::move(data)); + OnDiskBlock::CreateOnDisk(dir.path(), key, randomData.copy()); } return OnDiskBlock::LoadFromDisk(dir.path(), key); } unique_ptr CreateBlock() { - Data data(randomData.size()); - std::memcpy(data.data(), randomData.data(), randomData.size()); - return OnDiskBlock::CreateOnDisk(dir.path(), key, std::move(data)); + return OnDiskBlock::CreateOnDisk(dir.path(), key, randomData.copy()); } void WriteDataToBlock(const unique_ptr &block) { @@ -62,8 +58,7 @@ public: void EXPECT_STORED_FILE_DATA_CORRECT() { Data actual = Data::LoadFromFile(file.path()).value(); - EXPECT_EQ(randomData.size(), actual.size()); - EXPECT_EQ(0, std::memcmp(randomData.data(), actual.data(), randomData.size())); + EXPECT_EQ(randomData, actual); } }; INSTANTIATE_TEST_CASE_P(OnDiskBlockFlushTest, OnDiskBlockFlushTest, Values((size_t)0, (size_t)1, (size_t)1024, (size_t)4096, (size_t)10*1024*1024)); diff --git a/test/testutils/BlockStoreTest_Data.h b/test/testutils/BlockStoreTest_Data.h index daac33d8..202c8b32 100644 --- a/test/testutils/BlockStoreTest_Data.h +++ b/test/testutils/BlockStoreTest_Data.h @@ -46,11 +46,6 @@ private: cpputils::Data foregroundData; cpputils::Data backgroundData; - void EXPECT_DATA_EQ(const cpputils::Data &expected, const cpputils::Data &actual) { - EXPECT_EQ(expected.size(), actual.size()); - EXPECT_EQ(0, std::memcmp(expected.data(), actual.data(), expected.size())); - } - blockstore::Key CreateBlockWriteToItAndReturnKey(const cpputils::Data &to_write) { auto newblock = blockStore->create(cpputils::Data(testData.blocksize).FillWithZeroes()); @@ -61,7 +56,7 @@ private: void EXPECT_DATA_READS_AS(const cpputils::Data &expected, const blockstore::Block &block, off_t offset, size_t count) { cpputils::Data read(count); std::memcpy(read.data(), (uint8_t*)block.data() + offset, count); - EXPECT_DATA_EQ(expected, read); + EXPECT_EQ(expected, read); } void EXPECT_DATA_READS_AS_OUTSIDE_OF(const cpputils::Data &expected, const blockstore::Block &block, off_t start, size_t count) {