ValidateComparisons.h

// ValidateComparisons.h
//
// Author David Barrett-Lennard
// (C)opyright Cedanet Pty Ltd 2009

#pragma once
#ifndef Ceda_cxUtils_ValidateComparisons_H
#define Ceda_cxUtils_ValidateComparisons_H

#include "CedaAssert.h"

namespace ceda
{

///////////////////////////////////////////////////////////////////////////////////////////////////
// Check for consistency of the comparison operators == != < > <= >= between x,y

template <typename T>
void ValidateComparison(const T& x, const T& y)
{
    cxAlwaysAssert( (x == y) == (y == x) );
    cxAlwaysAssert( (x != y) == (y != x) );
    cxAlwaysAssert( (x < y) == (y > x) );
    cxAlwaysAssert( (y < x) == (x > y) );
    cxAlwaysAssert( (x <= y) == (y >= x) );
    cxAlwaysAssert( (y <= x) == (x >= y) );
    cxAlwaysAssert( (x == y) != (x != y) );
    cxAlwaysAssert( (x < y) != (x >= y) );
    cxAlwaysAssert( (y < x) != (y >= x) );
    cxAlwaysAssert( (x <= y) != (x > y) );
    cxAlwaysAssert( (y <= x) != (y > x) );
    cxAlwaysAssert( (x < y || x == y) == (x <= y) );
    cxAlwaysAssert( (y < x || y == x) == (y <= x) );
    cxAlwaysAssert( (x < y && y < x) == false );
    cxAlwaysAssert( (x <= y && y <= x) == (x == y) );
    
    if (x == y)
    {
        cxAlwaysAssert(y == x);
        
        cxAlwaysAssert(!(x != y));
        cxAlwaysAssert(!(y != x));
        
        cxAlwaysAssert(x <= y);
        cxAlwaysAssert(y <= x);
        
        cxAlwaysAssert(x >= y);
        cxAlwaysAssert(y >= x);
        
        cxAlwaysAssert(!(x < y));
        cxAlwaysAssert(!(y < x));
        
        cxAlwaysAssert(!(x > y));
        cxAlwaysAssert(!(y > x));
    }
    else
    {
        cxAlwaysAssert(!(y == x));
        
        cxAlwaysAssert(x != y);
        cxAlwaysAssert(y != x);

        if (x < y)
        {
            cxAlwaysAssert(x <= y);
            cxAlwaysAssert(!(y <= x));
            
            cxAlwaysAssert(!(x >= y));
            cxAlwaysAssert(y >= x);
            
            cxAlwaysAssert(!(y < x));

            cxAlwaysAssert(!(x > y));
            cxAlwaysAssert(y > x);
        }
        else
        {
            cxAlwaysAssert(!(x <= y));
            cxAlwaysAssert(y <= x);
            
            cxAlwaysAssert(x >= y);
            cxAlwaysAssert(!(y >= x));
            
            cxAlwaysAssert(y < x);

            cxAlwaysAssert(x > y);
            cxAlwaysAssert(!(y > x));
        }
    }
}


} // namespace ceda

#endif // include guard