Cleanup temporary test files

This commit is contained in:
Michael Krasnyk 2017-06-25 22:13:52 +02:00
parent 42f3401dd7
commit 383640caaf
3 changed files with 28 additions and 11 deletions

View File

@ -0,0 +1,15 @@
#ifndef UNIT_TESTS_TEMPORARY_FILE_HPP
#define UNIT_TESTS_TEMPORARY_FILE_HPP
#include <boost/filesystem.hpp>
struct TemporaryFile
{
TemporaryFile() : path(boost::filesystem::unique_path()) {}
~TemporaryFile() { boost::filesystem::remove(path); }
boost::filesystem::path path;
};
#endif // UNIT_TESTS_TEMPORARY_FILE_HPP

View File

@ -1,8 +1,7 @@
#include "util/indexed_data.hpp"
#include "common/temporary_file.hpp"
#include "util/exception.hpp"
#include <boost/filesystem.hpp>
#include <boost/test/test_case_template.hpp>
#include <boost/test/unit_test.hpp>
@ -36,15 +35,16 @@ BOOST_AUTO_TEST_CASE(check_variable_group_block_bitops)
template <typename IndexedData, typename Offsets, typename Data>
void test_rw(const Offsets &offsets, const Data &data)
{
TemporaryFile file;
IndexedData indexed_data;
auto path = boost::filesystem::unique_path();
{
storage::io::FileWriter writer(path, storage::io::FileWriter::HasNoFingerprint);
storage::io::FileWriter writer(file.path, storage::io::FileWriter::HasNoFingerprint);
indexed_data.write(writer, offsets.begin(), offsets.end(), data.begin());
}
storage::io::FileReader reader(path, storage::io::FileReader::HasNoFingerprint);
storage::io::FileReader reader(file.path, storage::io::FileReader::HasNoFingerprint);
auto length = reader.GetSize();
std::string str(length, '\0');
reader.ReadInto(const_cast<char *>(str.data()), length);
@ -184,17 +184,18 @@ BOOST_AUTO_TEST_CASE(check_corrupted_memory)
BOOST_AUTO_TEST_CASE(check_string_view)
{
auto path = boost::filesystem::unique_path();
TemporaryFile file;
std::string name_data = "hellostringview";
std::vector<std::uint32_t> name_offsets = {0, 5, 11, 15};
IndexedData<VariableGroupBlock<16, StringView>> indexed_data;
{
storage::io::FileWriter writer(path, storage::io::FileWriter::HasNoFingerprint);
storage::io::FileWriter writer(file.path, storage::io::FileWriter::HasNoFingerprint);
indexed_data.write(writer, name_offsets.begin(), name_offsets.end(), name_data.begin());
}
storage::io::FileReader reader(path, storage::io::FileReader::HasNoFingerprint);
storage::io::FileReader reader(file.path, storage::io::FileReader::HasNoFingerprint);
auto length = reader.GetSize();
std::string str(length, '\0');
reader.ReadInto(const_cast<char *>(str.data()), length);

View File

@ -1,4 +1,5 @@
#include "util/name_table.hpp"
#include "common/temporary_file.hpp"
#include "util/exception.hpp"
#include <boost/filesystem.hpp>
@ -54,14 +55,14 @@ std::string PrapareNameTableData(std::vector<std::string> &data, bool fill_all)
}
name_offsets.push_back(name_char_data.size());
auto path = boost::filesystem::unique_path();
TemporaryFile file;
{
storage::io::FileWriter writer(path, storage::io::FileWriter::HasNoFingerprint);
storage::io::FileWriter writer(file.path, storage::io::FileWriter::HasNoFingerprint);
indexed_data.write(
writer, name_offsets.begin(), name_offsets.end(), name_char_data.begin());
}
storage::io::FileReader reader(path, storage::io::FileReader::HasNoFingerprint);
storage::io::FileReader reader(file.path, storage::io::FileReader::HasNoFingerprint);
auto length = reader.GetSize();
std::string str(length, '\0');
reader.ReadInto(const_cast<char *>(str.data()), length);