?login_element?

Subversion Repositories NedoOS

Rev

Blame | Last modification | View Log | Download

  1. #include "UnitTest++/UnitTestPP.h"
  2. #include "UnitTest++/CurrentTest.h"
  3. #include "RecordingReporter.h"
  4. #include "ScopedCurrentTest.h"
  5.  
  6. using namespace std;
  7.  
  8. namespace {
  9.  
  10.    TEST(CheckSucceedsOnTrue)
  11.    {
  12.       bool failure = true;
  13.       {
  14.          RecordingReporter reporter;
  15.          UnitTest::TestResults testResults(&reporter);
  16.  
  17.          ScopedCurrentTest scopedResults(testResults);
  18.          CHECK(true);
  19.  
  20.          failure = (testResults.GetFailureCount() > 0);
  21.       }
  22.  
  23.       CHECK(!failure);
  24.    }
  25.  
  26.    TEST(CheckFailsOnFalse)
  27.    {
  28.       bool failure = false;
  29.       {
  30.          RecordingReporter reporter;
  31.          UnitTest::TestResults testResults(&reporter);
  32.          ScopedCurrentTest scopedResults(testResults);
  33.          CHECK(false);
  34.          failure = (testResults.GetFailureCount() > 0);
  35.       }
  36.  
  37.       CHECK(failure);
  38.    }
  39.  
  40.    TEST(FailureReportsCorrectTestName)
  41.    {
  42.       RecordingReporter reporter;
  43.       {
  44.          UnitTest::TestResults testResults(&reporter);
  45.          ScopedCurrentTest scopedResults(testResults);
  46.          CHECK(false);
  47.       }
  48.  
  49.       CHECK_EQUAL(m_details.testName, reporter.lastFailedTest);
  50.    }
  51.  
  52.    TEST(CheckFailureIncludesCheckContents)
  53.    {
  54.       RecordingReporter reporter;
  55.       {
  56.          UnitTest::TestResults testResults(&reporter);
  57.          ScopedCurrentTest scopedResults(testResults);
  58.          const bool yaddayadda = false;
  59.          CHECK(yaddayadda);
  60.       }
  61.  
  62.       CHECK(strstr(reporter.lastFailedMessage, "yaddayadda"));
  63.    }
  64.  
  65.    TEST(CheckEqualSucceedsOnEqual)
  66.    {
  67.       bool failure = true;
  68.       {
  69.          RecordingReporter reporter;
  70.          UnitTest::TestResults testResults(&reporter);
  71.          ScopedCurrentTest scopedResults(testResults);
  72.          CHECK_EQUAL(1, 1);
  73.          failure = (testResults.GetFailureCount() > 0);
  74.       }
  75.  
  76.       CHECK(!failure);
  77.    }
  78.  
  79.    TEST(CheckEqualFailsOnNotEqual)
  80.    {
  81.       bool failure = false;
  82.       {
  83.          RecordingReporter reporter;
  84.          UnitTest::TestResults testResults(&reporter);
  85.          ScopedCurrentTest scopedResults(testResults);
  86.          CHECK_EQUAL(1, 2);
  87.          failure = (testResults.GetFailureCount() > 0);
  88.       }
  89.  
  90.       CHECK(failure);
  91.    }
  92.  
  93.    TEST(CheckEqualFailureContainsCorrectDetails)
  94.    {
  95.       int line = 0;
  96.       RecordingReporter reporter;
  97.       {
  98.          UnitTest::TestResults testResults(&reporter);
  99.          UnitTest::TestDetails const testDetails("testName", "suiteName", "filename", -1);
  100.          ScopedCurrentTest scopedResults(testResults, &testDetails);
  101.  
  102.          CHECK_EQUAL(1, 123);    line = __LINE__;
  103.       }
  104.  
  105.       CHECK_EQUAL("testName", reporter.lastFailedTest);
  106.       CHECK_EQUAL("suiteName", reporter.lastFailedSuite);
  107.       CHECK_EQUAL("filename", reporter.lastFailedFile);
  108.       CHECK_EQUAL(line, reporter.lastFailedLine);
  109.    }
  110.  
  111.    int g_sideEffect = 0;
  112.    int FunctionWithSideEffects()
  113.    {
  114.       ++g_sideEffect;
  115.       return 1;
  116.    }
  117.  
  118.    TEST(CheckEqualDoesNotHaveSideEffectsWhenPassing)
  119.    {
  120.       g_sideEffect = 0;
  121.       {
  122.          UnitTest::TestResults testResults;
  123.          ScopedCurrentTest scopedResults(testResults);
  124.          CHECK_EQUAL(1, FunctionWithSideEffects());
  125.       }
  126.       CHECK_EQUAL(1, g_sideEffect);
  127.    }
  128.  
  129.    TEST(CheckEqualDoesNotHaveSideEffectsWhenFailing)
  130.    {
  131.       g_sideEffect = 0;
  132.       {
  133.          UnitTest::TestResults testResults;
  134.          ScopedCurrentTest scopedResults(testResults);
  135.          CHECK_EQUAL(2, FunctionWithSideEffects());
  136.       }
  137.       CHECK_EQUAL(1, g_sideEffect);
  138.    }
  139.  
  140.  
  141.    TEST(CheckCloseSucceedsOnEqual)
  142.    {
  143.       bool failure = true;
  144.       {
  145.          RecordingReporter reporter;
  146.          UnitTest::TestResults testResults(&reporter);
  147.          ScopedCurrentTest scopedResults(testResults);
  148.          CHECK_CLOSE (1.0f, 1.001f, 0.01f);
  149.          failure = (testResults.GetFailureCount() > 0);
  150.       }
  151.  
  152.       CHECK(!failure);
  153.    }
  154.  
  155.    TEST(CheckCloseFailsOnNotEqual)
  156.    {
  157.       bool failure = false;
  158.       {
  159.          RecordingReporter reporter;
  160.          UnitTest::TestResults testResults(&reporter);
  161.          ScopedCurrentTest scopedResults(testResults);
  162.          CHECK_CLOSE (1.0f, 1.1f, 0.01f);
  163.          failure = (testResults.GetFailureCount() > 0);
  164.       }
  165.  
  166.       CHECK(failure);
  167.    }
  168.  
  169.    TEST(CheckCloseFailureContainsCorrectDetails)
  170.    {
  171.       int line = 0;
  172.       RecordingReporter reporter;
  173.       {
  174.          UnitTest::TestResults testResults(&reporter);
  175.          UnitTest::TestDetails testDetails("test", "suite", "filename", -1);
  176.          ScopedCurrentTest scopedResults(testResults, &testDetails);
  177.  
  178.          CHECK_CLOSE (1.0f, 1.1f, 0.01f);    line = __LINE__;
  179.       }
  180.  
  181.       CHECK_EQUAL("test", reporter.lastFailedTest);
  182.       CHECK_EQUAL("suite", reporter.lastFailedSuite);
  183.       CHECK_EQUAL("filename", reporter.lastFailedFile);
  184.       CHECK_EQUAL(line, reporter.lastFailedLine);
  185.    }
  186.  
  187.    TEST(CheckCloseDoesNotHaveSideEffectsWhenPassing)
  188.    {
  189.       g_sideEffect = 0;
  190.       {
  191.          UnitTest::TestResults testResults;
  192.          ScopedCurrentTest scopedResults(testResults);
  193.          CHECK_CLOSE (1, FunctionWithSideEffects(), 0.1f);
  194.       }
  195.       CHECK_EQUAL(1, g_sideEffect);
  196.    }
  197.  
  198.    TEST(CheckCloseDoesNotHaveSideEffectsWhenFailing)
  199.    {
  200.       g_sideEffect = 0;
  201.       {
  202.          UnitTest::TestResults testResults;
  203.          ScopedCurrentTest scopedResults(testResults);
  204.          CHECK_CLOSE (2, FunctionWithSideEffects(), 0.1f);
  205.       }
  206.       CHECK_EQUAL(1, g_sideEffect);
  207.    }
  208.  
  209.    TEST(CheckArrayCloseSucceedsOnEqual)
  210.    {
  211.       bool failure = true;
  212.       {
  213.          RecordingReporter reporter;
  214.          UnitTest::TestResults testResults(&reporter);
  215.          ScopedCurrentTest scopedResults(testResults);
  216.          const float data[4] = { 0, 1, 2, 3 };
  217.          CHECK_ARRAY_CLOSE (data, data, 4, 0.01f);
  218.          failure = (testResults.GetFailureCount() > 0);
  219.       }
  220.  
  221.       CHECK(!failure);
  222.    }
  223.  
  224.    TEST(CheckArrayCloseFailsOnNotEqual)
  225.    {
  226.       bool failure = false;
  227.       {
  228.          RecordingReporter reporter;
  229.          UnitTest::TestResults testResults(&reporter);
  230.          ScopedCurrentTest scopedResults(testResults);
  231.  
  232.          int const data1[4] = { 0, 1, 2, 3 };
  233.          int const data2[4] = { 0, 1, 3, 3 };
  234.          CHECK_ARRAY_CLOSE (data1, data2, 4, 0.01f);
  235.  
  236.          failure = (testResults.GetFailureCount() > 0);
  237.       }
  238.  
  239.       CHECK(failure);
  240.    }
  241.  
  242.    TEST(CheckArrayCloseFailureIncludesCheckExpectedAndActual)
  243.    {
  244.       RecordingReporter reporter;
  245.       {
  246.          UnitTest::TestResults testResults(&reporter);
  247.          ScopedCurrentTest scopedResults(testResults);
  248.  
  249.          int const data1[4] = { 0, 1, 2, 3 };
  250.          int const data2[4] = { 0, 1, 3, 3 };
  251.          CHECK_ARRAY_CLOSE (data1, data2, 4, 0.01f);
  252.       }
  253.  
  254.       CHECK(strstr(reporter.lastFailedMessage, "xpected [ 0 1 2 3 ]"));
  255.       CHECK(strstr(reporter.lastFailedMessage, "was [ 0 1 3 3 ]"));
  256.    }
  257.  
  258.    TEST(CheckArrayCloseFailureContainsCorrectDetails)
  259.    {
  260.       int line = 0;
  261.       RecordingReporter reporter;
  262.       {
  263.          UnitTest::TestResults testResults(&reporter);
  264.          UnitTest::TestDetails testDetails("arrayCloseTest", "arrayCloseSuite", "filename", -1);
  265.          ScopedCurrentTest scopedResults(testResults, &testDetails);
  266.  
  267.          int const data1[4] = { 0, 1, 2, 3 };
  268.          int const data2[4] = { 0, 1, 3, 3 };
  269.          CHECK_ARRAY_CLOSE (data1, data2, 4, 0.01f);     line = __LINE__;
  270.       }
  271.  
  272.       CHECK_EQUAL("arrayCloseTest", reporter.lastFailedTest);
  273.       CHECK_EQUAL("arrayCloseSuite", reporter.lastFailedSuite);
  274.       CHECK_EQUAL("filename", reporter.lastFailedFile);
  275.       CHECK_EQUAL(line, reporter.lastFailedLine);
  276.    }
  277.  
  278.    TEST(CheckArrayCloseFailureIncludesTolerance)
  279.    {
  280.       RecordingReporter reporter;
  281.       {
  282.          UnitTest::TestResults testResults(&reporter);
  283.          ScopedCurrentTest scopedResults(testResults);
  284.  
  285.          float const data1[4] = { 0, 1, 2, 3 };
  286.          float const data2[4] = { 0, 1, 3, 3 };
  287.          CHECK_ARRAY_CLOSE (data1, data2, 4, 0.01f);
  288.       }
  289.  
  290.       CHECK(strstr(reporter.lastFailedMessage, "0.01"));
  291.    }
  292.  
  293.    TEST(CheckArrayEqualSuceedsOnEqual)
  294.    {
  295.       bool failure = true;
  296.       {
  297.          RecordingReporter reporter;
  298.          UnitTest::TestResults testResults(&reporter);
  299.          ScopedCurrentTest scopedResults(testResults);
  300.  
  301.          const float data[4] = { 0, 1, 2, 3 };
  302.          CHECK_ARRAY_EQUAL (data, data, 4);
  303.  
  304.          failure = (testResults.GetFailureCount() > 0);
  305.       }
  306.  
  307.       CHECK(!failure);
  308.    }
  309.  
  310.    TEST(CheckArrayEqualFailsOnNotEqual)
  311.    {
  312.       bool failure = false;
  313.       {
  314.          RecordingReporter reporter;
  315.          UnitTest::TestResults testResults(&reporter);
  316.          ScopedCurrentTest scopedResults(testResults);
  317.  
  318.          int const data1[4] = { 0, 1, 2, 3 };
  319.          int const data2[4] = { 0, 1, 3, 3 };
  320.          CHECK_ARRAY_EQUAL (data1, data2, 4);
  321.  
  322.          failure = (testResults.GetFailureCount() > 0);
  323.       }
  324.  
  325.       CHECK(failure);
  326.    }
  327.  
  328.    TEST(CheckArrayEqualFailureIncludesCheckExpectedAndActual)
  329.    {
  330.       RecordingReporter reporter;
  331.       {
  332.          UnitTest::TestResults testResults(&reporter);
  333.          ScopedCurrentTest scopedResults(testResults);
  334.  
  335.          int const data1[4] = { 0, 1, 2, 3 };
  336.          int const data2[4] = { 0, 1, 3, 3 };
  337.          CHECK_ARRAY_EQUAL (data1, data2, 4);
  338.       }
  339.  
  340.       CHECK(strstr(reporter.lastFailedMessage, "xpected [ 0 1 2 3 ]"));
  341.       CHECK(strstr(reporter.lastFailedMessage, "was [ 0 1 3 3 ]"));
  342.    }
  343.  
  344.    TEST(CheckArrayEqualFailureContainsCorrectInfo)
  345.    {
  346.       int line = 0;
  347.       RecordingReporter reporter;
  348.       {
  349.          UnitTest::TestResults testResults(&reporter);
  350.          ScopedCurrentTest scopedResults(testResults);
  351.  
  352.          int const data1[4] = { 0, 1, 2, 3 };
  353.          int const data2[4] = { 0, 1, 3, 3 };
  354.          CHECK_ARRAY_EQUAL (data1, data2, 4);     line = __LINE__;
  355.       }
  356.  
  357.       CHECK_EQUAL("CheckArrayEqualFailureContainsCorrectInfo", reporter.lastFailedTest);
  358.       CHECK_EQUAL(__FILE__, reporter.lastFailedFile);
  359.       CHECK_EQUAL(line, reporter.lastFailedLine);
  360.    }
  361.  
  362.    float const* FunctionWithSideEffects2()
  363.    {
  364.       ++g_sideEffect;
  365.       static float const data[] = {1,2,3,4};
  366.       return data;
  367.    }
  368.  
  369.    TEST(CheckArrayCloseDoesNotHaveSideEffectsWhenPassing)
  370.    {
  371.       g_sideEffect = 0;
  372.       {
  373.          UnitTest::TestResults testResults;
  374.          ScopedCurrentTest scopedResults(testResults);
  375.  
  376.          const float data[] = { 0, 1, 2, 3 };
  377.          CHECK_ARRAY_CLOSE (data, FunctionWithSideEffects2(), 4, 0.01f);
  378.       }
  379.       CHECK_EQUAL(1, g_sideEffect);
  380.    }
  381.  
  382.    TEST(CheckArrayCloseDoesNotHaveSideEffectsWhenFailing)
  383.    {
  384.       g_sideEffect = 0;
  385.       {
  386.          UnitTest::TestResults testResults;
  387.          ScopedCurrentTest scopedResults(testResults);
  388.  
  389.          const float data[] = { 0, 1, 3, 3 };
  390.          CHECK_ARRAY_CLOSE (data, FunctionWithSideEffects2(), 4, 0.01f);
  391.       }
  392.  
  393.       CHECK_EQUAL(1, g_sideEffect);
  394.    }
  395.  
  396.    TEST(CheckArray2DCloseSucceedsOnEqual)
  397.    {
  398.       bool failure = true;
  399.       {
  400.          RecordingReporter reporter;
  401.          UnitTest::TestResults testResults(&reporter);
  402.          ScopedCurrentTest scopedResults(testResults);
  403.  
  404.          const float data[2][2] = { {0, 1}, {2, 3} };
  405.          CHECK_ARRAY2D_CLOSE (data, data, 2, 2, 0.01f);
  406.  
  407.          failure = (testResults.GetFailureCount() > 0);
  408.       }
  409.  
  410.       CHECK(!failure);
  411.    }
  412.  
  413.    TEST(CheckArray2DCloseFailsOnNotEqual)
  414.    {
  415.       bool failure = false;
  416.       {
  417.          RecordingReporter reporter;
  418.          UnitTest::TestResults testResults(&reporter);
  419.          ScopedCurrentTest scopedResults(testResults);
  420.  
  421.          int const data1[2][2] = { {0, 1}, {2, 3} };
  422.          int const data2[2][2] = { {0, 1}, {3, 3} };
  423.          CHECK_ARRAY2D_CLOSE (data1, data2, 2, 2, 0.01f);
  424.  
  425.          failure = (testResults.GetFailureCount() > 0);
  426.       }
  427.  
  428.       CHECK(failure);
  429.    }
  430.  
  431.    TEST(CheckArray2DCloseFailureIncludesCheckExpectedAndActual)
  432.    {
  433.       RecordingReporter reporter;
  434.       {
  435.          UnitTest::TestResults testResults(&reporter);
  436.          ScopedCurrentTest scopedResults(testResults);
  437.  
  438.          int const data1[2][2] = { {0, 1}, {2, 3} };
  439.          int const data2[2][2] = { {0, 1}, {3, 3} };
  440.  
  441.          CHECK_ARRAY2D_CLOSE (data1, data2, 2, 2, 0.01f);
  442.       }
  443.  
  444.       CHECK(strstr(reporter.lastFailedMessage, "xpected [ [ 0 1 ] [ 2 3 ] ]"));
  445.       CHECK(strstr(reporter.lastFailedMessage, "was [ [ 0 1 ] [ 3 3 ] ]"));
  446.    }
  447.  
  448.    TEST(CheckArray2DCloseFailureContainsCorrectDetails)
  449.    {
  450.       int line = 0;
  451.       RecordingReporter reporter;
  452.       {
  453.          UnitTest::TestResults testResults(&reporter);
  454.          UnitTest::TestDetails testDetails("array2DCloseTest", "array2DCloseSuite", "filename", -1);
  455.          ScopedCurrentTest scopedResults(testResults, &testDetails);
  456.  
  457.          int const data1[2][2] = { {0, 1}, {2, 3} };
  458.          int const data2[2][2] = { {0, 1}, {3, 3} };
  459.          CHECK_ARRAY2D_CLOSE (data1, data2, 2, 2, 0.01f);     line = __LINE__;
  460.       }
  461.  
  462.       CHECK_EQUAL("array2DCloseTest", reporter.lastFailedTest);
  463.       CHECK_EQUAL("array2DCloseSuite", reporter.lastFailedSuite);
  464.       CHECK_EQUAL("filename", reporter.lastFailedFile);
  465.       CHECK_EQUAL(line, reporter.lastFailedLine);
  466.    }
  467.  
  468.    TEST(CheckArray2DCloseFailureIncludesTolerance)
  469.    {
  470.       RecordingReporter reporter;
  471.       {
  472.          UnitTest::TestResults testResults(&reporter);
  473.          ScopedCurrentTest scopedResults(testResults);
  474.  
  475.          float const data1[2][2] = { {0, 1}, {2, 3} };
  476.          float const data2[2][2] = { {0, 1}, {3, 3} };
  477.          CHECK_ARRAY2D_CLOSE (data1, data2, 2, 2, 0.01f);
  478.       }
  479.  
  480.       CHECK(strstr(reporter.lastFailedMessage, "0.01"));
  481.    }
  482.  
  483.    float const* const* FunctionWithSideEffects3()
  484.    {
  485.       ++g_sideEffect;
  486.       static float const data1[] = {0,1};
  487.       static float const data2[] = {2,3};
  488.       static const float* const data[] = {data1, data2};
  489.       return data;
  490.    }
  491.  
  492.    TEST(CheckArray2DCloseDoesNotHaveSideEffectsWhenPassing)
  493.    {
  494.       g_sideEffect = 0;
  495.       {
  496.          UnitTest::TestResults testResults;
  497.          ScopedCurrentTest scopedResults(testResults);
  498.  
  499.          const float data[2][2] = { {0, 1}, {2, 3} };
  500.          CHECK_ARRAY2D_CLOSE (data, FunctionWithSideEffects3(), 2, 2, 0.01f);
  501.       }
  502.       CHECK_EQUAL(1, g_sideEffect);
  503.    }
  504.  
  505.    TEST(CheckArray2DCloseDoesNotHaveSideEffectsWhenFailing)
  506.    {
  507.       g_sideEffect = 0;
  508.       {
  509.          UnitTest::TestResults testResults;
  510.          ScopedCurrentTest scopedResults(testResults);
  511.  
  512.          const float data[2][2] = { {0, 1}, {3, 3} };
  513.          CHECK_ARRAY2D_CLOSE (data, FunctionWithSideEffects3(), 2, 2, 0.01f);
  514.       }
  515.       CHECK_EQUAL(1, g_sideEffect);
  516.    }
  517.  
  518. }
  519.