forked from rogersce/cnpy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_cnpy.cpp
More file actions
945 lines (797 loc) · 34.1 KB
/
test_cnpy.cpp
File metadata and controls
945 lines (797 loc) · 34.1 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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
#include "cnpy.h"
#include <catch2/catch_approx.hpp>
#include <catch2/catch_test_macros.hpp>
#include <complex>
TEST_CASE("Placeholder test", "[example]") { REQUIRE(true); }
TEST_CASE("BigEndianTest returns correct endianness indicator", "[cnpy]") {
int x = 1;
char expected = ((char*)&x)[0] ? '<' : '>';
char result = cnpy::BigEndianTest();
REQUIRE(result == expected);
}
TEST_CASE("map_type returns correct type codes", "[cnpy]") {
// Floating point types should map to 'f'
REQUIRE(cnpy::map_type(typeid(float)) == 'f');
REQUIRE(cnpy::map_type(typeid(double)) == 'f');
REQUIRE(cnpy::map_type(typeid(long double)) == 'f');
// Signed integer types should map to 'i'
REQUIRE(cnpy::map_type(typeid(int)) == 'i');
REQUIRE(cnpy::map_type(typeid(char)) == 'i');
REQUIRE(cnpy::map_type(typeid(short)) == 'i');
REQUIRE(cnpy::map_type(typeid(long)) == 'i');
REQUIRE(cnpy::map_type(typeid(long long)) == 'i');
// Unsigned integer types should map to 'u'
REQUIRE(cnpy::map_type(typeid(unsigned char)) == 'u');
REQUIRE(cnpy::map_type(typeid(unsigned short)) == 'u');
REQUIRE(cnpy::map_type(typeid(unsigned int)) == 'u');
REQUIRE(cnpy::map_type(typeid(unsigned long)) == 'u');
REQUIRE(cnpy::map_type(typeid(unsigned long long)) == 'u');
// Boolean type should map to 'b'
REQUIRE(cnpy::map_type(typeid(bool)) == 'b');
// Complex types should map to 'c'
REQUIRE(cnpy::map_type(typeid(std::complex<float>)) == 'c');
REQUIRE(cnpy::map_type(typeid(std::complex<double>)) == 'c');
REQUIRE(cnpy::map_type(typeid(std::complex<long double>)) == 'c');
// Unknown type should map to '?'
struct Dummy {};
REQUIRE(cnpy::map_type(typeid(Dummy)) == '?');
}
// Additional unit tests for cnpy::NpyArray
TEST_CASE("NpyArray default constructor", "[cnpy]") {
cnpy::NpyArray arr;
REQUIRE(arr.shape.empty());
REQUIRE(arr.word_size == 0);
REQUIRE(arr.fortran_order == false);
REQUIRE(arr.num_vals == 0);
REQUIRE(!arr.data_holder);
}
TEST_CASE("NpyArray constructors and data access", "[cnpy]") {
std::vector<size_t> shape = {2, 3};
size_t word_size = sizeof(int);
bool fortran = false;
cnpy::NpyArray arr(shape, word_size, fortran);
// Verify shape and properties
REQUIRE(arr.shape == shape);
REQUIRE(arr.word_size == word_size);
REQUIRE(arr.fortran_order == fortran);
// Compute expected number of elements
size_t expected_num_vals = 1;
for (size_t dim : shape) {
expected_num_vals *= dim;
}
REQUIRE(arr.num_vals == expected_num_vals);
// Verify total byte size
REQUIRE(arr.num_bytes() == expected_num_vals * word_size);
// Test mutable data access
int* data = arr.data<int>();
for (size_t i = 0; i < expected_num_vals; ++i) {
data[i] = static_cast<int>(i * 10);
}
// Verify values via const accessor
const cnpy::NpyArray& const_arr = arr;
const int* const_data = const_arr.data<int>();
for (size_t i = 0; i < expected_num_vals; ++i) {
REQUIRE(const_data[i] == static_cast<int>(i * 10));
}
}
// Unit tests for cnpy::create_npy_header
TEST_CASE("create_npy_header generates correct header for int type and "
"multi-dimensional shape",
"[cnpy]") {
std::vector<size_t> shape = {2, 3};
// Generate header for int (4-byte) data
std::vector<char> header = cnpy::create_npy_header<int>(shape);
// Basic header checks
REQUIRE(header.size() >= 10);
// Magic number
REQUIRE(static_cast<unsigned char>(header[0]) == 0x93);
// Magic string "NUMPY"
REQUIRE(std::string(header.begin() + 1, header.begin() + 6) == "NUMPY");
// Version 1.0
REQUIRE(header[6] == 1);
REQUIRE(header[7] == 0);
// Extract dict length (little endian)
uint16_t dict_len =
static_cast<uint16_t>(static_cast<unsigned char>(header[8]) | (static_cast<unsigned char>(header[9]) << 8));
REQUIRE(dict_len == header.size() - 10);
// Extract dict string (including trailing newline)
std::string dict(header.begin() + 10, header.end());
// Verify dict content
// Expected descr: little-endian '<', type code 'i', size 4 -> "<i4"
REQUIRE(dict.find("'descr': '<i4'") != std::string::npos);
REQUIRE(dict.find("'fortran_order': False") != std::string::npos);
REQUIRE(dict.find("'shape': (2, 3)") != std::string::npos);
// Dict should end with a newline
REQUIRE(dict.back() == '\n');
// Header total size must be a multiple of 16 bytes
REQUIRE((header.size() % 16) == 0);
}
TEST_CASE("create_npy_header generates correct header for double type and "
"single-dimensional shape",
"[cnpy]") {
std::vector<size_t> shape = {5};
// Generate header for double (8-byte) data
std::vector<char> header = cnpy::create_npy_header<double>(shape);
// Basic header checks
REQUIRE(header.size() >= 10);
REQUIRE(static_cast<unsigned char>(header[0]) == 0x93);
REQUIRE(std::string(header.begin() + 1, header.begin() + 6) == "NUMPY");
REQUIRE(header[6] == 1);
REQUIRE(header[7] == 0);
uint16_t dict_len =
static_cast<uint16_t>(static_cast<unsigned char>(header[8]) | (static_cast<unsigned char>(header[9]) << 8));
REQUIRE(dict_len == header.size() - 10);
std::string dict(header.begin() + 10, header.end());
// Expected descr: little-endian '<', type code 'f', size 8 -> "<f8"
REQUIRE(dict.find("'descr': '<f8'") != std::string::npos);
REQUIRE(dict.find("'fortran_order': False") != std::string::npos);
// For a single dimension, a trailing comma is added after the size
REQUIRE(dict.find("'shape': (5,)") != std::string::npos);
REQUIRE(dict.back() == '\n');
REQUIRE((header.size() % 16) == 0);
}
// Unit tests for cnpy::parse_npy_header (buffer and FILE* overloads)
TEST_CASE("parse_npy_header from buffer extracts correct metadata", "[cnpy]") {
// Create a header for an int array with shape {2,3}
std::vector<size_t> shape = {2, 3};
std::vector<char> header = cnpy::create_npy_header<int>(shape);
size_t word_size = 0;
std::vector<size_t> parsed_shape;
bool fortran_order = true; // initialize to a non‑false value
// Call the buffer overload
cnpy::parse_npy_header(reinterpret_cast<unsigned char*>(header.data()), word_size, parsed_shape, fortran_order);
REQUIRE(word_size == sizeof(int));
REQUIRE(parsed_shape == shape);
REQUIRE(fortran_order == false);
}
TEST_CASE("parse_npy_header from FILE* extracts correct metadata", "[cnpy]") {
// Create a header for a double array with shape {5}
std::vector<size_t> shape = {5};
std::vector<char> header = cnpy::create_npy_header<double>(shape);
// Write the header to a temporary file
FILE* tmp = std::tmpfile();
REQUIRE(tmp != nullptr);
size_t written = std::fwrite(header.data(), 1, header.size(), tmp);
REQUIRE(written == header.size());
// Rewind to the beginning of the file before parsing
std::rewind(tmp);
size_t word_size = 0;
std::vector<size_t> parsed_shape;
bool fortran_order = true;
// Call the FILE* overload
cnpy::parse_npy_header(tmp, word_size, parsed_shape, fortran_order);
REQUIRE(word_size == sizeof(double));
REQUIRE(parsed_shape == shape);
REQUIRE(fortran_order == false);
std::fclose(tmp);
}
// Unit test for cnpy::parse_zip_footer
TEST_CASE("parse_zip_footer correctly parses zip footer", "[cnpy]") {
// Prepare a simple integer array and shape
std::vector<int> data = {1, 2, 3, 4};
std::vector<size_t> shape = {data.size()};
std::string zip_path = "test_npz.zip";
// Write the array to a .npz file using the library's npz_save
cnpy::npz_save<int>(zip_path, "arr", data.data(), shape);
// Open the generated zip file for reading
FILE* fp = std::fopen(zip_path.c_str(), "rb");
REQUIRE(fp != nullptr);
uint16_t nrecs = 0;
size_t global_header_size = 0;
size_t global_header_offset = 0;
// Parse the zip footer
cnpy::parse_zip_footer(fp, nrecs, global_header_size, global_header_offset);
// Verify that exactly one record (array) is present
REQUIRE(nrecs == 1);
// The global header size and offset should be non‑zero
REQUIRE(global_header_size > 0);
REQUIRE(global_header_offset > 0);
// Verify that the footer is positioned correctly at the end of the file:
// file size = offset of global header + size of global header + footer (22
// bytes)
std::fseek(fp, 0, SEEK_END);
long file_size = std::ftell(fp);
REQUIRE(file_size == static_cast<long>(global_header_offset + global_header_size + 22));
std::fclose(fp);
// Clean up the temporary zip file
std::remove(zip_path.c_str());
}
// Unit test for cnpy::npy_load
// Additional unit tests for cnpy::npy_load with various data types
TEST_CASE("npy_load correctly loads a saved .npy file for int type", "[cnpy]") {
std::vector<int> data = {2, -3, 4, 6, 7, 0};
std::string filename = "test_npy_load_int.npy";
cnpy::npy_save<int>(filename, data);
cnpy::NpyArray arr = cnpy::npy_load(filename);
REQUIRE(arr.shape.size() == 1);
REQUIRE(arr.shape[0] == data.size());
REQUIRE(arr.word_size == sizeof(int));
REQUIRE(arr.fortran_order == false);
REQUIRE(arr.num_vals == data.size());
REQUIRE(arr.num_bytes() == data.size() * sizeof(int));
const int* loaded = arr.data<int>();
for (size_t i = 0; i < data.size(); ++i) {
REQUIRE(loaded[i] == data[i]);
}
std::remove(filename.c_str());
}
// Additional unit tests for cnpy::npy_load with various data types
TEST_CASE("npy_load correctly loads a saved .npy file for unsigned short type", "[cnpy]") {
std::vector<unsigned short> data = {2, 3, 4, 6, 7, 0};
std::string filename = "test_npy_load_unsigned_short.npy";
cnpy::npy_save<unsigned short>(filename, data);
cnpy::NpyArray arr = cnpy::npy_load(filename);
REQUIRE(arr.shape.size() == 1);
REQUIRE(arr.shape[0] == data.size());
REQUIRE(arr.word_size == sizeof(unsigned short));
REQUIRE(arr.fortran_order == false);
REQUIRE(arr.num_vals == data.size());
REQUIRE(arr.num_bytes() == data.size() * sizeof(unsigned short));
const unsigned short* loaded = arr.data<unsigned short>();
for (size_t i = 0; i < data.size(); ++i) {
REQUIRE(loaded[i] == data[i]);
}
std::remove(filename.c_str());
}
// Additional unit tests for cnpy::npy_load with various data types
TEST_CASE("npy_load correctly loads a saved .npy file for float type", "[cnpy]") {
std::vector<float> data = {0.1f, 0.2f, 0.3f, 0.4f, 0.5f};
std::string filename = "test_npy_load_float.npy";
cnpy::npy_save<float>(filename, data);
cnpy::NpyArray arr = cnpy::npy_load(filename);
REQUIRE(arr.shape.size() == 1);
REQUIRE(arr.shape[0] == data.size());
REQUIRE(arr.word_size == sizeof(float));
REQUIRE(arr.fortran_order == false);
REQUIRE(arr.num_vals == data.size());
REQUIRE(arr.num_bytes() == data.size() * sizeof(float));
const float* loaded = arr.data<float>();
for (size_t i = 0; i < data.size(); ++i) {
REQUIRE(loaded[i] == Catch::Approx(data[i]));
}
std::remove(filename.c_str());
}
TEST_CASE("npy_load correctly loads a saved .npy file for double type", "[cnpy]") {
std::vector<double> data = {0.1, 0.2, 0.3, 0.4, 0.5};
std::string filename = "test_npy_load_double.npy";
cnpy::npy_save<double>(filename, data);
cnpy::NpyArray arr = cnpy::npy_load(filename);
REQUIRE(arr.shape.size() == 1);
REQUIRE(arr.shape[0] == data.size());
REQUIRE(arr.word_size == sizeof(double));
REQUIRE(arr.fortran_order == false);
REQUIRE(arr.num_vals == data.size());
REQUIRE(arr.num_bytes() == data.size() * sizeof(double));
const double* loaded = arr.data<double>();
for (size_t i = 0; i < data.size(); ++i) {
REQUIRE(loaded[i] == Catch::Approx(data[i]));
}
std::remove(filename.c_str());
}
TEST_CASE("npy_load correctly loads a saved .npy file for long double type", "[cnpy]") {
std::vector<long double> data = {0.1L, 0.2L, 0.3L, 0.4L, 0.5L};
std::string filename = "test_npy_load_longdouble.npy";
cnpy::npy_save<long double>(filename, data);
cnpy::NpyArray arr = cnpy::npy_load(filename);
REQUIRE(arr.shape.size() == 1);
REQUIRE(arr.shape[0] == data.size());
REQUIRE(arr.word_size == sizeof(long double));
REQUIRE(arr.fortran_order == false);
REQUIRE(arr.num_vals == data.size());
REQUIRE(arr.num_bytes() == data.size() * sizeof(long double));
const long double* loaded = arr.data<long double>();
for (size_t i = 0; i < data.size(); ++i) {
REQUIRE(loaded[i] == Catch::Approx(data[i]));
}
std::remove(filename.c_str());
}
TEST_CASE("npy_load correctly loads a saved .npy file for complex<float> type", "[cnpy]") {
std::vector<std::complex<float>> data = {{1.0f, 2.0f}, {3.0f, 4.0f}, {5.0f, 6.0f}};
std::string filename = "test_npy_load_complex_float.npy";
cnpy::npy_save<std::complex<float>>(filename, data);
cnpy::NpyArray arr = cnpy::npy_load(filename);
REQUIRE(arr.shape.size() == 1);
REQUIRE(arr.shape[0] == data.size());
REQUIRE(arr.word_size == sizeof(std::complex<float>));
REQUIRE(arr.fortran_order == false);
REQUIRE(arr.num_vals == data.size());
REQUIRE(arr.num_bytes() == data.size() * sizeof(std::complex<float>));
const std::complex<float>* loaded = arr.data<std::complex<float>>();
for (size_t i = 0; i < data.size(); ++i) {
REQUIRE(loaded[i].real() == Catch::Approx(data[i].real()));
REQUIRE(loaded[i].imag() == Catch::Approx(data[i].imag()));
}
std::remove(filename.c_str());
}
TEST_CASE("npy_load correctly loads a saved .npy file for unsigned char type", "[cnpy]") {
std::vector<unsigned char> data = {0, 255, 128, 64};
std::string filename = "test_npy_load_uchar.npy";
cnpy::npy_save<unsigned char>(filename, data);
cnpy::NpyArray arr = cnpy::npy_load(filename);
REQUIRE(arr.shape.size() == 1);
REQUIRE(arr.shape[0] == data.size());
REQUIRE(arr.word_size == sizeof(unsigned char));
REQUIRE(arr.fortran_order == false);
REQUIRE(arr.num_vals == data.size());
REQUIRE(arr.num_bytes() == data.size() * sizeof(unsigned char));
const unsigned char* loaded = arr.data<unsigned char>();
for (size_t i = 0; i < data.size(); ++i) {
REQUIRE(loaded[i] == data[i]);
}
std::remove(filename.c_str());
}
TEST_CASE("npy_load correctly loads a saved .npy file for long type", "[cnpy]") {
std::vector<long> data = {10, 20, 30, 40, 50};
std::string filename = "test_npy_load_long.npy";
// Save the data to a .npy file using the library's npy_save overload for
// vectors
cnpy::npy_save<long>(filename, data);
// Load the .npy file back into an NpyArray
cnpy::NpyArray arr = cnpy::npy_load(filename);
// Verify shape and metadata
REQUIRE(arr.shape.size() == 1);
REQUIRE(arr.shape[0] == data.size());
REQUIRE(arr.word_size == sizeof(long));
REQUIRE(arr.fortran_order == false);
REQUIRE(arr.num_vals == data.size());
REQUIRE(arr.num_bytes() == data.size() * sizeof(long));
// Verify the loaded data matches the original
const long* loaded = arr.data<long>();
for (size_t i = 0; i < data.size(); ++i) {
REQUIRE(loaded[i] == data[i]);
}
// Clean up the temporary .npy file
std::remove(filename.c_str());
}
// Unit tests for cnpy::npz_load
TEST_CASE("npz_load with zero arrays throws exception", "[cnpy]") {
std::string empty_npz = "empty.npz";
// Create an empty file
FILE* fp = std::fopen(empty_npz.c_str(), "wb");
REQUIRE(fp != nullptr);
std::fclose(fp);
REQUIRE_THROWS_AS(cnpy::npz_load(empty_npz), std::runtime_error);
std::remove(empty_npz.c_str());
}
TEST_CASE("npz_load with one array loads correctly", "[cnpy]") {
std::string filename = "test_npz_one.npz";
std::vector<int> data = {1, 2, 3, 4};
std::vector<size_t> shape = {data.size()};
cnpy::npz_save<int>(filename, "arr1", data.data(), shape);
cnpy::npz_t arrays = cnpy::npz_load(filename);
REQUIRE(arrays.size() == 1);
REQUIRE(arrays.count("arr1") == 1);
const cnpy::NpyArray& arr = arrays.at("arr1");
REQUIRE(arr.shape == shape);
REQUIRE(arr.word_size == sizeof(int));
REQUIRE(arr.fortran_order == false);
const int* loaded = arr.data<int>();
for (size_t i = 0; i < data.size(); ++i) {
REQUIRE(loaded[i] == data[i]);
}
// Test varname overload
cnpy::NpyArray arr2 = cnpy::npz_load(filename, "arr1");
REQUIRE(arr2.shape == shape);
REQUIRE(arr2.word_size == sizeof(int));
const int* loaded2 = arr2.data<int>();
for (size_t i = 0; i < data.size(); ++i) {
REQUIRE(loaded2[i] == data[i]);
}
std::remove(filename.c_str());
}
TEST_CASE("npz_load with two arrays loads correctly", "[cnpy]") {
std::string filename = "test_npz_two.npz";
// First array (int)
std::vector<int> data_int = {10, 20, 30};
std::vector<size_t> shape_int = {data_int.size()};
cnpy::npz_save<int>(filename, "int_arr", data_int.data(), shape_int, "w");
// Second array (double)
std::vector<double> data_double = {0.1, 0.2, 0.3, 0.4};
std::vector<size_t> shape_double = {data_double.size()};
cnpy::npz_save<double>(filename, "double_arr", data_double.data(), shape_double, "a");
cnpy::npz_t arrays = cnpy::npz_load(filename);
REQUIRE(arrays.size() == 2);
// Verify int_arr
const cnpy::NpyArray& arr_int = arrays.at("int_arr");
REQUIRE(arr_int.shape == shape_int);
REQUIRE(arr_int.word_size == sizeof(int));
const int* loaded_int = arr_int.data<int>();
for (size_t i = 0; i < data_int.size(); ++i) {
REQUIRE(loaded_int[i] == data_int[i]);
}
// Verify double_arr
const cnpy::NpyArray& arr_double = arrays.at("double_arr");
REQUIRE(arr_double.shape == shape_double);
REQUIRE(arr_double.word_size == sizeof(double));
const double* loaded_double = arr_double.data<double>();
for (size_t i = 0; i < data_double.size(); ++i) {
REQUIRE(loaded_double[i] == Catch::Approx(data_double[i]));
}
std::remove(filename.c_str());
}
TEST_CASE("npz_load with three arrays loads correctly", "[cnpy]") {
std::string filename = "test_npz_three.npz";
// First array (int)
std::vector<int> data_int = {5, 6, 7, 8};
std::vector<size_t> shape_int = {data_int.size()};
cnpy::npz_save<int>(filename, "int_arr", data_int.data(), shape_int, "w");
// Second array (double)
std::vector<double> data_double = {1.1, 2.2, 3.3};
std::vector<size_t> shape_double = {data_double.size()};
cnpy::npz_save<double>(filename, "double_arr", data_double.data(), shape_double, "a");
// Third array (float)
std::vector<float> data_float = {0.5f, 1.5f};
std::vector<size_t> shape_float = {data_float.size()};
cnpy::npz_save<float>(filename, "float_arr", data_float.data(), shape_float, "a");
cnpy::npz_t arrays = cnpy::npz_load(filename);
REQUIRE(arrays.size() == 3);
// Verify int_arr
const cnpy::NpyArray& arr_int = arrays.at("int_arr");
REQUIRE(arr_int.shape == shape_int);
REQUIRE(arr_int.word_size == sizeof(int));
const int* loaded_int = arr_int.data<int>();
for (size_t i = 0; i < data_int.size(); ++i) {
REQUIRE(loaded_int[i] == data_int[i]);
}
// Verify double_arr
const cnpy::NpyArray& arr_double = arrays.at("double_arr");
REQUIRE(arr_double.shape == shape_double);
REQUIRE(arr_double.word_size == sizeof(double));
const double* loaded_double = arr_double.data<double>();
for (size_t i = 0; i < data_double.size(); ++i) {
REQUIRE(loaded_double[i] == Catch::Approx(data_double[i]));
}
// Verify float_arr
const cnpy::NpyArray& arr_float = arrays.at("float_arr");
REQUIRE(arr_float.shape == shape_float);
REQUIRE(arr_float.word_size == sizeof(float));
const float* loaded_float = arr_float.data<float>();
for (size_t i = 0; i < data_float.size(); ++i) {
REQUIRE(loaded_float[i] == Catch::Approx(data_float[i]));
}
std::remove(filename.c_str());
}
// Unit test for npy_save (int vector)
TEST_CASE("npy_save correctly writes a .npy file for int type and loads correctly", "[cnpy]") {
std::vector<int> data = {1, 2, 3, 4, 5};
std::string filename = "test_npy_save_int.npy";
// Save using npy_save
cnpy::npy_save<int>(filename, data);
// Load using npy_load
cnpy::NpyArray arr = cnpy::npy_load(filename);
// Verify shape and metadata
REQUIRE(arr.shape.size() == 1);
REQUIRE(arr.shape[0] == data.size());
REQUIRE(arr.word_size == sizeof(int));
REQUIRE(arr.fortran_order == false);
REQUIRE(arr.num_vals == data.size());
REQUIRE(arr.num_bytes() == data.size() * sizeof(int));
// Verify data
const int* loaded = arr.data<int>();
for (size_t i = 0; i < data.size(); ++i) {
REQUIRE(loaded[i] == data[i]);
}
// Clean up
std::remove(filename.c_str());
}
// Unit test for npy_save with multi-dimensional data
TEST_CASE("npy_save correctly writes a multi-dimensional .npy file and loads correctly", "[cnpy]") {
std::vector<int> data = {0, 1, 2, 3, 4, 5};
std::vector<size_t> shape = {2, 3};
std::string filename = "test_npy_save_multi.npy";
// Save using pointer overload
cnpy::npy_save<int>(filename, data.data(), shape);
// Load using npy_load
cnpy::NpyArray arr = cnpy::npy_load(filename);
// Verify shape and metadata
REQUIRE(arr.shape == shape);
REQUIRE(arr.word_size == sizeof(int));
REQUIRE(arr.fortran_order == false);
REQUIRE(arr.num_vals == data.size());
REQUIRE(arr.num_bytes() == data.size() * sizeof(int));
// Verify data
const int* loaded = arr.data<int>();
for (size_t i = 0; i < data.size(); ++i) {
REQUIRE(loaded[i] == data[i]);
}
// Clean up
std::remove(filename.c_str());
}
// Unit test for npy_load with multi-dimensional data
TEST_CASE("npy_load correctly loads a multi-dimensional .npy file for int type", "[cnpy]") {
// Prepare data for a 2x3x4 array (24 elements)
std::vector<int> data(2 * 3 * 4);
for (size_t i = 0; i < data.size(); ++i) {
data[i] = static_cast<int>(i);
}
std::vector<size_t> shape = {2, 3, 4};
std::string filename = "test_npy_load_multi.npy";
// Save using the pointer overload of npy_save
cnpy::npy_save<int>(filename, data.data(), shape);
// Load the file using npy_load
cnpy::NpyArray arr = cnpy::npy_load(filename);
// Verify shape and metadata
REQUIRE(arr.shape == shape);
REQUIRE(arr.word_size == sizeof(int));
REQUIRE(arr.fortran_order == false);
REQUIRE(arr.num_vals == data.size());
REQUIRE(arr.num_bytes() == data.size() * sizeof(int));
// Verify the loaded data matches the original
const int* loaded = arr.data<int>();
for (size_t i = 0; i < data.size(); ++i) {
REQUIRE(loaded[i] == data[i]);
}
// Clean up the generated file
std::remove(filename.c_str());
}
// Unit test for npz_save with multiple arrays (including a multi-dimensional array)
TEST_CASE("npz_save writes multiple arrays (including multi-dimensional) and npz_load retrieves them correctly",
"[cnpy]") {
// Prepare data for a 1‑D integer array
std::vector<int> int_data = {1, 2, 3, 4, 5};
// Prepare data for a 2×3 double array (6 elements)
std::vector<double> double_data = {0.1, 0.2, 0.3, 0.4, 0.5, 0.6};
std::vector<size_t> double_shape = {2, 3};
std::string filename = "test_npz_save_multi.npz";
// Save the integer array using the vector overload (creates a 1‑D .npy inside the .npz)
cnpy::npz_save<int>(filename, "int_arr", int_data, "w");
// Append the multi‑dimensional double array using the pointer overload
cnpy::npz_save<double>(filename, "double_arr", double_data.data(), double_shape, "a");
// Load all arrays from the .npz file
cnpy::npz_t arrays = cnpy::npz_load(filename);
REQUIRE(arrays.size() == 2);
REQUIRE(arrays.count("int_arr") == 1);
REQUIRE(arrays.count("double_arr") == 1);
// Verify the integer array
const cnpy::NpyArray& int_arr = arrays.at("int_arr");
REQUIRE(int_arr.shape == std::vector<size_t>{int_data.size()});
REQUIRE(int_arr.word_size == sizeof(int));
REQUIRE(int_arr.fortran_order == false);
const int* loaded_int = int_arr.data<int>();
for (size_t i = 0; i < int_data.size(); ++i) {
REQUIRE(loaded_int[i] == int_data[i]);
}
// Verify the double array (multi‑dimensional)
const cnpy::NpyArray& double_arr = arrays.at("double_arr");
REQUIRE(double_arr.shape == double_shape);
REQUIRE(double_arr.word_size == sizeof(double));
REQUIRE(double_arr.fortran_order == false);
const double* loaded_double = double_arr.data<double>();
for (size_t i = 0; i < double_data.size(); ++i) {
REQUIRE(loaded_double[i] == Catch::Approx(double_data[i]));
}
// Verify loading a single array via the varname overload
cnpy::NpyArray int_arr_single = cnpy::npz_load(filename, "int_arr");
REQUIRE(int_arr_single.shape == std::vector<size_t>{int_data.size()});
const int* loaded_int_single = int_arr_single.data<int>();
for (size_t i = 0; i < int_data.size(); ++i) {
REQUIRE(loaded_int_single[i] == int_data[i]);
}
// Clean up the temporary .npz file
std::remove(filename.c_str());
}
// Unit test for npy_save append using pointer overload (multi-dimensional)
TEST_CASE("npy_save append works with pointer overload for multi-dimensional data", "[cnpy]") {
// Initial 2x2 array
std::vector<int> data1 = {0, 1, 2, 3};
std::vector<size_t> shape1 = {2, 2};
std::string filename = "test_npy_save_append_ptr.npy";
// Save initial data (write mode)
cnpy::npy_save<int>(filename, data1.data(), shape1, "w");
// Append additional 1x2 data
std::vector<int> data2 = {4, 5};
std::vector<size_t> shape2 = {1, 2};
cnpy::npy_save<int>(filename, data2.data(), shape2, "a");
// Load and verify combined shape and data
cnpy::NpyArray arr = cnpy::npy_load(filename);
std::vector<size_t> expected_shape = {3, 2};
REQUIRE(arr.shape == expected_shape);
REQUIRE(arr.word_size == sizeof(int));
REQUIRE(arr.fortran_order == false);
const int* loaded = arr.data<int>();
std::vector<int> expected = {0, 1, 2, 3, 4, 5};
for (size_t i = 0; i < expected.size(); ++i) {
REQUIRE(loaded[i] == expected[i]);
}
// Clean up
std::remove(filename.c_str());
}
// Unit test for npy_save append using vector overload (1‑D)
TEST_CASE("npy_save append works with vector overload for 1‑D data", "[cnpy]") {
// Initial data
std::vector<int> data1 = {1, 2, 3};
std::string filename = "test_npy_save_append_vec.npy";
// Save initial data (write mode)
cnpy::npy_save<int>(filename, data1, "w");
// Append additional data
std::vector<int> data2 = {4, 5, 6, 7};
cnpy::npy_save<int>(filename, data2, "a");
// Load and verify combined shape and data
cnpy::NpyArray arr = cnpy::npy_load(filename);
std::vector<size_t> expected_shape = {7};
REQUIRE(arr.shape == expected_shape);
REQUIRE(arr.word_size == sizeof(int));
REQUIRE(arr.fortran_order == false);
const int* loaded = arr.data<int>();
std::vector<int> expected = {1, 2, 3, 4, 5, 6, 7};
for (size_t i = 0; i < expected.size(); ++i) {
REQUIRE(loaded[i] == expected[i]);
}
// Clean up
std::remove(filename.c_str());
}
// Unit test for mmap-backed NpyArray constructor
TEST_CASE("NpyArray mmap constructor", "[cnpy]") {
std::vector<size_t> shape = {2, 3};
bool fortran = false;
std::string filename = "test_npy_mmap.npy";
// Create a new mmap-backed NpyArray using the library helper
cnpy::NpyArray arr = cnpy::new_npy_mmap<int>(filename, shape, fortran);
// Verify shape and properties
REQUIRE(arr.shape == shape);
REQUIRE(arr.word_size == sizeof(int));
REQUIRE(arr.fortran_order == fortran);
// Compute expected number of elements
size_t expected_num_vals = 1;
for (size_t dim : shape) {
expected_num_vals *= dim;
}
REQUIRE(arr.num_vals == expected_num_vals);
// Test mutable data access
int* data = arr.data<int>();
for (size_t i = 0; i < expected_num_vals; ++i) {
data[i] = static_cast<int>(i * 7);
}
// Verify values via const accessor
const cnpy::NpyArray& const_arr = arr;
const int* const_data = const_arr.data<int>();
for (size_t i = 0; i < expected_num_vals; ++i) {
REQUIRE(const_data[i] == static_cast<int>(i * 7));
}
// Clean up the temporary file
std::remove(filename.c_str());
}
TEST_CASE("new_npy_mmap unsigned short 1D", "[cnpy]") {
std::vector<size_t> shape = {10};
std::string filename = "test_npy_mmap_ushort.npy";
cnpy::NpyArray arr = cnpy::new_npy_mmap<unsigned short>(filename, shape, false);
// Verify shape and properties
REQUIRE(arr.shape == shape);
REQUIRE(arr.word_size == sizeof(unsigned short));
REQUIRE(arr.fortran_order == false);
// Compute expected number of elements
size_t expected_num_vals = 1;
for (size_t dim : shape) {
expected_num_vals *= dim;
}
REQUIRE(arr.num_vals == expected_num_vals);
// Test mutable data access
unsigned short* data = arr.data<unsigned short>();
for (size_t i = 0; i < expected_num_vals; ++i) {
data[i] = static_cast<unsigned short>(i * 3);
}
// Verify values via const accessor
const cnpy::NpyArray& const_arr = arr;
const unsigned short* const_data = const_arr.data<unsigned short>();
for (size_t i = 0; i < expected_num_vals; ++i) {
REQUIRE(const_data[i] == static_cast<unsigned short>(i * 3));
}
// Clean up the temporary file
std::remove(filename.c_str());
}
TEST_CASE("new_npy_mmap int 2D", "[cnpy]") {
std::vector<size_t> shape = {4, 5};
std::string filename = "test_npy_mmap_int_2d.npy";
cnpy::NpyArray arr = cnpy::new_npy_mmap<int>(filename, shape, false);
// Verify shape and properties
REQUIRE(arr.shape == shape);
REQUIRE(arr.word_size == sizeof(int));
REQUIRE(arr.fortran_order == false);
// Compute expected number of elements
size_t expected_num_vals = 1;
for (size_t dim : shape) {
expected_num_vals *= dim;
}
REQUIRE(arr.num_vals == expected_num_vals);
// Test mutable data access
int* data = arr.data<int>();
for (size_t i = 0; i < expected_num_vals; ++i) {
data[i] = static_cast<int>(i * 5);
}
// Verify values via const accessor
const cnpy::NpyArray& const_arr = arr;
const int* const_data = const_arr.data<int>();
for (size_t i = 0; i < expected_num_vals; ++i) {
REQUIRE(const_data[i] == static_cast<int>(i * 5));
}
// Clean up the temporary file
std::remove(filename.c_str());
}
TEST_CASE("new_npy_mmap double 3D", "[cnpy]") {
std::vector<size_t> shape = {2, 3, 4};
std::string filename = "test_npy_mmap_double_3d.npy";
cnpy::NpyArray arr = cnpy::new_npy_mmap<double>(filename, shape, false);
// Verify shape and properties
REQUIRE(arr.shape == shape);
REQUIRE(arr.word_size == sizeof(double));
REQUIRE(arr.fortran_order == false);
// Compute expected number of elements
size_t expected_num_vals = 1;
for (size_t dim : shape) {
expected_num_vals *= dim;
}
REQUIRE(arr.num_vals == expected_num_vals);
// Test mutable data access
double* data = arr.data<double>();
for (size_t i = 0; i < expected_num_vals; ++i) {
data[i] = static_cast<double>(i) * 0.1;
}
// Verify values via const accessor
const cnpy::NpyArray& const_arr = arr;
const double* const_data = const_arr.data<double>();
for (size_t i = 0; i < expected_num_vals; ++i) {
REQUIRE(const_data[i] == static_cast<double>(i) * 0.1);
}
// Clean up the temporary file
std::remove(filename.c_str());
}
// Test for npz_save with compression option
TEST_CASE("npz_save with compression option compresses data correctly", "[cnpy]") {
std::vector<int> data = {10, 20, 30, 40, 50, 60};
std::vector<size_t> shape = {2, 3};
std::string filename = "test_npz_compress.npz";
// Save with compression enabled
cnpy::npz_save<int>(filename, "arr_compressed", data.data(), shape, "w", true);
// Load and verify
cnpy::npz_t arrays = cnpy::npz_load(filename);
REQUIRE(arrays.size() == 1);
const cnpy::NpyArray& arr = arrays.at("arr_compressed");
REQUIRE(arr.shape == shape);
REQUIRE(arr.word_size == sizeof(int));
const int* loaded = arr.data<int>();
for (size_t i = 0; i < data.size(); ++i) {
REQUIRE(loaded[i] == data[i]);
}
// Clean up
std::remove(filename.c_str());
}
// New test cases for additional npy/npz types
TEST_CASE("npy_save/load for char type", "[cnpy]") {
std::vector<char> data = {'a', 'b', '\0', 'z'};
std::string filename = "test_npy_char.npy";
cnpy::npy_save<char>(filename, data);
cnpy::NpyArray arr = cnpy::npy_load(filename);
REQUIRE(arr.word_size == sizeof(char));
const char* loaded = arr.data<char>();
for (size_t i = 0; i < data.size(); ++i) {
REQUIRE(loaded[i] == data[i]);
}
std::remove(filename.c_str());
}
// Tests cnpy::npy_save/load for unsigned int and unsigned long long types
TEST_CASE("npy_save/load for unsigned int and unsigned long long types", "[cnpy]") {
{
std::vector<unsigned int> data = {0u, 1u, std::numeric_limits<unsigned int>::max()};
std::string filename = "test_npy_uint.npy";
cnpy::npy_save<unsigned int>(filename, data);
cnpy::NpyArray arr = cnpy::npy_load(filename);
REQUIRE(arr.word_size == sizeof(unsigned int));
const unsigned int* loaded = arr.data<unsigned int>();
for (size_t i = 0; i < data.size(); ++i) {
REQUIRE(loaded[i] == data[i]);
}
std::remove(filename.c_str());
}
{
std::vector<unsigned long long> data = {0ULL, 1ULL, std::numeric_limits<unsigned long long>::max()};
std::string filename = "test_npy_ull.npy";
cnpy::npy_save<unsigned long long>(filename, data);
cnpy::NpyArray arr = cnpy::npy_load(filename);
REQUIRE(arr.word_size == sizeof(unsigned long long));
const unsigned long long* loaded = arr.data<unsigned long long>();
for (size_t i = 0; i < data.size(); ++i) {
REQUIRE(loaded[i] == data[i]);
}
std::remove(filename.c_str());
}
}