summaryrefslogtreecommitdiff
path: root/src/mongo/unittest/unittest_test.cpp
blob: a93e33f7e9022dc6096d6cb7bbfb54d582aceefc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
/**
 * Copyright (C) 2012 10gen Inc.
 */

/**
 * Unit tests of the unittest framework itself.
 */

#include "mongo/unittest/unittest.h"

#include <limits>
#include <string>

namespace {

#define ASSERT_TEST_FAILS(TEST_EXPR)                                    \
    ASSERT_THROWS((TEST_EXPR), mongo::unittest::TestAssertionFailureException)

    TEST(UnitTestSelfTest, DoNothing) {
    }

    void throwSomething() { throw std::exception(); }

    TEST(UnitTestSelfTest, TestAssertThrowsSuccess) {
        ASSERT_THROWS(throwSomething(), ::std::exception);
    }

    TEST(UnitTestSelfTest, TestSuccessfulNumericComparisons) {
        ASSERT_EQUALS(1LL, 1.0);
        ASSERT_NOT_EQUALS(1LL, 0.5);
        ASSERT_LESS_THAN(1, 5);
        ASSERT_LESS_THAN_OR_EQUALS(1, 5);
        ASSERT_LESS_THAN_OR_EQUALS(5, 5);
        ASSERT_GREATER_THAN(5, 1);
        ASSERT_GREATER_THAN_OR_EQUALS(5, 1);
        ASSERT_GREATER_THAN_OR_EQUALS(5, 5);
        ASSERT_APPROX_EQUAL(5, 6, 1);
    }

    TEST(UnitTestSelfTest, TestNumericComparisonFailures) {
        ASSERT_TEST_FAILS(ASSERT_EQUALS(10, 1LL));
        ASSERT_TEST_FAILS(ASSERT_NOT_EQUALS(10, 10LL));
        ASSERT_TEST_FAILS(ASSERT_LESS_THAN(10, 10LL));
        ASSERT_TEST_FAILS(ASSERT_GREATER_THAN(10, 10LL));
        ASSERT_TEST_FAILS(ASSERT_NOT_LESS_THAN(9, 10LL));
        ASSERT_TEST_FAILS(ASSERT_NOT_GREATER_THAN(1, 0LL));
        ASSERT_TEST_FAILS(ASSERT_APPROX_EQUAL(5.0, 6.1, 1));
        if (std::numeric_limits<double>::has_quiet_NaN) {
            ASSERT_TEST_FAILS(ASSERT_APPROX_EQUAL(5, std::numeric_limits<double>::quiet_NaN(), 1));
        }
        if (std::numeric_limits<double>::has_infinity) {
            ASSERT_TEST_FAILS(ASSERT_APPROX_EQUAL(5, std::numeric_limits<double>::infinity(), 1));
        }
    }

    TEST(UnitTestSelfTest, TestStringComparisons) {
        ASSERT_EQUALS(std::string("hello"), "hello");
        ASSERT_EQUALS("hello", std::string("hello"));

        ASSERT_NOT_EQUALS(std::string("hello"), "good bye!");
        ASSERT_NOT_EQUALS("hello", std::string("good bye!"));

        ASSERT_TEST_FAILS(ASSERT_NOT_EQUALS(std::string("hello"), "hello"));
        ASSERT_TEST_FAILS(ASSERT_NOT_EQUALS("hello", std::string("hello")));

        ASSERT_TEST_FAILS(ASSERT_EQUALS(std::string("hello"), "good bye!"));
        ASSERT_TEST_FAILS(ASSERT_EQUALS("hello", std::string("good bye!")));
    }

}  // namespace