From 24900a1986bfc1e432687317b11b56c7f9b9c4a9 Mon Sep 17 00:00:00 2001 From: David Hampton Date: Sat, 20 Jun 2020 11:40:46 -0400 Subject: [PATCH] cppcheck: Use std::transform instead of a raw loop in iso639.cpp. Add some test cases for the iso639 code. --- mythtv/libs/libmythbase/iso639.cpp | 5 +- .../libmythbase/test/test_iso639/.gitignore | 1 + .../test/test_iso639/test_iso639.cpp | 161 ++++++++++++++++++ .../test/test_iso639/test_iso639.h | 43 +++++ .../test/test_iso639/test_iso639.pro | 26 +++ 5 files changed, 234 insertions(+), 2 deletions(-) create mode 100644 mythtv/libs/libmythbase/test/test_iso639/.gitignore create mode 100644 mythtv/libs/libmythbase/test/test_iso639/test_iso639.cpp create mode 100644 mythtv/libs/libmythbase/test/test_iso639/test_iso639.h create mode 100644 mythtv/libs/libmythbase/test/test_iso639/test_iso639.pro diff --git a/mythtv/libs/libmythbase/iso639.cpp b/mythtv/libs/libmythbase/iso639.cpp index 8fca55b6654..a86898e0193 100644 --- a/mythtv/libs/libmythbase/iso639.cpp +++ b/mythtv/libs/libmythbase/iso639.cpp @@ -60,8 +60,9 @@ vector iso639_get_language_key_list(void) if (s_language_keys.empty()) { const QStringList list = iso639_get_language_list(); - for (const auto& it : qAsConst(list)) - s_language_keys.push_back(iso639_str3_to_key(it)); + std::transform(list.cbegin(), list.cend(), + s_language_keys.end(), + [](const QString &str) -> int {return iso639_str3_to_key(str);}); } return s_language_keys; } diff --git a/mythtv/libs/libmythbase/test/test_iso639/.gitignore b/mythtv/libs/libmythbase/test/test_iso639/.gitignore new file mode 100644 index 00000000000..ece1822261c --- /dev/null +++ b/mythtv/libs/libmythbase/test/test_iso639/.gitignore @@ -0,0 +1 @@ +test_iso639 diff --git a/mythtv/libs/libmythbase/test/test_iso639/test_iso639.cpp b/mythtv/libs/libmythbase/test/test_iso639/test_iso639.cpp new file mode 100644 index 00000000000..23dc5635f3d --- /dev/null +++ b/mythtv/libs/libmythbase/test/test_iso639/test_iso639.cpp @@ -0,0 +1,161 @@ +/* + * Class TestISO639 + * + * Copyright (c) David Hampton 2018 + * + * 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 2 of the License, or + * (at your option) any later version. + * + * 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, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ +#include +#include "test_iso639.h" + +void TestISO639::test_str2name_data(void) +{ + QTest::addColumn("input"); + QTest::addColumn("expectedOutput"); + + QTest::newRow("en") << "en" << "English"; + QTest::newRow("eng") << "eng" << "English"; + QTest::newRow("zh") << "zh" << "Chinese"; + QTest::newRow("zho") << "zho" << "Chinese"; + QTest::newRow("chi") << "chi" << "Chinese"; +} + +void TestISO639::test_str2name(void) +{ + QFETCH(QString, input); + QFETCH(QString, expectedOutput); + + QString output = iso639_str_toName(reinterpret_cast(qPrintable(input))); +// std::cerr << "Expected: " << qPrintable(expectedOutput) << std::endl; +// std::cerr << "Actual: " << qPrintable(output) << std::endl; + QCOMPARE(output, expectedOutput); +} + +void TestISO639::test_key2str3_data(void) +{ + QTest::addColumn("input"); + QTest::addColumn("expectedOutput"); + + QTest::newRow("eng") << 0x656E67 << "eng"; + QTest::newRow("zho") << 0X7A686F << "zho"; + QTest::newRow("cho") << 0x636869 << "chi"; +} + +void TestISO639::test_key2str3(void) +{ + QFETCH(int, input); + QFETCH(QString, expectedOutput); + + QString output = iso639_key_to_str3(input); +// std::cerr << "Expected: " << qPrintable(expectedOutput) << std::endl; +// std::cerr << "Actual: " << qPrintable(output) << std::endl; + QCOMPARE(output, expectedOutput); +} + +void TestISO639::test_str3_to_key_data(void) +{ + QTest::addColumn("input"); + QTest::addColumn("expectedOutput"); + QTest::addColumn("expectedDefined"); + + QTest::newRow("aaa") << "aaa" << 0x616161 << true; + QTest::newRow("eng") << "eng" << 0x656E67 << true; + QTest::newRow("zho") << "zho" << 0X7A686F << true; + QTest::newRow("cho") << "chi" << 0x636869 << true; + QTest::newRow("und") << "und" << 0x756E64 << false; +} + +void TestISO639::test_str3_to_key(void) +{ + QFETCH(QString, input); + QFETCH(int, expectedOutput); + QFETCH(bool, expectedDefined); + + int actualOutput = iso639_str3_to_key(input); +// std::cerr << "Expected: " << qPrintable(expectedOutput) << std::endl; +// std::cerr << "Actual: " << qPrintable(output) << std::endl; + QCOMPARE(actualOutput, expectedOutput); + bool actualDefined = !iso639_is_key_undefined(actualOutput); + QCOMPARE(actualDefined, expectedDefined); +} + +void TestISO639::test_key2name_data(void) +{ + QTest::addColumn("input"); + QTest::addColumn("expectedOutput"); + + QTest::newRow("eng") << 0x656E67 << "English"; + QTest::newRow("zho") << 0X7A686F << "Unknown"; + QTest::newRow("cho") << 0x636869 << "Chinese"; +} + +void TestISO639::test_key2name(void) +{ + QFETCH(int, input); + QFETCH(QString, expectedOutput); + + QString output = iso639_key_toName(input); +// std::cerr << "Expected: " << qPrintable(expectedOutput) << std::endl; +// std::cerr << "Actual: " << qPrintable(output) << std::endl; + QCOMPARE(output, expectedOutput); +} + +void TestISO639::test_str2_to_str3_data(void) +{ + QTest::addColumn("input"); + QTest::addColumn("expectedOutput"); + + QTest::newRow("eng") << "en" << "eng"; + QTest::newRow("chi") << "zh" << "chi"; + QTest::newRow("xyz") << "xy" << "und"; +} + +void TestISO639::test_str2_to_str3(void) +{ + QFETCH(QString, input); + QFETCH(QString, expectedOutput); + + QString output = iso639_str2_to_str3(input); +// std::cerr << "Expected: " << qPrintable(expectedOutput) << std::endl; +// std::cerr << "Actual: " << qPrintable(output) << std::endl; + QCOMPARE(output, expectedOutput); +} + +void TestISO639::test_key_to_cankey_data(void) +{ + QTest::addColumn("input"); + QTest::addColumn("expectedOutput"); + + QTest::newRow("zho") + << (('z' << 16) | ('h' << 8) | 'o') + << (('c' << 16) | ('h' << 8) | 'i'); + QTest::newRow("xyz") + << (('x' << 16) | ('y' << 8) | 'z') + << (('x' << 16) | ('y' << 8) | 'z'); +} + +void TestISO639::test_key_to_cankey(void) +{ + QFETCH(int, input); + QFETCH(int, expectedOutput); + + int output = iso639_key_to_canonical_key(input); +// std::cerr << "Expected: " << qPrintable(expectedOutput) << std::endl; +// std::cerr << "Actual: " << qPrintable(output) << std::endl; + QCOMPARE(output, expectedOutput); +} + + +QTEST_APPLESS_MAIN(TestISO639) diff --git a/mythtv/libs/libmythbase/test/test_iso639/test_iso639.h b/mythtv/libs/libmythbase/test/test_iso639/test_iso639.h new file mode 100644 index 00000000000..926a544eb82 --- /dev/null +++ b/mythtv/libs/libmythbase/test/test_iso639/test_iso639.h @@ -0,0 +1,43 @@ +/* + * Class TestISO639 + * + * Copyright (c) David Hampton 2020 + * + * 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 2 of the License, or + * (at your option) any later version. + * + * 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, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include +#include + +#include "iso639.h" + +class TestISO639 : public QObject +{ + Q_OBJECT + +private slots: + static void test_str2name_data(void); + static void test_str2name(void); + static void test_key2str3_data(void); + static void test_key2str3(void); + static void test_str3_to_key_data(void); + static void test_str3_to_key(void); + static void test_key2name_data(void); + static void test_key2name(void); + static void test_str2_to_str3_data(void); + static void test_str2_to_str3(void); + static void test_key_to_cankey_data(void); + static void test_key_to_cankey(void); +}; diff --git a/mythtv/libs/libmythbase/test/test_iso639/test_iso639.pro b/mythtv/libs/libmythbase/test/test_iso639/test_iso639.pro new file mode 100644 index 00000000000..48a258c563b --- /dev/null +++ b/mythtv/libs/libmythbase/test/test_iso639/test_iso639.pro @@ -0,0 +1,26 @@ +include ( ../../../../settings.pro ) + +QT += testlib + +TEMPLATE = app +TARGET = test_iso639 +DEPENDPATH += . ../.. ../../logging +INCLUDEPATH += . ../.. ../../logging +LIBS += -L../.. -lmythbase-$$LIBVERSION +LIBS += -Wl,$$_RPATH_$${PWD}/../.. + +contains(QMAKE_CXX, "g++") { + QMAKE_CXXFLAGS += -O0 -fprofile-arcs -ftest-coverage + QMAKE_LFLAGS += -fprofile-arcs +} + +QMAKE_LFLAGS += -Wl,$$_RPATH_$(PWD)/../.. + +# Input +HEADERS += test_iso639.h +SOURCES += test_iso639.cpp + +QMAKE_CLEAN += $(TARGET) +QMAKE_CLEAN += ; ( cd $(OBJECTS_DIR) && rm -f *.gcov *.gcda *.gcno ) + +LIBS += $$EXTRA_LIBS $$LATE_LIBS