SynAPSE  0.6.0
Synergistic Analysis of Phase Space Evolution
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends
Assert-inl.hh
1 template<typename T> void IsNonZero(const std::string& location,
2  const std::string& what,
3  const T& value) {
4 
5  if ( !value )
6  throw(Exceptions::Exception(Exceptions::nonRecoverable, what+" is zero", location));
7 }
8 
9 template<typename T> void IsGreater(const std::string& location,
10  const std::string& what,
11  const T& value,
12  const T& limit,
13  const bool strictly) {
14 
15  if ( !strictly && value < limit ) {
16  throw(Exceptions::Exception(Exceptions::nonRecoverable,
17  what+" is strictly lower than "+std::to_string(limit),
18  location));
19  } else if ( strictly && value <= limit ) {
20  throw(Exceptions::Exception(Exceptions::nonRecoverable,
21  what+" is lower or equal to "+std::to_string(limit),
22  location));
23  }
24 }
25 
26 template<typename T> void IsLower(const std::string& location,
27  const std::string& what,
28  const T& value,
29  const T& limit,
30  const bool strictly) {
31 
32  if ( !strictly && value > limit ) {
33  throw(Exceptions::Exception(Exceptions::nonRecoverable,
34  what+" is strictly greater than "+std::to_string(limit),
35  location));
36  } else if ( strictly && value >= limit ) {
37  throw(Exceptions::Exception(Exceptions::nonRecoverable,
38  what+" is greater or equal to "+std::to_string(limit),
39  location));
40  }
41 }
42 
43 template<typename T> void IsEqual(const std::string& location,
44  const std::string& what,
45  const T& a,
46  const T& b) {
47 
48  if ( a != b )
49  throw(Exceptions::Exception(Exceptions::nonRecoverable, what+" are not equal", location));
50 }
51 
52 template<typename T> void IsNotEmpty(const std::string& location,
53  const std::string& what,
54  const std::vector<T>& vector) {
55 
56  if ( !vector.size() )
57  throw(Exceptions::Exception(Exceptions::nonRecoverable, what+" is empty", location));
58 }
59 
60 template<typename T> void HasAtLeast(const std::string& location,
61  const std::string& what,
62  const std::vector<T>& vector,
63  const size_t n) {
64 
65  if ( vector.size() < n )
66  throw(Exceptions::Exception(Exceptions::nonRecoverable,
67  what+" contains less than "+std::to_string(n)+" elements",
68  location));
69 }
70 
71 template<typename T> void HasAtMost(const std::string& location,
72  const std::string& what,
73  const std::vector<T>& vector,
74  const size_t n) {
75 
76  if ( vector.size() > n )
77  throw(Exceptions::Exception(Exceptions::nonRecoverable,
78  what+" contains more than "+std::to_string(n)+" elements",
79  location));
80 }
81 
82 template<typename T> void SameSize(const std::string& location,
83  const std::string& what,
84  const std::vector<T>& va,
85  const std::vector<T>& vb) {
86 
87  if ( va.size() != vb.size() )
88  throw(Exceptions::Exception(Exceptions::nonRecoverable,
89  what+" have different dimensions",
90  location));
91 }
92 
93 template<typename T> void SameSize(const std::string& location,
94  const std::string& what,
95  const std::vector<std::vector<T>>& ma,
96  const std::vector<std::vector<T>>& mb) {
97 
98  if ( ma.size() != mb.size() || ma[0].size() != mb[0].size() )
99  throw(Exceptions::Exception(Exceptions::nonRecoverable,
100  what+" have different dimensions",
101  location));
102 }
103 
104 template<typename T> void Multiplicable(const std::string& location,
105  const std::string& what,
106  const std::vector<std::vector<T>>& ma,
107  const std::vector<std::vector<T>>& mb) {
108 
109  if ( ma[0].size() != mb.size() )
110  throw(Exceptions::Exception(Exceptions::nonRecoverable,
111  what+" have incompatible dimensions to be multiplied",
112  location));
113 }
114 
115 template<typename T> void IsSquare(const std::string& location,
116  const std::string& what,
117  const std::vector<std::vector<T>>& matrix) {
118 
119  if ( matrix.size() != matrix[0].size() )
120  throw(Exceptions::Exception(Exceptions::nonRecoverable,
121  what+" is not square",
122  location));
123 }
124 
125 template<typename T> void IsSymmetric(const std::string& location,
126  const std::string& what,
127  const std::vector<std::vector<T>>& matrix) {
128 
129  try {
130  Assert::IsSquare(location, what, matrix);
131  } catch ( Exceptions::Exception& e ) {
132  throw(Exceptions::Exception(Exceptions::nonRecoverable,
133  what+" cannot be symmetric"+std::string(e.what()),
134  location));
135  }
136 
137  size_t i, j;
138  for (i = 0; i < matrix.size(); i++)
139  for (j = i+1; j < matrix.size(); j++)
140  if ( matrix[i][j] != matrix[j][i] )
141  throw(Exceptions::Exception(Exceptions::nonRecoverable,
142  what+" is not symmetric",
143  location));
144 }
145 
146 template<typename T1, typename T2> void Contains(const std::string& location,
147  const std::string& what,
148  const std::map<T1, T2>& map,
149  const T1& key) {
150 
151  if ( map.find(key) == map.end() )
152  throw(Exceptions::Exception(Exceptions::nonRecoverable,
153  what+" does not contain "+key,
154  location));
155 }
const char * what() const
Return char buffer of message+" at "+location, memory remains owned by Exception. ...
Definition: Exception.hh:52
void IsSquare(const std::string &location, const std::string &what, const std::vector< std::vector< T >> &matrix)
Asserts that a matrix is square.
Definition: Assert.hh:116
Global exception object for all non-standard exceptions thrown by the code.
Definition: Exception.hh:30