⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 selftest.cpp

📁 "More for C++" is a class library that provides some features that are usually common for object ori
💻 CPP
字号:
////  This file is part of the "More for C++" library////  Copyright (c) 1999-2003 by Thorsten Goertz (thorsten@morefor.org)////  The "More for C++" library is free software; you can redistribute it and/or//  modify it under the terms of the license that comes with this package.////  Read "license.txt" for more details.////  THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED//  WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES//  OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.////////////////////////////////////////////////////////////////////////////////#include "selftest.hpp"#include "selftestobserver.hpp"using namespace more::test;////////////////////////////////////////////////////////////////////////////////class SelfTest::Exception{  public:    virtual ~Exception( );};////////////////////////////////////////////////////////////////////////////////SelfTest::Exception::~Exception( ){}////////////////////////////////////////////////////////////////////////////////void SelfTest::execute(  TestObserver& rTestObserver,  size_t        nNestingLevel){  if( !SelfTest::inExecution( ) )  {    TestSuite         suite( "SelfTest" );    SelfTestObserver  selfTestObserver( rTestObserver );    suite.addTestCase( "testSetUp",           &SelfTest::testSetUp );    suite.addTestCase( "testTearDown",        &SelfTest::testTearDown );    suite.addTestCase( "testThrowsException", &SelfTest::testThrowsException );    suite.addTestCase( "testFail",            &SelfTest::testFail );    suite.addTestCase( "testAssertNull",      &SelfTest::testAssertNull );    suite.addTestCase( "testAssertNotNull",   &SelfTest::testAssertNotNull );    suite.addTestCase( "testAssertCondition", &SelfTest::testAssertCondition );    suite.addTestCase( "testAssertEquals",    &SelfTest::testAssertEquals );    suite.addTestCase( "testAssertEqualsNot", &SelfTest::testAssertEqualsNot );    SelfTest::bInExecution = true;    suite.run( selfTestObserver, nNestingLevel );    SelfTest::bInExecution = false;    SelfTest::bHasBeenExecuted = true;    SelfTest::bWasSuccessful = selfTestObserver.selfTestSucceeded( );  }}////////////////////////////////////////////////////////////////////////////////bool SelfTest::inExecution( ){  return SelfTest::bInExecution;}////////////////////////////////////////////////////////////////////////////////bool SelfTest::hasBeenExecuted( ){  return SelfTest::bHasBeenExecuted;}////////////////////////////////////////////////////////////////////////////////bool SelfTest::wasSuccessful( ){  return SelfTest::bWasSuccessful;}////////////////////////////////////////////////////////////////////////////////SelfTest::SelfTest(  const char* pcName): TestCase( pcName ){}////////////////////////////////////////////////////////////////////////////////void SelfTest::setUp( ){  if( SelfTest::bExceptionInSetUp )  {    SelfTest::bExceptionInSetUp = false;    throw SelfTest::Exception( );  }}////////////////////////////////////////////////////////////////////////////////void SelfTest::tearDown( ){  if( SelfTest::bExceptionInTearDown )  {    SelfTest::bExceptionInTearDown = false;    throw SelfTest::Exception( );  }}////////////////////////////////////////////////////////////////////////////////void SelfTest::testSetUp( ){  throw SelfTest::Exception( ); // should never be called}////////////////////////////////////////////////////////////////////////////////void SelfTest::testTearDown( ){  SelfTest::bExceptionInTearDown = true;}////////////////////////////////////////////////////////////////////////////////void SelfTest::testThrowsException( ){  throw SelfTest::Exception( );}////////////////////////////////////////////////////////////////////////////////void SelfTest::testFail( ){  bool bExceptionNotThrown = false;  try  {    fail( "Should throw an exception" );    bExceptionNotThrown = true;  }  catch( ... )  {  }  if( bExceptionNotThrown )  {    throw SelfTest::Exception( );  }}////////////////////////////////////////////////////////////////////////////////void SelfTest::testAssertNull( ){  bool bExceptionNotThrown = false;  try  {    SelfTest* pNull = 0;    assertNull( pNull, "assertNull should not throw an exception" );  }  catch( ... )  {    throw SelfTest::Exception( );  }  try  {    assertNull( this, "Should throw an exception" );    bExceptionNotThrown = true;  }  catch( ... )  {  }  if( bExceptionNotThrown )  {    throw SelfTest::Exception( );  }}////////////////////////////////////////////////////////////////////////////////void SelfTest::testAssertNotNull( ){  bool bExceptionNotThrown = false;  try  {    assertNotNull( this, "assertNull should not throw an exception" );  }  catch( ... )  {    throw SelfTest::Exception( );  }  try  {    SelfTest* pNull = 0;    assertNotNull( pNull, "Should throw an exception" );    bExceptionNotThrown = true;  }  catch( ... )  {  }  if( bExceptionNotThrown )  {    throw SelfTest::Exception( );  }}////////////////////////////////////////////////////////////////////////////////void SelfTest::testAssertCondition( ){  bool bExceptionNotThrown = false;  try  {    assertCondition( true, "assertCondition should not throw an exception" );  }  catch( ... )  {    throw SelfTest::Exception( );  }  try  {    assertCondition( false, "Should throw an exception" );    bExceptionNotThrown = true;  }  catch( ... )  {  }  if( bExceptionNotThrown )  {    throw SelfTest::Exception( );  }}////////////////////////////////////////////////////////////////////////////////void SelfTest::testAssertEquals( ){  try  {    assertEquals('1', ( char )           '1',     "assertEquals failed" );    assertEquals( 10,  ( unsigned char )   10,     "assertEquals failed" );    assertEquals( 100,  ( int )             100,    "assertEquals failed" );    assertEquals( 1000,  ( unsigned int )    1000,   "assertEquals failed" );    assertEquals( 10000,  ( long )            10000,   "assertEquals failed" );    assertEquals( 100000,  ( unsigned long )   100000,   "assertEquals failed" );    assertEquals( 100.001F, ( float )           100.001F,  "assertEquals failed" );    assertEquals( 1000.001L, ( double )          1000.001L,   "assertEquals failed" );  }  catch( ... )  {    throw SelfTest::Exception( );  }}////////////////////////////////////////////////////////////////////////////////template<typename T>inline void assertEqualsNot(  Assert* pAssert,  T       value,  T       expected){  bool bExceptionNotThrown = false;  try  {    pAssert -> assertEquals( value, expected, "" );    bExceptionNotThrown = true;  }  catch( ... )  {  }  if( bExceptionNotThrown )  {    throw SelfTest::Exception( );  }}////////////////////////////////////////////////////////////////////////////////void SelfTest::testAssertEqualsNot( ){  assertEqualsNot<char>( this, 'A', 'B' );  assertEqualsNot<unsigned char>( this, 0x00, 0xFF );  assertEqualsNot<int>( this, -1, 1 );  assertEqualsNot<unsigned int>( this, 0, 65535 );  assertEqualsNot<long>( this, 10000000, 10000001 );  assertEqualsNot<unsigned long>( this, 0x0000000, 0xFFFFFFFF );  assertEqualsNot<float>( this, -22.02F, 1969.0F );  assertEqualsNot<double>( this, 0.1L, 0.2L );}////////////////////////////////////////////////////////////////////////////////bool SelfTest::bHasBeenExecuted = false;bool SelfTest::bInExecution = false;bool SelfTest::bWasSuccessful = false;bool SelfTest::bExceptionInSetUp = true;bool SelfTest::bExceptionInTearDown = false;////////////////////////////////////////////////////////////////////////////////

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -