Version 6.1.4.1, tag libreoffice-6.1.4.1
[LibreOffice.git] / o3tl / qa / test-array_view.cxx
blobb82aa8cd1bf452b5fba0a1f654b9f81496df067a
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
2 /*
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/.
8 */
10 #include <sal/config.h>
12 #include <stdexcept>
14 #include <cppunit/TestAssert.h>
15 #include <cppunit/TestFixture.h>
16 #include <cppunit/extensions/HelperMacros.h>
18 #include <o3tl/array_view.hxx>
20 namespace {
22 class Test: public CppUnit::TestFixture {
23 private:
24 CPPUNIT_TEST_SUITE(Test);
25 CPPUNIT_TEST(testOperations);
26 CPPUNIT_TEST_SUITE_END();
29 void testOperations() {
30 int const some_data[] { 1, 2, 3 };
31 o3tl::array_view<int const> v(some_data);
33 CPPUNIT_ASSERT_EQUAL(1, *v.begin());
34 CPPUNIT_ASSERT_EQUAL(
35 o3tl::array_view<int>::difference_type(3), v.end() - v.begin());
36 CPPUNIT_ASSERT_EQUAL(1, *v.cbegin());
37 CPPUNIT_ASSERT_EQUAL(
38 o3tl::array_view<int>::difference_type(3), v.cend() - v.cbegin());
39 CPPUNIT_ASSERT_EQUAL(3, *v.rbegin());
40 CPPUNIT_ASSERT_EQUAL(
41 o3tl::array_view<int>::difference_type(3), v.rend() - v.rbegin());
42 CPPUNIT_ASSERT_EQUAL(3, *v.crbegin());
43 CPPUNIT_ASSERT_EQUAL(
44 o3tl::array_view<int>::difference_type(3), v.crend() - v.crbegin());
45 CPPUNIT_ASSERT_EQUAL(o3tl::array_view<int>::size_type(3), v.size());
46 CPPUNIT_ASSERT_EQUAL(o3tl::array_view<int>::size_type(3), v.length());
47 CPPUNIT_ASSERT_EQUAL(o3tl::array_view<int>::npos - 1, v.max_size());
48 CPPUNIT_ASSERT(!v.empty());
49 CPPUNIT_ASSERT_EQUAL(2, v[1]);
50 try {
51 v.at(o3tl::array_view<int>::npos);
52 CPPUNIT_FAIL("missing exception");
53 } catch (std::out_of_range &) {}
54 CPPUNIT_ASSERT_EQUAL(1, v.at(0));
55 CPPUNIT_ASSERT_EQUAL(3, v.at(2));
56 try {
57 v.at(3);
58 CPPUNIT_FAIL("missing exception");
59 } catch (std::out_of_range &) {}
60 CPPUNIT_ASSERT_EQUAL(1, v.front());
61 CPPUNIT_ASSERT_EQUAL(3, v.back());
62 CPPUNIT_ASSERT_EQUAL(1, *v.data());
64 int const d1[] { 1, 2 };
65 int const d2[] { 3, 4, 5, 6 };
66 o3tl::array_view<int const> v1( d1 );
67 o3tl::array_view<int const> v2( d2 );
68 v1.swap(v2);
69 CPPUNIT_ASSERT_EQUAL(o3tl::array_view<int>::size_type(4), v1.size());
70 CPPUNIT_ASSERT_EQUAL(o3tl::array_view<int>::size_type(2), v2.size());
73 int d1[] { 1, 2, 3 };
74 o3tl::array_view<int> v1(d1);
75 o3tl::array_view<int const> v2;
76 v2 = v1; // the special operator=
81 CPPUNIT_TEST_SUITE_REGISTRATION(Test);
85 /* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */