1 // SPDX-License-Identifier: Apache-2.0
2 //
3 // Copyright 2011-2017 Ryan Curtin (http://www.ratml.org/)
4 // Copyright 2017 National ICT Australia (NICTA)
5 //
6 // Licensed under the Apache License, Version 2.0 (the "License");
7 // you may not use this file except in compliance with the License.
8 // You may obtain a copy of the License at
9 // http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 // ------------------------------------------------------------------------
17 
18 #include <cstdio>
19 #include <armadillo>
20 
21 #include "catch.hpp"
22 
23 using namespace arma;
24 
25 #if defined(ARMA_USE_HDF5)
26 
27 TEST_CASE("hdf5_u8_test")
28   {
29   arma::Mat<u8> a;
30   a.randu(20, 20);
31 
32   // Save first.
33   a.save("file.h5", hdf5_binary);
34 
35   // Load as different matrix.
36   arma::Mat<u8> b;
37   b.load("file.h5", hdf5_binary);
38 
39   // Check that they are the same.
40   for (uword i = 0; i < a.n_elem; ++i)
41     {
42     REQUIRE( a[i] == b[i] );
43     }
44 
45   // Now autoload.
46   arma::Mat<u8> c;
47   c.load("file.h5");
48 
49   // Check that they are the same.
50   for (uword i = 0; i < a.n_elem; ++i)
51     {
52     REQUIRE( a[i] == c[i] );
53     }
54 
55   std::remove("file.h5");
56   }
57 
58 
59 
60 TEST_CASE("hdf5_u16_test")
61   {
62   arma::Mat<u16> a;
63   a.randu(20, 20);
64 
65   // Save first.
66   a.save("file.h5", hdf5_binary);
67 
68   // Load as different matrix.
69   arma::Mat<u16> b;
70   b.load("file.h5", hdf5_binary);
71 
72   // Check that they are the same.
73   for (uword i = 0; i < a.n_elem; ++i)
74     {
75     REQUIRE( a[i] == b[i] );
76     }
77 
78   // Now autoload.
79   arma::Mat<u16> c;
80   c.load("file.h5");
81 
82   // Check that they are the same.
83   for (uword i = 0; i < a.n_elem; ++i)
84     {
85     REQUIRE( a[i] == c[i] );
86     }
87 
88   std::remove("file.h5");
89   }
90 
91 
92 
93 TEST_CASE("hdf5_u32_test")
94   {
95   arma::Mat<u32> a;
96   a.randu(20, 20);
97 
98   // Save first.
99   a.save("file.h5", hdf5_binary);
100 
101   // Load as different matrix.
102   arma::Mat<u32> b;
103   b.load("file.h5", hdf5_binary);
104 
105   // Check that they are the same.
106   for (uword i = 0; i < a.n_elem; ++i)
107     {
108     REQUIRE( a[i] == b[i] );
109     }
110 
111   // Now autoload.
112   arma::Mat<u32> c;
113   c.load("file.h5");
114 
115   // Check that they are the same.
116   for (uword i = 0; i < a.n_elem; ++i)
117     {
118     REQUIRE( a[i] == c[i] );
119     }
120 
121   std::remove("file.h5");
122   }
123 
124 
125 
126 #ifdef ARMA_USE_U64S64
127 TEST_CASE("hdf5_u64_test")
128   {
129   arma::Mat<u64> a;
130   a.randu(20, 20);
131 
132   // Save first.
133   a.save("file.h5", hdf5_binary);
134 
135   // Load as different matrix.
136   arma::Mat<u64> b;
137   b.load("file.h5", hdf5_binary);
138 
139   // Check that they are the same.
140   for (uword i = 0; i < a.n_elem; ++i)
141     {
142     REQUIRE( a[i] == b[i] );
143     }
144 
145   // Now autoload.
146   arma::Mat<u64> c;
147   c.load("file.h5");
148 
149   // Check that they are the same.
150   for (uword i = 0; i < a.n_elem; ++i)
151     {
152     REQUIRE( a[i] == c[i] );
153     }
154 
155   std::remove("file.h5");
156   }
157 #endif
158 
159 
160 
161 TEST_CASE("hdf5_s8_test")
162   {
163   arma::Mat<s8> a;
164   a.randu(20, 20);
165 
166   // Save first.
167   a.save("file.h5", hdf5_binary);
168 
169   // Load as different matrix.
170   arma::Mat<s8> b;
171   b.load("file.h5", hdf5_binary);
172 
173   // Check that they are the same.
174   for (uword i = 0; i < a.n_elem; ++i)
175     {
176     REQUIRE( a[i] == b[i] );
177     }
178 
179   // Now autoload.
180   arma::Mat<s8> c;
181   c.load("file.h5");
182 
183   // Check that they are the same.
184   for (uword i = 0; i < a.n_elem; ++i)
185     {
186     REQUIRE( a[i] == c[i] );
187     }
188 
189   std::remove("file.h5");
190   }
191 
192 
193 
194 TEST_CASE("hdf5_s16_test")
195   {
196   arma::Mat<s16> a;
197   a.randu(20, 20);
198 
199   // Save first.
200   a.save("file.h5", hdf5_binary);
201 
202   // Load as different matrix.
203   arma::Mat<s16> b;
204   b.load("file.h5", hdf5_binary);
205 
206   // Check that they are the same.
207   for (uword i = 0; i < a.n_elem; ++i)
208     {
209     REQUIRE( a[i] == b[i] );
210     }
211 
212   // Now autoload.
213   arma::Mat<s16> c;
214   c.load("file.h5");
215 
216   // Check that they are the same.
217   for (uword i = 0; i < a.n_elem; ++i)
218     {
219     REQUIRE( a[i] == c[i] );
220     }
221 
222   std::remove("file.h5");
223   }
224 
225 
226 
227 TEST_CASE("hdf5_s32_test")
228   {
229   arma::Mat<s32> a;
230   a.randu(20, 20);
231 
232   // Save first.
233   a.save("file.h5", hdf5_binary);
234 
235   // Load as different matrix.
236   arma::Mat<s32> b;
237   b.load("file.h5", hdf5_binary);
238 
239   // Check that they are the same.
240   for (uword i = 0; i < a.n_elem; ++i)
241     {
242     REQUIRE( a[i] == b[i] );
243     }
244 
245   // Now autoload.
246   arma::Mat<s32> c;
247   c.load("file.h5");
248 
249   // Check that they are the same.
250   for (uword i = 0; i < a.n_elem; ++i)
251     {
252     REQUIRE( a[i] == c[i] );
253     }
254 
255   std::remove("file.h5");
256   }
257 
258 
259 
260 #ifdef ARMA_USE_U64S64
261 TEST_CASE("hdf5_s64_test")
262   {
263   arma::Mat<s64> a;
264   a.randu(20, 20);
265 
266   // Save first.
267   a.save("file.h5", hdf5_binary);
268 
269   // Load as different matrix.
270   arma::Mat<s64> b;
271   b.load("file.h5", hdf5_binary);
272 
273   // Check that they are the same.
274   for (uword i = 0; i < a.n_elem; ++i)
275     {
276     REQUIRE( a[i] == b[i] );
277     }
278 
279   // Now autoload.
280   arma::Mat<s64> c;
281   c.load("file.h5");
282 
283   // Check that they are the same.
284   for (uword i = 0; i < a.n_elem; ++i)
285     {
286     REQUIRE( a[i] == c[i] );
287     }
288 
289   std::remove("file.h5");
290   }
291 #endif
292 
293 
294 
295 TEST_CASE("hdf5_char_test")
296   {
297   arma::Mat<char> a;
298   a.randu(20, 20);
299 
300   // Save first.
301   a.save("file.h5", hdf5_binary);
302 
303   // Load as different matrix.
304   arma::Mat<char> b;
305   b.load("file.h5", hdf5_binary);
306 
307   // Check that they are the same.
308   for (uword i = 0; i < a.n_elem; ++i)
309     {
310     REQUIRE( a[i] == b[i] );
311     }
312 
313   // Now autoload.
314   arma::Mat<char> c;
315   c.load("file.h5");
316 
317   // Check that they are the same.
318   for (uword i = 0; i < a.n_elem; ++i)
319     {
320     REQUIRE( a[i] == c[i] );
321     }
322 
323   std::remove("file.h5");
324   }
325 
326 
327 
328 TEST_CASE("hdf5_int_test")
329   {
330   arma::Mat<signed int> a;
331   a.randu(20, 20);
332 
333   // Save first.
334   a.save("file.h5", hdf5_binary);
335 
336   // Load as different matrix.
337   arma::Mat<signed int> b;
338   b.load("file.h5", hdf5_binary);
339 
340   // Check that they are the same.
341   for (uword i = 0; i < a.n_elem; ++i)
342     {
343     REQUIRE( a[i] == b[i] );
344     }
345 
346   // Now autoload.
347   arma::Mat<signed int> c;
348   c.load("file.h5");
349 
350   // Check that they are the same.
351   for (uword i = 0; i < a.n_elem; ++i)
352     {
353     REQUIRE( a[i] == c[i] );
354     }
355 
356   std::remove("file.h5");
357   }
358 
359 
360 
361 TEST_CASE("hdf5_uint_test")
362   {
363   arma::Mat<unsigned int> a;
364   a.randu(20, 20);
365 
366   // Save first.
367   a.save("file.h5", hdf5_binary);
368 
369   // Load as different matrix.
370   arma::Mat<unsigned int> b;
371   b.load("file.h5", hdf5_binary);
372 
373   // Check that they are the same.
374   for (uword i = 0; i < a.n_elem; ++i)
375     {
376     REQUIRE( a[i] == b[i] );
377     }
378 
379   // Now autoload.
380   arma::Mat<unsigned int> c;
381   c.load("file.h5");
382 
383   // Check that they are the same.
384   for (uword i = 0; i < a.n_elem; ++i)
385     {
386     REQUIRE( a[i] == c[i] );
387     }
388 
389   std::remove("file.h5");
390   }
391 
392 
393 
394 TEST_CASE("hdf5_short_test")
395   {
396   arma::Mat<signed short> a;
397   a.randu(20, 20);
398 
399   // Save first.
400   a.save("file.h5", hdf5_binary);
401 
402   // Load as different matrix.
403   arma::Mat<signed short> b;
404   b.load("file.h5", hdf5_binary);
405 
406   // Check that they are the same.
407   for (uword i = 0; i < a.n_elem; ++i)
408     {
409     REQUIRE( a[i] == b[i] );
410     }
411 
412   // Now autoload.
413   arma::Mat<signed short> c;
414   c.load("file.h5");
415 
416   // Check that they are the same.
417   for (uword i = 0; i < a.n_elem; ++i)
418     {
419     REQUIRE( a[i] == c[i] );
420     }
421 
422   std::remove("file.h5");
423   }
424 
425 
426 
427 TEST_CASE("hdf5_ushort_test")
428   {
429   arma::Mat<unsigned short> a;
430   a.randu(20, 20);
431 
432   // Save first.
433   a.save("file.h5", hdf5_binary);
434 
435   // Load as different matrix.
436   arma::Mat<unsigned short> b;
437   b.load("file.h5", hdf5_binary);
438 
439   // Check that they are the same.
440   for (uword i = 0; i < a.n_elem; ++i)
441     {
442     REQUIRE( a[i] == b[i] );
443     }
444 
445   // Now autoload.
446   arma::Mat<unsigned short> c;
447   c.load("file.h5");
448 
449   // Check that they are the same.
450   for (uword i = 0; i < a.n_elem; ++i)
451     {
452     REQUIRE( a[i] == c[i] );
453     }
454 
455   std::remove("file.h5");
456   }
457 
458 
459 
460 TEST_CASE("hdf5_long_test")
461   {
462   arma::Mat<signed long> a;
463   a.randu(20, 20);
464 
465   // Save first.
466   a.save("file.h5", hdf5_binary);
467 
468   // Load as different matrix.
469   arma::Mat<signed long> b;
470   b.load("file.h5", hdf5_binary);
471 
472   // Check that they are the same.
473   for (uword i = 0; i < a.n_elem; ++i)
474     {
475     REQUIRE( a[i] == b[i] );
476     }
477 
478   // Now autoload.
479   arma::Mat<signed long> c;
480   c.load("file.h5");
481 
482   // Check that they are the same.
483   for (uword i = 0; i < a.n_elem; ++i)
484     {
485     REQUIRE( a[i] == c[i] );
486     }
487 
488   std::remove("file.h5");
489   }
490 
491 
492 
493 TEST_CASE("hdf5_ulong_test")
494   {
495   arma::Mat<unsigned long> a;
496   a.randu(20, 20);
497 
498   // Save first.
499   a.save("file.h5", hdf5_binary);
500 
501   // Load as different matrix.
502   arma::Mat<unsigned long> b;
503   b.load("file.h5", hdf5_binary);
504 
505   // Check that they are the same.
506   for (uword i = 0; i < a.n_elem; ++i)
507     {
508     REQUIRE( a[i] == b[i] );
509     }
510 
511   // Now autoload.
512   arma::Mat<unsigned long> c;
513   c.load("file.h5");
514 
515   // Check that they are the same.
516   for (uword i = 0; i < a.n_elem; ++i)
517     {
518     REQUIRE( a[i] == c[i] );
519     }
520 
521   std::remove("file.h5");
522   }
523 
524 
525 
526 #ifdef ARMA_USE_U64S64
527 TEST_CASE("hdf5_llong_test")
528   {
529   arma::Mat<signed long long> a;
530   a.randu(20, 20);
531 
532   // Save first.
533   a.save("file.h5", hdf5_binary);
534 
535   // Load as different matrix.
536   arma::Mat<signed long long> b;
537   b.load("file.h5", hdf5_binary);
538 
539   // Check that they are the same.
540   for (uword i = 0; i < a.n_elem; ++i)
541     {
542     REQUIRE( a[i] == b[i] );
543     }
544 
545   // Now autoload.
546   arma::Mat<signed long long> c;
547   c.load("file.h5");
548 
549   // Check that they are the same.
550   for (uword i = 0; i < a.n_elem; ++i)
551     {
552     REQUIRE( a[i] == c[i] );
553     }
554 
555   std::remove("file.h5");
556   }
557 
558 
559 
560 TEST_CASE("hdf5_ullong_test")
561   {
562   arma::Mat<unsigned long long> a;
563   a.randu(20, 20);
564 
565   // Save first.
566   a.save("file.h5", hdf5_binary);
567 
568   // Load as different matrix.
569   arma::Mat<unsigned long long> b;
570   b.load("file.h5", hdf5_binary);
571 
572   // Check that they are the same.
573   for (uword i = 0; i < a.n_elem; ++i)
574     {
575     REQUIRE( a[i] == b[i] );
576     }
577 
578   // Now autoload.
579   arma::Mat<unsigned long long> c;
580   c.load("file.h5");
581 
582   // Check that they are the same.
583   for (uword i = 0; i < a.n_elem; ++i)
584     {
585     REQUIRE( a[i] == c[i] );
586     }
587 
588   std::remove("file.h5");
589   }
590 #endif
591 
592 
593 
594 TEST_CASE("hdf5_float_test")
595   {
596   arma::Mat<float> a;
597   a.randu(20, 20);
598 
599   // Save first.
600   a.save("file.h5", hdf5_binary);
601 
602   // Load as different matrix.
603   arma::Mat<float> b;
604   b.load("file.h5", hdf5_binary);
605 
606   // Check that they are the same.
607   for (uword i = 0; i < a.n_elem; ++i)
608     {
609     REQUIRE( a[i] == b[i] );
610     }
611 
612   // Now autoload.
613   arma::Mat<float> c;
614   c.load("file.h5");
615 
616   // Check that they are the same.
617   for (uword i = 0; i < a.n_elem; ++i)
618     {
619     REQUIRE( a[i] == c[i] );
620     }
621 
622   std::remove("file.h5");
623   }
624 
625 
626 
627 TEST_CASE("hdf5_double_test")
628   {
629   arma::Mat<double> a;
630   a.randu(20, 20);
631 
632   // Save first.
633   a.save("file.h5", hdf5_binary);
634 
635   // Load as different matrix.
636   arma::Mat<double> b;
637   b.load("file.h5", hdf5_binary);
638 
639   // Check that they are the same.
640   for (uword i = 0; i < a.n_elem; ++i)
641     {
642     REQUIRE( a[i] == b[i] );
643     }
644 
645   // Now autoload.
646   arma::Mat<double> c;
647   c.load("file.h5");
648 
649   // Check that they are the same.
650   for (uword i = 0; i < a.n_elem; ++i)
651     {
652     REQUIRE( a[i] == c[i] );
653     }
654 
655   std::remove("file.h5");
656   }
657 
658 
659 
660 TEST_CASE("hdf5_complex_float_test")
661   {
662   arma::Mat<std::complex<float> > a;
663   a.randu(20, 20);
664 
665   // Save first.
666   a.save("file.h5", hdf5_binary);
667 
668   // Load as different matrix.
669   arma::Mat<std::complex<float> > b;
670   b.load("file.h5", hdf5_binary);
671 
672   // Check that they are the same.
673   for (uword i = 0; i < a.n_elem; ++i)
674     {
675     REQUIRE( a[i] == b[i] );
676     }
677 
678   // Now autoload.
679   arma::Mat<std::complex<float> > c;
680   c.load("file.h5");
681 
682   // Check that they are the same.
683   for (uword i = 0; i < a.n_elem; ++i)
684     {
685     REQUIRE( a[i] == c[i] );
686     }
687 
688   std::remove("file.h5");
689   }
690 
691 
692 
693 TEST_CASE("hdf5_complex_double_test")
694   {
695   arma::Mat<std::complex<double> > a;
696   a.randu(20, 20);
697 
698   // Save first.
699   a.save("file.h5", hdf5_binary);
700 
701   // Load as different matrix.
702   arma::Mat<std::complex<double> > b;
703   b.load("file.h5", hdf5_binary);
704 
705   // Check that they are the same.
706   for (uword i = 0; i < a.n_elem; ++i)
707     REQUIRE( a[i] == b[i] );
708 
709   // Now autoload.
710   arma::Mat<std::complex<double> > c;
711   c.load("file.h5");
712 
713   // Check that they are the same.
714   for (uword i = 0; i < a.n_elem; ++i)
715     {
716     REQUIRE( a[i] == c[i] );
717     }
718 
719   std::remove("file.h5");
720   }
721 
722 
723 
724 TEST_CASE("hdf5_dataset_append_test")
725   {
726   arma::Mat<double> a;
727   a.randu(20, 20);
728 
729   // Save first dataset.
730   a.save( hdf5_name("file.h5", "dataset1") );
731 
732   arma::Mat<double> b;
733   b.randu(10, 10);
734 
735   // Save second dataset.
736   b.save( hdf5_name("file.h5", "dataset2", hdf5_opts::append) );
737 
738   // Load first dataset as different matrix.
739   arma::Mat<double> c;
740   c.load( hdf5_name("file.h5", "dataset1") );
741 
742   // Check that they are the same.
743   for (uword i = 0; i < a.n_elem; ++i)
744     {
745     REQUIRE( a[i] == c[i] );
746     }
747 
748   // Load second dataset as different matrix.
749   arma::Mat<double> d;
750   d.load( hdf5_name("file.h5", "dataset2") );
751 
752   // Check that they are the same.
753   for (uword i = 0; i < b.n_elem; ++i)
754     {
755     REQUIRE( b[i] == d[i] );
756     }
757 
758   std::remove("file.h5");
759   }
760 
761 TEST_CASE("hdf5_cube_dataset_append_test")
762   {
763   arma::Mat<double> a;
764   a.randu(20, 20);
765 
766   // Save first dataset.
767   a.save( hdf5_name("file.h5", "dataset1") );
768 
769   arma::Cube<double> b;
770   b.randu(10, 10, 10);
771 
772   // Save second dataset.
773   b.save( hdf5_name("file.h5", "dataset2", hdf5_opts::append) );
774 
775   // Load first dataset as different matrix.
776   arma::Mat<double> c;
777   c.load( hdf5_name("file.h5", "dataset1") );
778 
779   // Check that they are the same.
780   for (uword i = 0; i < a.n_elem; ++i)
781     {
782     REQUIRE( a[i] == c[i] );
783     }
784 
785   // Load second dataset as different matrix.
786   arma::Cube<double> d;
787   d.load( hdf5_name("file.h5", "dataset2") );
788 
789   // Check that they are the same.
790   for (uword i = 0; i < b.n_elem; ++i)
791     {
792     REQUIRE( b[i] == d[i] );
793     }
794 
795   std::remove("file.h5");
796   }
797 
798 
799 TEST_CASE("hdf5_dataset_append-overwrite-test")
800   {
801   arma::Mat<double> a;
802   a.randu(20, 20);
803 
804   // Save first dataset.
805   a.save( hdf5_name("file.h5", "dataset1") );
806 
807   arma::Mat<double> b;
808   b.randu(10, 10);
809 
810   // Save second dataset.
811   b.save( hdf5_name("file.h5", "dataset2") );
812 
813   // Load first dataset as different matrix and check that first dataset has been overwritten.
814   arma::Mat<double> c;
815   REQUIRE_FALSE( c.load( hdf5_name("file.h5", "dataset1") ) );
816 
817   // Load second dataset as different matrix.
818   arma::Mat<double> d;
819   d.load( hdf5_name("file.h5", "dataset2") );
820 
821   // Check that they are the same.
822   for (uword i = 0; i < b.n_elem; ++i)
823     {
824     REQUIRE( b[i] == d[i] );
825     }
826 
827   std::remove("file.h5");
828   }
829 
830 
831 
832 TEST_CASE("hdf5_dataset_same_dataset_twice_test")
833   {
834   arma::Mat<double> a;
835   a.randu(20, 20);
836 
837   // Save first dataset.
838   a.save(hdf5_name("file.h5", "dataset1"), hdf5_binary);
839 
840   arma::Mat<double> b;
841   b.randu(10, 10);
842 
843   // Append second dataset with same name, causing failure.
844   REQUIRE_FALSE( b.save(hdf5_name("file.h5", "dataset1", hdf5_opts::append) ) );
845 
846   std::remove("file.h5");
847   }
848 
849 #endif
850