From 4eb42094093a3eb899d6e8169959dfa8985f3cb6 Mon Sep 17 00:00:00 2001 From: King_DuckZ Date: Sat, 13 Oct 2018 00:44:38 +0100 Subject: [PATCH] Add unit test and fix for non-char types. --- .../implem/reversed_sized_array_bt.hpp | 10 ++-- test/unit/CMakeLists.txt | 1 + test/unit/reversed_sized_array_test.cpp | 59 +++++++++++++++++++ 3 files changed, 65 insertions(+), 5 deletions(-) create mode 100644 test/unit/reversed_sized_array_test.cpp diff --git a/include/duckhandy/implem/reversed_sized_array_bt.hpp b/include/duckhandy/implem/reversed_sized_array_bt.hpp index a52c4b8..bd6d967 100644 --- a/include/duckhandy/implem/reversed_sized_array_bt.hpp +++ b/include/duckhandy/implem/reversed_sized_array_bt.hpp @@ -51,13 +51,13 @@ namespace dhandy { constexpr V to() const { return V(data(), size() - (not empty() and not back() ? 1 : 0)); } #if !defined(INT_CONV_WITHOUT_HELPERS) - constexpr std::string_view to_string_view() const { return to(); } - bool operator== (const std::string_view& other) const { return to_string_view() == other; } - bool operator!= (const std::string_view& other) const { return not operator==(other); } + constexpr std::basic_string_view to_string_view() const { return to>(); } + bool operator== (const std::basic_string_view& other) const { return to_string_view() == other; } + bool operator!= (const std::basic_string_view& other) const { return not operator==(other); } bool operator== (const std::string& other) const { return to_string_view() == other; } bool operator!= (const std::string& other) const { return not operator==(other); } - bool operator== (const char* other) const { return to_string_view() == std::string_view(other); } - bool operator!= (const char* other) const { return not operator==(other); } + bool operator== (const T* other) const { return to_string_view() == std::basic_string_view(other); } + bool operator!= (const T* other) const { return not operator==(other); } #endif private: diff --git a/test/unit/CMakeLists.txt b/test/unit/CMakeLists.txt index bd2cac5..f2db58b 100644 --- a/test/unit/CMakeLists.txt +++ b/test/unit/CMakeLists.txt @@ -5,6 +5,7 @@ add_executable(${PROJECT_NAME} lexical_cast_test.cpp endianness_test.cpp int_conv_test.cpp + reversed_sized_array_test.cpp ) set_property(TARGET ${PROJECT_NAME} PROPERTY CXX_STANDARD 17) set_property(TARGET ${PROJECT_NAME} PROPERTY CXX_STANDARD_REQUIRED ON) diff --git a/test/unit/reversed_sized_array_test.cpp b/test/unit/reversed_sized_array_test.cpp new file mode 100644 index 0000000..01c0052 --- /dev/null +++ b/test/unit/reversed_sized_array_test.cpp @@ -0,0 +1,59 @@ +/* Copyright 2016-2018 Michele Santullo + * This file is part of "duckhandy". + * + * "duckhandy" is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * "duckhandy" is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with "duckhandy". If not, see . + */ + +#include "catch2/catch.hpp" +#include "duckhandy/implem/reversed_sized_array_bt.hpp" + +TEST_CASE ("Check conversion functions in ReversedSizedArray", "[ReversedSizedArray][containers]") { + using dhandy::ReversedSizedArray; + + { + ReversedSizedArray ary; + CHECK(ary.empty()); + CHECK(ary.size() == 0); + ary.push_front('a'); + CHECK(not ary.empty()); + REQUIRE(ary.size() == 1); + CHECK(ary.back() == 'a'); + CHECK(ary.to_string_view().size() == 1); + CHECK(ary.to_string_view() == L"a"); + + ary.push_front('n'); + ary.push_front('z'); + ary.push_front('*'); + REQUIRE(ary.size() == 4); + CHECK(ary.to_string_view() == L"*zna"); + } + + { + ReversedSizedArray ary; + CHECK(ary.empty()); + CHECK(ary.size() == 0); + ary.push_front('\0'); + CHECK(not ary.empty()); + REQUIRE(ary.size() == 1); + CHECK(ary.back() == '\0'); + CHECK(ary.to_string_view().size() == 0); + CHECK(ary.to_string_view() == ""); + + ary.push_front('8'); + ary.push_front('+'); + ary.push_front('&'); + REQUIRE(ary.size() == 4); + CHECK(ary.to_string_view() == "&+8"); + } +}