diff options
author | Marco Bubke <marco.bubke@theqtcompany.com> | 2016-02-22 15:46:55 +0100 |
---|---|---|
committer | Marco Bubke <marco.bubke@theqtcompany.com> | 2016-02-22 14:53:12 +0000 |
commit | 0f61908ab0241665dab90d0c50399347f4c83123 (patch) | |
tree | 554bcb82cca700a2cac5ac9fb1eaa6d003d67993 /tests/unit/unittest | |
parent | c632ce44f783804c28f50aeed7f4e36b823cd238 (diff) | |
download | qt-creator-0f61908ab0241665dab90d0c50399347f4c83123.tar.gz |
Utils: Add sized array
Sometimes you need a very small array which has a size and max size.
QVarLengthArray is simular but has some size and run time overhead and it
has no max size. It will instead malloc. So this array is for very small
collections under 256 values which never allocate and have only a size
overhead of a byte.
Change-Id: Ia392c750d566c4accc6077c3dc4d9d4ae501e599
Reviewed-by: Eike Ziller <eike.ziller@theqtcompany.com>
Diffstat (limited to 'tests/unit/unittest')
-rw-r--r-- | tests/unit/unittest/sizedarraytest.cpp | 150 | ||||
-rw-r--r-- | tests/unit/unittest/unittest.pro | 3 |
2 files changed, 152 insertions, 1 deletions
diff --git a/tests/unit/unittest/sizedarraytest.cpp b/tests/unit/unittest/sizedarraytest.cpp new file mode 100644 index 0000000000..1af4f45f54 --- /dev/null +++ b/tests/unit/unittest/sizedarraytest.cpp @@ -0,0 +1,150 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of Qt Creator. +** +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3 as published by the Free Software +** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-3.0.html. +** +****************************************************************************/ + +#include "gtest/gtest.h" +#include "gmock/gmock-matchers.h" +#include "gtest-qt-printing.h" + +#include <utils/sizedarray.h> + +namespace { + +using Utils::SizedArray; + +TEST(SizedArray, EmptySize) +{ + SizedArray<char, 8> array; + + ASSERT_THAT(array.size(), 0); +} + +TEST(SizedArray, IsEmpty) +{ + SizedArray<char, 8> array; + + ASSERT_TRUE(array.empty()); +} + +TEST(SizedArray, IsNotEmpty) +{ + SizedArray<char, 8> array; + + array.push_back('x'); + + ASSERT_FALSE(array.empty()); +} + +TEST(SizedArray, SizeOneAfterPushBack) +{ + SizedArray<char, 8> array; + + array.push_back('x'); + + ASSERT_THAT(array.size(), 1); +} + +TEST(SizedArray, FirstValueAfterPushBack) +{ + SizedArray<char, 8> array; + + array.push_back('x'); + + ASSERT_THAT(array.front(), 'x'); +} + +TEST(SizedArray, LastValueAfterPushBack) +{ + SizedArray<char, 8> array; + + array.push_back('x'); + + ASSERT_THAT(array.back(), 'x'); +} + +TEST(SizedArray, EndIteratorIsEqualBeginForEmptyArray) +{ + SizedArray<char, 8> array; + + ASSERT_THAT(array.begin(), array.end()); +} + +TEST(SizedArray, ConstEndIteratorIsEqualBeginForEmptyArray) +{ + const SizedArray<char, 8> array = {}; + + ASSERT_THAT(array.begin(), array.end()); +} + +TEST(SizedArray, EndIteratorIsOneAfterBeginForOneSizedArray) +{ + SizedArray<char, 8> array; + + array.push_back('x'); + + ASSERT_THAT(std::next(array.begin(), 1), array.end()); +} + +TEST(SizedArray, CEndIteratorIsOneAfterBeginForOneSizedArray) +{ + SizedArray<char, 8> array = {}; + + array.push_back('x'); + + ASSERT_THAT(std::next(array.cbegin(), 1), array.cend()); +} + +TEST(SizedArray, REndIteratorIsEqualRBeginForEmptyArray) +{ + SizedArray<char, 8> array; + + ASSERT_THAT(array.rbegin(), array.rend()); +} + +TEST(SizedArray, ConstREndIteratorIsEqualRBeginForEmptyArray) +{ + const SizedArray<char, 8> array = {}; + + ASSERT_THAT(array.rbegin(), array.rend()); +} + +TEST(SizedArray, REndIteratorIsOneAfterRBeginForOneSizedArray) +{ + SizedArray<char, 8> array; + + array.push_back('x'); + + ASSERT_THAT(std::next(array.rbegin(), 1), array.rend()); +} + +TEST(SizedArray, ConstREndIteratorIsOneAfterRBeginForOneSizedArray) +{ + SizedArray<char, 8> array = {}; + + array.push_back('x'); + + ASSERT_THAT(std::next(array.crbegin(), 1), array.crend()); +} + +} // anonymous namespace diff --git a/tests/unit/unittest/unittest.pro b/tests/unit/unittest/unittest.pro index 9704c2572c..577e7fc359 100644 --- a/tests/unit/unittest/unittest.pro +++ b/tests/unit/unittest/unittest.pro @@ -67,7 +67,8 @@ SOURCES += \ chunksreportedmonitor.cpp \ unsavedfiletest.cpp \ clangisdiagnosticrelatedtolocationtest.cpp \ - smallstringtest.cpp + smallstringtest.cpp \ + sizedarraytest.cpp exists($$GOOGLEBENCHMARK_DIR) { SOURCES += \ |