|
// ErrCodeTest.cpp : Example code for using ActiveErrors.h
// Written by Stuart Dootson (c) 2009
//
// This code is made available under the CodeProject Open License (http://www.codeproject.com/info/cpol10.aspx)
#include <Windows.h>
#include <string>
#include <stdexcept>
#include <iostream>
#if defined(_MSC_VER)
#include <crtdbg.h>
#else
inline void local_assert(bool b, std::string const& s)
{
if (!b) throw std::runtime_error((s + ": assertion failed ").c_str());
}
#define _ASSERTE(expr) local_assert((expr), #expr)
#endif
#include "ActiveErrors.h"
bool HRIsGood(HRESULT hr) { return SUCCEEDED(hr); }
typedef ErrCodeWithFn<HRESULT, &HRIsGood> eHRESULT;
eHRESULT Test(bool isGood)
{
return isGood?S_OK:E_UNEXPECTED;
}
typedef ErrCodeWithGoodValue<int, 0> Err;
Err Test2(bool isGood)
{
int e = isGood?(Err::value):(Err::value+1);
return Err(e);
}
int main(int argc, char* argv[])
{
int testNum = 1;
try
{
eHRESULT ev = Test(false);
eHRESULT errValue(Test(true));
if (SUCCEEDED(errValue))
{
std::cerr << testNum << " OK" << std::endl;
_ASSERTE(true);
}
else
{
std::cerr << testNum << " Bad" << std::endl;
_ASSERTE(false);
}
}
catch (UncheckedErrorBase&)
{
std::cerr << testNum << " OK" << std::endl;
_ASSERTE(true);
}
++testNum;
try
{
Test(false);
std::cerr << testNum << " Bad" << std::endl;
_ASSERTE(false);
}
catch (UncheckedErrorBase&)
{
std::cerr << testNum << " OK" << std::endl;
_ASSERTE(true);
}
++testNum;
try
{
if (Test(false).good())
{
std::cerr << testNum << " Bad" << std::endl;
_ASSERTE(false);
}
else
{
std::cerr << testNum << " OK" << std::endl;
_ASSERTE(true);
}
}
catch (UncheckedErrorBase&)
{
std::cerr << testNum << " Bad" << std::endl;
_ASSERTE(false);
}
++testNum;
try
{
Test2(true);
std::cerr << testNum << " OK" << std::endl;
_ASSERTE(true);
}
catch (UncheckedErrorBase&)
{
std::cerr << testNum << " Bad" << std::endl;
_ASSERTE(false);
}
++testNum;
try
{
Test2(false);
std::cerr << testNum << " Bad" << std::endl;
_ASSERTE(false);
}
catch (UncheckedErrorBase&)
{
std::cerr << testNum << " OK" << std::endl;
_ASSERTE(true);
}
++testNum;
try
{
if (Test2(false).good())
{
std::cerr << testNum << " Bad" << std::endl;
_ASSERTE(false);
}
else
{
std::cerr << testNum << " OK" << std::endl;
_ASSERTE(true);
}
}
catch (UncheckedErrorBase&)
{
std::cerr << testNum << " Bad" << std::endl;
_ASSERTE(false);
}
++testNum;
return 0;
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.