1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
10 #include <sal/config.h>
14 #include <cppunit/TestAssert.h>
15 #include <cppunit/TestFixture.h>
16 #include <cppunit/extensions/HelperMacros.h>
18 #include <o3tl/safeint.hxx>
22 class Test
: public CppUnit::TestFixture
25 CPPUNIT_TEST_SUITE(Test
);
26 CPPUNIT_TEST(testSignedSaturatingAdd
);
27 CPPUNIT_TEST(testUnsignedSaturatingAdd
);
28 CPPUNIT_TEST_SUITE_END();
30 void testSignedSaturatingAdd()
32 auto const min
= std::numeric_limits
<int>::min();
33 auto const max
= std::numeric_limits
<int>::max();
35 CPPUNIT_ASSERT_EQUAL(min
, o3tl::saturating_add(min
, min
));
36 CPPUNIT_ASSERT_EQUAL(min
, o3tl::saturating_add(min
, min
+ 1));
37 CPPUNIT_ASSERT_EQUAL(min
, o3tl::saturating_add(min
, -1));
38 CPPUNIT_ASSERT_EQUAL(min
, o3tl::saturating_add(min
, 0));
39 CPPUNIT_ASSERT_EQUAL(min
+ 1, o3tl::saturating_add(min
, 1));
40 CPPUNIT_ASSERT_EQUAL(min
+ max
- 1, o3tl::saturating_add(min
, max
- 1));
41 CPPUNIT_ASSERT_EQUAL(min
+ max
, o3tl::saturating_add(min
, max
));
43 CPPUNIT_ASSERT_EQUAL(min
, o3tl::saturating_add(min
+ 1, min
));
44 CPPUNIT_ASSERT_EQUAL(min
, o3tl::saturating_add(min
+ 1, min
+ 1));
45 CPPUNIT_ASSERT_EQUAL(min
, o3tl::saturating_add(min
+ 1, -1));
46 CPPUNIT_ASSERT_EQUAL(min
+ 1, o3tl::saturating_add(min
+ 1, 0));
47 CPPUNIT_ASSERT_EQUAL(min
+ 2, o3tl::saturating_add(min
+ 1, 1));
48 CPPUNIT_ASSERT_EQUAL(min
+ max
, o3tl::saturating_add(min
+ 1, max
- 1));
49 CPPUNIT_ASSERT_EQUAL(min
+ max
+ 1, o3tl::saturating_add(min
+ 1, max
));
51 CPPUNIT_ASSERT_EQUAL(min
, o3tl::saturating_add(-1, min
));
52 CPPUNIT_ASSERT_EQUAL(min
, o3tl::saturating_add(-1, min
+ 1));
53 CPPUNIT_ASSERT_EQUAL(-2, o3tl::saturating_add(-1, -1));
54 CPPUNIT_ASSERT_EQUAL(-1, o3tl::saturating_add(-1, 0));
55 CPPUNIT_ASSERT_EQUAL(0, o3tl::saturating_add(-1, 1));
56 CPPUNIT_ASSERT_EQUAL(max
- 2, o3tl::saturating_add(-1, max
- 1));
57 CPPUNIT_ASSERT_EQUAL(max
- 1, o3tl::saturating_add(-1, max
));
59 CPPUNIT_ASSERT_EQUAL(min
, o3tl::saturating_add(0, min
));
60 CPPUNIT_ASSERT_EQUAL(min
+ 1, o3tl::saturating_add(0, min
+ 1));
61 CPPUNIT_ASSERT_EQUAL(-1, o3tl::saturating_add(0, -1));
62 CPPUNIT_ASSERT_EQUAL(0, o3tl::saturating_add(0, 0));
63 CPPUNIT_ASSERT_EQUAL(1, o3tl::saturating_add(0, 1));
64 CPPUNIT_ASSERT_EQUAL(max
- 1, o3tl::saturating_add(0, max
- 1));
65 CPPUNIT_ASSERT_EQUAL(max
, o3tl::saturating_add(0, max
));
67 CPPUNIT_ASSERT_EQUAL(min
+ 1, o3tl::saturating_add(1, min
));
68 CPPUNIT_ASSERT_EQUAL(min
+ 2, o3tl::saturating_add(1, min
+ 1));
69 CPPUNIT_ASSERT_EQUAL(0, o3tl::saturating_add(1, -1));
70 CPPUNIT_ASSERT_EQUAL(1, o3tl::saturating_add(1, 0));
71 CPPUNIT_ASSERT_EQUAL(2, o3tl::saturating_add(1, 1));
72 CPPUNIT_ASSERT_EQUAL(max
, o3tl::saturating_add(1, max
- 1));
73 CPPUNIT_ASSERT_EQUAL(max
, o3tl::saturating_add(1, max
));
75 CPPUNIT_ASSERT_EQUAL(min
+ max
- 1, o3tl::saturating_add(max
- 1, min
));
76 CPPUNIT_ASSERT_EQUAL(min
+ max
, o3tl::saturating_add(max
- 1, min
+ 1));
77 CPPUNIT_ASSERT_EQUAL(max
- 2, o3tl::saturating_add(max
- 1, -1));
78 CPPUNIT_ASSERT_EQUAL(max
- 1, o3tl::saturating_add(max
- 1, 0));
79 CPPUNIT_ASSERT_EQUAL(max
, o3tl::saturating_add(max
- 1, 1));
80 CPPUNIT_ASSERT_EQUAL(max
, o3tl::saturating_add(max
- 1, max
- 1));
81 CPPUNIT_ASSERT_EQUAL(max
, o3tl::saturating_add(max
- 1, max
));
83 CPPUNIT_ASSERT_EQUAL(min
+ max
, o3tl::saturating_add(max
, min
));
84 CPPUNIT_ASSERT_EQUAL(min
+ max
+ 1, o3tl::saturating_add(max
, min
+ 1));
85 CPPUNIT_ASSERT_EQUAL(max
- 1, o3tl::saturating_add(max
, -1));
86 CPPUNIT_ASSERT_EQUAL(max
, o3tl::saturating_add(max
, 0));
87 CPPUNIT_ASSERT_EQUAL(max
, o3tl::saturating_add(max
, 1));
88 CPPUNIT_ASSERT_EQUAL(max
, o3tl::saturating_add(max
, max
- 1));
89 CPPUNIT_ASSERT_EQUAL(max
, o3tl::saturating_add(max
, max
));
92 void testUnsignedSaturatingAdd()
94 auto const max
= std::numeric_limits
<unsigned int>::max();
96 CPPUNIT_ASSERT_EQUAL(0U, o3tl::saturating_add(0U, 0U));
97 CPPUNIT_ASSERT_EQUAL(1U, o3tl::saturating_add(0U, 1U));
98 CPPUNIT_ASSERT_EQUAL(max
- 1, o3tl::saturating_add(0U, max
- 1));
99 CPPUNIT_ASSERT_EQUAL(max
, o3tl::saturating_add(0U, max
));
101 CPPUNIT_ASSERT_EQUAL(1U, o3tl::saturating_add(1U, 0U));
102 CPPUNIT_ASSERT_EQUAL(2U, o3tl::saturating_add(1U, 1U));
103 CPPUNIT_ASSERT_EQUAL(max
, o3tl::saturating_add(1U, max
- 1));
104 CPPUNIT_ASSERT_EQUAL(max
, o3tl::saturating_add(1U, max
));
106 CPPUNIT_ASSERT_EQUAL(max
- 1, o3tl::saturating_add(max
- 1, 0U));
107 CPPUNIT_ASSERT_EQUAL(max
, o3tl::saturating_add(max
- 1, 1U));
108 CPPUNIT_ASSERT_EQUAL(max
, o3tl::saturating_add(max
- 1, max
- 1));
109 CPPUNIT_ASSERT_EQUAL(max
, o3tl::saturating_add(max
- 1, max
));
111 CPPUNIT_ASSERT_EQUAL(max
, o3tl::saturating_add(max
, 0U));
112 CPPUNIT_ASSERT_EQUAL(max
, o3tl::saturating_add(max
, 1U));
113 CPPUNIT_ASSERT_EQUAL(max
, o3tl::saturating_add(max
, max
- 1));
114 CPPUNIT_ASSERT_EQUAL(max
, o3tl::saturating_add(max
, max
));
118 CPPUNIT_TEST_SUITE_REGISTRATION(Test
);
121 /* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */