hops
tests_high_five.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (c), 2017-2019, Blue Brain Project - EPFL
3  *
4  * Distributed under the Boost Software License, Version 1.0.
5  * (See accompanying file LICENSE_1_0.txt or copy at
6  * http://www.boost.org/LICENSE_1_0.txt)
7  *
8  */
9 
10 #include <complex>
11 #include <random>
12 #include <string>
13 #include <vector>
14 #include <boost/mpl/list.hpp>
15 
16 // Since, 1.59: semicolon has been removed from the end of the BOOST_GLOBAL_FIXTURE
17 // https://github.com/boostorg/test/commit/3f7216db3db2e11a768d8d0c8bb18632f106c466
18 #if BOOST_VERSION >= 105900
19 #define BOOST_GLOBAL_FIXTURE_END ;
20 #else
21 #define BOOST_GLOBAL_FIXTURE_END
22 #endif
23 
24 using complex = std::complex<double>;
25 
26 typedef boost::mpl::list<float, double> floating_numerics_test_types;
27 
28 typedef boost::mpl::list<int, unsigned int, long, unsigned long, unsigned char, char,
29  float, double, long long, unsigned long long, complex>
31 
32 typedef boost::mpl::list<int, unsigned int, long, unsigned long, unsigned char, char,
33  float, double>
35 
36 
37 template <typename T, typename Func>
38 void fillVec(std::vector<std::vector<T>>& v, std::vector<size_t> dims, const Func& f) {
39  v.resize(dims[0]);
40  dims.erase(dims.begin());
41  for (auto& subvec : v) {
42  fillVec(subvec, dims, f);
43  }
44 }
45 
46 template <typename T, typename Func>
47 void fillVec(std::vector<T>& v, std::vector<size_t> dims, const Func& f) {
48  v.resize(dims[0]);
49  std::generate(v.begin(), v.end(), f);
50 }
51 
52 
53 template <typename T>
54 bool checkLength(const std::vector<T>& v, std::vector<size_t> dims) {
55  return (dims.size() == 1 && v.size() == dims[0]);
56 }
57 
58 template <typename T>
59 bool checkLength(const std::vector<std::vector<T>>& v, std::vector<size_t> dims) {
60  size_t dim0 = dims[0];
61  dims.erase(dims.begin());
62  if (v.size() != dim0) {
63  return false;
64  }
65  return checkLength(v[0], dims);
66 }
67 
68 
69 template <typename T, typename Func>
70 void generate2D(T* table, size_t x, size_t y, Func& func) {
71  for (size_t i = 0; i < x; i++) {
72  for (size_t j = 0; j < y; j++) {
73  table[i][j] = func();
74  }
75  }
76 }
77 
78 
79 template <typename T>
82  : _init(0)
83  , _inc(T(1) + T(1) / T(10)) {}
84 
85  T operator()() {
86  T ret = _init;
87  _init = static_cast<T>(_init + _inc);
88  return ret;
89  }
90 
91  T _init, _inc;
92 };
93 
94 template <>
96  : _init(0, 0)
97  , _inc(complex(1, 1) + complex(1, 1) / complex(10)) {}
98 
99 template <>
100 struct ContentGenerate<char> {
102  : _init('a') {}
103 
104  char operator()() {
105  char ret = _init;
106  if (++_init >= ('a' + 26))
107  _init = 'a';
108  return ret;
109  }
110 
111  char _init;
112 };
113 
114 template <>
115 struct ContentGenerate<std::string> {
117 
120  std::string random_string;
121  std::mt19937_64 rgen;
122  rgen.seed(88);
123  std::uniform_int_distribution<unsigned> int_dist(0, 1000);
124  const size_t size_string = int_dist(rgen);
125 
126  random_string.resize(size_string);
127  std::generate(random_string.begin(), random_string.end(), gen);
128  return random_string;
129  }
130 };
131 
132 
133 template <typename T>
135  std::string name = typeid(T).name();
136 #if defined(WIN32)
137  //Replace illegal windows file path characters
138  std::replace(std::begin(name), std::end(name), ' ', '_');
139  std::replace(std::begin(name), std::end(name), '<', '_');
140  std::replace(std::begin(name), std::end(name), '>', '_');
141  std::replace(std::begin(name), std::end(name), ':', '_');
142 #endif
143  return name;
144 }
145 
146 template <typename ElemT, typename DataT>
147 inline HighFive::DataSet
148 readWriteDataset(const DataT& ndvec,
149  DataT& result,
150  const size_t ndims,
151  const std::string& struct_t) {
152  using namespace HighFive;
153  const std::string DATASET_NAME("dset");
154 
155  std::ostringstream filename;
156  filename << "h5_rw_" << struct_t << "_" << ndims << "d_"
157  << typeNameHelper<ElemT>() << "_test.h5";
158 
159  // Create a new file using the default property lists.
160  File file(filename.str(), File::Truncate);
161 
162  // Create a dataset with type T points
163  DataSet dataset = file.createDataSet<ElemT>(DATASET_NAME, DataSpace::From(ndvec));
164  dataset.write(ndvec);
165 
166  dataset.read(result);
167  return dataset;
168 }
169 
ContentGenerate< std::string >::ContentGenerate
ContentGenerate()
Definition: tests_high_five.hpp:116
ContentGenerate< char >
Definition: tests_high_five.hpp:100
ContentGenerate::ContentGenerate
ContentGenerate()
Definition: tests_high_five.hpp:81
dataset_test_types
boost::mpl::list< int, unsigned int, long, unsigned long, unsigned char, char, float, double > dataset_test_types
Definition: tests_high_five.hpp:34
ContentGenerate::_init
T _init
Definition: tests_high_five.hpp:91
ContentGenerate< char >::_init
char _init
Definition: tests_high_five.hpp:111
ContentGenerate::_inc
T _inc
Definition: tests_high_five.hpp:91
HighFive::SliceTraits::read
void read(T &array) const
Definition: H5Slice_traits_misc.hpp:158
generate2D
void generate2D(T *table, size_t x, size_t y, Func &func)
Definition: tests_high_five.hpp:70
ContentGenerate< char >::ContentGenerate
ContentGenerate()
Definition: tests_high_five.hpp:101
HighFive::DataSpace::From
static DataSpace From(const ScalarValue &scalar_value)
Definition: H5Dataspace_misc.hpp:129
typeNameHelper
std::string typeNameHelper()
Definition: tests_high_five.hpp:134
HighFive::SliceTraits::write
void write(const T &buffer)
Definition: H5Slice_traits_misc.hpp:200
ContentGenerate
Definition: tests_high_five.hpp:80
checkLength
bool checkLength(const std::vector< T > &v, std::vector< size_t > dims)
Definition: tests_high_five.hpp:54
ContentGenerate< char >::operator()
char operator()()
Definition: tests_high_five.hpp:104
ContentGenerate< std::string >::operator()
std::string operator()()
Definition: tests_high_five.hpp:118
floating_numerics_test_types
boost::mpl::list< float, double > floating_numerics_test_types
Definition: tests_high_five.hpp:26
HighFive::File
File class.
Definition: H5File.hpp:24
HighFive::File::Truncate
@ Truncate
Open flag: Truncate a file if already existing.
Definition: H5File.hpp:37
numerical_test_types
boost::mpl::list< int, unsigned int, long, unsigned long, unsigned char, char, float, double, long long, unsigned long long, complex > numerical_test_types
Definition: tests_high_five.hpp:30
readWriteDataset
HighFive::DataSet readWriteDataset(const DataT &ndvec, DataT &result, const size_t ndims, const std::string &struct_t)
Definition: tests_high_five.hpp:148
ContentGenerate::operator()
T operator()()
Definition: tests_high_five.hpp:85
string
NAME string(REPLACE ".cpp" "_bin" example_name ${example_filename}) if($
Definition: hops/Third-party/HighFive/src/examples/CMakeLists.txt:6
fillVec
void fillVec(std::vector< std::vector< T >> &v, std::vector< size_t > dims, const Func &f)
Definition: tests_high_five.hpp:38
HighFive::NodeTraits::createDataSet
DataSet createDataSet(const std::string &dataset_name, const DataSpace &space, const DataType &type, const DataSetCreateProps &createProps=DataSetCreateProps(), const DataSetAccessProps &accessProps=DataSetAccessProps())
createDataSet Create a new dataset in the current file of datatype type and of size space
Definition: H5Node_traits_misc.hpp:36
complex
std::complex< double > complex
Definition: tests_high_five.hpp:24
HighFive::DataSet
Class representing a dataset.
Definition: H5DataSet.hpp:27
HighFive
Definition: H5_definitions.hpp:15
DATASET_NAME
const std::string DATASET_NAME("dset")