blitzdg
an open-source project aiming to implement parallel discontinuous Galerkin (dg) solvers for common partial differential equations systems using blitz++ for array and tensor manipulations and MPI for distributed parallelism.
assert.h
1 
2 // Copyright Joakim Karlsson & Kim Gräsman 2010-2013.
3 // Distributed under the Boost Software License, Version 1.0.
4 // (See accompanying file LICENSE_1_0.txt or copy at
5 // http://www.boost.org/LICENSE_1_0.txt)
6 
7 #ifndef IGLOO_ASSERT_H
8 #define IGLOO_ASSERT_H
9 
10 #include "stringize.h"
11 #include "stringizers.h"
12 
13 namespace snowhouse {
14 
15  class Assert
16  {
17  public:
18 
19  template <typename ActualType, typename ConstraintListType>
20  static void That(const ActualType& actual, ExpressionBuilder<ConstraintListType> expression)
21  {
22  const char* no_file = "";
23  int line_number = 0;
24 
25  Assert::That(actual, expression, no_file, line_number);
26  }
27 
28  template <typename ActualType, typename ConstraintListType>
29  static void That(const ActualType& actual, ExpressionBuilder<ConstraintListType> expression, const char* file_name, int line_number)
30  {
31  try
32  {
33  ResultStack result;
34  OperatorStack operators;
35  expression.Evaluate(result, operators, actual);
36 
37  while (!operators.empty())
38  {
39  ConstraintOperator* op = operators.top();
40  op->PerformOperation(result);
41  operators.pop();
42  }
43 
44  if (result.empty())
45  {
46  throw InvalidExpressionException("The expression did not yield any result");
47  }
48 
49  if (!result.top())
50  {
51  throw AssertionException(CreateErrorText(expression, actual), file_name, line_number);
52  }
53  }
54  catch (const InvalidExpressionException& e)
55  {
56  throw AssertionException("Malformed expression: \"" + snowhouse::Stringize(expression) + "\"\n" + e.Message());
57  }
58  }
59 
60  template <typename ConstraintListType>
61  static void That(const char* actual, ExpressionBuilder<ConstraintListType> expression)
62  {
63  return That(std::string(actual), expression);
64  }
65 
66  template <typename ActualType, typename ExpressionType>
67  static void That(const ActualType& actual, const ExpressionType& expression)
68  {
69  const char* no_file = "";
70  int no_line = 0;
71  That(actual, expression, no_file, no_line);
72  }
73 
74  template <typename ActualType, typename ExpressionType>
75  static void That(const ActualType& actual, const ExpressionType& expression, const char* file_name, int line_number)
76  {
77  if (!expression(actual))
78  {
79  throw AssertionException(CreateErrorText(expression, actual), file_name, line_number);
80  }
81  }
82 
83  template <typename ExpressionType>
84  static void That(const char* actual, const ExpressionType& expression)
85  {
86  return That(std::string(actual), expression);
87  }
88 
89  static void That(bool actual)
90  {
91  if (!actual)
92  {
93  throw AssertionException("Expected: true\nActual: false");
94  }
95  }
96 
97  static void Failure(const std::string& message)
98  {
99  throw AssertionException(message);
100  }
101 
102  private:
103  template <class ExpectedType, class ActualType>
104  static std::string CreateErrorText(const ExpectedType& expected, const ActualType& actual)
105  {
106  std::ostringstream str;
107 
108  str << "Expected: " << snowhouse::Stringize(expected) << std::endl;
109  str << "Actual: " << snowhouse::Stringize(actual) << std::endl;
110 
111  return str.str();
112  }
113  };
114 }
115 
116 #endif // IGLOO_ASSERT_H
Definition: assert.h:13
Definition: expressionbuilder.h:25
Definition: constraintoperator.h:26
Definition: assert.h:15
Definition: assertionexception.h:11
Definition: constraintoperator.h:12