From 7cc8b8254feeda7f26ee0b0a187f34c5d8e52b34 Mon Sep 17 00:00:00 2001 From: Stephan Bergmann Date: Thu, 26 Oct 2017 21:30:08 +0200 Subject: [PATCH] Add test for o3tl::saturating_add Change-Id: I19bb1d566bd80f859810b4023d965c8fd80b98f2 --- o3tl/CppunitTest_o3tl_tests.mk | 1 + o3tl/qa/test-safeint.cxx | 119 +++++++++++++++++++++++++++++++++ 2 files changed, 120 insertions(+) create mode 100644 o3tl/qa/test-safeint.cxx 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 + +#include + +#include +#include +#include + +#include + +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::min(); + auto const max = std::numeric_limits::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::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: */