Skip to content

Commit 8947482

Browse files
committed
Read dumped npz
1 parent 2f5d317 commit 8947482

1 file changed

Lines changed: 15 additions & 36 deletions

File tree

test/test_xnpz.cpp

Lines changed: 15 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -33,52 +33,31 @@ namespace xt
3333
EXPECT_TRUE(xt::all(xt::equal(arr_0, xarr_0)));
3434
}
3535

36-
bool compare_binary_files(std::string fn1, std::string fn2, std::size_t n_zipped_files)
37-
{
38-
std::ifstream stream1(fn1, std::ios::in | std::ios::binary);
39-
std::vector<uint8_t> fn1_contents((std::istreambuf_iterator<char>(stream1)),
40-
std::istreambuf_iterator<char>());
41-
42-
std::ifstream stream2(fn2, std::ios::in | std::ios::binary);
43-
std::vector<uint8_t> fn2_contents((std::istreambuf_iterator<char>(stream2)),
44-
std::istreambuf_iterator<char>());
45-
std::size_t unequal = 0;
46-
47-
if (fn1_contents.size() != fn2_contents.size())
48-
{
49-
return false;
50-
}
51-
52-
for (auto iter_fn1 = fn1_contents.begin(), iter_fn2 = fn2_contents.begin();
53-
iter_fn1 != fn1_contents.end();
54-
iter_fn1++, iter_fn2++)
55-
{
56-
if (*iter_fn1 != *iter_fn2)
57-
{
58-
unequal += 1;
59-
}
60-
}
61-
// this is date + time == 4 bytes per file + once in global header
62-
std::size_t unequal_allowed = (n_zipped_files + 2) * 3;
63-
if (unequal > unequal_allowed)
64-
{
65-
return false;
66-
}
67-
}
68-
6936
TEST(xnpz, save_uncompressed)
7037
{
7138
dump_npz("files/dump_uncompressed.npz", "arr_0", linspace<double>(0, 100), false, false);
7239
xt::xarray<int64_t> arr = {{0, 1, 2}, {3, 4, 5}, {6, 7, 8}};
7340
dump_npz("files/dump_uncompressed.npz", "arr_1", arr);
74-
EXPECT_TRUE(compare_binary_files("files/dump_uncompressed.npz", "files/uncompressed.npz", 2));
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)));
7548
}
7649

7750
TEST(xnpz, save_compressed)
7851
{
7952
xt::xarray<int64_t> arr = {{0, 1, 2}, {3, 4, 5}, {6, 7, 8}};
8053
dump_npz("files/dump_compressed.npz", "arr_1", linspace<double>(0, 100), true, false );
8154
dump_npz("files/dump_compressed.npz", "arr_0", arr, true);
82-
EXPECT_TRUE(compare_binary_files("files/dump_compressed.npz", "files/compressed.npz", 2));
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)));
8362
}
84-
}
63+
}

0 commit comments

Comments
 (0)