From d7e32b4dd49a96a897ff1371a628e3721c30834f Mon Sep 17 00:00:00 2001 From: ChristianFeldmann Date: Mon, 14 Oct 2024 22:28:57 +0200 Subject: [PATCH] Improve formatting and add tests --- YUViewLib/src/common/Formatting.h | 6 +- YUViewUnitTest/common/FormattingTest.cpp | 88 ++++++++++++++++++++++++ 2 files changed, 91 insertions(+), 3 deletions(-) create mode 100644 YUViewUnitTest/common/FormattingTest.cpp diff --git a/YUViewLib/src/common/Formatting.h b/YUViewLib/src/common/Formatting.h index 808ee7371..6696de333 100644 --- a/YUViewLib/src/common/Formatting.h +++ b/YUViewLib/src/common/Formatting.h @@ -51,7 +51,7 @@ template std::string to_string(const std::pair &typePair) return stream.str(); } -template std::ostream &operator<<(std::ostream &stream, const std::vector vec) +template std::ostream &operator<<(std::ostream &stream, const std::vector &vec) { stream << "["; for (auto it = vec.begin(); it != vec.end(); it++) @@ -64,7 +64,7 @@ template std::ostream &operator<<(std::ostream &stream, const std:: return stream; } -template std::string to_string(const std::vector vec) +template std::string to_string(const std::vector &vec) { std::ostringstream stream; stream << vec; @@ -93,7 +93,7 @@ template static std::ostream &operator<<(std::ostream &stream, const std::optional &opt) { if (opt) - stream << opt; + stream << opt.value(); else stream << "NA"; return stream; diff --git a/YUViewUnitTest/common/FormattingTest.cpp b/YUViewUnitTest/common/FormattingTest.cpp new file mode 100644 index 000000000..3db63e057 --- /dev/null +++ b/YUViewUnitTest/common/FormattingTest.cpp @@ -0,0 +1,88 @@ +/* This file is part of YUView - The YUV player with advanced analytics toolset + * + * Copyright (C) 2015 Institut für Nachrichtentechnik, RWTH Aachen University, GERMANY + * + * This program 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. + * + * In addition, as a special exception, the copyright holders give + * permission to link the code of portions of this program with the + * OpenSSL library under certain conditions as described in each + * individual source file, and distribute linked combinations including + * the two. + * + * You must obey the GNU General Public License in all respects for all + * of the code used other than OpenSSL. If you modify file(s) with this + * exception, you may extend this exception to your version of the + * file(s), but you are not obligated to do so. If you do not wish to do + * so, delete this exception statement from your version. If you delete + * this exception statement from all source files in the program, then + * also delete it here. + * + * This program 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 this program. If not, see . + */ + +#include + +#include + +namespace +{ + +template void runFormatTest(const T &value, const std::string_view expectedString) +{ + std::ostringstream stream; + stream << value; + EXPECT_EQ(stream.str(), expectedString); + EXPECT_EQ(to_string(value), expectedString); +} + +TEST(FormattingTest, FormattingOfPair) +{ + runFormatTest(std::make_pair(11, 472), "(11, 472)"); + runFormatTest(std::make_pair(11.0, 472.23), "(11, 472.23)"); + runFormatTest(std::make_pair("abcde", "ghij"), "(abcde, ghij)"); +} + +TEST(FormattingTest, FormattingOfVector) +{ + runFormatTest(std::vector({8, 22, 99, 0}), "[8, 22, 99, 0]"); + runFormatTest(std::vector({8.22, 22.12, 99.0, 0.01}), "[8.22, 22.12, 99, 0.01]"); + runFormatTest(std::vector({"abc", "def", "g", "hi"}), "[abc, def, g, hi]"); +} + +TEST(FormattingTest, FormattingOfSize) +{ + runFormatTest(Size(124, 156), "124x156"); + runFormatTest(Size(999, 125682), "999x125682"); +} + +TEST(FormattingTest, FormattingOfBool) +{ + EXPECT_EQ(to_string(false), "False"); + EXPECT_EQ(to_string(true), "True"); +} + +TEST(FormattingTest, FormattingOfOptional) +{ + runFormatTest(std::optional(), "NA"); + runFormatTest(std::make_optional(22), "22"); + runFormatTest(std::make_optional(22.556), "22.556"); + runFormatTest(std::make_optional("Test234"), "Test234"); +} + +TEST(FormattingTest, StringReplaceAll) +{ + EXPECT_EQ(stringReplaceAll("abcdefghi", 'c', 'g'), "abgdefghi"); + EXPECT_EQ(stringReplaceAll("abcdefghijklmn", {'c', 'h', 'm'}, 'g'), "abgdefggijklgn"); +} + +} // namespace