MRPT  2.0.1
CFileGZStreams_unittest.cpp
Go to the documentation of this file.
1 /* +------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | https://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2020, Individual contributors, see AUTHORS file |
6  | See: https://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See: https://www.mrpt.org/License |
8  +------------------------------------------------------------------------+ */
9 
10 #include <gtest/gtest.h>
11 #include <mrpt/core/format.h>
15 #include <mrpt/system/filesystem.h>
16 #include <test_mrpt_common.h>
17 #include <algorithm> // std::equal
18 
19 const size_t tst_data_len = 1000U;
20 
21 // Generate random data:
22 void generate_test_data(std::vector<uint8_t>& tst_data)
23 {
24  const unsigned int random_seed = 123U;
25  mrpt::random::Generator_MT19937 mersenne_engine;
26  mersenne_engine.seed(random_seed);
27 
28  // Was: std::uniform_int_distribution<unsigned short> dist{0, 3};
29  // But that class seems not to be reproducible across compilers.
30  auto gen = [&mersenne_engine]() {
31  // low entropy
32  return mersenne_engine() % 4;
33  };
34 
35  tst_data.resize(tst_data_len);
36  std::generate(std::begin(tst_data), std::end(tst_data), gen);
37 }
38 
39 TEST(CFileGZStreams, readwriteTmpFileCompressed)
40 {
41  std::vector<uint8_t> tst_data;
42  generate_test_data(tst_data);
43 
44  // Test for all compress levels:
45  for (int compress_level = 1; compress_level <= 9; compress_level++)
46  {
47  const std::string fil = mrpt::system::getTempFileName() +
48  std::string("_") +
49  std::to_string(compress_level);
50  // Write:
51  {
53  const bool open_ok = fil_out.open(fil, compress_level);
54  EXPECT_TRUE(open_ok);
55 
56  const size_t wr_count = fil_out.Write(&tst_data[0], tst_data_len);
57  EXPECT_EQ(wr_count, tst_data_len);
58  }
59  // Read:
60  {
62  const bool open_ok = fil_in.open(fil);
63  EXPECT_TRUE(open_ok);
64 
65  uint8_t rd_buf[tst_data_len + 5];
66  const size_t rd_count = fil_in.Read(rd_buf, tst_data_len);
67  EXPECT_EQ(rd_count, tst_data_len);
68 
69  EXPECT_TRUE(std::equal(
70  std::begin(tst_data), std::end(tst_data), std::begin(rd_buf)));
71  }
72  }
73 }
74 
75 TEST(CFileGZStreams, compareWithTestGZFiles)
76 {
77  std::vector<uint8_t> tst_data;
78  generate_test_data(tst_data);
79 
80  // Test for all compress levels:
81  for (int compress_level = 1; compress_level <= 9; compress_level++)
82  {
83  const std::string fil = mrpt::format(
84  "%s/tests/gz-tests/%i.gz", mrpt::UNITTEST_BASEDIR.c_str(),
85  compress_level);
86 
87  if (!mrpt::system::fileExists(fil))
88  {
89  GTEST_FAIL() << "ERROR: test due to missing file: " << fil << "\n";
90  continue;
91  }
93  const bool open_ok = fil_in.open(fil);
94  EXPECT_TRUE(open_ok);
95 
96  uint8_t rd_buf[tst_data_len + 5];
97  const size_t rd_count = fil_in.Read(rd_buf, tst_data_len);
98  EXPECT_EQ(rd_count, tst_data_len);
99 
100  EXPECT_TRUE(std::equal(
101  std::begin(tst_data), std::end(tst_data), std::begin(rd_buf)))
102  << " compress_level:" << compress_level;
103  }
104 }
std::string to_string(T v)
Just like std::to_string(), but with an overloaded version for std::string arguments.
Definition: format.h:36
EXPECT_TRUE(mrpt::system::fileExists(ini_fil))
std::string std::string format(std::string_view fmt, ARGS &&... args)
Definition: format.h:26
bool open(const std::string &fileName, int compress_level=1, mrpt::optional_ref< std::string > error_msg=std::nullopt)
Open a file for write, choosing the compression level.
void generate_test_data(std::vector< uint8_t > &tst_data)
bool fileExists(const std::string &fileName)
Test if a given file (or directory) exists.
Definition: filesystem.cpp:128
size_t Read(void *Buffer, size_t Count) override
Introduces a pure virtual method responsible for reading from the stream.
bool open(const std::string &fileName, mrpt::optional_ref< std::string > error_msg=std::nullopt)
Opens the file for read.
std::string getTempFileName()
Returns the name of a proposed temporary file name.
Definition: filesystem.cpp:283
Portable MT19937 random generator, C++11 UniformRandomBitGenerator compliant.
TEST(CFileGZStreams, readwriteTmpFileCompressed)
const_iterator end() const
Definition: ts_hash_map.h:246
const_iterator begin() const
Definition: ts_hash_map.h:240
EXPECT_EQ(out.image_pair_was_used.size(), NUM_IMGS)
size_t Write(const void *Buffer, size_t Count) override
Introduces a pure virtual method responsible for writing to the stream.
Transparently opens a compressed "gz" file and reads uncompressed data from it.
Saves data to a file and transparently compress the data using the given compression level...
void seed(const uint32_t seed)
const size_t tst_data_len



Page generated by Doxygen 1.8.14 for MRPT 2.0.1 Git: 0fef1a6d7 Fri Apr 3 23:00:21 2020 +0200 at vie abr 3 23:20:28 CEST 2020