?login_element?

Subversion Repositories NedoOS

Rev

Blame | Last modification | View Log | Download

  1. #include "UnitTest++/UnitTestPP.h"
  2. #include "RecordingReporter.h"
  3.  
  4. #include <cstring>
  5.  
  6. using namespace UnitTest;
  7.  
  8.  
  9. namespace {
  10.  
  11.  
  12.    TEST(CheckEqualWithUnsignedLong)
  13.    {
  14.       TestResults results;
  15.       unsigned long something = 2;
  16.       CHECK_EQUAL(something, something);
  17.    }
  18.  
  19.    TEST(CheckEqualsWithStringsFailsOnDifferentStrings)
  20.    {
  21.       char txt1[] = "Hello";
  22.       char txt2[] = "Hallo";
  23.       TestResults results;
  24.       CheckEqual(results, txt1, txt2, TestDetails("", "", "", 0));
  25.       CHECK_EQUAL(1, results.GetFailureCount());
  26.    }
  27.  
  28.    char txt1[] = "Hello"; // non-const on purpose so no folding of duplicate data
  29.    char txt2[] = "Hello";
  30.  
  31.    TEST(CheckEqualsWithStringsWorksOnContentsNonConstNonConst)
  32.    {
  33.       char const* const p1 = txt1;
  34.       char const* const p2 = txt2;
  35.       TestResults results;
  36.       CheckEqual(results, p1, p2, TestDetails("", "", "", 0));
  37.       CHECK_EQUAL(0, results.GetFailureCount());
  38.    }
  39.  
  40.    TEST(CheckEqualsWithStringsWorksOnContentsConstConst)
  41.    {
  42.       char* const p1 = txt1;
  43.       char* const p2 = txt2;
  44.       TestResults results;
  45.       CheckEqual(results, p1, p2, TestDetails("", "", "", 0));
  46.       CHECK_EQUAL(0, results.GetFailureCount());
  47.    }
  48.  
  49.    TEST(CheckEqualsWithStringsWorksOnContentsNonConstConst)
  50.    {
  51.       char* const p1 = txt1;
  52.       char const* const p2 = txt2;
  53.       TestResults results;
  54.       CheckEqual(results, p1, p2, TestDetails("", "", "", 0));
  55.       CHECK_EQUAL(0, results.GetFailureCount());
  56.    }
  57.  
  58.    TEST(CheckEqualsWithStringsWorksOnContentsConstNonConst)
  59.    {
  60.       char const* const p1 = txt1;
  61.       char* const p2 = txt2;
  62.       TestResults results;
  63.       CheckEqual(results, p1, p2, TestDetails("", "", "", 0));
  64.       CHECK_EQUAL(0, results.GetFailureCount());
  65.    }
  66.  
  67.    TEST(CheckEqualsWithStringsWorksOnContentsWithALiteral)
  68.    {
  69.       char const* const p1 = txt1;
  70.       TestResults results;
  71.       CheckEqual(results, "Hello", p1, TestDetails("", "", "", 0));
  72.       CHECK_EQUAL(0, results.GetFailureCount());
  73.    }
  74.  
  75.    TEST(CheckEqualsWithStringsWorksOnNullExpected)
  76.    {
  77.       char const* const expected = "hi";
  78.       char const* const actual = NULL;
  79.       TestResults results;
  80.       CheckEqual(results, expected, actual, TestDetails("", "", "", 0));
  81.       CHECK_EQUAL (1, results.GetFailureCount());
  82.    }
  83.  
  84.    TEST(CheckEqualsWithStringsWorksOnNullActual)
  85.    {
  86.       char const* const expected = NULL;
  87.       char const* const actual = "hi";
  88.       TestResults results;
  89.       CheckEqual(results, expected, actual, TestDetails("", "", "", 0));
  90.       CHECK_EQUAL (1, results.GetFailureCount());
  91.    }
  92.  
  93.    TEST(CheckEqualsWithStringsWorksOnNullExpectedAndActual)
  94.    {
  95.       char const* const expected = NULL;
  96.       char const* const actual = NULL;
  97.       TestResults results;
  98.       CheckEqual(results, expected, actual, TestDetails("", "", "", 0));
  99.       CHECK_EQUAL (0, results.GetFailureCount());
  100.    }
  101.  
  102.    TEST(CheckEqualFailureIncludesCheckExpectedAndActual)
  103.    {
  104.       RecordingReporter reporter;
  105.       TestResults results(&reporter);
  106.       const int something = 2;
  107.       CheckEqual(results, 1, something, TestDetails("", "", "", 0));
  108.  
  109.       using namespace std;
  110.       CHECK(strstr(reporter.lastFailedMessage, "xpected 1"));
  111.       CHECK(strstr(reporter.lastFailedMessage, "was 2"));
  112.    }
  113.  
  114.    TEST(CheckEqualFailureIncludesDetails)
  115.    {
  116.       RecordingReporter reporter;
  117.       TestResults results(&reporter);
  118.       TestDetails const details("mytest", "mysuite", "file.h", 101);
  119.  
  120.       CheckEqual(results, 1, 2, details);
  121.  
  122.       CHECK_EQUAL("mytest", reporter.lastFailedTest);
  123.       CHECK_EQUAL("mysuite", reporter.lastFailedSuite);
  124.       CHECK_EQUAL("file.h", reporter.lastFailedFile);
  125.       CHECK_EQUAL(101, reporter.lastFailedLine);
  126.    }
  127.  
  128.    TEST(CheckCloseTrue)
  129.    {
  130.       TestResults results;
  131.       CheckClose(results, 3.001f, 3.0f, 0.1f, TestDetails("", "", "", 0));
  132.       CHECK_EQUAL(0, results.GetFailureCount());
  133.    }
  134.  
  135.    TEST(CheckCloseFalse)
  136.    {
  137.       TestResults results;
  138.       CheckClose(results, 3.12f, 3.0f, 0.1f, TestDetails("", "", "", 0));
  139.       CHECK_EQUAL(1, results.GetFailureCount());
  140.    }
  141.  
  142.    TEST(CheckCloseWithZeroEpsilonWorksForSameNumber)
  143.    {
  144.       TestResults results;
  145.       CheckClose(results, 0.1f, 0.1f, 0, TestDetails("", "", "", 0));
  146.       CHECK_EQUAL(0, results.GetFailureCount());
  147.    }
  148.  
  149.    TEST(CheckCloseWithNaNFails)
  150.    {
  151.       const unsigned int bitpattern = 0xFFFFFFFF;
  152.       float nan;
  153.       UNIITEST_NS_QUAL_STD(memcpy)(&nan, &bitpattern, sizeof(bitpattern));
  154.  
  155.       TestResults results;
  156.       CheckClose(results, 3.0f, nan, 0.1f, TestDetails("", "", "", 0));
  157.       CHECK_EQUAL(1, results.GetFailureCount());
  158.    }
  159.  
  160.    TEST(CheckCloseWithNaNAgainstItselfFails)
  161.    {
  162.       const unsigned int bitpattern = 0xFFFFFFFF;
  163.       float nan;
  164.       UNIITEST_NS_QUAL_STD(memcpy)(&nan, &bitpattern, sizeof(bitpattern));
  165.  
  166.       TestResults results;
  167.       CheckClose(results, nan, nan, 0.1f, TestDetails("", "", "", 0));
  168.       CHECK_EQUAL(1, results.GetFailureCount());
  169.    }
  170.  
  171.    TEST(CheckCloseFailureIncludesCheckExpectedAndActual)
  172.    {
  173.       RecordingReporter reporter;
  174.       TestResults results(&reporter);
  175.       const float expected = 0.9f;
  176.       const float actual = 1.1f;
  177.       CheckClose(results, expected, actual, 0.01f, TestDetails("", "", "", 0));
  178.  
  179.       using namespace std;
  180.       CHECK(strstr(reporter.lastFailedMessage, "xpected 0.9"));
  181.       CHECK(strstr(reporter.lastFailedMessage, "was 1.1"));
  182.    }
  183.  
  184.    TEST(CheckCloseFailureIncludesTolerance)
  185.    {
  186.       RecordingReporter reporter;
  187.       TestResults results(&reporter);
  188.       CheckClose(results, 2, 3, 0.01f, TestDetails("", "", "", 0));
  189.  
  190.       using namespace std;
  191.       CHECK(strstr(reporter.lastFailedMessage, "0.01"));
  192.    }
  193.  
  194.    TEST(CheckCloseFailureIncludesDetails)
  195.    {
  196.       RecordingReporter reporter;
  197.       TestResults results(&reporter);
  198.       TestDetails const details("mytest", "mysuite", "header.h", 10);
  199.  
  200.       CheckClose(results, 2, 3, 0.01f, details);
  201.  
  202.       CHECK_EQUAL("mytest", reporter.lastFailedTest);
  203.       CHECK_EQUAL("mysuite", reporter.lastFailedSuite);
  204.       CHECK_EQUAL("header.h", reporter.lastFailedFile);
  205.       CHECK_EQUAL(10, reporter.lastFailedLine);
  206.    }
  207.  
  208.  
  209.    TEST(CheckArrayEqualTrue)
  210.    {
  211.       TestResults results;
  212.  
  213.       int const array[3] = { 1, 2, 3 };
  214.       CheckArrayEqual(results, array, array, 3, TestDetails("", "", "", 0));
  215.       CHECK_EQUAL(0, results.GetFailureCount());
  216.    }
  217.  
  218.    TEST(CheckArrayEqualFalse)
  219.    {
  220.       TestResults results;
  221.  
  222.       int const array1[3] = { 1, 2, 3 };
  223.       int const array2[3] = { 1, 2, 2 };
  224.       CheckArrayEqual(results, array1, array2, 3, TestDetails("", "", "", 0));
  225.       CHECK_EQUAL(1, results.GetFailureCount());
  226.    }
  227.  
  228.    TEST(CheckArrayCloseTrue)
  229.    {
  230.       TestResults results;
  231.  
  232.       float const array1[3] = { 1.0f, 1.5f, 2.0f };
  233.       float const array2[3] = { 1.01f, 1.51f, 2.01f };
  234.       CheckArrayClose(results, array1, array2, 3, 0.02f, TestDetails("", "", "", 0));
  235.       CHECK_EQUAL(0, results.GetFailureCount());
  236.    }
  237.  
  238.    TEST(CheckArrayCloseFalse)
  239.    {
  240.       TestResults results;
  241.  
  242.       float const array1[3] = { 1.0f, 1.5f, 2.0f };
  243.       float const array2[3] = { 1.01f, 1.51f, 2.01f };
  244.       CheckArrayClose(results, array1, array2, 3, 0.001f, TestDetails("", "", "", 0));
  245.       CHECK_EQUAL(1, results.GetFailureCount());
  246.    }
  247.  
  248.    TEST(CheckArrayCloseFailureIncludesDetails)
  249.    {
  250.       RecordingReporter reporter;
  251.       TestResults results(&reporter);
  252.       TestDetails const details("arrayCloseTest", "arrayCloseSuite", "file", 1337);
  253.  
  254.       float const array1[3] = { 1.0f, 1.5f, 2.0f };
  255.       float const array2[3] = { 1.01f, 1.51f, 2.01f };
  256.       CheckArrayClose(results, array1, array2, 3, 0.001f, details);
  257.  
  258.       CHECK_EQUAL("arrayCloseTest", reporter.lastFailedTest);
  259.       CHECK_EQUAL("arrayCloseSuite", reporter.lastFailedSuite);
  260.       CHECK_EQUAL("file", reporter.lastFailedFile);
  261.       CHECK_EQUAL(1337, reporter.lastFailedLine);
  262.    }
  263.  
  264.  
  265.    TEST(CheckArray2DCloseTrue)
  266.    {
  267.       TestResults results;
  268.  
  269.       float const array1[3][3] = { { 1.0f, 1.5f, 2.0f },
  270.                                    { 2.0f, 2.5f, 3.0f },
  271.                                    { 3.0f, 3.5f, 4.0f } };
  272.       float const array2[3][3] = { { 1.01f, 1.51f, 2.01f },
  273.                                    { 2.01f, 2.51f, 3.01f },
  274.                                    { 3.01f, 3.51f, 4.01f } };
  275.       CheckArray2DClose(results, array1, array2, 3, 3, 0.02f, TestDetails("", "", "", 0));
  276.       CHECK_EQUAL(0, results.GetFailureCount());
  277.    }
  278.  
  279.    TEST(CheckArray2DCloseFalse)
  280.    {
  281.       TestResults results;
  282.  
  283.       float const array1[3][3] = { { 1.0f, 1.5f, 2.0f },
  284.                                    { 2.0f, 2.5f, 3.0f },
  285.                                    { 3.0f, 3.5f, 4.0f } };
  286.       float const array2[3][3] = { { 1.01f, 1.51f, 2.01f },
  287.                                    { 2.01f, 2.51f, 3.01f },
  288.                                    { 3.01f, 3.51f, 4.01f } };
  289.       CheckArray2DClose(results, array1, array2, 3, 3, 0.001f, TestDetails("", "", "", 0));
  290.       CHECK_EQUAL(1, results.GetFailureCount());
  291.    }
  292.  
  293.    TEST(CheckCloseWithDoublesSucceeds)
  294.    {
  295.       CHECK_CLOSE(0.5, 0.5, 0.0001);
  296.    }
  297.  
  298.    TEST(CheckArray2DCloseFailureIncludesDetails)
  299.    {
  300.       RecordingReporter reporter;
  301.       TestResults results(&reporter);
  302.       TestDetails const details("array2DCloseTest", "array2DCloseSuite", "file", 1234);
  303.  
  304.       float const array1[3][3] = { { 1.0f, 1.5f, 2.0f },
  305.                                    { 2.0f, 2.5f, 3.0f },
  306.                                    { 3.0f, 3.5f, 4.0f } };
  307.       float const array2[3][3] = { { 1.01f, 1.51f, 2.01f },
  308.                                    { 2.01f, 2.51f, 3.01f },
  309.                                    { 3.01f, 3.51f, 4.01f } };
  310.       CheckArray2DClose(results, array1, array2, 3, 3, 0.001f, details);
  311.  
  312.       CHECK_EQUAL("array2DCloseTest", reporter.lastFailedTest);
  313.       CHECK_EQUAL("array2DCloseSuite", reporter.lastFailedSuite);
  314.       CHECK_EQUAL("file", reporter.lastFailedFile);
  315.       CHECK_EQUAL(1234, reporter.lastFailedLine);
  316.    }
  317.  
  318.    class TruthyUnlessCopied
  319.    {
  320.    public:
  321.       TruthyUnlessCopied()
  322.       : truthy_(true)
  323.       {
  324.       }
  325.  
  326.       TruthyUnlessCopied(const TruthyUnlessCopied&)
  327.       : truthy_(false)
  328.       {
  329.       }
  330.  
  331.       operator bool() const
  332.       {
  333.          return truthy_;
  334.       }
  335.  
  336.    private:
  337.       bool truthy_;
  338.    };
  339.  
  340.    TEST(CheckProperlyDealsWithOperatorBoolOverrides)
  341.    {
  342.       TruthyUnlessCopied objectThatShouldBeTruthy;
  343.       CHECK(objectThatShouldBeTruthy);
  344.    }
  345.  
  346. }
  347.