?login_element?

Subversion Repositories NedoOS

Rev

Blame | Last modification | View Log | Download

  1. #include "UnitTest++/UnitTestPP.h"
  2. #include "ScopedCurrentTest.h"
  3.  
  4. // These are sample tests that show the different features of the framework
  5.  
  6. namespace {
  7.  
  8.    TEST(ValidCheckSucceeds)
  9.    {
  10.       bool const b = true;
  11.       CHECK(b);
  12.    }
  13.  
  14.    TEST(CheckWorksWithPointers)
  15.    {
  16.       void* p = (void *)0x100;
  17.       CHECK(p);
  18.       CHECK(p != 0);
  19.    }
  20.  
  21.    TEST(ValidCheckEqualSucceeds)
  22.    {
  23.       int const x = 3;
  24.       int const y = 3;
  25.       CHECK_EQUAL(x, y);
  26.    }
  27.  
  28.    TEST(CheckEqualWorksWithPointers)
  29.    {
  30.       void* p = (void *)0;
  31.       CHECK_EQUAL((void*)0, p);
  32.    }
  33.  
  34.    TEST(ValidCheckCloseSucceeds)
  35.    {
  36.       CHECK_CLOSE(2.0f, 2.001f, 0.01f);
  37.       CHECK_CLOSE(2.001f, 2.0f, 0.01f);
  38.    }
  39.  
  40.    TEST(ArrayCloseSucceeds)
  41.    {
  42.       float const a1[] = {1, 2, 3};
  43.       float const a2[] = {1, 2.01f, 3};
  44.       CHECK_ARRAY_CLOSE(a1, a2, 3, 0.1f);
  45.    }
  46.  
  47. #ifndef UNITTEST_NO_EXCEPTIONS
  48.  
  49.    TEST(CheckThrowMacroSucceedsOnCorrectException)
  50.    {
  51.       struct TestException {};
  52.       CHECK_THROW(throw TestException(), TestException);
  53.    }
  54.  
  55.    TEST(CheckAssertSucceeds)
  56.    {
  57.       CHECK_ASSERT(UnitTest::ReportAssert("desc", "file", 0));
  58.    }
  59.  
  60.    TEST(CheckThrowMacroFailsOnMissingException)
  61.    {
  62.       class NoThrowTest : public UnitTest::Test
  63.       {
  64.       public:
  65.          NoThrowTest() : Test("nothrow") {}
  66.          void DontThrow() const
  67.          {}
  68.  
  69.          virtual void RunImpl() const
  70.          {
  71.             CHECK_THROW(DontThrow(), int);
  72.          }
  73.       };
  74.  
  75.       UnitTest::TestResults results;
  76.       {
  77.          ScopedCurrentTest scopedResults(results);
  78.  
  79.          NoThrowTest test;
  80.          test.Run();
  81.       }
  82.  
  83.       CHECK_EQUAL(1, results.GetFailureCount());
  84.    }
  85.  
  86.    TEST(CheckThrowMacroFailsOnWrongException)
  87.    {
  88.       class WrongThrowTest : public UnitTest::Test
  89.       {
  90.       public:
  91.          WrongThrowTest() : Test("wrongthrow") {}
  92.          virtual void RunImpl() const
  93.          {
  94.             CHECK_THROW(throw "oops", int);
  95.          }
  96.       };
  97.  
  98.       UnitTest::TestResults results;
  99.       {
  100.          ScopedCurrentTest scopedResults(results);
  101.  
  102.          WrongThrowTest test;
  103.          test.Run();
  104.       }
  105.  
  106.       CHECK_EQUAL(1, results.GetFailureCount());
  107.    }
  108.  
  109. #endif
  110.  
  111.    struct SimpleFixture
  112.    {
  113.       SimpleFixture()
  114.       {
  115.          ++instanceCount;
  116.       }
  117.       ~SimpleFixture()
  118.       {
  119.          --instanceCount;
  120.       }
  121.  
  122.       static int instanceCount;
  123.    };
  124.  
  125.    int SimpleFixture::instanceCount = 0;
  126.  
  127.    TEST_FIXTURE(SimpleFixture, DefaultFixtureCtorIsCalled)
  128.    {
  129.       CHECK(SimpleFixture::instanceCount > 0);
  130.    }
  131.  
  132.    TEST_FIXTURE(SimpleFixture, OnlyOneFixtureAliveAtATime)
  133.    {
  134.       CHECK_EQUAL(1, SimpleFixture::instanceCount);
  135.    }
  136.  
  137.    void CheckBool(const bool b)
  138.    {
  139.       CHECK(b);
  140.    }
  141.  
  142.    TEST(CanCallCHECKOutsideOfTestFunction)
  143.    {
  144.       CheckBool(true);
  145.    }
  146.  
  147. }
  148.