summaryrefslogtreecommitdiff
path: root/o3tl
diff options
context:
space:
mode:
authorStephan Bergmann <sbergman@redhat.com>2017-10-26 21:30:08 +0200
committerStephan Bergmann <sbergman@redhat.com>2017-10-26 21:30:08 +0200
commit7cc8b8254feeda7f26ee0b0a187f34c5d8e52b34 (patch)
treed51294372822383de65f67dac8e6b2d2989fcca0 /o3tl
parent1061875d6747f3cb89466aa6df568620a7ddfa05 (diff)
Add test for o3tl::saturating_add
Change-Id: I19bb1d566bd80f859810b4023d965c8fd80b98f2
Diffstat (limited to 'o3tl')
-rw-r--r--o3tl/CppunitTest_o3tl_tests.mk1
-rw-r--r--o3tl/qa/test-safeint.cxx119
2 files changed, 120 insertions, 0 deletions
diff --git a/o3tl/CppunitTest_o3tl_tests.mk b/o3tl/CppunitTest_o3tl_tests.mk
index e8fd27095b06..9d802b98e167 100644
--- a/o3tl/CppunitTest_o3tl_tests.mk
+++ b/o3tl/CppunitTest_o3tl_tests.mk
@@ -31,6 +31,7 @@ $(eval $(call gb_CppunitTest_add_exception_objects,o3tl_tests,\
o3tl/qa/test-cow_wrapper \
o3tl/qa/test-enumarray \
o3tl/qa/test-lru_map \
+ o3tl/qa/test-safeint \
o3tl/qa/test-sorted_vector \
o3tl/qa/test-string_view \
o3tl/qa/test-typed_flags \
diff --git a/o3tl/qa/test-safeint.cxx b/o3tl/qa/test-safeint.cxx
new file mode 100644
index 000000000000..19269d4b05b3
--- /dev/null
+++ b/o3tl/qa/test-safeint.cxx
@@ -0,0 +1,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: */