"Fossies" - the Fresh Open Source Software Archive

Member "armadillo-9.800.3/tests/hdf5.cpp" (16 Jun 2016, 15281 Bytes) of package /linux/misc/armadillo-9.800.3.tar.xz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

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