Skip to content

Commit bd5a087

Browse files
Merge pull request #21 from wolfv/change_year_bin_diff
as the years change ...
2 parents 206ecfb + 008e45c commit bd5a087

1 file changed

Lines changed: 40 additions & 14 deletions

File tree

test/test_xnpz.cpp

Lines changed: 40 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
****************************************************************************/
88

99
#include <cstdint>
10+
#include <sstream>
11+
#include <exception>
1012

1113
#include "gtest/gtest.h"
1214
#include "xtensor-io/xnpz.hpp"
@@ -33,31 +35,55 @@ namespace xt
3335
EXPECT_TRUE(xt::all(xt::equal(arr_0, xarr_0)));
3436
}
3537

38+
bool compare_binary_files(std::string fn1, std::string fn2, std::size_t n_zipped_files)
39+
{
40+
std::ifstream stream1(fn1, std::ios::in | std::ios::binary);
41+
std::vector<uint8_t> fn1_contents((std::istreambuf_iterator<char>(stream1)),
42+
std::istreambuf_iterator<char>());
43+
44+
std::ifstream stream2(fn2, std::ios::in | std::ios::binary);
45+
std::vector<uint8_t> fn2_contents((std::istreambuf_iterator<char>(stream2)),
46+
std::istreambuf_iterator<char>());
47+
std::size_t unequal = 0;
48+
49+
if (fn1_contents.size() != fn2_contents.size())
50+
{
51+
throw std::runtime_error("Content sizes are not the same!");
52+
}
53+
54+
for (auto iter_fn1 = fn1_contents.begin(), iter_fn2 = fn2_contents.begin();
55+
iter_fn1 != fn1_contents.end();
56+
iter_fn1++, iter_fn2++)
57+
{
58+
if (*iter_fn1 != *iter_fn2)
59+
{
60+
unequal += 1;
61+
}
62+
}
63+
// this is date + time == 4 bytes per file + once in global header
64+
std::size_t unequal_allowed = (n_zipped_files + 2) * 4;
65+
if (unequal != unequal_allowed)
66+
{
67+
std::stringstream ss;
68+
ss << "Number of unequal elements not allowed size: " << unequal << " vs allowed: " << unequal_allowed << std::endl;
69+
throw std::runtime_error(ss.str());
70+
}
71+
return true;
72+
}
73+
3674
TEST(xnpz, save_uncompressed)
3775
{
3876
dump_npz("files/dump_uncompressed.npz", "arr_0", linspace<double>(0, 100), false, false);
3977
xt::xarray<int64_t> arr = {{0, 1, 2}, {3, 4, 5}, {6, 7, 8}};
4078
dump_npz("files/dump_uncompressed.npz", "arr_1", arr);
41-
// Re-read
42-
auto npz_map = xt::load_npz("files/dump_uncompressed.npz");
43-
auto arr_0 = npz_map["arr_0"].cast<double>();
44-
auto arr_1 = npz_map["arr_1"].cast<uint64_t>(false);
45-
xt::xarray<uint64_t> xarr_1 = {{0, 1, 2}, {3, 4, 5}, {6, 7, 8}};
46-
EXPECT_TRUE(xt::all(xt::isclose(arr_0, linspace<double>(0, 100))));
47-
EXPECT_TRUE(xt::all(xt::equal(arr_1, xarr_1)));
79+
EXPECT_TRUE(compare_binary_files("files/dump_uncompressed.npz", "files/uncompressed.npz", 2));
4880
}
4981

5082
TEST(xnpz, save_compressed)
5183
{
5284
xt::xarray<int64_t> arr = {{0, 1, 2}, {3, 4, 5}, {6, 7, 8}};
5385
dump_npz("files/dump_compressed.npz", "arr_1", linspace<double>(0, 100), true, false );
5486
dump_npz("files/dump_compressed.npz", "arr_0", arr, true);
55-
// Re-read
56-
auto npz_map = xt::load_npz("files/dump_compressed.npz");
57-
auto arr_0 = npz_map["arr_0"].cast<uint64_t>(false);
58-
auto arr_1 = npz_map["arr_1"].cast<double>();
59-
xt::xarray<uint64_t> xarr_0 = {{0, 1, 2}, {3, 4, 5}, {6, 7, 8}};
60-
EXPECT_TRUE(xt::all(xt::isclose(arr_1, linspace<double>(0, 100))));
61-
EXPECT_TRUE(xt::all(xt::equal(arr_0, xarr_0)));
87+
EXPECT_TRUE(compare_binary_files("files/dump_compressed.npz", "files/compressed.npz", 2));
6288
}
6389
}

0 commit comments

Comments
 (0)