forked from xtensor-stack/xtensor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_xnpy.cpp
119 lines (100 loc) · 4.58 KB
/
test_xnpy.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/***************************************************************************
* Copyright (c) 2016, Johan Mabille, Sylvain Corlay and Wolf Vollprecht *
* *
* Distributed under the terms of the BSD 3-Clause License. *
* *
* The full license is in the file LICENSE, distributed with this software. *
****************************************************************************/
#include "gtest/gtest.h"
#include "xtensor/xnpy.hpp"
#include "xtensor/xarray.hpp"
#include <fstream>
#include <cstdint>
namespace xt
{
TEST(xnpy, load)
{
xarray<double> darr = {{{ 0.29731723, 0.04380157, 0.94748308},
{ 0.85020643, 0.52958618, 0.0598172 },
{ 0.77253259, 0.47564231, 0.70274005}},
{{ 0.85998447, 0.61160158, 0.44432939},
{ 0.25506765, 0.97420976, 0.15455842},
{ 0.05873659, 0.66191764, 0.01448838}},
{{ 0.175919 , 0.13850365, 0.94059426},
{ 0.79941809, 0.5124432 , 0.51364796},
{ 0.25721979, 0.41608858, 0.06255319}}};
xarray<bool> barr = {{{ 0, 0, 1},
{ 1, 1, 0},
{ 1, 0, 1}},
{{ 1, 1, 0},
{ 0, 1, 0},
{ 0, 1, 0}},
{{ 0, 0, 1},
{ 1, 1, 1},
{ 0, 0, 0}}};
auto darr_loaded = load_npy<double>("files/xnpy_files/double.npy");
EXPECT_TRUE(all(isclose(darr, darr_loaded)));
auto barr_loaded = load_npy<bool>("files/xnpy_files/bool.npy");
EXPECT_TRUE(all(equal(barr, barr_loaded)));
auto dfarr_loaded = load_npy<double, layout_type::column_major>("files/xnpy_files/double_fortran.npy");
EXPECT_TRUE(all(isclose(darr, dfarr_loaded)));
}
bool compare_binary_files(std::string fn1, std::string fn2)
{
std::ifstream stream1(fn1, std::ios::in | std::ios::binary);
std::vector<uint8_t> fn1_contents((std::istreambuf_iterator<char>(stream1)),
std::istreambuf_iterator<char>());
std::ifstream stream2(fn2, std::ios::in | std::ios::binary);
std::vector<uint8_t> fn2_contents((std::istreambuf_iterator<char>(stream2)),
std::istreambuf_iterator<char>());
return std::equal(fn1_contents.begin(), fn1_contents.end(), fn2_contents.begin()) &&
fn1_contents.size() == fn2_contents.size();
}
std::string get_filename()
{
std::string filename = std::tmpnam(nullptr);
filename += ".npy";
return filename;
}
TEST(xnpy, dump)
{
std::string filename = get_filename();
xarray<bool> barr = {{{0, 0, 1},
{1, 1, 0},
{1, 0, 1}},
{{1, 1, 0},
{0, 1, 0},
{0, 1, 0}},
{{0, 0, 1},
{1, 1, 1},
{0, 0, 0}}};
xtensor<uint64_t, 1> ularr = {12ul, 14ul, 16ul, 18ul, 1234321ul};
dump_npy(filename, barr);
std::string compare_name = "files/xnpy_files/bool.npy";
if (barr.layout() == layout_type::column_major)
{
compare_name = "files/xnpy_files/bool_fortran.npy";
}
EXPECT_TRUE(compare_binary_files(filename, compare_name));
std::remove(filename.c_str());
filename = get_filename();
dump_npy(filename, ularr);
auto ularrcpy = load_npy<uint64_t>(filename);
EXPECT_TRUE(all(equal(ularr, ularrcpy)));
compare_name = "files/xnpy_files/unsignedlong.npy";
if (barr.layout() == layout_type::column_major)
{
compare_name = "files/xnpy_files/unsignedlong_fortran.npy";
}
EXPECT_TRUE(compare_binary_files(filename, compare_name));
std::remove(filename.c_str());
}
TEST(xnpy, xfunction_cast)
{
// compilation test, cf: https://github.com/QuantStack/xtensor/issues/1070
auto dc = cast<char>(load_npy<double>("files/xnpy_files/double.npy"));
EXPECT_EQ(dc(0, 0), 0);
xarray<char> adc = dc;
EXPECT_EQ(adc(0, 0), 0);
}
}