Skip to content

Commit

Permalink
Refactor tests: use a vector instead of a basic array, for automatic …
Browse files Browse the repository at this point in the history
…deallocation.
  • Loading branch information
cedric-le-cam committed Jul 19, 2024
1 parent c853d6c commit f860211
Showing 1 changed file with 28 additions and 30 deletions.
58 changes: 28 additions & 30 deletions test/basic_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1078,15 +1078,16 @@ TEST_F(GCSDriverTestFixture, Read_NFiles_NoCommonHeader)
ReadSimulatorParams mock_read_params_1{ mock_content_1, mock_size_1, &mock_offset_1 };
ReadSimulatorParams mock_read_params_2{ mock_content_2, mock_size_2, &mock_offset_2 };

char* buff = new char[2 * static_cast<size_t>(filesize)] {};
std::vector<char> buff(2 * static_cast<size_t>(filesize));
char* buff_data = buff.data();

constexpr size_t size{ sizeof(uint8_t) };

// read 1 byte from start of an intermediate file
test_reader->Reader().offset_ = cast_mock_size_0;

EXPECT_CALL(*mock_client, ReadObject).WillOnce(READ_MOCK_LAMBDA(GenerateReadSimulator(mock_read_params_1)));
EXPECT_EQ(driver_fread(buff, size, 1, test_reader), 1);
EXPECT_EQ(driver_fread(buff_data, size, 1, test_reader), 1);
EXPECT_EQ(test_reader->Reader().offset_, cast_mock_size_0 + 1);
EXPECT_EQ(buff[0], mock_read_params_1.content[0]);

Expand All @@ -1098,7 +1099,7 @@ TEST_F(GCSDriverTestFixture, Read_NFiles_NoCommonHeader)
EXPECT_CALL(*mock_client, ReadObject)
.WillOnce(READ_MOCK_LAMBDA(GenerateReadSimulator(mock_read_params_0)))
.WillOnce(READ_MOCK_LAMBDA(GenerateReadSimulator(mock_read_params_1)));
EXPECT_EQ(driver_fread(buff, size, 2, test_reader), 2);
EXPECT_EQ(driver_fread(buff_data, size, 2, test_reader), 2);
EXPECT_EQ(test_reader->Reader().offset_, cast_mock_size_0 + 1);
EXPECT_EQ(buff[0], mock_content_0[mock_size_0 - 1]);
EXPECT_EQ(buff[1], mock_content_1[0]);
Expand All @@ -1113,10 +1114,10 @@ TEST_F(GCSDriverTestFixture, Read_NFiles_NoCommonHeader)
EXPECT_CALL(*mock_client, ReadObject)
.WillOnce(READ_MOCK_LAMBDA(GenerateReadSimulator(mock_read_params_0)))
.WillOnce(READ_MOCK_LAMBDA(GenerateReadSimulator(mock_read_params_1)));
EXPECT_EQ(driver_fread(buff, size, static_cast<size_t>(to_read), test_reader), to_read);
EXPECT_EQ(driver_fread(buff_data, size, static_cast<size_t>(to_read), test_reader), to_read);
EXPECT_EQ(test_reader->Reader().offset_, to_read);
EXPECT_EQ(std::strncmp(buff, mock_content_0, mock_size_0), 0);
EXPECT_EQ(std::strncmp(buff + mock_size_0, mock_content_1, mock_size_1 - 1), 0);
EXPECT_EQ(std::strncmp(buff_data, mock_content_0, mock_size_0), 0);
EXPECT_EQ(std::strncmp(buff_data + mock_size_0, mock_content_1, mock_size_1 - 1), 0);


// tests reading the whole file
Expand All @@ -1131,21 +1132,19 @@ TEST_F(GCSDriverTestFixture, Read_NFiles_NoCommonHeader)
.WillOnce(READ_MOCK_LAMBDA(GenerateReadSimulator(mock_read_params_0)))
.WillOnce(READ_MOCK_LAMBDA(GenerateReadSimulator(mock_read_params_1)))
.WillOnce(READ_MOCK_LAMBDA(GenerateReadSimulator(mock_read_params_2)));
EXPECT_EQ(driver_fread(buff, size, static_cast<size_t>(bytes_to_read), test_reader), filesize);
EXPECT_EQ(driver_fread(buff_data, size, static_cast<size_t>(bytes_to_read), test_reader), filesize);
EXPECT_EQ(test_reader->Reader().offset_, filesize);
EXPECT_EQ(std::strlen(buff), filesize);
EXPECT_EQ(std::strncmp(buff, mock_content_0, mock_size_0), 0);
EXPECT_EQ(std::strncmp(buff + mock_size_0, mock_content_1, mock_size_1), 0);
EXPECT_EQ(std::strncmp(buff + mock_size_0 + mock_size_1, mock_content_2, mock_size_2), 0);
EXPECT_EQ(std::strlen(buff_data), filesize);
EXPECT_EQ(std::strncmp(buff_data, mock_content_0, mock_size_0), 0);
EXPECT_EQ(std::strncmp(buff_data + mock_size_0, mock_content_1, mock_size_1), 0);
EXPECT_EQ(std::strncmp(buff_data + mock_size_0 + mock_size_1, mock_content_2, mock_size_2), 0);
};

// read the whole file
test_whole_file(filesize);

// try to read more than the whole file
test_whole_file(filesize + 1);

delete[] buff;
}

TEST_F(GCSDriverTestFixture, Read_NFiles_CommonHeader)
Expand Down Expand Up @@ -1184,15 +1183,16 @@ TEST_F(GCSDriverTestFixture, Read_NFiles_CommonHeader)
ReadSimulatorParams mock_read_params_1{ mock_content_1, mock_size_1, &mock_offset_1 };
ReadSimulatorParams mock_read_params_2{ mock_content_2, mock_size_2, &mock_offset_2 };

char* buff = new char[2 * static_cast<size_t>(filesize)] {};
std::vector<char> buff(2 * static_cast<size_t>(filesize));
char* buff_data = buff.data();

constexpr size_t size{ sizeof(uint8_t) };

// read 1 byte from start of an intermediate file
test_reader->Reader().offset_ = cast_mock_size_0;

EXPECT_CALL(*mock_client, ReadObject).WillOnce(READ_MOCK_LAMBDA(GenerateReadSimulator(mock_read_params_1)));
EXPECT_EQ(driver_fread(buff, size, 1, test_reader), 1);
EXPECT_EQ(driver_fread(buff_data, size, 1, test_reader), 1);
EXPECT_EQ(test_reader->Reader().offset_, cast_mock_size_0 + 1);
EXPECT_EQ(buff[0], mock_read_params_1.content[hdr_size]);

Expand All @@ -1204,7 +1204,7 @@ TEST_F(GCSDriverTestFixture, Read_NFiles_CommonHeader)
EXPECT_CALL(*mock_client, ReadObject)
.WillOnce(READ_MOCK_LAMBDA(GenerateReadSimulator(mock_read_params_0)))
.WillOnce(READ_MOCK_LAMBDA(GenerateReadSimulator(mock_read_params_1)));
EXPECT_EQ(driver_fread(buff, size, 2, test_reader), 2);
EXPECT_EQ(driver_fread(buff_data, size, 2, test_reader), 2);
EXPECT_EQ(test_reader->Reader().offset_, cast_mock_size_0 + 1);
EXPECT_EQ(buff[0], mock_content_0[mock_size_0 - 1]);
EXPECT_EQ(buff[1], mock_content_1[hdr_size]);
Expand All @@ -1220,10 +1220,10 @@ TEST_F(GCSDriverTestFixture, Read_NFiles_CommonHeader)
EXPECT_CALL(*mock_client, ReadObject)
.WillOnce(READ_MOCK_LAMBDA(GenerateReadSimulator(mock_read_params_0)))
.WillOnce(READ_MOCK_LAMBDA(GenerateReadSimulator(mock_read_params_1)));
EXPECT_EQ(driver_fread(buff, size, static_cast<size_t>(to_read), test_reader), to_read);
EXPECT_EQ(driver_fread(buff_data, size, static_cast<size_t>(to_read), test_reader), to_read);
EXPECT_EQ(test_reader->Reader().offset_, to_read);
EXPECT_EQ(std::strncmp(buff, mock_content_0, mock_size_0), 0);
EXPECT_EQ(std::strncmp(buff + mock_size_0, mock_content_1 + hdr_size, read_in_file1), 0);
EXPECT_EQ(std::strncmp(buff_data, mock_content_0, mock_size_0), 0);
EXPECT_EQ(std::strncmp(buff_data + mock_size_0, mock_content_1 + hdr_size, read_in_file1), 0);


// tests reading the whole file
Expand All @@ -1238,21 +1238,19 @@ TEST_F(GCSDriverTestFixture, Read_NFiles_CommonHeader)
.WillOnce(READ_MOCK_LAMBDA(GenerateReadSimulator(mock_read_params_0)))
.WillOnce(READ_MOCK_LAMBDA(GenerateReadSimulator(mock_read_params_1)))
.WillOnce(READ_MOCK_LAMBDA(GenerateReadSimulator(mock_read_params_2)));
EXPECT_EQ(driver_fread(buff, size, static_cast<size_t>(bytes_to_read), test_reader), filesize);
EXPECT_EQ(driver_fread(buff_data, size, static_cast<size_t>(bytes_to_read), test_reader), filesize);
EXPECT_EQ(test_reader->Reader().offset_, filesize);
EXPECT_EQ(std::strlen(buff), filesize);
EXPECT_EQ(std::strncmp(buff, mock_content_0, mock_size_0), 0);
EXPECT_EQ(std::strncmp(buff + mock_size_0, mock_content_1 + hdr_size, mock_size_1 - hdr_size), 0);
EXPECT_EQ(std::strncmp(buff + mock_size_0 + mock_size_1 - hdr_size, mock_content_2 + hdr_size, mock_size_2 - hdr_size), 0);
EXPECT_EQ(std::strlen(buff_data), filesize);
EXPECT_EQ(std::strncmp(buff_data, mock_content_0, mock_size_0), 0);
EXPECT_EQ(std::strncmp(buff_data + mock_size_0, mock_content_1 + hdr_size, mock_size_1 - hdr_size), 0);
EXPECT_EQ(std::strncmp(buff_data + mock_size_0 + mock_size_1 - hdr_size, mock_content_2 + hdr_size, mock_size_2 - hdr_size), 0);
};

// read the whole file
test_whole_file(filesize);

// try to read more than the whole file
test_whole_file(filesize + 1);

delete[] buff;
}

TEST_F(GCSDriverTestFixture, Read_NFiles_ReadFailures)
Expand Down Expand Up @@ -1289,7 +1287,8 @@ TEST_F(GCSDriverTestFixture, Read_NFiles_ReadFailures)
ReadSimulatorParams mock_read_params_1{ mock_content_1, mock_size_1, &mock_offset_1 };

Check warning on line 1287 in test/basic_test.cpp

View workflow job for this annotation

GitHub Actions / ubuntu-latest-hosted-ninja-vcpkg_submod-autocache

unused variable ‘mock_read_params_1’ [-Wunused-variable]

Check warning on line 1287 in test/basic_test.cpp

View workflow job for this annotation

GitHub Actions / macos-13-hosted-ninja-vcpkg_submod-autocache

unused variable 'mock_read_params_1' [-Wunused-variable]

Check warning on line 1287 in test/basic_test.cpp

View workflow job for this annotation

GitHub Actions / macos-14-hosted-ninja-vcpkg_submod-autocache

unused variable 'mock_read_params_1' [-Wunused-variable]
ReadSimulatorParams mock_read_params_2{ mock_content_2, mock_size_2, &mock_offset_2 };

Check warning on line 1288 in test/basic_test.cpp

View workflow job for this annotation

GitHub Actions / ubuntu-latest-hosted-ninja-vcpkg_submod-autocache

unused variable ‘mock_read_params_2’ [-Wunused-variable]

Check warning on line 1288 in test/basic_test.cpp

View workflow job for this annotation

GitHub Actions / macos-13-hosted-ninja-vcpkg_submod-autocache

unused variable 'mock_read_params_2' [-Wunused-variable]

Check warning on line 1288 in test/basic_test.cpp

View workflow job for this annotation

GitHub Actions / macos-14-hosted-ninja-vcpkg_submod-autocache

unused variable 'mock_read_params_2' [-Wunused-variable]

char* buff = new char[2 * static_cast<size_t>(filesize)] {};
std::vector<char> buff(2 * static_cast<size_t>(filesize));
char* buff_data = buff.data();

constexpr size_t size{ sizeof(uint8_t) };

Expand All @@ -1300,7 +1299,7 @@ TEST_F(GCSDriverTestFixture, Read_NFiles_ReadFailures)

set_mock_calls();

EXPECT_EQ(driver_fread(buff, size, to_read, test_reader), -1);
EXPECT_EQ(driver_fread(buff_data, size, to_read, test_reader), -1);
EXPECT_EQ(test_reader->Reader().offset_, offset_before_read);
};

Expand All @@ -1322,6 +1321,5 @@ TEST_F(GCSDriverTestFixture, Read_NFiles_ReadFailures)
.WillOnce(READ_MOCK_LAMBDA_FAILURE);
});
}
}

delete[] buff;
}

0 comments on commit f860211

Please sign in to comment.