summaryrefslogtreecommitdiff
path: root/o3tl/qa/test-safeint.cxx
blob: 19269d4b05b361fd1fd0e559a451970cbe66c378 (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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
/*
 * This file is part of the LibreOffice project.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */

#include <sal/config.h>

#include <limits>

#include <cppunit/TestAssert.h>
#include <cppunit/TestFixture.h>
#include <cppunit/extensions/HelperMacros.h>

#include <o3tl/safeint.hxx>

namespace {

class Test: public CppUnit::TestFixture {
private:
    CPPUNIT_TEST_SUITE(Test);
    CPPUNIT_TEST(testSignedSaturatingAdd);
    CPPUNIT_TEST(testUnsignedSaturatingAdd);
    CPPUNIT_TEST_SUITE_END();

    void testSignedSaturatingAdd() {
        auto const min = std::numeric_limits<int>::min();
        auto const max = std::numeric_limits<int>::max();

        CPPUNIT_ASSERT_EQUAL(min, o3tl::saturating_add(min, min));
        CPPUNIT_ASSERT_EQUAL(min, o3tl::saturating_add(min, min + 1));
        CPPUNIT_ASSERT_EQUAL(min, o3tl::saturating_add(min, -1));
        CPPUNIT_ASSERT_EQUAL(min, o3tl::saturating_add(min, 0));
        CPPUNIT_ASSERT_EQUAL(min + 1, o3tl::saturating_add(min, 1));
        CPPUNIT_ASSERT_EQUAL(min + max - 1, o3tl::saturating_add(min, max - 1));
        CPPUNIT_ASSERT_EQUAL(min + max, o3tl::saturating_add(min, max));

        CPPUNIT_ASSERT_EQUAL(min, o3tl::saturating_add(min + 1, min));
        CPPUNIT_ASSERT_EQUAL(min, o3tl::saturating_add(min + 1, min + 1));
        CPPUNIT_ASSERT_EQUAL(min, o3tl::saturating_add(min + 1, -1));
        CPPUNIT_ASSERT_EQUAL(min + 1, o3tl::saturating_add(min + 1, 0));
        CPPUNIT_ASSERT_EQUAL(min + 2, o3tl::saturating_add(min + 1, 1));
        CPPUNIT_ASSERT_EQUAL(min + max, o3tl::saturating_add(min + 1, max - 1));
        CPPUNIT_ASSERT_EQUAL(min + max + 1, o3tl::saturating_add(min + 1, max));

        CPPUNIT_ASSERT_EQUAL(min, o3tl::saturating_add(-1, min));
        CPPUNIT_ASSERT_EQUAL(min, o3tl::saturating_add(-1, min + 1));
        CPPUNIT_ASSERT_EQUAL(-2, o3tl::saturating_add(-1, -1));
        CPPUNIT_ASSERT_EQUAL(-1, o3tl::saturating_add(-1, 0));
        CPPUNIT_ASSERT_EQUAL(0, o3tl::saturating_add(-1, 1));
        CPPUNIT_ASSERT_EQUAL(max - 2, o3tl::saturating_add(-1, max - 1));
        CPPUNIT_ASSERT_EQUAL(max - 1, o3tl::saturating_add(-1, max));

        CPPUNIT_ASSERT_EQUAL(min, o3tl::saturating_add(0, min));
        CPPUNIT_ASSERT_EQUAL(min + 1, o3tl::saturating_add(0, min + 1));
        CPPUNIT_ASSERT_EQUAL(-1, o3tl::saturating_add(0, -1));
        CPPUNIT_ASSERT_EQUAL(0, o3tl::saturating_add(0, 0));
        CPPUNIT_ASSERT_EQUAL(1, o3tl::saturating_add(0, 1));
        CPPUNIT_ASSERT_EQUAL(max - 1, o3tl::saturating_add(0, max - 1));
        CPPUNIT_ASSERT_EQUAL(max, o3tl::saturating_add(0, max));

        CPPUNIT_ASSERT_EQUAL(min + 1, o3tl::saturating_add(1, min));
        CPPUNIT_ASSERT_EQUAL(min + 2, o3tl::saturating_add(1, min + 1));
        CPPUNIT_ASSERT_EQUAL(0, o3tl::saturating_add(1, -1));
        CPPUNIT_ASSERT_EQUAL(1, o3tl::saturating_add(1, 0));
        CPPUNIT_ASSERT_EQUAL(2, o3tl::saturating_add(1, 1));
        CPPUNIT_ASSERT_EQUAL(max, o3tl::saturating_add(1, max - 1));
        CPPUNIT_ASSERT_EQUAL(max, o3tl::saturating_add(1, max));

        CPPUNIT_ASSERT_EQUAL(min + max - 1, o3tl::saturating_add(max - 1, min));
        CPPUNIT_ASSERT_EQUAL(min + max, o3tl::saturating_add(max - 1, min + 1));
        CPPUNIT_ASSERT_EQUAL(max - 2, o3tl::saturating_add(max - 1, -1));
        CPPUNIT_ASSERT_EQUAL(max - 1, o3tl::saturating_add(max - 1, 0));
        CPPUNIT_ASSERT_EQUAL(max, o3tl::saturating_add(max - 1, 1));
        CPPUNIT_ASSERT_EQUAL(max, o3tl::saturating_add(max - 1, max - 1));
        CPPUNIT_ASSERT_EQUAL(max, o3tl::saturating_add(max - 1, max));

        CPPUNIT_ASSERT_EQUAL(min + max, o3tl::saturating_add(max, min));
        CPPUNIT_ASSERT_EQUAL(min + max + 1, o3tl::saturating_add(max, min + 1));
        CPPUNIT_ASSERT_EQUAL(max - 1, o3tl::saturating_add(max, -1));
        CPPUNIT_ASSERT_EQUAL(max, o3tl::saturating_add(max, 0));
        CPPUNIT_ASSERT_EQUAL(max, o3tl::saturating_add(max, 1));
        CPPUNIT_ASSERT_EQUAL(max, o3tl::saturating_add(max, max - 1));
        CPPUNIT_ASSERT_EQUAL(max, o3tl::saturating_add(max, max));
    }

    void testUnsignedSaturatingAdd() {
        auto const max = std::numeric_limits<unsigned int>::max();

        CPPUNIT_ASSERT_EQUAL(0U, o3tl::saturating_add(0U, 0U));
        CPPUNIT_ASSERT_EQUAL(1U, o3tl::saturating_add(0U, 1U));
        CPPUNIT_ASSERT_EQUAL(max - 1, o3tl::saturating_add(0U, max - 1));
        CPPUNIT_ASSERT_EQUAL(max, o3tl::saturating_add(0U, max));

        CPPUNIT_ASSERT_EQUAL(1U, o3tl::saturating_add(1U, 0U));
        CPPUNIT_ASSERT_EQUAL(2U, o3tl::saturating_add(1U, 1U));
        CPPUNIT_ASSERT_EQUAL(max, o3tl::saturating_add(1U, max - 1));
        CPPUNIT_ASSERT_EQUAL(max, o3tl::saturating_add(1U, max));

        CPPUNIT_ASSERT_EQUAL(max - 1, o3tl::saturating_add(max - 1, 0U));
        CPPUNIT_ASSERT_EQUAL(max, o3tl::saturating_add(max - 1, 1U));
        CPPUNIT_ASSERT_EQUAL(max, o3tl::saturating_add(max - 1, max - 1));
        CPPUNIT_ASSERT_EQUAL(max, o3tl::saturating_add(max - 1, max));

        CPPUNIT_ASSERT_EQUAL(max, o3tl::saturating_add(max, 0U));
        CPPUNIT_ASSERT_EQUAL(max, o3tl::saturating_add(max, 1U));
        CPPUNIT_ASSERT_EQUAL(max, o3tl::saturating_add(max, max - 1));
        CPPUNIT_ASSERT_EQUAL(max, o3tl::saturating_add(max, max));
    }
};

CPPUNIT_TEST_SUITE_REGISTRATION(Test);

}

/* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */