forked from rogersce/cnpy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_generic_regression.cpp
More file actions
104 lines (89 loc) · 2.78 KB
/
test_generic_regression.cpp
File metadata and controls
104 lines (89 loc) · 2.78 KB
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
// Generic regression test for cnpy API
#include "cnpy.h"
#include <catch2/catch_approx.hpp>
#include <catch2/catch_test_macros.hpp>
#include <cstdio>
#include <cstdlib>
#include <functional>
#include <string>
#include <typeinfo>
#include <vector>
TEST_CASE("Generic regression test for cnpy API", "[cnpy][regression]") {
// Prepare temporary file names
const std::string npy_file = "temp_test.npy";
const std::string npz_file = "temp_test.npz";
const std::string mmap_file = "temp_mmap.npy";
// Ensure cleanup at the end
auto cleanup = [&]() {
std::remove(npy_file.c_str());
std::remove(npz_file.c_str());
std::remove(mmap_file.c_str());
};
// In case of early exit, use RAII
struct Cleanup {
std::function<void()> f;
~Cleanup() { f(); }
};
Cleanup guard{cleanup};
// Individual API calls with INFO and REQUIRE_NOTHROW
INFO("BigEndianTest");
REQUIRE_NOTHROW(cnpy::BigEndianTest());
INFO("map_type_int");
REQUIRE_NOTHROW(cnpy::map_type(typeid(int)));
INFO("create_npy_header_int");
REQUIRE_NOTHROW(cnpy::create_npy_header<int>({1}));
INFO("npy_save_int");
REQUIRE_NOTHROW(cnpy::npy_save<int>(npy_file, std::vector<int>{1}));
INFO("npy_load");
REQUIRE_NOTHROW(cnpy::npy_load(npy_file));
INFO("npz_save_int");
REQUIRE_NOTHROW(cnpy::npz_save<int>(npz_file, "arr", std::vector<int>{1}));
INFO("npz_load");
REQUIRE_NOTHROW(cnpy::npz_load(npz_file));
INFO("npz_load_varname");
REQUIRE_NOTHROW(cnpy::npz_load(npz_file, "arr"));
// parse_npy_header from FILE*
{
FILE* fp = fopen(npy_file.c_str(), "rb");
REQUIRE(fp != nullptr);
size_t ws;
std::vector<size_t> sh;
bool fo;
REQUIRE_NOTHROW(cnpy::parse_npy_header(fp, ws, sh, fo));
fclose(fp);
}
// parse_npy_header from buffer
{
FILE* fp = fopen(npy_file.c_str(), "rb");
REQUIRE(fp != nullptr);
unsigned char buf[256];
size_t read = fread(buf, 1, sizeof(buf), fp);
(void)read;
size_t ws;
std::vector<size_t> sh;
bool fo;
REQUIRE_NOTHROW(cnpy::parse_npy_header(buf, ws, sh, fo));
fclose(fp);
}
// parse_zip_footer
{
FILE* fp = fopen(npz_file.c_str(), "rb");
REQUIRE(fp != nullptr);
uint16_t nrecs;
size_t ghs, goff;
REQUIRE_NOTHROW(cnpy::parse_zip_footer(fp, nrecs, ghs, goff));
fclose(fp);
}
INFO("new_npy_mmap_int");
REQUIRE_NOTHROW(cnpy::new_npy_mmap<int>(mmap_file, {1}, false));
// Test operator+= overloads
using cnpy::operator+=;
{
std::vector<char> v;
REQUIRE_NOTHROW(v += std::string("test"));
}
{
std::vector<char> v;
REQUIRE_NOTHROW(v += "cstr");
}
}