Listing 1 Quantifiers in Assertions for C++

(c) HMMueller 1993

// Author:      Harald M. Mueller
//              Siemens AG Oesterreich
//              Hainburgerstr. 33
//              A-1030 Wien / Austria
// email:       mueller@garwein.hai.siemens.co.at

#ifndef _ASSERT_H_
#define _ASSERT_H_

#include <stream.h>

static int asstInvert, asstResult, asstEval, asstShortCut;

static struct AssertCt {
   // empty
} asstCt;

inline AssertCt& anAssertCt() {
   asstInvert = 0;
   asstEval = 1;
   return asstCt;
}

inline int asstDoEval(int& asstShortCut) {
   int eval = asstEval;
   asstShortCut = ! asstEval && asstResult;
   asstEval = 0;
   return eval;
}

inline const AssertCt& operator!(const AssertCt& a)
{
   asstInvert = !asstInvert;
   return a;
}

inline int operator&&(int left, const AssertCt&)
{
   asstEval = left;
   return left;
}

inline int operator||(int left, const AssertCt&)
{
   asstEval = !left;
   return left;
}

static ostream& assertout(ostream& os, unsigned line,
                      char* file)
{
   return os << "Assertion in line" << line << ", file"
           << file <<" failed for ";
}

#define asstLoopTest(asstFor,asstAll,asstCond)//             \
       anAssertCt();                                         \
       { int asstShortCut;                                   \
         if (asstDoEval(asstShortCut)) {                     \
            int asstInvert = ::asstInvert;                   \
            asstResult = asstAll;                            \
            for asstFor {                                    \
               asstResult = 0 || asstCond;                   \
               if (asstResult != asstAll) break;             \
            }                                                \
            if (asstInvert) asstResult = !asstResult;        \
         }                                                   \
         ::asstShortCut = asstShortCut;                      \
       }                                                     \
       asstResult = ::asstShortCut ? asstResult : asstResult

#define assertp(asstCond,asstOutput)                         \
       asstResult = 0 || asstCond, asstResult |              \
       (assertout(ASSERTSTREAM,_LINE_,_FILE_)                \
          << asstOutput << endl, ASSERTACTION, 0)

#define assert(asstCond)                                     \
   assertp(asstCond,"condition \'" #asstCond "\'")

#define forall(asstFor,asstCond) asstLoopTest(asstFor,1,asstCond)
#define exists(asstFor,asstCond) asstLoopTest(asstFor,0,asstCond)

#define old(asstname)              old_##asstname
#define saveold(assttype,asstname) assttype old_##asstname = asstname

#define ASSERTSTREAM     cerr
#define ASSERTACTION     exit(125)

#endif
// End of File