MachineIntelligenceCore:Algorithms
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
MatrixArrayTests.cpp
Go to the documentation of this file.
1 
26 #include <gtest/gtest.h>
27 
28 #include <types/MatrixArray.hpp>
29 
30 #include <fstream>
31 // Include headers that implement a archive in simple text format
32 #include <boost/archive/text_iarchive.hpp>
33 #include <boost/archive/text_oarchive.hpp>
34 
38 TEST(MatrixArray, Dimensions2x3x4) {
39  // Default sizes of matrices.
40  const size_t N = 2;
41  const size_t M = 3;
42  const size_t B = 4;
43 
44  // Test MatrixArray operations.
45  mic::types::MatrixArray<double> ma("test_array");
46 
47  // Two different methods of adding matrices to array.
48  ma.add (
49  {
50  std::make_tuple ( "x", M, B ), // input
51  std::make_tuple ( "y", N, B ) // output
52  } );
53 
54  ma.add (std::make_tuple ( "w", N, M ));
55 
56  ASSERT_EQ(ma["x"]->rows(), M);
57  ASSERT_EQ(ma["x"]->cols(), B);
58  ASSERT_EQ(ma["y"]->rows(), N);
59  ASSERT_EQ(ma["y"]->cols(), B);
60  ASSERT_EQ(ma["w"]->rows(), N);
61  ASSERT_EQ(ma["w"]->cols(), M);
62 }
63 
64 
68 TEST(MatrixArray, Serialization) {
69  // Default sizes of matrices.
70  const size_t N = 2;
71  const size_t M = 3;
72 
73  // Test MatrixArray operations.
74  mic::types::MatrixArray<double> ma1("test_array");
75 
76  // Add single matrix.
77  ma1.add (std::make_tuple ( "w", N, M ));
78  ma1["w"]->randn();
79 
80 // std::cout<<"Saved MatrixArray = " << ma1;
81  const char* fileName = "saved.txt";
82  // Save data.
83  {
84  // Create an output archive.
85  std::ofstream ofs(fileName);
86  boost::archive::text_oarchive ar(ofs);
87  // Write data
88  ar & ma1;
89  }
90 
91  // Restore data.
93  {
94  // Create and input archive.
95  std::ifstream ifs(fileName);
96  boost::archive::text_iarchive ar(ifs);
97  // Load data.
98  ar & restored_ma;
99 // std::cout << "Restored MatrixArray = " << restored_ma << std::endl;
100  }
101 
102  for (size_t i =0; i< (size_t)ma1["w"]->size(); i++)
103  ASSERT_EQ((*ma1["w"])(i), (*restored_ma["w"])(i));
104 }
105 
106 
107 int main(int argc, char **argv) {
108  testing::InitGoogleTest(&argc, argv);
109  return RUN_ALL_TESTS();
110 }
111 
112 
TEST(MatrixArray, Dimensions2x3x4)
int main(int argc, char **argv)
A dynamic array of matrices. It's just what it looks like - std::vector<Matrix>; elements are are acc...
Definition: MatrixArray.hpp:73
void add(std::initializer_list< std::tuple< std::string, size_t, size_t > > params_)