Skip to content

Commit

Permalink
cppcheck: Use std::transform instead of a raw loop in iso639.cpp.
Browse files Browse the repository at this point in the history
Add some test cases for the iso639 code.
  • Loading branch information
linuxdude42 committed Jun 20, 2020
1 parent 6f8d618 commit 24900a1
Show file tree
Hide file tree
Showing 5 changed files with 234 additions and 2 deletions.
5 changes: 3 additions & 2 deletions mythtv/libs/libmythbase/iso639.cpp
Expand Up @@ -60,8 +60,9 @@ vector<int> 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);});

This comment has been minimized.

Copy link
@ijc

ijc Jun 21, 2020

Contributor

Given static inline int iso639_str3_to_key(const QString &iso639_2) is this the same as just passing ìso639_str3_to_key as the third argument without the lambda wrapper?

This comment has been minimized.

Copy link
@linuxdude42

linuxdude42 via email Jun 21, 2020

Author Contributor
}
return s_language_keys;
}
Expand Down
1 change: 1 addition & 0 deletions mythtv/libs/libmythbase/test/test_iso639/.gitignore
@@ -0,0 +1 @@
test_iso639
161 changes: 161 additions & 0 deletions 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 <iostream>
#include "test_iso639.h"

void TestISO639::test_str2name_data(void)
{
QTest::addColumn<QString>("input");
QTest::addColumn<QString>("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<const uchar *>(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<int>("input");
QTest::addColumn<QString>("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<QString>("input");
QTest::addColumn<int>("expectedOutput");
QTest::addColumn<bool>("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<int>("input");
QTest::addColumn<QString>("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<QString>("input");
QTest::addColumn<QString>("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<int>("input");
QTest::addColumn<int>("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)
43 changes: 43 additions & 0 deletions 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 <QtTest/QtTest>
#include <iostream>

#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);
};
26 changes: 26 additions & 0 deletions 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

0 comments on commit 24900a1

Please sign in to comment.