From 01b6e1cbf28f54793e22cc40affac7fb03511180 Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Tue, 16 Oct 2018 19:01:13 +0200 Subject: [PATCH] test: use GTest instead of cppunit --- .travis.yml | 7 +- NEWS | 1 + doc/user.rst | 2 +- test/DivideStringTest.hxx | 54 --------- test/MimeTypeTest.hxx | 49 -------- test/SplitStringTest.hxx | 65 ----------- test/TestAudioFormat.cxx | 50 ++------ test/TestAudioFormat.hxx | 39 ------- test/TestCircularBuffer.cxx | 151 ++++++++++++++++++++++++ test/TestCircularBuffer.hxx | 158 ------------------------- test/TestDivideString.cxx | 45 ++++++++ test/TestFs.cxx | 161 +++++++++++--------------- test/TestIcu.cxx | 92 +++++---------- test/TestMimeType.cxx | 40 +++++++ test/TestRewindInputStream.cxx | 126 ++++++++++++++++++++ test/TestSplitString.cxx | 57 +++++++++ test/TestUriUtil.cxx | 48 ++++++++ test/UriUtilTest.hxx | 66 ----------- test/meson.build | 68 ++++++----- test/test_archive.cxx | 62 +++------- test/test_byte_reverse.cxx | 51 ++------- test/test_icy_parser.cxx | 56 +++------ test/test_mixramp.cxx | 130 +++++++++------------ test/test_pcm_all.hxx | 152 ------------------------ test/test_pcm_channels.cxx | 73 ++++++------ test/test_pcm_dither.cxx | 17 ++- test/test_pcm_export.cxx | 96 ++++++++-------- test/test_pcm_format.cxx | 49 ++++---- test/test_pcm_interleave.cxx | 76 ++++++------ test/test_pcm_main.cxx | 47 -------- test/test_pcm_mix.cxx | 24 ++-- test/test_pcm_pack.cxx | 18 ++- test/test_pcm_util.hxx | 8 +- test/test_pcm_volume.cxx | 44 ++++--- test/test_protocol.cxx | 48 ++------ test/test_queue_priority.cxx | 56 +++------ test/test_rewind.cxx | 149 ------------------------ test/test_translate_song.cxx | 204 ++++++++++++++++----------------- test/test_util.cxx | 32 ------ 39 files changed, 1027 insertions(+), 1644 deletions(-) delete mode 100644 test/DivideStringTest.hxx delete mode 100644 test/MimeTypeTest.hxx delete mode 100644 test/SplitStringTest.hxx delete mode 100644 test/TestAudioFormat.hxx create mode 100644 test/TestCircularBuffer.cxx delete mode 100644 test/TestCircularBuffer.hxx create mode 100644 test/TestDivideString.cxx create mode 100644 test/TestMimeType.cxx create mode 100644 test/TestRewindInputStream.cxx create mode 100644 test/TestSplitString.cxx create mode 100644 test/TestUriUtil.cxx delete mode 100644 test/UriUtilTest.hxx delete mode 100644 test/test_pcm_all.hxx delete mode 100644 test/test_pcm_main.cxx delete mode 100644 test/test_rewind.cxx delete mode 100644 test/test_util.cxx diff --git a/.travis.yml b/.travis.yml index 05f52da85..b0f9bc831 100644 --- a/.travis.yml +++ b/.travis.yml @@ -13,7 +13,7 @@ matrix: - sourceline: 'ppa:deadsnakes/ppa' # for Python 3.7 (required by Meson) packages: - g++-6 - - libcppunit-dev + - libgtest-dev - boost1.67 - python3.6 - python3-urllib3 @@ -38,7 +38,7 @@ matrix: - sourceline: 'ppa:deadsnakes/ppa' # for Python 3.7 (required by Meson) packages: - g++-8 - - libcppunit-dev + - libgtest-dev - boost1.67 - python3.6 - python3-urllib3 @@ -68,7 +68,8 @@ before_install: install: # C++14 - - test "$TRAVIS_OS_NAME" != "osx" || brew install cppunit ccache meson + - test "$TRAVIS_OS_NAME" != "osx" || brew install ccache meson + - test "$TRAVIS_OS_NAME" != "osx" || brew install --HEAD https://gist.githubusercontent.com/Kronuz/96ac10fbd8472eb1e7566d740c4034f8/raw/gtest.rb before_script: - ccache -s diff --git a/NEWS b/NEWS index b67d51ccd..fdf301876 100644 --- a/NEWS +++ b/NEWS @@ -47,6 +47,7 @@ ver 0.21 (not yet released) * systemd watchdog support * require GCC 6 * build with Meson instead of autotools +* use GTest instead of cppunit ver 0.20.22 (not yet released) * storage diff --git a/doc/user.rst b/doc/user.rst index 7a6eded7a..07a25fcd2 100644 --- a/doc/user.rst +++ b/doc/user.rst @@ -80,7 +80,7 @@ For example, the following installs a fairly complete list of build dependencies libavahi-client-dev \ libsqlite3-dev \ libsystemd-dev libwrap0-dev \ - libcppunit-dev xmlto \ + libgtest-dev xmlto \ libboost-dev \ libicu-dev diff --git a/test/DivideStringTest.hxx b/test/DivideStringTest.hxx deleted file mode 100644 index f1834fa82..000000000 --- a/test/DivideStringTest.hxx +++ /dev/null @@ -1,54 +0,0 @@ -/* - * Unit tests for src/util/ - */ - -#include "check.h" -#include "util/DivideString.hxx" - -#include -#include - -#include - -class DivideStringTest : public CppUnit::TestFixture { - CPPUNIT_TEST_SUITE(DivideStringTest); - CPPUNIT_TEST(TestBasic); - CPPUNIT_TEST(TestEmpty); - CPPUNIT_TEST(TestFail); - CPPUNIT_TEST(TestStrip); - CPPUNIT_TEST_SUITE_END(); - -public: - void TestBasic() { - constexpr char input[] = "foo.bar"; - const DivideString ds(input, '.'); - CPPUNIT_ASSERT(ds.IsDefined()); - CPPUNIT_ASSERT(!ds.empty()); - CPPUNIT_ASSERT_EQUAL(0, strcmp(ds.GetFirst(), "foo")); - CPPUNIT_ASSERT_EQUAL(input + 4, ds.GetSecond()); - } - - void TestEmpty() { - constexpr char input[] = ".bar"; - const DivideString ds(input, '.'); - CPPUNIT_ASSERT(ds.IsDefined()); - CPPUNIT_ASSERT(ds.empty()); - CPPUNIT_ASSERT_EQUAL(0, strcmp(ds.GetFirst(), "")); - CPPUNIT_ASSERT_EQUAL(input + 1, ds.GetSecond()); - } - - void TestFail() { - constexpr char input[] = "foo!bar"; - const DivideString ds(input, '.'); - CPPUNIT_ASSERT(!ds.IsDefined()); - } - - void TestStrip() { - constexpr char input[] = " foo\t.\nbar\r"; - const DivideString ds(input, '.', true); - CPPUNIT_ASSERT(ds.IsDefined()); - CPPUNIT_ASSERT(!ds.empty()); - CPPUNIT_ASSERT_EQUAL(0, strcmp(ds.GetFirst(), "foo")); - CPPUNIT_ASSERT_EQUAL(input + 7, ds.GetSecond()); - } -}; diff --git a/test/MimeTypeTest.hxx b/test/MimeTypeTest.hxx deleted file mode 100644 index 2d4286279..000000000 --- a/test/MimeTypeTest.hxx +++ /dev/null @@ -1,49 +0,0 @@ -/* - * Unit tests for src/util/ - */ - -#include "check.h" -#include "util/MimeType.hxx" - -#include -#include - -#include - -class MimeTypeTest : public CppUnit::TestFixture { - CPPUNIT_TEST_SUITE(MimeTypeTest); - CPPUNIT_TEST(TestBase); - CPPUNIT_TEST(TestParameters); - CPPUNIT_TEST_SUITE_END(); - -public: - void TestBase() { - CPPUNIT_ASSERT("" == GetMimeTypeBase("")); - CPPUNIT_ASSERT("" == GetMimeTypeBase(";")); - CPPUNIT_ASSERT("foo" == GetMimeTypeBase("foo")); - CPPUNIT_ASSERT("foo/bar" == GetMimeTypeBase("foo/bar")); - CPPUNIT_ASSERT("foo/bar" == GetMimeTypeBase("foo/bar;")); - CPPUNIT_ASSERT("foo/bar" == GetMimeTypeBase("foo/bar; x=y")); - CPPUNIT_ASSERT("foo/bar" == GetMimeTypeBase("foo/bar;x=y")); - } - - void TestParameters() { - CPPUNIT_ASSERT(ParseMimeTypeParameters("").empty()); - CPPUNIT_ASSERT(ParseMimeTypeParameters("foo/bar").empty()); - CPPUNIT_ASSERT(ParseMimeTypeParameters("foo/bar;").empty()); - CPPUNIT_ASSERT(ParseMimeTypeParameters("foo/bar;garbage").empty()); - CPPUNIT_ASSERT(ParseMimeTypeParameters("foo/bar; garbage").empty()); - - auto p = ParseMimeTypeParameters("foo/bar;a=b"); - CPPUNIT_ASSERT(!p.empty()); - CPPUNIT_ASSERT(p["a"] == "b"); - CPPUNIT_ASSERT(p.size() == 1); - - p = ParseMimeTypeParameters("foo/bar; a=b;c;d=e ; f=g "); - CPPUNIT_ASSERT(!p.empty()); - CPPUNIT_ASSERT(p["a"] == "b"); - CPPUNIT_ASSERT(p["d"] == "e"); - CPPUNIT_ASSERT(p["f"] == "g"); - CPPUNIT_ASSERT(p.size() == 3); - } -}; diff --git a/test/SplitStringTest.hxx b/test/SplitStringTest.hxx deleted file mode 100644 index 87ed385ea..000000000 --- a/test/SplitStringTest.hxx +++ /dev/null @@ -1,65 +0,0 @@ -/* - * Unit tests for src/util/ - */ - -#include "check.h" -#include "util/SplitString.hxx" -#include "util/Macros.hxx" - -#include -#include - -#include - -class SplitStringTest : public CppUnit::TestFixture { - CPPUNIT_TEST_SUITE(SplitStringTest); - CPPUNIT_TEST(TestBasic); - CPPUNIT_TEST(TestStrip); - CPPUNIT_TEST(TestNoStrip); - CPPUNIT_TEST(TestEmpty); - CPPUNIT_TEST_SUITE_END(); - -public: - void TestBasic() { - constexpr char input[] = "foo.bar"; - const char *const output[] = { "foo", "bar" }; - size_t i = 0; - for (auto p : SplitString(input, '.')) { - CPPUNIT_ASSERT(i < ARRAY_SIZE(output)); - CPPUNIT_ASSERT(p == output[i]); - ++i; - } - - CPPUNIT_ASSERT_EQUAL(ARRAY_SIZE(output), i); - } - - void TestStrip() { - constexpr char input[] = " foo\t.\r\nbar\r\n2"; - const char *const output[] = { "foo", "bar\r\n2" }; - size_t i = 0; - for (auto p : SplitString(input, '.')) { - CPPUNIT_ASSERT(i < ARRAY_SIZE(output)); - CPPUNIT_ASSERT(p == output[i]); - ++i; - } - - CPPUNIT_ASSERT_EQUAL(ARRAY_SIZE(output), i); - } - - void TestNoStrip() { - constexpr char input[] = " foo\t.\r\nbar\r\n2"; - const char *const output[] = { " foo\t", "\r\nbar\r\n2" }; - size_t i = 0; - for (auto p : SplitString(input, '.', false)) { - CPPUNIT_ASSERT(i < ARRAY_SIZE(output)); - CPPUNIT_ASSERT(p == output[i]); - ++i; - } - - CPPUNIT_ASSERT_EQUAL(ARRAY_SIZE(output), i); - } - - void TestEmpty() { - CPPUNIT_ASSERT(SplitString("", '.').empty()); - } -}; diff --git a/test/TestAudioFormat.cxx b/test/TestAudioFormat.cxx index e5a5041b9..47d1edad8 100644 --- a/test/TestAudioFormat.cxx +++ b/test/TestAudioFormat.cxx @@ -18,45 +18,14 @@ */ #include "config.h" -#include "TestAudioFormat.hxx" #include "AudioFormat.hxx" #include "AudioParser.hxx" #include "util/StringBuffer.hxx" -#include +#include #include -namespace CppUnit { -template<> -struct assertion_traits -{ - static bool equal(const char *x, const char *y) - { - return strcmp(x, y) == 0; - } - - static std::string toString(const char *x) - { - return std::string("\"") + x + "\""; - } -}; - -template<> -struct assertion_traits -{ - static bool equal(AudioFormat x, AudioFormat y) - { - return x == y; - } - - static std::string toString(AudioFormat x) - { - return ToString(x).c_str(); - } -}; -} - struct AudioFormatStringTest { AudioFormat af; const char *s; @@ -78,24 +47,19 @@ static constexpr AudioFormatStringTest af_mask_tests[] = { { AudioFormat::Undefined(), "*:*:*" }, }; -void -AudioFormatTest::TestToString() +TEST(AudioFormatTest, ToString) { for (const auto &i : af_string_tests) - CPPUNIT_ASSERT_EQUAL(i.s, ToString(i.af).c_str()); + EXPECT_STREQ(i.s, ToString(i.af).c_str()); } -void -AudioFormatTest::TestParse() +TEST(AudioFormatTest, Parse) { for (const auto &i : af_string_tests) { - CPPUNIT_ASSERT_EQUAL(i.af, - ParseAudioFormat(i.s, false)); - CPPUNIT_ASSERT_EQUAL(i.af, - ParseAudioFormat(i.s, true)); + EXPECT_EQ(i.af, ParseAudioFormat(i.s, false)); + EXPECT_EQ(i.af, ParseAudioFormat(i.s, true)); } for (const auto &i : af_mask_tests) - CPPUNIT_ASSERT_EQUAL(i.af, - ParseAudioFormat(i.s, true)); + EXPECT_EQ(i.af, ParseAudioFormat(i.s, true)); } diff --git a/test/TestAudioFormat.hxx b/test/TestAudioFormat.hxx deleted file mode 100644 index 5caf598bd..000000000 --- a/test/TestAudioFormat.hxx +++ /dev/null @@ -1,39 +0,0 @@ -/* - * Copyright 2003-2017 The Music Player Daemon Project - * http://www.musicpd.org - * - * 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. - */ - -#ifndef MPD_TEST_AUDIO_FORMAT_HXX -#define MPD_TEST_AUDIO_FORMAT_HXX - -#include "check.h" - -#include -#include - -class AudioFormatTest : public CppUnit::TestFixture { - CPPUNIT_TEST_SUITE(AudioFormatTest); - CPPUNIT_TEST(TestToString); - CPPUNIT_TEST(TestParse); - CPPUNIT_TEST_SUITE_END(); - -public: - void TestToString(); - void TestParse(); -}; - -#endif diff --git a/test/TestCircularBuffer.cxx b/test/TestCircularBuffer.cxx new file mode 100644 index 000000000..d0e41bba4 --- /dev/null +++ b/test/TestCircularBuffer.cxx @@ -0,0 +1,151 @@ +/* + * Unit tests for class CircularBuffer. + */ + +#include "config.h" +#include "util/CircularBuffer.hxx" + +#include + +TEST(CircularBuffer, Basic) +{ + static size_t N = 8; + int data[N]; + CircularBuffer buffer(data, N); + + EXPECT_EQ(size_t(N), buffer.GetCapacity()); + + /* '.' = empty; 'O' = occupied; 'X' = blocked */ + + /* checks on empty buffer */ + /* [.......X] */ + EXPECT_TRUE(buffer.empty()); + EXPECT_FALSE(buffer.IsFull()); + EXPECT_EQ(size_t(0), buffer.GetSize()); + EXPECT_EQ(size_t(7), buffer.GetSpace()); + EXPECT_TRUE(buffer.Read().empty()); + EXPECT_FALSE(buffer.Write().empty()); + EXPECT_EQ(&data[0], buffer.Write().data); + EXPECT_EQ(size_t(7), buffer.Write().size); + + /* append one element */ + /* [O......X] */ + buffer.Append(1); + EXPECT_FALSE(buffer.empty()); + EXPECT_FALSE(buffer.IsFull()); + EXPECT_FALSE(buffer.Read().empty()); + EXPECT_EQ(size_t(1), buffer.GetSize()); + EXPECT_EQ(size_t(6), buffer.GetSpace()); + EXPECT_EQ(size_t(1), buffer.Read().size); + EXPECT_EQ(&data[0], buffer.Read().data); + EXPECT_FALSE(buffer.Write().empty()); + EXPECT_EQ(&data[1], buffer.Write().data); + EXPECT_EQ(size_t(6), buffer.Write().size); + + /* append 6 elements, buffer is now full */ + /* [OOOOOOOX] */ + buffer.Append(6); + EXPECT_FALSE(buffer.empty()); + EXPECT_TRUE(buffer.IsFull()); + EXPECT_FALSE(buffer.Read().empty()); + EXPECT_EQ(size_t(7), buffer.GetSize()); + EXPECT_EQ(size_t(0), buffer.GetSpace()); + EXPECT_EQ(size_t(7), buffer.Read().size); + EXPECT_EQ(&data[0], buffer.Read().data); + EXPECT_TRUE(buffer.Write().empty()); + + /* consume [0]; can append one at [7] */ + /* [XOOOOOO.] */ + buffer.Consume(1); + EXPECT_FALSE(buffer.empty()); + EXPECT_FALSE(buffer.IsFull()); + EXPECT_FALSE(buffer.Read().empty()); + EXPECT_EQ(size_t(6), buffer.GetSize()); + EXPECT_EQ(size_t(1), buffer.GetSpace()); + EXPECT_EQ(size_t(6), buffer.Read().size); + EXPECT_EQ(&data[1], buffer.Read().data); + EXPECT_FALSE(buffer.Write().empty()); + EXPECT_EQ(&data[7], buffer.Write().data); + EXPECT_EQ(size_t(1), buffer.Write().size); + + /* append one element; [0] is still empty but cannot + be written to because head==1 */ + /* [XOOOOOOO] */ + buffer.Append(1); + EXPECT_FALSE(buffer.empty()); + EXPECT_TRUE(buffer.IsFull()); + EXPECT_FALSE(buffer.Read().empty()); + EXPECT_EQ(size_t(7), buffer.GetSize()); + EXPECT_EQ(size_t(0), buffer.GetSpace()); + EXPECT_EQ(size_t(7), buffer.Read().size); + EXPECT_EQ(&data[1], buffer.Read().data); + EXPECT_TRUE(buffer.Write().empty()); + + /* consume [1..3]; can append [0..2] */ + /* [...XOOOO] */ + buffer.Consume(3); + EXPECT_FALSE(buffer.empty()); + EXPECT_FALSE(buffer.IsFull()); + EXPECT_FALSE(buffer.Read().empty()); + EXPECT_EQ(size_t(4), buffer.GetSize()); + EXPECT_EQ(size_t(3), buffer.GetSpace()); + EXPECT_EQ(size_t(4), buffer.Read().size); + EXPECT_EQ(&data[4], buffer.Read().data); + EXPECT_FALSE(buffer.Write().empty()); + EXPECT_EQ(&data[0], buffer.Write().data); + EXPECT_EQ(size_t(3), buffer.Write().size); + + /* append [0..1] */ + /* [OO.XOOOO] */ + buffer.Append(2); + EXPECT_FALSE(buffer.empty()); + EXPECT_FALSE(buffer.IsFull()); + EXPECT_FALSE(buffer.Read().empty()); + EXPECT_EQ(size_t(6), buffer.GetSize()); + EXPECT_EQ(size_t(1), buffer.GetSpace()); + EXPECT_EQ(size_t(4), buffer.Read().size); + EXPECT_EQ(&data[4], buffer.Read().data); + EXPECT_FALSE(buffer.Write().empty()); + EXPECT_EQ(&data[2], buffer.Write().data); + EXPECT_EQ(size_t(1), buffer.Write().size); + + /* append [2] */ + /* [OOOXOOOO] */ + buffer.Append(1); + EXPECT_FALSE(buffer.empty()); + EXPECT_TRUE(buffer.IsFull()); + EXPECT_FALSE(buffer.Read().empty()); + EXPECT_EQ(size_t(7), buffer.GetSize()); + EXPECT_EQ(size_t(0), buffer.GetSpace()); + EXPECT_EQ(size_t(4), buffer.Read().size); + EXPECT_EQ(&data[4], buffer.Read().data); + EXPECT_TRUE(buffer.Write().empty()); + + /* consume [4..7] */ + /* [OOO....X] */ + buffer.Consume(4); + EXPECT_FALSE(buffer.empty()); + EXPECT_FALSE(buffer.IsFull()); + EXPECT_FALSE(buffer.Read().empty()); + EXPECT_EQ(size_t(3), buffer.GetSize()); + EXPECT_EQ(size_t(4), buffer.GetSpace()); + EXPECT_EQ(size_t(3), buffer.Read().size); + EXPECT_EQ(&data[0], buffer.Read().data); + EXPECT_FALSE(buffer.Write().empty()); + EXPECT_EQ(&data[3], buffer.Write().data); + EXPECT_EQ(size_t(4), buffer.Write().size); + + /* consume [0..2]; after that, we can only write 5, + because the CircularBuffer class doesn't have + special code to rewind/reset an empty buffer */ + /* [..X.....] */ + buffer.Consume(3); + EXPECT_TRUE(buffer.empty()); + EXPECT_FALSE(buffer.IsFull()); + EXPECT_EQ(size_t(0), buffer.GetSize()); + EXPECT_EQ(size_t(7), buffer.GetSpace()); + EXPECT_TRUE(buffer.Read().empty()); + EXPECT_FALSE(buffer.Write().empty()); + EXPECT_EQ(&data[3], buffer.Write().data); + EXPECT_EQ(size_t(5), buffer.Write().size); +} diff --git a/test/TestCircularBuffer.hxx b/test/TestCircularBuffer.hxx deleted file mode 100644 index a48a01614..000000000 --- a/test/TestCircularBuffer.hxx +++ /dev/null @@ -1,158 +0,0 @@ -/* - * Unit tests for class CircularBuffer. - */ - -#include "check.h" -#include "util/CircularBuffer.hxx" - -#include -#include - -class TestCircularBuffer : public CppUnit::TestFixture { - CPPUNIT_TEST_SUITE(TestCircularBuffer); - CPPUNIT_TEST(TestIt); - CPPUNIT_TEST_SUITE_END(); - -public: - void TestIt() { - static size_t N = 8; - int data[N]; - CircularBuffer buffer(data, N); - - CPPUNIT_ASSERT_EQUAL(size_t(N), buffer.GetCapacity()); - - /* '.' = empty; 'O' = occupied; 'X' = blocked */ - - /* checks on empty buffer */ - /* [.......X] */ - CPPUNIT_ASSERT_EQUAL(true, buffer.empty()); - CPPUNIT_ASSERT_EQUAL(false, buffer.IsFull()); - CPPUNIT_ASSERT_EQUAL(size_t(0), buffer.GetSize()); - CPPUNIT_ASSERT_EQUAL(size_t(7), buffer.GetSpace()); - CPPUNIT_ASSERT_EQUAL(true, buffer.Read().empty()); - CPPUNIT_ASSERT_EQUAL(false, buffer.Write().empty()); - CPPUNIT_ASSERT_EQUAL(&data[0], buffer.Write().data); - CPPUNIT_ASSERT_EQUAL(size_t(7), buffer.Write().size); - - /* append one element */ - /* [O......X] */ - buffer.Append(1); - CPPUNIT_ASSERT_EQUAL(false, buffer.empty()); - CPPUNIT_ASSERT_EQUAL(false, buffer.IsFull()); - CPPUNIT_ASSERT_EQUAL(false, buffer.Read().empty()); - CPPUNIT_ASSERT_EQUAL(size_t(1), buffer.GetSize()); - CPPUNIT_ASSERT_EQUAL(size_t(6), buffer.GetSpace()); - CPPUNIT_ASSERT_EQUAL(size_t(1), buffer.Read().size); - CPPUNIT_ASSERT_EQUAL(&data[0], buffer.Read().data); - CPPUNIT_ASSERT_EQUAL(false, buffer.Write().empty()); - CPPUNIT_ASSERT_EQUAL(&data[1], buffer.Write().data); - CPPUNIT_ASSERT_EQUAL(size_t(6), buffer.Write().size); - - /* append 6 elements, buffer is now full */ - /* [OOOOOOOX] */ - buffer.Append(6); - CPPUNIT_ASSERT_EQUAL(false, buffer.empty()); - CPPUNIT_ASSERT_EQUAL(true, buffer.IsFull()); - CPPUNIT_ASSERT_EQUAL(false, buffer.Read().empty()); - CPPUNIT_ASSERT_EQUAL(size_t(7), buffer.GetSize()); - CPPUNIT_ASSERT_EQUAL(size_t(0), buffer.GetSpace()); - CPPUNIT_ASSERT_EQUAL(size_t(7), buffer.Read().size); - CPPUNIT_ASSERT_EQUAL(&data[0], buffer.Read().data); - CPPUNIT_ASSERT_EQUAL(true, buffer.Write().empty()); - - /* consume [0]; can append one at [7] */ - /* [XOOOOOO.] */ - buffer.Consume(1); - CPPUNIT_ASSERT_EQUAL(false, buffer.empty()); - CPPUNIT_ASSERT_EQUAL(false, buffer.IsFull()); - CPPUNIT_ASSERT_EQUAL(false, buffer.Read().empty()); - CPPUNIT_ASSERT_EQUAL(size_t(6), buffer.GetSize()); - CPPUNIT_ASSERT_EQUAL(size_t(1), buffer.GetSpace()); - CPPUNIT_ASSERT_EQUAL(size_t(6), buffer.Read().size); - CPPUNIT_ASSERT_EQUAL(&data[1], buffer.Read().data); - CPPUNIT_ASSERT_EQUAL(false, buffer.Write().empty()); - CPPUNIT_ASSERT_EQUAL(&data[7], buffer.Write().data); - CPPUNIT_ASSERT_EQUAL(size_t(1), buffer.Write().size); - - /* append one element; [0] is still empty but cannot - be written to because head==1 */ - /* [XOOOOOOO] */ - buffer.Append(1); - CPPUNIT_ASSERT_EQUAL(false, buffer.empty()); - CPPUNIT_ASSERT_EQUAL(true, buffer.IsFull()); - CPPUNIT_ASSERT_EQUAL(false, buffer.Read().empty()); - CPPUNIT_ASSERT_EQUAL(size_t(7), buffer.GetSize()); - CPPUNIT_ASSERT_EQUAL(size_t(0), buffer.GetSpace()); - CPPUNIT_ASSERT_EQUAL(size_t(7), buffer.Read().size); - CPPUNIT_ASSERT_EQUAL(&data[1], buffer.Read().data); - CPPUNIT_ASSERT_EQUAL(true, buffer.Write().empty()); - - /* consume [1..3]; can append [0..2] */ - /* [...XOOOO] */ - buffer.Consume(3); - CPPUNIT_ASSERT_EQUAL(false, buffer.empty()); - CPPUNIT_ASSERT_EQUAL(false, buffer.IsFull()); - CPPUNIT_ASSERT_EQUAL(false, buffer.Read().empty()); - CPPUNIT_ASSERT_EQUAL(size_t(4), buffer.GetSize()); - CPPUNIT_ASSERT_EQUAL(size_t(3), buffer.GetSpace()); - CPPUNIT_ASSERT_EQUAL(size_t(4), buffer.Read().size); - CPPUNIT_ASSERT_EQUAL(&data[4], buffer.Read().data); - CPPUNIT_ASSERT_EQUAL(false, buffer.Write().empty()); - CPPUNIT_ASSERT_EQUAL(&data[0], buffer.Write().data); - CPPUNIT_ASSERT_EQUAL(size_t(3), buffer.Write().size); - - /* append [0..1] */ - /* [OO.XOOOO] */ - buffer.Append(2); - CPPUNIT_ASSERT_EQUAL(false, buffer.empty()); - CPPUNIT_ASSERT_EQUAL(false, buffer.IsFull()); - CPPUNIT_ASSERT_EQUAL(false, buffer.Read().empty()); - CPPUNIT_ASSERT_EQUAL(size_t(6), buffer.GetSize()); - CPPUNIT_ASSERT_EQUAL(size_t(1), buffer.GetSpace()); - CPPUNIT_ASSERT_EQUAL(size_t(4), buffer.Read().size); - CPPUNIT_ASSERT_EQUAL(&data[4], buffer.Read().data); - CPPUNIT_ASSERT_EQUAL(false, buffer.Write().empty()); - CPPUNIT_ASSERT_EQUAL(&data[2], buffer.Write().data); - CPPUNIT_ASSERT_EQUAL(size_t(1), buffer.Write().size); - - /* append [2] */ - /* [OOOXOOOO] */ - buffer.Append(1); - CPPUNIT_ASSERT_EQUAL(false, buffer.empty()); - CPPUNIT_ASSERT_EQUAL(true, buffer.IsFull()); - CPPUNIT_ASSERT_EQUAL(false, buffer.Read().empty()); - CPPUNIT_ASSERT_EQUAL(size_t(7), buffer.GetSize()); - CPPUNIT_ASSERT_EQUAL(size_t(0), buffer.GetSpace()); - CPPUNIT_ASSERT_EQUAL(size_t(4), buffer.Read().size); - CPPUNIT_ASSERT_EQUAL(&data[4], buffer.Read().data); - CPPUNIT_ASSERT_EQUAL(true, buffer.Write().empty()); - - /* consume [4..7] */ - /* [OOO....X] */ - buffer.Consume(4); - CPPUNIT_ASSERT_EQUAL(false, buffer.empty()); - CPPUNIT_ASSERT_EQUAL(false, buffer.IsFull()); - CPPUNIT_ASSERT_EQUAL(false, buffer.Read().empty()); - CPPUNIT_ASSERT_EQUAL(size_t(3), buffer.GetSize()); - CPPUNIT_ASSERT_EQUAL(size_t(4), buffer.GetSpace()); - CPPUNIT_ASSERT_EQUAL(size_t(3), buffer.Read().size); - CPPUNIT_ASSERT_EQUAL(&data[0], buffer.Read().data); - CPPUNIT_ASSERT_EQUAL(false, buffer.Write().empty()); - CPPUNIT_ASSERT_EQUAL(&data[3], buffer.Write().data); - CPPUNIT_ASSERT_EQUAL(size_t(4), buffer.Write().size); - - /* consume [0..2]; after that, we can only write 5, - because the CircularBuffer class doesn't have - special code to rewind/reset an empty buffer */ - /* [..X.....] */ - buffer.Consume(3); - CPPUNIT_ASSERT_EQUAL(true, buffer.empty()); - CPPUNIT_ASSERT_EQUAL(false, buffer.IsFull()); - CPPUNIT_ASSERT_EQUAL(size_t(0), buffer.GetSize()); - CPPUNIT_ASSERT_EQUAL(size_t(7), buffer.GetSpace()); - CPPUNIT_ASSERT_EQUAL(true, buffer.Read().empty()); - CPPUNIT_ASSERT_EQUAL(false, buffer.Write().empty()); - CPPUNIT_ASSERT_EQUAL(&data[3], buffer.Write().data); - CPPUNIT_ASSERT_EQUAL(size_t(5), buffer.Write().size); - } -}; diff --git a/test/TestDivideString.cxx b/test/TestDivideString.cxx new file mode 100644 index 000000000..4ec9a2838 --- /dev/null +++ b/test/TestDivideString.cxx @@ -0,0 +1,45 @@ +/* + * Unit tests for src/util/ + */ + +#include "config.h" +#include "util/DivideString.hxx" + +#include + +TEST(DivideString, Basic) +{ + constexpr char input[] = "foo.bar"; + const DivideString ds(input, '.'); + EXPECT_TRUE(ds.IsDefined()); + EXPECT_FALSE(ds.empty()); + EXPECT_EQ(0, strcmp(ds.GetFirst(), "foo")); + EXPECT_EQ(input + 4, ds.GetSecond()); +} + +TEST(DivideString, Empty) +{ + constexpr char input[] = ".bar"; + const DivideString ds(input, '.'); + EXPECT_TRUE(ds.IsDefined()); + EXPECT_TRUE(ds.empty()); + EXPECT_EQ(0, strcmp(ds.GetFirst(), "")); + EXPECT_EQ(input + 1, ds.GetSecond()); +} + +TEST(DivideString, Fail) +{ + constexpr char input[] = "foo!bar"; + const DivideString ds(input, '.'); + EXPECT_FALSE(ds.IsDefined()); +} + +TEST(DivideString, Strip) +{ + constexpr char input[] = " foo\t.\nbar\r"; + const DivideString ds(input, '.', true); + EXPECT_TRUE(ds.IsDefined()); + EXPECT_FALSE(ds.empty()); + EXPECT_EQ(0, strcmp(ds.GetFirst(), "foo")); + EXPECT_EQ(input + 7, ds.GetSecond()); +} diff --git a/test/TestFs.cxx b/test/TestFs.cxx index 3719244a7..e7ab4cc33 100644 --- a/test/TestFs.cxx +++ b/test/TestFs.cxx @@ -5,102 +5,75 @@ #include "config.h" #include "fs/Glob.hxx" -#include -#include -#include -#include - -#include -#include +#include #ifdef HAVE_CLASS_GLOB -class TestGlob : public CppUnit::TestFixture { - CPPUNIT_TEST_SUITE(TestGlob); - CPPUNIT_TEST(Basic); - CPPUNIT_TEST(Asterisk); - CPPUNIT_TEST(QuestionMark); - CPPUNIT_TEST(Wildcard); - CPPUNIT_TEST(PrefixWildcard); - CPPUNIT_TEST(SuffixWildcard); - CPPUNIT_TEST_SUITE_END(); - -public: - void Basic() { - const Glob glob("foo"); - CPPUNIT_ASSERT(glob.Check("foo")); - CPPUNIT_ASSERT(!glob.Check("fooo")); - CPPUNIT_ASSERT(!glob.Check("_foo")); - CPPUNIT_ASSERT(!glob.Check("a/foo")); - CPPUNIT_ASSERT(!glob.Check("")); - CPPUNIT_ASSERT(!glob.Check("*")); - } - - void Asterisk() { - const Glob glob("*"); - CPPUNIT_ASSERT(glob.Check("foo")); - CPPUNIT_ASSERT(glob.Check("bar")); - CPPUNIT_ASSERT(glob.Check("*")); - CPPUNIT_ASSERT(glob.Check("?")); - } - - void QuestionMark() { - const Glob glob("foo?bar"); - CPPUNIT_ASSERT(glob.Check("foo_bar")); - CPPUNIT_ASSERT(glob.Check("foo?bar")); - CPPUNIT_ASSERT(glob.Check("foo bar")); - CPPUNIT_ASSERT(!glob.Check("foobar")); - CPPUNIT_ASSERT(!glob.Check("foo__bar")); - } - - void Wildcard() { - const Glob glob("foo*bar"); - CPPUNIT_ASSERT(glob.Check("foo_bar")); - CPPUNIT_ASSERT(glob.Check("foo?bar")); - CPPUNIT_ASSERT(glob.Check("foo bar")); - CPPUNIT_ASSERT(glob.Check("foobar")); - CPPUNIT_ASSERT(glob.Check("foo__bar")); - CPPUNIT_ASSERT(!glob.Check("_foobar")); - CPPUNIT_ASSERT(!glob.Check("foobar_")); - } - - void PrefixWildcard() { - const Glob glob("*bar"); - CPPUNIT_ASSERT(glob.Check("foo_bar")); - CPPUNIT_ASSERT(glob.Check("foo?bar")); - CPPUNIT_ASSERT(glob.Check("foo bar")); - CPPUNIT_ASSERT(glob.Check("foobar")); - CPPUNIT_ASSERT(glob.Check("foo__bar")); - CPPUNIT_ASSERT(glob.Check("_foobar")); - CPPUNIT_ASSERT(glob.Check("bar")); - CPPUNIT_ASSERT(!glob.Check("foobar_")); - } - - void SuffixWildcard() { - const Glob glob("foo*"); - CPPUNIT_ASSERT(glob.Check("foo_bar")); - CPPUNIT_ASSERT(glob.Check("foo?bar")); - CPPUNIT_ASSERT(glob.Check("foo bar")); - CPPUNIT_ASSERT(glob.Check("foobar")); - CPPUNIT_ASSERT(glob.Check("foo__bar")); - CPPUNIT_ASSERT(glob.Check("foobar_")); - CPPUNIT_ASSERT(glob.Check("foo")); - } -}; - -CPPUNIT_TEST_SUITE_REGISTRATION(TestGlob); - -#endif - -int -main(gcc_unused int argc, gcc_unused char **argv) +TEST(Glob, Basic) { -#ifdef HAVE_CLASS_GLOB - CppUnit::TextUi::TestRunner runner; - auto ®istry = CppUnit::TestFactoryRegistry::getRegistry(); - runner.addTest(registry.makeTest()); - return runner.run() ? EXIT_SUCCESS : EXIT_FAILURE; -#else - return EXIT_SUCCESS; -#endif + const Glob glob("foo"); + EXPECT_TRUE(glob.Check("foo")); + EXPECT_FALSE(glob.Check("fooo")); + EXPECT_FALSE(glob.Check("_foo")); + EXPECT_FALSE(glob.Check("a/foo")); + EXPECT_FALSE(glob.Check("")); + EXPECT_FALSE(glob.Check("*")); } + +TEST(Glob, Asterisk) +{ + const Glob glob("*"); + EXPECT_TRUE(glob.Check("foo")); + EXPECT_TRUE(glob.Check("bar")); + EXPECT_TRUE(glob.Check("*")); + EXPECT_TRUE(glob.Check("?")); +} + +TEST(Glob, QuestionMark) +{ + const Glob glob("foo?bar"); + EXPECT_TRUE(glob.Check("foo_bar")); + EXPECT_TRUE(glob.Check("foo?bar")); + EXPECT_TRUE(glob.Check("foo bar")); + EXPECT_FALSE(glob.Check("foobar")); + EXPECT_FALSE(glob.Check("foo__bar")); +} + +TEST(Glob, Wildcard) +{ + const Glob glob("foo*bar"); + EXPECT_TRUE(glob.Check("foo_bar")); + EXPECT_TRUE(glob.Check("foo?bar")); + EXPECT_TRUE(glob.Check("foo bar")); + EXPECT_TRUE(glob.Check("foobar")); + EXPECT_TRUE(glob.Check("foo__bar")); + EXPECT_FALSE(glob.Check("_foobar")); + EXPECT_FALSE(glob.Check("foobar_")); +} + +TEST(Glob, PrefixWildcard) +{ + const Glob glob("*bar"); + EXPECT_TRUE(glob.Check("foo_bar")); + EXPECT_TRUE(glob.Check("foo?bar")); + EXPECT_TRUE(glob.Check("foo bar")); + EXPECT_TRUE(glob.Check("foobar")); + EXPECT_TRUE(glob.Check("foo__bar")); + EXPECT_TRUE(glob.Check("_foobar")); + EXPECT_TRUE(glob.Check("bar")); + EXPECT_FALSE(glob.Check("foobar_")); +} + +TEST(Glob, SuffixWildcard) +{ + const Glob glob("foo*"); + EXPECT_TRUE(glob.Check("foo_bar")); + EXPECT_TRUE(glob.Check("foo?bar")); + EXPECT_TRUE(glob.Check("foo bar")); + EXPECT_TRUE(glob.Check("foobar")); + EXPECT_TRUE(glob.Check("foo__bar")); + EXPECT_TRUE(glob.Check("foobar_")); + EXPECT_TRUE(glob.Check("foo")); +} + +#endif diff --git a/test/TestIcu.cxx b/test/TestIcu.cxx index 33de1fc6c..d5345556d 100644 --- a/test/TestIcu.cxx +++ b/test/TestIcu.cxx @@ -5,17 +5,8 @@ #include "config.h" #include "lib/icu/Converter.hxx" #include "util/AllocatedString.hxx" -#include "util/StringAPI.hxx" -#include -#include -#include -#include - -#include - -#include -#include +#include #ifdef HAVE_ICU_CONVERTER @@ -32,61 +23,30 @@ static constexpr StringPair latin1_tests[] = { { "\xc3\xbc", "\xfc" }, }; -class TestIcuConverter : public CppUnit::TestFixture { - CPPUNIT_TEST_SUITE(TestIcuConverter); - CPPUNIT_TEST(TestInvalidCharset); - CPPUNIT_TEST(TestLatin1); - CPPUNIT_TEST_SUITE_END(); - -public: - void TestInvalidCharset() { - try { - IcuConverter::Create("doesntexist"); - CPPUNIT_FAIL("Exception expected"); - } catch (...) { - } - } - - void TestLatin1() { - IcuConverter *const converter = - IcuConverter::Create("iso-8859-1"); - CPPUNIT_ASSERT(converter != nullptr); - - for (const auto i : invalid_utf8) { - try { - auto f = converter->FromUTF8(i); - CPPUNIT_FAIL("Exception expected"); - } catch (...) { - } - } - - for (const auto i : latin1_tests) { - auto f = converter->FromUTF8(i.utf8); - CPPUNIT_ASSERT_EQUAL(true, StringIsEqual(f.c_str(), - i.other)); - - auto t = converter->ToUTF8(i.other); - CPPUNIT_ASSERT_EQUAL(true, StringIsEqual(t.c_str(), - i.utf8)); - } - - delete converter; - } -}; - -CPPUNIT_TEST_SUITE_REGISTRATION(TestIcuConverter); - -#endif - -int -main(gcc_unused int argc, gcc_unused char **argv) +TEST(IcuConverter, InvalidCharset) { -#ifdef HAVE_ICU_CONVERTER - CppUnit::TextUi::TestRunner runner; - auto ®istry = CppUnit::TestFactoryRegistry::getRegistry(); - runner.addTest(registry.makeTest()); - return runner.run() ? EXIT_SUCCESS : EXIT_FAILURE; -#else - return EXIT_SUCCESS; -#endif + EXPECT_ANY_THROW(IcuConverter::Create("doesntexist")); } + +TEST(IcuConverter, Latin1) +{ + IcuConverter *const converter = + IcuConverter::Create("iso-8859-1"); + ASSERT_NE(converter, nullptr); + + for (const auto i : invalid_utf8) { + EXPECT_ANY_THROW(converter->FromUTF8(i)); + } + + for (const auto i : latin1_tests) { + auto f = converter->FromUTF8(i.utf8); + EXPECT_STREQ(f.c_str(), i.other); + + auto t = converter->ToUTF8(i.other); + EXPECT_STREQ(t.c_str(), i.utf8); + } + + delete converter; +} + +#endif diff --git a/test/TestMimeType.cxx b/test/TestMimeType.cxx new file mode 100644 index 000000000..5f110172c --- /dev/null +++ b/test/TestMimeType.cxx @@ -0,0 +1,40 @@ +/* + * Unit tests for src/util/ + */ + +#include "config.h" +#include "util/MimeType.hxx" + +#include + +TEST(MimeType, Base) +{ + EXPECT_EQ("", GetMimeTypeBase("")); + EXPECT_EQ("", GetMimeTypeBase(";")); + EXPECT_EQ("foo", GetMimeTypeBase("foo")); + EXPECT_EQ("foo/bar", GetMimeTypeBase("foo/bar")); + EXPECT_EQ("foo/bar", GetMimeTypeBase("foo/bar;")); + EXPECT_EQ("foo/bar", GetMimeTypeBase("foo/bar; x=y")); + EXPECT_EQ("foo/bar", GetMimeTypeBase("foo/bar;x=y")); +} + +TEST(UriUtil, Parameters) +{ + EXPECT_TRUE(ParseMimeTypeParameters("").empty()); + EXPECT_TRUE(ParseMimeTypeParameters("foo/bar").empty()); + EXPECT_TRUE(ParseMimeTypeParameters("foo/bar;").empty()); + EXPECT_TRUE(ParseMimeTypeParameters("foo/bar;garbage").empty()); + EXPECT_TRUE(ParseMimeTypeParameters("foo/bar; garbage").empty()); + + auto p = ParseMimeTypeParameters("foo/bar;a=b"); + EXPECT_FALSE(p.empty()); + EXPECT_EQ(p["a"], "b"); + EXPECT_EQ(p.size(), 1u); + + p = ParseMimeTypeParameters("foo/bar; a=b;c;d=e ; f=g "); + EXPECT_FALSE(p.empty()); + EXPECT_EQ(p["a"], "b"); + EXPECT_EQ(p["d"], "e"); + EXPECT_EQ(p["f"], "g"); + EXPECT_EQ(p.size(), 3u); +} diff --git a/test/TestRewindInputStream.cxx b/test/TestRewindInputStream.cxx new file mode 100644 index 000000000..769bd710e --- /dev/null +++ b/test/TestRewindInputStream.cxx @@ -0,0 +1,126 @@ +/* + * Unit tests for class RewindInputStream. + */ + +#include "config.h" +#include "input/RewindInputStream.hxx" +#include "input/InputStream.hxx" +#include "thread/Mutex.hxx" + +#include + +#include + +class StringInputStream final : public InputStream { + const char *data; + size_t remaining; + +public: + StringInputStream(const char *_uri, + Mutex &_mutex, + const char *_data) + :InputStream(_uri, _mutex), + data(_data), remaining(strlen(data)) { + SetReady(); + } + + /* virtual methods from InputStream */ + bool IsEOF() noexcept override { + return remaining == 0; + } + + size_t Read(void *ptr, size_t read_size) override { + size_t nbytes = std::min(remaining, read_size); + memcpy(ptr, data, nbytes); + data += nbytes; + remaining -= nbytes; + offset += nbytes; + return nbytes; + } +}; + +TEST(RewindInputStream, Basic) +{ + Mutex mutex; + + StringInputStream *sis = + new StringInputStream("foo://", mutex, + "foo bar"); + EXPECT_TRUE(sis->IsReady()); + + auto ris = input_rewind_open(InputStreamPtr(sis)); + EXPECT_TRUE(ris.get() != sis); + EXPECT_TRUE(ris != nullptr); + + const std::lock_guard protect(mutex); + + ris->Update(); + EXPECT_TRUE(ris->IsReady()); + EXPECT_FALSE(ris->KnownSize()); + EXPECT_EQ(offset_type(0), ris->GetOffset()); + + char buffer[16]; + size_t nbytes = ris->Read(buffer, 2); + EXPECT_EQ(size_t(2), nbytes); + EXPECT_EQ('f', buffer[0]); + EXPECT_EQ('o', buffer[1]); + EXPECT_EQ(offset_type(2), ris->GetOffset()); + EXPECT_FALSE(ris->IsEOF()); + + nbytes = ris->Read(buffer, 2); + EXPECT_EQ(size_t(2), nbytes); + EXPECT_EQ('o', buffer[0]); + EXPECT_EQ(' ', buffer[1]); + EXPECT_EQ(offset_type(4), ris->GetOffset()); + EXPECT_FALSE(ris->IsEOF()); + + ris->Seek(1); + EXPECT_EQ(offset_type(1), ris->GetOffset()); + EXPECT_FALSE(ris->IsEOF()); + + nbytes = ris->Read(buffer, 2); + EXPECT_EQ(size_t(2), nbytes); + EXPECT_EQ('o', buffer[0]); + EXPECT_EQ('o', buffer[1]); + EXPECT_EQ(offset_type(3), ris->GetOffset()); + EXPECT_FALSE(ris->IsEOF()); + + ris->Seek(0); + EXPECT_EQ(offset_type(0), ris->GetOffset()); + EXPECT_FALSE(ris->IsEOF()); + + nbytes = ris->Read(buffer, 2); + EXPECT_EQ(size_t(2), nbytes); + EXPECT_EQ('f', buffer[0]); + EXPECT_EQ('o', buffer[1]); + EXPECT_EQ(offset_type(2), ris->GetOffset()); + EXPECT_FALSE(ris->IsEOF()); + + nbytes = ris->Read(buffer, sizeof(buffer)); + EXPECT_EQ(size_t(2), nbytes); + EXPECT_EQ('o', buffer[0]); + EXPECT_EQ(' ', buffer[1]); + EXPECT_EQ(offset_type(4), ris->GetOffset()); + EXPECT_FALSE(ris->IsEOF()); + + nbytes = ris->Read(buffer, sizeof(buffer)); + EXPECT_EQ(size_t(3), nbytes); + EXPECT_EQ('b', buffer[0]); + EXPECT_EQ('a', buffer[1]); + EXPECT_EQ('r', buffer[2]); + EXPECT_EQ(offset_type(7), ris->GetOffset()); + EXPECT_TRUE(ris->IsEOF()); + + ris->Seek(3); + EXPECT_EQ(offset_type(3), ris->GetOffset()); + EXPECT_FALSE(ris->IsEOF()); + + nbytes = ris->Read(buffer, sizeof(buffer)); + EXPECT_EQ(size_t(4), nbytes); + EXPECT_EQ(' ', buffer[0]); + EXPECT_EQ('b', buffer[1]); + EXPECT_EQ('a', buffer[2]); + EXPECT_EQ('r', buffer[3]); + EXPECT_EQ(offset_type(7), ris->GetOffset()); + EXPECT_TRUE(ris->IsEOF()); +} diff --git a/test/TestSplitString.cxx b/test/TestSplitString.cxx new file mode 100644 index 000000000..4464fe29a --- /dev/null +++ b/test/TestSplitString.cxx @@ -0,0 +1,57 @@ +/* + * Unit tests for src/util/ + */ + +#include "config.h" +#include "util/SplitString.hxx" +#include "util/Macros.hxx" + +#include + + +TEST(SplitString, Basic) +{ + constexpr char input[] = "foo.bar"; + const char *const output[] = { "foo", "bar" }; + size_t i = 0; + for (auto p : SplitString(input, '.')) { + EXPECT_LT(i, ARRAY_SIZE(output)); + EXPECT_EQ(p, output[i]); + ++i; + } + + EXPECT_EQ(ARRAY_SIZE(output), i); +} + +TEST(SplitString, Strip) +{ + constexpr char input[] = " foo\t.\r\nbar\r\n2"; + const char *const output[] = { "foo", "bar\r\n2" }; + size_t i = 0; + for (auto p : SplitString(input, '.')) { + EXPECT_LT(i, ARRAY_SIZE(output)); + EXPECT_EQ(p, output[i]); + ++i; + } + + EXPECT_EQ(ARRAY_SIZE(output), i); +} + +TEST(SplitString, NoStrip) +{ + constexpr char input[] = " foo\t.\r\nbar\r\n2"; + const char *const output[] = { " foo\t", "\r\nbar\r\n2" }; + size_t i = 0; + for (auto p : SplitString(input, '.', false)) { + EXPECT_LT(i, ARRAY_SIZE(output)); + EXPECT_EQ(p, output[i]); + ++i; + } + + EXPECT_EQ(ARRAY_SIZE(output), i); +} + +TEST(SplitString, Empty) +{ + EXPECT_TRUE(SplitString("", '.').empty()); +} diff --git a/test/TestUriUtil.cxx b/test/TestUriUtil.cxx new file mode 100644 index 000000000..25bbad1ec --- /dev/null +++ b/test/TestUriUtil.cxx @@ -0,0 +1,48 @@ +/* + * Unit tests for src/util/ + */ + +#include "config.h" +#include "util/UriUtil.hxx" + +#include + +TEST(UriUtil, Suffix) +{ + EXPECT_EQ((const char *)nullptr, uri_get_suffix("/foo/bar")); + EXPECT_EQ((const char *)nullptr, uri_get_suffix("/foo.jpg/bar")); + EXPECT_STREQ(uri_get_suffix("/foo/bar.jpg"), "jpg"); + EXPECT_STREQ(uri_get_suffix("/foo.png/bar.jpg"), "jpg"); + EXPECT_EQ((const char *)nullptr, uri_get_suffix(".jpg")); + EXPECT_EQ((const char *)nullptr, uri_get_suffix("/foo/.jpg")); + + /* the first overload does not eliminate the query + string */ + EXPECT_STREQ(uri_get_suffix("/foo/bar.jpg?query_string"), + "jpg?query_string"); + + /* ... but the second one does */ + UriSuffixBuffer buffer; + EXPECT_STREQ(uri_get_suffix("/foo/bar.jpg?query_string", buffer), + "jpg"); + + /* repeat some of the above tests with the second overload */ + EXPECT_EQ((const char *)nullptr, uri_get_suffix("/foo/bar", buffer)); + EXPECT_EQ((const char *)nullptr, + uri_get_suffix("/foo.jpg/bar", buffer)); + EXPECT_STREQ(uri_get_suffix("/foo/bar.jpg", buffer), "jpg"); +} + +TEST(UriUtil, RemoveAuth) +{ + EXPECT_EQ(std::string(), + uri_remove_auth("http://www.example.com/")); + EXPECT_EQ(std::string("http://www.example.com/"), + uri_remove_auth("http://foo:bar@www.example.com/")); + EXPECT_EQ(std::string("http://www.example.com/"), + uri_remove_auth("http://foo@www.example.com/")); + EXPECT_EQ(std::string(), + uri_remove_auth("http://www.example.com/f:oo@bar")); + EXPECT_EQ(std::string("ftp://ftp.example.com/"), + uri_remove_auth("ftp://foo:bar@ftp.example.com/")); +} diff --git a/test/UriUtilTest.hxx b/test/UriUtilTest.hxx deleted file mode 100644 index 07f52a475..000000000 --- a/test/UriUtilTest.hxx +++ /dev/null @@ -1,66 +0,0 @@ -/* - * Unit tests for src/util/ - */ - -#include "check.h" -#include "util/UriUtil.hxx" - -#include -#include - -#include - -class UriUtilTest : public CppUnit::TestFixture { - CPPUNIT_TEST_SUITE(UriUtilTest); - CPPUNIT_TEST(TestSuffix); - CPPUNIT_TEST(TestRemoveAuth); - CPPUNIT_TEST_SUITE_END(); - -public: - void TestSuffix() { - CPPUNIT_ASSERT_EQUAL((const char *)nullptr, - uri_get_suffix("/foo/bar")); - CPPUNIT_ASSERT_EQUAL((const char *)nullptr, - uri_get_suffix("/foo.jpg/bar")); - CPPUNIT_ASSERT_EQUAL(0, strcmp(uri_get_suffix("/foo/bar.jpg"), - "jpg")); - CPPUNIT_ASSERT_EQUAL(0, strcmp(uri_get_suffix("/foo.png/bar.jpg"), - "jpg")); - CPPUNIT_ASSERT_EQUAL((const char *)nullptr, - uri_get_suffix(".jpg")); - CPPUNIT_ASSERT_EQUAL((const char *)nullptr, - uri_get_suffix("/foo/.jpg")); - - /* the first overload does not eliminate the query - string */ - CPPUNIT_ASSERT_EQUAL(0, strcmp(uri_get_suffix("/foo/bar.jpg?query_string"), - "jpg?query_string")); - - /* ... but the second one does */ - UriSuffixBuffer buffer; - CPPUNIT_ASSERT_EQUAL(0, strcmp(uri_get_suffix("/foo/bar.jpg?query_string", - buffer), - "jpg")); - - /* repeat some of the above tests with the second overload */ - CPPUNIT_ASSERT_EQUAL((const char *)nullptr, - uri_get_suffix("/foo/bar", buffer)); - CPPUNIT_ASSERT_EQUAL((const char *)nullptr, - uri_get_suffix("/foo.jpg/bar", buffer)); - CPPUNIT_ASSERT_EQUAL(0, strcmp(uri_get_suffix("/foo/bar.jpg", buffer), - "jpg")); - } - - void TestRemoveAuth() { - CPPUNIT_ASSERT_EQUAL(std::string(), - uri_remove_auth("http://www.example.com/")); - CPPUNIT_ASSERT_EQUAL(std::string("http://www.example.com/"), - uri_remove_auth("http://foo:bar@www.example.com/")); - CPPUNIT_ASSERT_EQUAL(std::string("http://www.example.com/"), - uri_remove_auth("http://foo@www.example.com/")); - CPPUNIT_ASSERT_EQUAL(std::string(), - uri_remove_auth("http://www.example.com/f:oo@bar")); - CPPUNIT_ASSERT_EQUAL(std::string("ftp://ftp.example.com/"), - uri_remove_auth("ftp://foo:bar@ftp.example.com/")); - } -}; diff --git a/test/meson.build b/test/meson.build index 2cc30b5d1..f88dd9f80 100644 --- a/test/meson.build +++ b/test/meson.build @@ -1,4 +1,22 @@ -cppunit_dep = dependency('cppunit', required: true) +gtest_compile_args = [ + '-Wno-undef', +] + +if compiler.get_id() == 'gcc' + gtest_compile_args += [ + '-Wno-suggest-attribute=format', + '-Wno-suggest-attribute=noreturn', + '-Wno-missing-declarations', + + # needed on Jessie for gtest's IsNullLiteralHelper + '-Wno-conversion-null', + ] +endif + +gtest_dep = declare_dependency( + dependencies: [dependency('gtest', main: true)], + compile_args: gtest_compile_args, +) executable( 'ParseSongFilter', @@ -21,33 +39,28 @@ executable( ], ) -test('test_util', executable( - 'test_util', - 'test_util.cxx', - include_directories: inc, - dependencies: [ - util_dep, - cppunit_dep, - ], -)) - -test('test_byte_reverse', executable( - 'test_byte_reverse', +test('TestUtil', executable( + 'TestUtil', + 'TestCircularBuffer.cxx', + 'TestDivideString.cxx', + 'TestMimeType.cxx', + 'TestSplitString.cxx', + 'TestUriUtil.cxx', 'test_byte_reverse.cxx', include_directories: inc, dependencies: [ util_dep, - cppunit_dep, + gtest_dep, ], )) -test('test_rewind', executable( - 'test_rewind', - 'test_rewind.cxx', +test('TestRewindInputStream', executable( + 'TestRewindInputStream', + 'TestRewindInputStream.cxx', include_directories: inc, dependencies: [ input_glue_dep, - cppunit_dep, + gtest_dep, ], )) @@ -59,7 +72,7 @@ test('test_mixramp', executable( include_directories: inc, dependencies: [ util_dep, - cppunit_dep, + gtest_dep, ], )) @@ -69,7 +82,7 @@ test('test_protocol', executable( '../src/protocol/ArgParser.cxx', include_directories: inc, dependencies: [ - cppunit_dep, + gtest_dep, ], )) @@ -80,7 +93,7 @@ test('test_queue_priority', executable( include_directories: inc, dependencies: [ util_dep, - cppunit_dep, + gtest_dep, ], )) @@ -90,7 +103,7 @@ test('TestFs', executable( include_directories: inc, dependencies: [ fs_dep, - cppunit_dep, + gtest_dep, ], )) @@ -101,7 +114,7 @@ test('TestIcu', executable( dependencies: [ util_dep, icu_dep, - cppunit_dep, + gtest_dep, ], )) @@ -265,7 +278,7 @@ if enable_database dependencies: [ tag_dep, storage_glue_dep, - cppunit_dep, + gtest_dep, ], )) endif @@ -296,7 +309,7 @@ if curl_dep.found() include_directories: inc, dependencies: [ tag_dep, - cppunit_dep, + gtest_dep, ], )) endif @@ -314,7 +327,7 @@ if archive_glue_dep.found() include_directories: inc, dependencies: [ archive_glue_dep, - cppunit_dep, + gtest_dep, ], )) @@ -457,11 +470,10 @@ test('test_pcm', executable( 'test_pcm_mix.cxx', 'test_pcm_interleave.cxx', 'test_pcm_export.cxx', - 'test_pcm_main.cxx', include_directories: inc, dependencies: [ pcm_dep, - cppunit_dep, + gtest_dep, ], )) diff --git a/test/test_archive.cxx b/test/test_archive.cxx index f023da886..f183e55c6 100644 --- a/test/test_archive.cxx +++ b/test/test_archive.cxx @@ -2,76 +2,46 @@ #include "archive/ArchiveLookup.hxx" #include "util/Compiler.h" -#include -#include -#include -#include +#include #include #include -class ArchiveLookupTest : public CppUnit::TestFixture { - CPPUNIT_TEST_SUITE(ArchiveLookupTest); - CPPUNIT_TEST(TestArchiveLookup); - CPPUNIT_TEST_SUITE_END(); - -public: - void TestArchiveLookup(); -}; - -void -ArchiveLookupTest::TestArchiveLookup() +TEST(ArchiveTest, Lookup) { const char *archive, *inpath, *suffix; char *path = strdup(""); - CPPUNIT_ASSERT_EQUAL(false, - archive_lookup(path, &archive, &inpath, &suffix)); + EXPECT_FALSE(archive_lookup(path, &archive, &inpath, &suffix)); free(path); path = strdup("."); - CPPUNIT_ASSERT_EQUAL(false, - archive_lookup(path, &archive, &inpath, &suffix)); + EXPECT_FALSE(archive_lookup(path, &archive, &inpath, &suffix)); free(path); path = strdup("config.h"); - CPPUNIT_ASSERT_EQUAL(false, - archive_lookup(path, &archive, &inpath, &suffix)); + EXPECT_FALSE(archive_lookup(path, &archive, &inpath, &suffix)); free(path); path = strdup("src/foo/bar"); - CPPUNIT_ASSERT_EQUAL(false, - archive_lookup(path, &archive, &inpath, &suffix)); + EXPECT_FALSE(archive_lookup(path, &archive, &inpath, &suffix)); free(path); fclose(fopen("dummy", "w")); path = strdup("dummy/foo/bar"); - CPPUNIT_ASSERT_EQUAL(true, - archive_lookup(path, &archive, &inpath, &suffix)); - CPPUNIT_ASSERT_EQUAL((const char *)path, archive); - CPPUNIT_ASSERT_EQUAL(0, strcmp(archive, "dummy")); - CPPUNIT_ASSERT_EQUAL(0, strcmp(inpath, "foo/bar")); - CPPUNIT_ASSERT_EQUAL((const char *)nullptr, suffix); + EXPECT_TRUE(archive_lookup(path, &archive, &inpath, &suffix)); + EXPECT_EQ((const char *)path, archive); + EXPECT_STREQ(archive, "dummy"); + EXPECT_STREQ(inpath, "foo/bar"); + EXPECT_EQ((const char *)nullptr, suffix); free(path); path = strdup("config.h/foo/bar"); - CPPUNIT_ASSERT_EQUAL(true, - archive_lookup(path, &archive, &inpath, &suffix)); - CPPUNIT_ASSERT_EQUAL((const char *)path, archive); - CPPUNIT_ASSERT_EQUAL(0, strcmp(archive, "config.h")); - CPPUNIT_ASSERT_EQUAL(0, strcmp(inpath, "foo/bar")); - CPPUNIT_ASSERT_EQUAL(0, strcmp(suffix, "h")); + EXPECT_TRUE(archive_lookup(path, &archive, &inpath, &suffix)); + EXPECT_EQ((const char *)path, archive); + EXPECT_STREQ(archive, "config.h"); + EXPECT_STREQ(inpath, "foo/bar"); + EXPECT_STREQ(suffix, "h"); free(path); } - -CPPUNIT_TEST_SUITE_REGISTRATION(ArchiveLookupTest); - -int -main(gcc_unused int argc, gcc_unused char **argv) -{ - CppUnit::TextUi::TestRunner runner; - auto ®istry = CppUnit::TestFactoryRegistry::getRegistry(); - runner.addTest(registry.makeTest()); - return runner.run() ? EXIT_SUCCESS : EXIT_FAILURE; -} diff --git a/test/test_byte_reverse.cxx b/test/test_byte_reverse.cxx index 69bae7c77..4a22c9de6 100644 --- a/test/test_byte_reverse.cxx +++ b/test/test_byte_reverse.cxx @@ -21,33 +21,12 @@ #include "util/Macros.hxx" #include "util/Compiler.h" -#include -#include -#include -#include +#include #include #include -class ByteReverseTest : public CppUnit::TestFixture { - CPPUNIT_TEST_SUITE(ByteReverseTest); - CPPUNIT_TEST(TestByteReverse2); - CPPUNIT_TEST(TestByteReverse3); - CPPUNIT_TEST(TestByteReverse4); - CPPUNIT_TEST(TestByteReverse5); - CPPUNIT_TEST_SUITE_END(); - -public: - void TestByteReverse2(); - void TestByteReverse3(); - void TestByteReverse4(); - void TestByteReverse5(); -}; - -CPPUNIT_TEST_SUITE_REGISTRATION(ByteReverseTest); - -void -ByteReverseTest::TestByteReverse2() +TEST(ByteReverse, A) { alignas(uint16_t) static const char src[] = "123456"; static const char result[] = "214365"; @@ -55,11 +34,10 @@ ByteReverseTest::TestByteReverse2() reverse_bytes(dest, (const uint8_t *)src, (const uint8_t *)(src + ARRAY_SIZE(src) - 1), 2); - CPPUNIT_ASSERT(strcmp(result, (const char *)dest) == 0); + EXPECT_STREQ(result, (const char *)dest); } -void -ByteReverseTest::TestByteReverse3() +TEST(ByteReverse, B) { static const char src[] = "123456"; static const char result[] = "321654"; @@ -67,11 +45,10 @@ ByteReverseTest::TestByteReverse3() reverse_bytes(dest, (const uint8_t *)src, (const uint8_t *)(src + ARRAY_SIZE(src) - 1), 3); - CPPUNIT_ASSERT(strcmp(result, (const char *)dest) == 0); + EXPECT_STREQ(result, (const char *)dest); } -void -ByteReverseTest::TestByteReverse4() +TEST(ByteReverse, C) { alignas(uint32_t) static const char src[] = "12345678"; static const char result[] = "43218765"; @@ -79,11 +56,10 @@ ByteReverseTest::TestByteReverse4() reverse_bytes(dest, (const uint8_t *)src, (const uint8_t *)(src + ARRAY_SIZE(src) - 1), 4); - CPPUNIT_ASSERT(strcmp(result, (const char *)dest) == 0); + EXPECT_STREQ(result, (const char *)dest); } -void -ByteReverseTest::TestByteReverse5() +TEST(ByteReverse, D) { static const char src[] = "1234567890"; static const char result[] = "5432109876"; @@ -91,14 +67,5 @@ ByteReverseTest::TestByteReverse5() reverse_bytes(dest, (const uint8_t *)src, (const uint8_t *)(src + ARRAY_SIZE(src) - 1), 5); - CPPUNIT_ASSERT(strcmp(result, (const char *)dest) == 0); -} - -int -main(gcc_unused int argc, gcc_unused char **argv) -{ - CppUnit::TextUi::TestRunner runner; - auto ®istry = CppUnit::TestFactoryRegistry::getRegistry(); - runner.addTest(registry.makeTest()); - return runner.run() ? EXIT_SUCCESS : EXIT_FAILURE; + EXPECT_STREQ(result, (const char *)dest); } diff --git a/test/test_icy_parser.cxx b/test/test_icy_parser.cxx index c213f9656..14467ddb6 100644 --- a/test/test_icy_parser.cxx +++ b/test/test_icy_parser.cxx @@ -8,10 +8,7 @@ /* include the .cxx file to get access to internal functions */ #include "IcyMetaDataParser.cxx" -#include -#include -#include -#include +#include #include @@ -28,11 +25,11 @@ icy_parse_tag(const char *p) static void CompareTagTitle(const Tag &tag, const std::string &title) { - CPPUNIT_ASSERT_EQUAL(uint16_t(1), tag.num_items); + EXPECT_EQ(uint16_t(1), tag.num_items); const TagItem &item = *tag.items[0]; - CPPUNIT_ASSERT_EQUAL(TAG_TITLE, item.type); - CPPUNIT_ASSERT_EQUAL(title, std::string(item.value)); + EXPECT_EQ(TAG_TITLE, item.type); + EXPECT_EQ(title, std::string(item.value)); } static void @@ -46,38 +43,21 @@ static void TestIcyParserEmpty(const char *input) { const auto tag = icy_parse_tag(input); - CPPUNIT_ASSERT_EQUAL(uint16_t(0), tag->num_items); + EXPECT_EQ(uint16_t(0), tag->num_items); } -class IcyTest : public CppUnit::TestFixture { - CPPUNIT_TEST_SUITE(IcyTest); - CPPUNIT_TEST(TestIcyMetadataParser); - CPPUNIT_TEST_SUITE_END(); - -public: - void TestIcyMetadataParser() { - TestIcyParserEmpty("foo=bar;"); - TestIcyParserTitle("StreamTitle='foo bar'", "foo bar"); - TestIcyParserTitle("StreamTitle='foo bar';", "foo bar"); - TestIcyParserTitle("StreamTitle='foo\"bar';", "foo\"bar"); - TestIcyParserTitle("StreamTitle='foo=bar';", "foo=bar"); - TestIcyParserTitle("a=b;StreamTitle='foo';", "foo"); - TestIcyParserTitle("a=;StreamTitle='foo';", "foo"); - TestIcyParserTitle("a=b;StreamTitle='foo';c=d", "foo"); - TestIcyParserTitle("a=b;StreamTitle='foo'", "foo"); - TestIcyParserTitle("a='b;c';StreamTitle='foo;bar'", "foo;bar"); - TestIcyParserTitle("a='b'c';StreamTitle='foo'bar'", "foo'bar"); - TestIcyParserTitle("StreamTitle='fo'o'b'ar';a='b'c'd'", "fo'o'b'ar"); - } -}; - -CPPUNIT_TEST_SUITE_REGISTRATION(IcyTest); - -int -main(gcc_unused int argc, gcc_unused char **argv) +TEST(IcyMetadataParserTest, Basic) { - CppUnit::TextUi::TestRunner runner; - auto ®istry = CppUnit::TestFactoryRegistry::getRegistry(); - runner.addTest(registry.makeTest()); - return runner.run() ? EXIT_SUCCESS : EXIT_FAILURE; + TestIcyParserEmpty("foo=bar;"); + TestIcyParserTitle("StreamTitle='foo bar'", "foo bar"); + TestIcyParserTitle("StreamTitle='foo bar';", "foo bar"); + TestIcyParserTitle("StreamTitle='foo\"bar';", "foo\"bar"); + TestIcyParserTitle("StreamTitle='foo=bar';", "foo=bar"); + TestIcyParserTitle("a=b;StreamTitle='foo';", "foo"); + TestIcyParserTitle("a=;StreamTitle='foo';", "foo"); + TestIcyParserTitle("a=b;StreamTitle='foo';c=d", "foo"); + TestIcyParserTitle("a=b;StreamTitle='foo'", "foo"); + TestIcyParserTitle("a='b;c';StreamTitle='foo;bar'", "foo;bar"); + TestIcyParserTitle("a='b'c';StreamTitle='foo'bar'", "foo'bar"); + TestIcyParserTitle("StreamTitle='fo'o'b'ar';a='b'c'd'", "fo'o'b'ar"); } diff --git a/test/test_mixramp.cxx b/test/test_mixramp.cxx index 898452c89..61bb26046 100644 --- a/test/test_mixramp.cxx +++ b/test/test_mixramp.cxx @@ -5,83 +5,63 @@ #include "config.h" #include "player/CrossFade.cxx" -#include -#include -#include -#include +#include #include -class MixRampTest : public CppUnit::TestFixture { - CPPUNIT_TEST_SUITE(MixRampTest); - CPPUNIT_TEST(TestInterpolate); - CPPUNIT_TEST_SUITE_END(); - -public: - void TestInterpolate() { - const char *input = "1.0 0.00;3.0 0.10;6.0 2.50;"; - - char *foo = strdup(input); - CPPUNIT_ASSERT_DOUBLES_EQUAL(double(0), - mixramp_interpolate(foo, 0).count(), - 0.05); - free(foo); - - foo = strdup(input); - CPPUNIT_ASSERT_DOUBLES_EQUAL(float(0), - mixramp_interpolate(foo, 1).count(), - 0.005); - free(foo); - - foo = strdup(input); - CPPUNIT_ASSERT_DOUBLES_EQUAL(float(0.1), - mixramp_interpolate(foo, 3).count(), - 0.005); - free(foo); - - foo = strdup(input); - CPPUNIT_ASSERT_DOUBLES_EQUAL(float(2.5), - mixramp_interpolate(foo, 6).count(), - 0.01); - free(foo); - - foo = strdup(input); - CPPUNIT_ASSERT(mixramp_interpolate(foo, 6.1) < FloatDuration::zero()); - free(foo); - - foo = strdup(input); - CPPUNIT_ASSERT_DOUBLES_EQUAL(float(0.05), - mixramp_interpolate(foo, 2).count(), - 0.05); - free(foo); - - foo = strdup(input); - CPPUNIT_ASSERT_DOUBLES_EQUAL(float(1.3), - mixramp_interpolate(foo, 4.5).count(), - 0.05); - free(foo); - - foo = strdup(input); - CPPUNIT_ASSERT_DOUBLES_EQUAL(float(0.9), - mixramp_interpolate(foo, 4).count(), - 0.05); - free(foo); - - foo = strdup(input); - CPPUNIT_ASSERT_DOUBLES_EQUAL(float(1.7), - mixramp_interpolate(foo, 5).count(), - 0.05); - free(foo); - } -}; - -CPPUNIT_TEST_SUITE_REGISTRATION(MixRampTest); - -int -main(gcc_unused int argc, gcc_unused char **argv) +TEST(MixRamp, Interpolate) { - CppUnit::TextUi::TestRunner runner; - auto ®istry = CppUnit::TestFactoryRegistry::getRegistry(); - runner.addTest(registry.makeTest()); - return runner.run() ? EXIT_SUCCESS : EXIT_FAILURE; + const char *input = "1.0 0.00;3.0 0.10;6.0 2.50;"; + + char *foo = strdup(input); + EXPECT_NEAR(double(0), + mixramp_interpolate(foo, 0).count(), + 0.05); + free(foo); + + foo = strdup(input); + EXPECT_NEAR(float(0), + mixramp_interpolate(foo, 1).count(), + 0.005); + free(foo); + + foo = strdup(input); + EXPECT_NEAR(float(0.1), + mixramp_interpolate(foo, 3).count(), + 0.005); + free(foo); + + foo = strdup(input); + EXPECT_NEAR(float(2.5), + mixramp_interpolate(foo, 6).count(), + 0.01); + free(foo); + + foo = strdup(input); + EXPECT_LT(mixramp_interpolate(foo, 6.1), FloatDuration::zero()); + free(foo); + + foo = strdup(input); + EXPECT_NEAR(float(0.05), + mixramp_interpolate(foo, 2).count(), + 0.05); + free(foo); + + foo = strdup(input); + EXPECT_NEAR(float(1.3), + mixramp_interpolate(foo, 4.5).count(), + 0.05); + free(foo); + + foo = strdup(input); + EXPECT_NEAR(float(0.9), + mixramp_interpolate(foo, 4).count(), + 0.05); + free(foo); + + foo = strdup(input); + EXPECT_NEAR(float(1.7), + mixramp_interpolate(foo, 5).count(), + 0.05); + free(foo); } diff --git a/test/test_pcm_all.hxx b/test/test_pcm_all.hxx deleted file mode 100644 index 594012468..000000000 --- a/test/test_pcm_all.hxx +++ /dev/null @@ -1,152 +0,0 @@ -/* - * Copyright 2003-2017 The Music Player Daemon Project - * http://www.musicpd.org - * - * 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. - */ - -#ifndef MPD_TEST_PCM_ALL_HXX -#define MPD_TEST_PCM_ALL_HXX - -#include "check.h" - -#include -#include - -class PcmDitherTest : public CppUnit::TestFixture { - CPPUNIT_TEST_SUITE(PcmDitherTest); - CPPUNIT_TEST(TestDither24); - CPPUNIT_TEST(TestDither32); - CPPUNIT_TEST_SUITE_END(); - -public: - void TestDither24(); - void TestDither32(); -}; - -class PcmPackTest : public CppUnit::TestFixture { - CPPUNIT_TEST_SUITE(PcmPackTest); - CPPUNIT_TEST(TestPack24); - CPPUNIT_TEST(TestUnpack24); - CPPUNIT_TEST(TestUnpack24BE); - CPPUNIT_TEST_SUITE_END(); - -public: - void TestPack24(); - void TestUnpack24(); - void TestUnpack24BE(); -}; - -class PcmChannelsTest : public CppUnit::TestFixture { - CPPUNIT_TEST_SUITE(PcmChannelsTest); - CPPUNIT_TEST(TestChannels16); - CPPUNIT_TEST(TestChannels32); - CPPUNIT_TEST_SUITE_END(); - -public: - void TestChannels16(); - void TestChannels32(); -}; - -class PcmVolumeTest : public CppUnit::TestFixture { - CPPUNIT_TEST_SUITE(PcmVolumeTest); - CPPUNIT_TEST(TestVolume8); - CPPUNIT_TEST(TestVolume16); - CPPUNIT_TEST(TestVolume24); - CPPUNIT_TEST(TestVolume32); - CPPUNIT_TEST(TestVolumeFloat); - CPPUNIT_TEST_SUITE_END(); - -public: - void TestVolume8(); - void TestVolume16(); - void TestVolume24(); - void TestVolume32(); - void TestVolumeFloat(); -}; - -class PcmFormatTest : public CppUnit::TestFixture { - CPPUNIT_TEST_SUITE(PcmFormatTest); - CPPUNIT_TEST(TestFormat8to16); - CPPUNIT_TEST(TestFormat16to24); - CPPUNIT_TEST(TestFormat16to32); - CPPUNIT_TEST(TestFormatFloat); - CPPUNIT_TEST_SUITE_END(); - -public: - void TestFormat8to16(); - void TestFormat16to24(); - void TestFormat16to32(); - void TestFormatFloat(); -}; - -class PcmMixTest : public CppUnit::TestFixture { - CPPUNIT_TEST_SUITE(PcmMixTest); - CPPUNIT_TEST(TestMix8); - CPPUNIT_TEST(TestMix16); - CPPUNIT_TEST(TestMix24); - CPPUNIT_TEST(TestMix32); - CPPUNIT_TEST_SUITE_END(); - -public: - void TestMix8(); - void TestMix16(); - void TestMix24(); - void TestMix32(); -}; - -class PcmInterleaveTest : public CppUnit::TestFixture { - CPPUNIT_TEST_SUITE(PcmInterleaveTest); - CPPUNIT_TEST(TestInterleave8); - CPPUNIT_TEST(TestInterleave16); - CPPUNIT_TEST(TestInterleave24); - CPPUNIT_TEST(TestInterleave32); - CPPUNIT_TEST(TestInterleave64); - CPPUNIT_TEST_SUITE_END(); - -public: - void TestInterleave8(); - void TestInterleave16(); - void TestInterleave24(); - void TestInterleave32(); - void TestInterleave64(); -}; - -class PcmExportTest : public CppUnit::TestFixture { - CPPUNIT_TEST_SUITE(PcmExportTest); - CPPUNIT_TEST(TestShift8); - CPPUNIT_TEST(TestPack24); - CPPUNIT_TEST(TestReverseEndian); -#ifdef ENABLE_DSD - CPPUNIT_TEST(TestDsdU16); - CPPUNIT_TEST(TestDsdU32); - CPPUNIT_TEST(TestDop); -#endif - CPPUNIT_TEST(TestAlsaChannelOrder); - CPPUNIT_TEST_SUITE_END(); - -public: - void TestShift8(); - void TestPack24(); - void TestReverseEndian(); -#ifdef ENABLE_DSD - void TestDsdU16(); - void TestDsdU32(); - void TestDop(); -#endif - void TestAlsaChannelOrder(); -}; - -#endif diff --git a/test/test_pcm_channels.cxx b/test/test_pcm_channels.cxx index 39f03ab80..e303254f6 100644 --- a/test/test_pcm_channels.cxx +++ b/test/test_pcm_channels.cxx @@ -18,14 +18,14 @@ */ #include "config.h" -#include "test_pcm_all.hxx" #include "test_pcm_util.hxx" #include "pcm/PcmChannels.hxx" #include "pcm/PcmBuffer.hxx" #include "util/ConstBuffer.hxx" -void -PcmChannelsTest::TestChannels16() +#include + +TEST(PcmTest, Channels16) { constexpr size_t N = 509; const auto src = TestDataBuffer(); @@ -35,40 +35,39 @@ PcmChannelsTest::TestChannels16() /* stereo to mono */ auto dest = pcm_convert_channels_16(buffer, 1, 2, { src, N * 2 }); - CPPUNIT_ASSERT(!dest.IsNull()); - CPPUNIT_ASSERT_EQUAL(N, dest.size); + EXPECT_FALSE(dest.IsNull()); + EXPECT_EQ(N, dest.size); for (unsigned i = 0; i < N; ++i) - CPPUNIT_ASSERT_EQUAL(int16_t((src[i * 2] + src[i * 2 + 1]) / 2), - dest[i]); + EXPECT_EQ(int16_t((src[i * 2] + src[i * 2 + 1]) / 2), + dest[i]); /* mono to stereo */ dest = pcm_convert_channels_16(buffer, 2, 1, { src, N * 2 }); - CPPUNIT_ASSERT(!dest.IsNull()); - CPPUNIT_ASSERT_EQUAL(N * 4, dest.size); + EXPECT_FALSE(dest.IsNull()); + EXPECT_EQ(N * 4, dest.size); for (unsigned i = 0; i < N; ++i) { - CPPUNIT_ASSERT_EQUAL(src[i], dest[i * 2]); - CPPUNIT_ASSERT_EQUAL(src[i], dest[i * 2 + 1]); + EXPECT_EQ(src[i], dest[i * 2]); + EXPECT_EQ(src[i], dest[i * 2 + 1]); } /* stereo to 5.1 */ dest = pcm_convert_channels_16(buffer, 6, 2, { src, N * 2 }); - CPPUNIT_ASSERT(!dest.IsNull()); - CPPUNIT_ASSERT_EQUAL(N * 6, dest.size); + EXPECT_FALSE(dest.IsNull()); + EXPECT_EQ(N * 6, dest.size); constexpr int16_t silence = 0; for (unsigned i = 0; i < N; ++i) { - CPPUNIT_ASSERT_EQUAL(src[i * 2], dest[i * 6]); - CPPUNIT_ASSERT_EQUAL(src[i * 2 + 1], dest[i * 6+ 1]); - CPPUNIT_ASSERT_EQUAL(silence, dest[i * 6 + 2]); - CPPUNIT_ASSERT_EQUAL(silence, dest[i * 6 + 3]); - CPPUNIT_ASSERT_EQUAL(silence, dest[i * 6 + 4]); - CPPUNIT_ASSERT_EQUAL(silence, dest[i * 6 + 5]); + EXPECT_EQ(src[i * 2], dest[i * 6]); + EXPECT_EQ(src[i * 2 + 1], dest[i * 6+ 1]); + EXPECT_EQ(silence, dest[i * 6 + 2]); + EXPECT_EQ(silence, dest[i * 6 + 3]); + EXPECT_EQ(silence, dest[i * 6 + 4]); + EXPECT_EQ(silence, dest[i * 6 + 5]); } } -void -PcmChannelsTest::TestChannels32() +TEST(PcmTest, Channels32) { constexpr size_t N = 509; const auto src = TestDataBuffer(); @@ -78,34 +77,34 @@ PcmChannelsTest::TestChannels32() /* stereo to mono */ auto dest = pcm_convert_channels_32(buffer, 1, 2, { src, N * 2 }); - CPPUNIT_ASSERT(!dest.IsNull()); - CPPUNIT_ASSERT_EQUAL(N, dest.size); + EXPECT_FALSE(dest.IsNull()); + EXPECT_EQ(N, dest.size); for (unsigned i = 0; i < N; ++i) - CPPUNIT_ASSERT_EQUAL(int32_t(((int64_t)src[i * 2] + (int64_t)src[i * 2 + 1]) / 2), - dest[i]); + EXPECT_EQ(int32_t(((int64_t)src[i * 2] + (int64_t)src[i * 2 + 1]) / 2), + dest[i]); /* mono to stereo */ dest = pcm_convert_channels_32(buffer, 2, 1, { src, N * 2 }); - CPPUNIT_ASSERT(!dest.IsNull()); - CPPUNIT_ASSERT_EQUAL(N * 4, dest.size); + EXPECT_FALSE(dest.IsNull()); + EXPECT_EQ(N * 4, dest.size); for (unsigned i = 0; i < N; ++i) { - CPPUNIT_ASSERT_EQUAL(src[i], dest[i * 2]); - CPPUNIT_ASSERT_EQUAL(src[i], dest[i * 2 + 1]); + EXPECT_EQ(src[i], dest[i * 2]); + EXPECT_EQ(src[i], dest[i * 2 + 1]); } /* stereo to 5.1 */ dest = pcm_convert_channels_32(buffer, 6, 2, { src, N * 2 }); - CPPUNIT_ASSERT(!dest.IsNull()); - CPPUNIT_ASSERT_EQUAL(N * 6, dest.size); + EXPECT_FALSE(dest.IsNull()); + EXPECT_EQ(N * 6, dest.size); constexpr int32_t silence = 0; for (unsigned i = 0; i < N; ++i) { - CPPUNIT_ASSERT_EQUAL(src[i * 2], dest[i * 6]); - CPPUNIT_ASSERT_EQUAL(src[i * 2 + 1], dest[i * 6+ 1]); - CPPUNIT_ASSERT_EQUAL(silence, dest[i * 6 + 2]); - CPPUNIT_ASSERT_EQUAL(silence, dest[i * 6 + 3]); - CPPUNIT_ASSERT_EQUAL(silence, dest[i * 6 + 4]); - CPPUNIT_ASSERT_EQUAL(silence, dest[i * 6 + 5]); + EXPECT_EQ(src[i * 2], dest[i * 6]); + EXPECT_EQ(src[i * 2 + 1], dest[i * 6+ 1]); + EXPECT_EQ(silence, dest[i * 6 + 2]); + EXPECT_EQ(silence, dest[i * 6 + 3]); + EXPECT_EQ(silence, dest[i * 6 + 4]); + EXPECT_EQ(silence, dest[i * 6 + 5]); } } diff --git a/test/test_pcm_dither.cxx b/test/test_pcm_dither.cxx index 016cdbfac..a87f7ad15 100644 --- a/test/test_pcm_dither.cxx +++ b/test/test_pcm_dither.cxx @@ -18,12 +18,12 @@ */ #include "config.h" -#include "test_pcm_all.hxx" #include "test_pcm_util.hxx" #include "pcm/PcmDither.cxx" -void -PcmDitherTest::TestDither24() +#include + +TEST(PcmTest, Dither24) { constexpr unsigned N = 509; const auto src = TestDataBuffer(RandomInt24()); @@ -33,13 +33,12 @@ PcmDitherTest::TestDither24() dither.Dither24To16(dest, src.begin(), src.end()); for (unsigned i = 0; i < N; ++i) { - CPPUNIT_ASSERT(dest[i] >= (src[i] >> 8) - 8); - CPPUNIT_ASSERT(dest[i] < (src[i] >> 8) + 8); + EXPECT_GE(dest[i], (src[i] >> 8) - 8); + EXPECT_LT(dest[i], (src[i] >> 8) + 8); } } -void -PcmDitherTest::TestDither32() +TEST(PcmTest, Dither32) { constexpr unsigned N = 509; const auto src = TestDataBuffer(); @@ -49,7 +48,7 @@ PcmDitherTest::TestDither32() dither.Dither32To16(dest, src.begin(), src.end()); for (unsigned i = 0; i < N; ++i) { - CPPUNIT_ASSERT(dest[i] >= (src[i] >> 16) - 8); - CPPUNIT_ASSERT(dest[i] < (src[i] >> 16) + 8); + EXPECT_GE(dest[i], (src[i] >> 16) - 8); + EXPECT_LT(dest[i], (src[i] >> 16) + 8); } } diff --git a/test/test_pcm_export.cxx b/test/test_pcm_export.cxx index ebbeeaf57..c2725440c 100644 --- a/test/test_pcm_export.cxx +++ b/test/test_pcm_export.cxx @@ -18,16 +18,16 @@ */ #include "config.h" -#include "test_pcm_all.hxx" #include "pcm/PcmExport.hxx" #include "pcm/Traits.hxx" #include "system/ByteOrder.hxx" #include "util/ConstBuffer.hxx" +#include + #include -void -PcmExportTest::TestShift8() +TEST(PcmTest, ExportShift8) { static constexpr int32_t src[] = { 0x0, 0x1, 0x100, 0x10000, 0xffffff }; static constexpr uint32_t expected[] = { 0x0, 0x100, 0x10000, 0x1000000, 0xffffff00 }; @@ -35,19 +35,18 @@ PcmExportTest::TestShift8() PcmExport::Params params; params.shift8 = true; - CPPUNIT_ASSERT_EQUAL(params.CalcOutputSampleRate(42u), 42u); - CPPUNIT_ASSERT_EQUAL(params.CalcInputSampleRate(42u), 42u); + EXPECT_EQ(params.CalcOutputSampleRate(42u), 42u); + EXPECT_EQ(params.CalcInputSampleRate(42u), 42u); PcmExport e; e.Open(SampleFormat::S24_P32, 2, params); auto dest = e.Export({src, sizeof(src)}); - CPPUNIT_ASSERT_EQUAL(sizeof(expected), dest.size); - CPPUNIT_ASSERT(memcmp(dest.data, expected, dest.size) == 0); + EXPECT_EQ(sizeof(expected), dest.size); + EXPECT_TRUE(memcmp(dest.data, expected, dest.size) == 0); } -void -PcmExportTest::TestPack24() +TEST(PcmTest, ExportPack24) { static constexpr int32_t src[] = { 0x0, 0x1, 0x100, 0x10000, 0xffffff }; @@ -74,19 +73,18 @@ PcmExportTest::TestPack24() PcmExport::Params params; params.pack24 = true; - CPPUNIT_ASSERT_EQUAL(params.CalcOutputSampleRate(42u), 42u); - CPPUNIT_ASSERT_EQUAL(params.CalcInputSampleRate(42u), 42u); + EXPECT_EQ(params.CalcOutputSampleRate(42u), 42u); + EXPECT_EQ(params.CalcInputSampleRate(42u), 42u); PcmExport e; e.Open(SampleFormat::S24_P32, 2, params); auto dest = e.Export({src, sizeof(src)}); - CPPUNIT_ASSERT_EQUAL(expected_size, dest.size); - CPPUNIT_ASSERT(memcmp(dest.data, expected, dest.size) == 0); + EXPECT_EQ(expected_size, dest.size); + EXPECT_TRUE(memcmp(dest.data, expected, dest.size) == 0); } -void -PcmExportTest::TestReverseEndian() +TEST(PcmTest, ExportReverseEndian) { static constexpr uint8_t src[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 @@ -103,31 +101,30 @@ PcmExportTest::TestReverseEndian() PcmExport::Params params; params.reverse_endian = true; - CPPUNIT_ASSERT_EQUAL(params.CalcOutputSampleRate(42u), 42u); - CPPUNIT_ASSERT_EQUAL(params.CalcInputSampleRate(42u), 42u); + EXPECT_EQ(params.CalcOutputSampleRate(42u), 42u); + EXPECT_EQ(params.CalcInputSampleRate(42u), 42u); PcmExport e; e.Open(SampleFormat::S8, 2, params); auto dest = e.Export({src, sizeof(src)}); - CPPUNIT_ASSERT_EQUAL(sizeof(src), dest.size); - CPPUNIT_ASSERT(memcmp(dest.data, src, dest.size) == 0); + EXPECT_EQ(sizeof(src), dest.size); + EXPECT_TRUE(memcmp(dest.data, src, dest.size) == 0); e.Open(SampleFormat::S16, 2, params); dest = e.Export({src, sizeof(src)}); - CPPUNIT_ASSERT_EQUAL(sizeof(expected2), dest.size); - CPPUNIT_ASSERT(memcmp(dest.data, expected2, dest.size) == 0); + EXPECT_EQ(sizeof(expected2), dest.size); + EXPECT_TRUE(memcmp(dest.data, expected2, dest.size) == 0); e.Open(SampleFormat::S32, 2, params); dest = e.Export({src, sizeof(src)}); - CPPUNIT_ASSERT_EQUAL(sizeof(expected4), dest.size); - CPPUNIT_ASSERT(memcmp(dest.data, expected4, dest.size) == 0); + EXPECT_EQ(sizeof(expected4), dest.size); + EXPECT_TRUE(memcmp(dest.data, expected4, dest.size) == 0); } #ifdef ENABLE_DSD -void -PcmExportTest::TestDsdU16() +TEST(PcmTest, ExportDsdU16) { static constexpr uint8_t src[] = { 0x01, 0x23, 0x45, 0x67, @@ -146,19 +143,18 @@ PcmExportTest::TestDsdU16() PcmExport::Params params; params.dsd_u16 = true; - CPPUNIT_ASSERT_EQUAL(params.CalcOutputSampleRate(705600u), 352800u); - CPPUNIT_ASSERT_EQUAL(params.CalcInputSampleRate(352800u), 705600u); + EXPECT_EQ(params.CalcOutputSampleRate(705600u), 352800u); + EXPECT_EQ(params.CalcInputSampleRate(352800u), 705600u); PcmExport e; e.Open(SampleFormat::DSD, 2, params); auto dest = e.Export({src, sizeof(src)}); - CPPUNIT_ASSERT_EQUAL(sizeof(expected), dest.size); - CPPUNIT_ASSERT(memcmp(dest.data, expected, dest.size) == 0); + EXPECT_EQ(sizeof(expected), dest.size); + EXPECT_TRUE(memcmp(dest.data, expected, dest.size) == 0); } -void -PcmExportTest::TestDsdU32() +TEST(PcmTest, ExportDsdU32) { static constexpr uint8_t src[] = { 0x01, 0x23, 0x45, 0x67, @@ -177,19 +173,18 @@ PcmExportTest::TestDsdU32() PcmExport::Params params; params.dsd_u32 = true; - CPPUNIT_ASSERT_EQUAL(params.CalcOutputSampleRate(705600u), 176400u); - CPPUNIT_ASSERT_EQUAL(params.CalcInputSampleRate(176400u), 705600u); + EXPECT_EQ(params.CalcOutputSampleRate(705600u), 176400u); + EXPECT_EQ(params.CalcInputSampleRate(176400u), 705600u); PcmExport e; e.Open(SampleFormat::DSD, 2, params); auto dest = e.Export({src, sizeof(src)}); - CPPUNIT_ASSERT_EQUAL(sizeof(expected), dest.size); - CPPUNIT_ASSERT(memcmp(dest.data, expected, dest.size) == 0); + EXPECT_EQ(sizeof(expected), dest.size); + EXPECT_TRUE(memcmp(dest.data, expected, dest.size) == 0); } -void -PcmExportTest::TestDop() +TEST(PcmTest, ExportDop) { static constexpr uint8_t src[] = { 0x01, 0x23, 0x45, 0x67, @@ -206,15 +201,15 @@ PcmExportTest::TestDop() PcmExport::Params params; params.dop = true; - CPPUNIT_ASSERT_EQUAL(params.CalcOutputSampleRate(705600u), 352800u); - CPPUNIT_ASSERT_EQUAL(params.CalcInputSampleRate(352800u), 705600u); + EXPECT_EQ(params.CalcOutputSampleRate(705600u), 352800u); + EXPECT_EQ(params.CalcInputSampleRate(352800u), 705600u); PcmExport e; e.Open(SampleFormat::DSD, 2, params); auto dest = e.Export({src, sizeof(src)}); - CPPUNIT_ASSERT_EQUAL(sizeof(expected), dest.size); - CPPUNIT_ASSERT(memcmp(dest.data, expected, dest.size) == 0); + EXPECT_EQ(sizeof(expected), dest.size); + EXPECT_TRUE(memcmp(dest.data, expected, dest.size) == 0); } #endif @@ -238,15 +233,15 @@ TestAlsaChannelOrder51() PcmExport::Params params; params.alsa_channel_order = true; - CPPUNIT_ASSERT_EQUAL(params.CalcOutputSampleRate(42u), 42u); - CPPUNIT_ASSERT_EQUAL(params.CalcInputSampleRate(42u), 42u); + EXPECT_EQ(params.CalcOutputSampleRate(42u), 42u); + EXPECT_EQ(params.CalcInputSampleRate(42u), 42u); PcmExport e; e.Open(F, 6, params); auto dest = e.Export({src, sizeof(src)}); - CPPUNIT_ASSERT_EQUAL(sizeof(expected), dest.size); - CPPUNIT_ASSERT(memcmp(dest.data, expected, dest.size) == 0); + EXPECT_EQ(sizeof(expected), dest.size); + EXPECT_TRUE(memcmp(dest.data, expected, dest.size) == 0); } template> @@ -268,19 +263,18 @@ TestAlsaChannelOrder71() PcmExport::Params params; params.alsa_channel_order = true; - CPPUNIT_ASSERT_EQUAL(params.CalcOutputSampleRate(42u), 42u); - CPPUNIT_ASSERT_EQUAL(params.CalcInputSampleRate(42u), 42u); + EXPECT_EQ(params.CalcOutputSampleRate(42u), 42u); + EXPECT_EQ(params.CalcInputSampleRate(42u), 42u); PcmExport e; e.Open(F, 8, params); auto dest = e.Export({src, sizeof(src)}); - CPPUNIT_ASSERT_EQUAL(sizeof(expected), dest.size); - CPPUNIT_ASSERT(memcmp(dest.data, expected, dest.size) == 0); + EXPECT_EQ(sizeof(expected), dest.size); + EXPECT_TRUE(memcmp(dest.data, expected, dest.size) == 0); } -void -PcmExportTest::TestAlsaChannelOrder() +TEST(PcmTest, ExportAlsaChannelOrder) { TestAlsaChannelOrder51(); TestAlsaChannelOrder71(); diff --git a/test/test_pcm_format.cxx b/test/test_pcm_format.cxx index 728e3c1f2..63f30bef9 100644 --- a/test/test_pcm_format.cxx +++ b/test/test_pcm_format.cxx @@ -18,7 +18,6 @@ */ #include "config.h" -#include "test_pcm_all.hxx" #include "test_pcm_util.hxx" #include "pcm/PcmFormat.hxx" #include "pcm/PcmDither.hxx" @@ -26,8 +25,9 @@ #include "pcm/PcmBuffer.hxx" #include "pcm/SampleFormat.hxx" -void -PcmFormatTest::TestFormat8to16() +#include + +TEST(PcmTest, Format8To16) { constexpr size_t N = 509; const auto src = TestDataBuffer(); @@ -36,14 +36,13 @@ PcmFormatTest::TestFormat8to16() PcmDither dither; auto d = pcm_convert_to_16(buffer, dither, SampleFormat::S8, src); - CPPUNIT_ASSERT_EQUAL(N, d.size); + EXPECT_EQ(N, d.size); for (size_t i = 0; i < N; ++i) - CPPUNIT_ASSERT_EQUAL(int(src[i]), d[i] >> 8); + EXPECT_EQ(int(src[i]), d[i] >> 8); } -void -PcmFormatTest::TestFormat16to24() +TEST(PcmTest, Format16To24) { constexpr size_t N = 509; const auto src = TestDataBuffer(); @@ -51,14 +50,13 @@ PcmFormatTest::TestFormat16to24() PcmBuffer buffer; auto d = pcm_convert_to_24(buffer, SampleFormat::S16, src); - CPPUNIT_ASSERT_EQUAL(N, d.size); + EXPECT_EQ(N, d.size); for (size_t i = 0; i < N; ++i) - CPPUNIT_ASSERT_EQUAL(int(src[i]), d[i] >> 8); + EXPECT_EQ(int(src[i]), d[i] >> 8); } -void -PcmFormatTest::TestFormat16to32() +TEST(PcmTest, Format16To32) { constexpr size_t N = 509; const auto src = TestDataBuffer(); @@ -66,14 +64,13 @@ PcmFormatTest::TestFormat16to32() PcmBuffer buffer; auto d = pcm_convert_to_32(buffer, SampleFormat::S16, src); - CPPUNIT_ASSERT_EQUAL(N, d.size); + EXPECT_EQ(N, d.size); for (size_t i = 0; i < N; ++i) - CPPUNIT_ASSERT_EQUAL(int(src[i]), d[i] >> 16); + EXPECT_EQ(int(src[i]), d[i] >> 16); } -void -PcmFormatTest::TestFormatFloat() +TEST(PcmTest, FormatFloat) { constexpr size_t N = 509; const auto src = TestDataBuffer(); @@ -81,11 +78,11 @@ PcmFormatTest::TestFormatFloat() PcmBuffer buffer1, buffer2; auto f = pcm_convert_to_float(buffer1, SampleFormat::S16, src); - CPPUNIT_ASSERT_EQUAL(N, f.size); + EXPECT_EQ(N, f.size); for (size_t i = 0; i != f.size; ++i) { - CPPUNIT_ASSERT(f[i] >= -1.); - CPPUNIT_ASSERT(f[i] <= 1.); + EXPECT_GE(f[i], -1.); + EXPECT_LE(f[i], 1.); } PcmDither dither; @@ -93,10 +90,10 @@ PcmFormatTest::TestFormatFloat() auto d = pcm_convert_to_16(buffer2, dither, SampleFormat::FLOAT, f.ToVoid()); - CPPUNIT_ASSERT_EQUAL(N, d.size); + EXPECT_EQ(N, d.size); for (size_t i = 0; i < N; ++i) - CPPUNIT_ASSERT_EQUAL(src[i], d[i]); + EXPECT_EQ(src[i], d[i]); /* check if clamping works */ float *writable = const_cast(f.data); @@ -108,13 +105,13 @@ PcmFormatTest::TestFormatFloat() d = pcm_convert_to_16(buffer2, dither, SampleFormat::FLOAT, f.ToVoid()); - CPPUNIT_ASSERT_EQUAL(N, d.size); + EXPECT_EQ(N, d.size); - CPPUNIT_ASSERT_EQUAL(32767, int(d[0])); - CPPUNIT_ASSERT_EQUAL(32767, int(d[1])); - CPPUNIT_ASSERT_EQUAL(-32768, int(d[2])); - CPPUNIT_ASSERT_EQUAL(-32768, int(d[3])); + EXPECT_EQ(32767, int(d[0])); + EXPECT_EQ(32767, int(d[1])); + EXPECT_EQ(-32768, int(d[2])); + EXPECT_EQ(-32768, int(d[3])); for (size_t i = 4; i < N; ++i) - CPPUNIT_ASSERT_EQUAL(src[i], d[i]); + EXPECT_EQ(src[i], d[i]); } diff --git a/test/test_pcm_interleave.cxx b/test/test_pcm_interleave.cxx index 19cd54324..2ed9181d5 100644 --- a/test/test_pcm_interleave.cxx +++ b/test/test_pcm_interleave.cxx @@ -18,10 +18,11 @@ */ #include "config.h" -#include "test_pcm_all.hxx" #include "pcm/Interleave.hxx" #include "util/Macros.hxx" +#include + #include template @@ -45,32 +46,29 @@ TestInterleaveN() PcmInterleave(dest, src, n_frames, sizeof(T)); - CPPUNIT_ASSERT_EQUAL(T(1), dest[0]); - CPPUNIT_ASSERT_EQUAL(T(2), dest[1]); - CPPUNIT_ASSERT_EQUAL(T(3), dest[2]); - CPPUNIT_ASSERT_EQUAL(T(4), dest[3]); - CPPUNIT_ASSERT_EQUAL(T(5), dest[4]); - CPPUNIT_ASSERT_EQUAL(T(6), dest[5]); - CPPUNIT_ASSERT_EQUAL(T(7), dest[6]); - CPPUNIT_ASSERT_EQUAL(T(8), dest[7]); - CPPUNIT_ASSERT_EQUAL(T(9), dest[8]); - CPPUNIT_ASSERT_EQUAL(poison, dest[9]); + EXPECT_EQ(T(1), dest[0]); + EXPECT_EQ(T(2), dest[1]); + EXPECT_EQ(T(3), dest[2]); + EXPECT_EQ(T(4), dest[3]); + EXPECT_EQ(T(5), dest[4]); + EXPECT_EQ(T(6), dest[5]); + EXPECT_EQ(T(7), dest[6]); + EXPECT_EQ(T(8), dest[7]); + EXPECT_EQ(T(9), dest[8]); + EXPECT_EQ(poison, dest[9]); } -void -PcmInterleaveTest::TestInterleave8() +TEST(PcmTest, Interleave8) { TestInterleaveN(); } -void -PcmInterleaveTest::TestInterleave16() +TEST(PcmTest, Interleave16) { TestInterleaveN(); } -void -PcmInterleaveTest::TestInterleave24() +TEST(PcmTest, Interleave24) { typedef uint8_t T; static constexpr T src1[] = { 1, 2, 3, 4, 5, 6 }; @@ -90,37 +88,35 @@ PcmInterleaveTest::TestInterleave24() PcmInterleave(dest, src, n_frames, 3); - CPPUNIT_ASSERT_EQUAL(T(1), dest[0]); - CPPUNIT_ASSERT_EQUAL(T(2), dest[1]); - CPPUNIT_ASSERT_EQUAL(T(3), dest[2]); - CPPUNIT_ASSERT_EQUAL(T(7), dest[3]); - CPPUNIT_ASSERT_EQUAL(T(8), dest[4]); - CPPUNIT_ASSERT_EQUAL(T(9), dest[5]); - CPPUNIT_ASSERT_EQUAL(T(13), dest[6]); - CPPUNIT_ASSERT_EQUAL(T(14), dest[7]); - CPPUNIT_ASSERT_EQUAL(T(15), dest[8]); - CPPUNIT_ASSERT_EQUAL(T(4), dest[9]); - CPPUNIT_ASSERT_EQUAL(T(5), dest[10]); - CPPUNIT_ASSERT_EQUAL(T(6), dest[11]); - CPPUNIT_ASSERT_EQUAL(T(10), dest[12]); - CPPUNIT_ASSERT_EQUAL(T(11), dest[13]); - CPPUNIT_ASSERT_EQUAL(T(12), dest[14]); - CPPUNIT_ASSERT_EQUAL(T(16), dest[15]); - CPPUNIT_ASSERT_EQUAL(T(17), dest[16]); - CPPUNIT_ASSERT_EQUAL(T(18), dest[17]); - CPPUNIT_ASSERT_EQUAL(poison, dest[18]); + EXPECT_EQ(T(1), dest[0]); + EXPECT_EQ(T(2), dest[1]); + EXPECT_EQ(T(3), dest[2]); + EXPECT_EQ(T(7), dest[3]); + EXPECT_EQ(T(8), dest[4]); + EXPECT_EQ(T(9), dest[5]); + EXPECT_EQ(T(13), dest[6]); + EXPECT_EQ(T(14), dest[7]); + EXPECT_EQ(T(15), dest[8]); + EXPECT_EQ(T(4), dest[9]); + EXPECT_EQ(T(5), dest[10]); + EXPECT_EQ(T(6), dest[11]); + EXPECT_EQ(T(10), dest[12]); + EXPECT_EQ(T(11), dest[13]); + EXPECT_EQ(T(12), dest[14]); + EXPECT_EQ(T(16), dest[15]); + EXPECT_EQ(T(17), dest[16]); + EXPECT_EQ(T(18), dest[17]); + EXPECT_EQ(poison, dest[18]); TestInterleaveN(); } -void -PcmInterleaveTest::TestInterleave32() +TEST(PcmTest, Interleave32) { TestInterleaveN(); } -void -PcmInterleaveTest::TestInterleave64() +TEST(PcmTest, Interleave64) { TestInterleaveN(); } diff --git a/test/test_pcm_main.cxx b/test/test_pcm_main.cxx deleted file mode 100644 index f65862fe2..000000000 --- a/test/test_pcm_main.cxx +++ /dev/null @@ -1,47 +0,0 @@ -/* - * Copyright 2003-2017 The Music Player Daemon Project - * http://www.musicpd.org - * - * 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 "config.h" -#include "test_pcm_all.hxx" -#include "TestAudioFormat.hxx" -#include "util/Compiler.h" - -#include -#include - -#include - -CPPUNIT_TEST_SUITE_REGISTRATION(PcmDitherTest); -CPPUNIT_TEST_SUITE_REGISTRATION(PcmPackTest); -CPPUNIT_TEST_SUITE_REGISTRATION(PcmChannelsTest); -CPPUNIT_TEST_SUITE_REGISTRATION(PcmVolumeTest); -CPPUNIT_TEST_SUITE_REGISTRATION(PcmFormatTest); -CPPUNIT_TEST_SUITE_REGISTRATION(PcmMixTest); -CPPUNIT_TEST_SUITE_REGISTRATION(PcmInterleaveTest); -CPPUNIT_TEST_SUITE_REGISTRATION(PcmExportTest); -CPPUNIT_TEST_SUITE_REGISTRATION(AudioFormatTest); - -int -main(gcc_unused int argc, gcc_unused char **argv) -{ - CppUnit::TextUi::TestRunner runner; - auto ®istry = CppUnit::TestFactoryRegistry::getRegistry(); - runner.addTest(registry.makeTest()); - return runner.run() ? EXIT_SUCCESS : EXIT_FAILURE; -} diff --git a/test/test_pcm_mix.cxx b/test/test_pcm_mix.cxx index 2f8f94e2e..d08a730b9 100644 --- a/test/test_pcm_mix.cxx +++ b/test/test_pcm_mix.cxx @@ -18,11 +18,12 @@ */ #include "config.h" -#include "test_pcm_all.hxx" #include "test_pcm_util.hxx" #include "pcm/PcmMix.hxx" #include "pcm/PcmDither.hxx" +#include + template> static void TestPcmMix(G g=G()) @@ -38,49 +39,46 @@ TestPcmMix(G g=G()) bool success = pcm_mix(dither, result.begin(), src2.begin(), sizeof(result), format, 1.0); - CPPUNIT_ASSERT(success); + ASSERT_TRUE(success); AssertEqualWithTolerance(result, src1, 3); /* portion1=0.0: result must be equal to src2 */ result = src1; success = pcm_mix(dither, result.begin(), src2.begin(), sizeof(result), format, 0.0); - CPPUNIT_ASSERT(success); + ASSERT_TRUE(success); AssertEqualWithTolerance(result, src2, 3); /* portion1=0.5 */ result = src1; success = pcm_mix(dither, result.begin(), src2.begin(), sizeof(result), format, 0.5); - CPPUNIT_ASSERT(success); + ASSERT_TRUE(success); auto expected = src1; for (unsigned i = 0; i < N; ++i) expected[i] = (int64_t(src1[i]) + int64_t(src2[i])) / 2; - AssertEqualWithTolerance(result, expected, 3); + for (unsigned i = 0; i < N; ++i) + EXPECT_NEAR(result[i], expected[i], 3); } -void -PcmMixTest::TestMix8() +TEST(PcmTest, Mix8) { TestPcmMix(); } -void -PcmMixTest::TestMix16() +TEST(PcmTest, Mix16) { TestPcmMix(); } -void -PcmMixTest::TestMix24() +TEST(PcmTest, Mix24) { TestPcmMix(RandomInt24()); } -void -PcmMixTest::TestMix32() +TEST(PcmTest, Mix32) { TestPcmMix(); } diff --git a/test/test_pcm_pack.cxx b/test/test_pcm_pack.cxx index df3a99459..3b7036267 100644 --- a/test/test_pcm_pack.cxx +++ b/test/test_pcm_pack.cxx @@ -18,13 +18,13 @@ */ #include "config.h" -#include "test_pcm_all.hxx" #include "test_pcm_util.hxx" #include "pcm/PcmPack.hxx" #include "system/ByteOrder.hxx" -void -PcmPackTest::TestPack24() +#include + +TEST(PcmTest, Pack24) { constexpr unsigned N = 509; const auto src = TestDataBuffer(RandomInt24()); @@ -43,12 +43,11 @@ PcmPackTest::TestPack24() if (d & 0x800000) d |= 0xff000000; - CPPUNIT_ASSERT_EQUAL(d, src[i]); + EXPECT_EQ(d, src[i]); } } -void -PcmPackTest::TestUnpack24() +TEST(PcmTest, Unpack24) { constexpr unsigned N = 509; const auto src = TestDataBuffer(); @@ -67,12 +66,11 @@ PcmPackTest::TestUnpack24() if (s & 0x800000) s |= 0xff000000; - CPPUNIT_ASSERT_EQUAL(s, dest[i]); + EXPECT_EQ(s, dest[i]); } } -void -PcmPackTest::TestUnpack24BE() +TEST(PcmTest, Unpack24BE) { constexpr unsigned N = 509; const auto src = TestDataBuffer(); @@ -87,6 +85,6 @@ PcmPackTest::TestUnpack24BE() if (s & 0x800000) s |= 0xff000000; - CPPUNIT_ASSERT_EQUAL(s, dest[i]); + EXPECT_EQ(s, dest[i]); } } diff --git a/test/test_pcm_util.hxx b/test/test_pcm_util.hxx index ec7b7e36a..ae8206230 100644 --- a/test/test_pcm_util.hxx +++ b/test/test_pcm_util.hxx @@ -19,6 +19,8 @@ #include "util/ConstBuffer.hxx" +#include + #include #include @@ -92,13 +94,13 @@ template bool AssertEqualWithTolerance(const T &a, const T &b, unsigned tolerance) { - CPPUNIT_ASSERT_EQUAL(a.size(), b.size()); + EXPECT_EQ(a.size(), b.size()); for (unsigned i = 0; i < a.size(); ++i) { int64_t x = a[i], y = b[i]; - CPPUNIT_ASSERT(x >= y - int64_t(tolerance)); - CPPUNIT_ASSERT(x <= y + int64_t(tolerance)); + EXPECT_GE(x, y - int64_t(tolerance)); + EXPECT_LE(x, y + int64_t(tolerance)); } return true; diff --git a/test/test_pcm_volume.cxx b/test/test_pcm_volume.cxx index 235e7a11a..ee498e60a 100644 --- a/test/test_pcm_volume.cxx +++ b/test/test_pcm_volume.cxx @@ -18,12 +18,13 @@ */ #include "config.h" -#include "test_pcm_all.hxx" #include "pcm/Volume.hxx" #include "pcm/Traits.hxx" #include "util/ConstBuffer.hxx" #include "test_pcm_util.hxx" +#include + #include #include @@ -45,54 +46,49 @@ TestVolume(G g=G()) pv.SetVolume(0); auto dest = pv.Apply(src); - CPPUNIT_ASSERT_EQUAL(src.size, dest.size); - CPPUNIT_ASSERT_EQUAL(0, memcmp(dest.data, zero, sizeof(zero))); + EXPECT_EQ(src.size, dest.size); + EXPECT_EQ(0, memcmp(dest.data, zero, sizeof(zero))); pv.SetVolume(PCM_VOLUME_1); dest = pv.Apply(src); - CPPUNIT_ASSERT_EQUAL(src.size, dest.size); - CPPUNIT_ASSERT_EQUAL(0, memcmp(dest.data, src.data, src.size)); + EXPECT_EQ(src.size, dest.size); + EXPECT_EQ(0, memcmp(dest.data, src.data, src.size)); pv.SetVolume(PCM_VOLUME_1 / 2); dest = pv.Apply(src); - CPPUNIT_ASSERT_EQUAL(src.size, dest.size); + EXPECT_EQ(src.size, dest.size); const auto _dest = ConstBuffer::FromVoid(dest); for (unsigned i = 0; i < N; ++i) { const auto expected = (_src[i] + 1) / 2; - CPPUNIT_ASSERT(_dest[i] >= expected - 4); - CPPUNIT_ASSERT(_dest[i] <= expected + 4); + EXPECT_GE(_dest[i], expected - 4); + EXPECT_LE(_dest[i], expected + 4); } pv.Close(); } -void -PcmVolumeTest::TestVolume8() +TEST(PcmTest, Volume8) { TestVolume(); } -void -PcmVolumeTest::TestVolume16() +TEST(PcmTest, Volume16) { TestVolume(); } -void -PcmVolumeTest::TestVolume24() +TEST(PcmTest, Volume24) { TestVolume(RandomInt24()); } -void -PcmVolumeTest::TestVolume32() +TEST(PcmTest, Volume32) { TestVolume(); } -void -PcmVolumeTest::TestVolumeFloat() +TEST(PcmTest, VolumeFloat) { PcmVolume pv; pv.Open(SampleFormat::FLOAT); @@ -104,21 +100,21 @@ PcmVolumeTest::TestVolumeFloat() pv.SetVolume(0); auto dest = pv.Apply(src); - CPPUNIT_ASSERT_EQUAL(src.size, dest.size); - CPPUNIT_ASSERT_EQUAL(0, memcmp(dest.data, zero, sizeof(zero))); + EXPECT_EQ(src.size, dest.size); + EXPECT_EQ(0, memcmp(dest.data, zero, sizeof(zero))); pv.SetVolume(PCM_VOLUME_1); dest = pv.Apply(src); - CPPUNIT_ASSERT_EQUAL(src.size, dest.size); - CPPUNIT_ASSERT_EQUAL(0, memcmp(dest.data, src.data, src.size)); + EXPECT_EQ(src.size, dest.size); + EXPECT_EQ(0, memcmp(dest.data, src.data, src.size)); pv.SetVolume(PCM_VOLUME_1 / 2); dest = pv.Apply(src); - CPPUNIT_ASSERT_EQUAL(src.size, dest.size); + EXPECT_EQ(src.size, dest.size); const auto _dest = ConstBuffer::FromVoid(dest); for (unsigned i = 0; i < N; ++i) - CPPUNIT_ASSERT_DOUBLES_EQUAL(_src[i] / 2, _dest[i], 1); + EXPECT_NEAR(_src[i] / 2, _dest[i], 1); pv.Close(); } diff --git a/test/test_protocol.cxx b/test/test_protocol.cxx index b587561a6..86c4a7206 100644 --- a/test/test_protocol.cxx +++ b/test/test_protocol.cxx @@ -4,52 +4,24 @@ #include "protocol/RangeArg.hxx" #include "util/Compiler.h" -#include -#include -#include -#include +#include #include -class ArgParserTest : public CppUnit::TestFixture { - CPPUNIT_TEST_SUITE(ArgParserTest); - CPPUNIT_TEST(TestRange); - CPPUNIT_TEST_SUITE_END(); - -public: - void TestRange(); -}; - -void -ArgParserTest::TestRange() +TEST(ArgParser, Range) { RangeArg range = ParseCommandArgRange("1"); - CPPUNIT_ASSERT_EQUAL(1u, range.start); - CPPUNIT_ASSERT_EQUAL(2u, range.end); + EXPECT_EQ(1u, range.start); + EXPECT_EQ(2u, range.end); range = ParseCommandArgRange("1:5"); - CPPUNIT_ASSERT_EQUAL(1u, range.start); - CPPUNIT_ASSERT_EQUAL(5u, range.end); + EXPECT_EQ(1u, range.start); + EXPECT_EQ(5u, range.end); range = ParseCommandArgRange("1:"); - CPPUNIT_ASSERT_EQUAL(1u, range.start); - CPPUNIT_ASSERT(range.end >= 999999u); + EXPECT_EQ(1u, range.start); + EXPECT_GE(range.end, 999999u); - try { - range = ParseCommandArgRange("-2"); - CPPUNIT_ASSERT(false); - } catch (const ProtocolError &) { - CPPUNIT_ASSERT(true); - } -} - -CPPUNIT_TEST_SUITE_REGISTRATION(ArgParserTest); - -int -main(gcc_unused int argc, gcc_unused char **argv) -{ - CppUnit::TextUi::TestRunner runner; - auto ®istry = CppUnit::TestFactoryRegistry::getRegistry(); - runner.addTest(registry.makeTest()); - return runner.run() ? EXIT_SUCCESS : EXIT_FAILURE; + EXPECT_THROW(range = ParseCommandArgRange("-2"), + ProtocolError); } diff --git a/test/test_queue_priority.cxx b/test/test_queue_priority.cxx index a3418093c..6a8fbf782 100644 --- a/test/test_queue_priority.cxx +++ b/test/test_queue_priority.cxx @@ -3,10 +3,7 @@ #include "song/DetachedSong.hxx" #include "util/Macros.hxx" -#include -#include -#include -#include +#include Tag::Tag(const Tag &) noexcept {} void Tag::Clear() noexcept {} @@ -27,17 +24,7 @@ check_descending_priority(const Queue *queue, } } -class QueuePriorityTest : public CppUnit::TestFixture { - CPPUNIT_TEST_SUITE(QueuePriorityTest); - CPPUNIT_TEST(TestPriority); - CPPUNIT_TEST_SUITE_END(); - -public: - void TestPriority(); -}; - -void -QueuePriorityTest::TestPriority() +TEST(QueuePriority, Priority) { DetachedSong songs[16] = { DetachedSong("0.ogg"), @@ -63,7 +50,7 @@ QueuePriorityTest::TestPriority() for (unsigned i = 0; i < ARRAY_SIZE(songs); ++i) queue.Append(DetachedSong(songs[i]), 0); - CPPUNIT_ASSERT_EQUAL(unsigned(ARRAY_SIZE(songs)), queue.GetLength()); + EXPECT_EQ(unsigned(ARRAY_SIZE(songs)), queue.GetLength()); /* priority=10 for 4 items */ @@ -90,7 +77,7 @@ QueuePriorityTest::TestPriority() queue.SetPriorityRange(15, 16, 50, -1); check_descending_priority(&queue, 0); - CPPUNIT_ASSERT_EQUAL(0u, queue.PositionToOrder(15)); + EXPECT_EQ(0u, queue.PositionToOrder(15)); for (unsigned i = 0; i < 4; ++i) { assert(queue.PositionToOrder(i) >= 4); @@ -110,8 +97,8 @@ QueuePriorityTest::TestPriority() queue.SetPriorityRange(3, 4, 20, -1); check_descending_priority(&queue, 0); - CPPUNIT_ASSERT_EQUAL(1u, queue.PositionToOrder(3)); - CPPUNIT_ASSERT_EQUAL(0u, queue.PositionToOrder(15)); + EXPECT_EQ(1u, queue.PositionToOrder(3)); + EXPECT_EQ(0u, queue.PositionToOrder(15)); for (unsigned i = 0; i < 3; ++i) { assert(queue.PositionToOrder(i) >= 5); @@ -136,14 +123,14 @@ QueuePriorityTest::TestPriority() unsigned a_order = 3; unsigned a_position = queue.OrderToPosition(a_order); - CPPUNIT_ASSERT_EQUAL(10u, unsigned(queue.items[a_position].priority)); + EXPECT_EQ(10u, unsigned(queue.items[a_position].priority)); queue.SetPriority(a_position, 20, current_order); current_order = queue.PositionToOrder(current_position); - CPPUNIT_ASSERT_EQUAL(3u, current_order); + EXPECT_EQ(3u, current_order); a_order = queue.PositionToOrder(a_position); - CPPUNIT_ASSERT_EQUAL(4u, a_order); + EXPECT_EQ(4u, a_order); check_descending_priority(&queue, current_order + 1); @@ -153,38 +140,27 @@ QueuePriorityTest::TestPriority() unsigned b_order = 10; unsigned b_position = queue.OrderToPosition(b_order); - CPPUNIT_ASSERT_EQUAL(0u, unsigned(queue.items[b_position].priority)); + EXPECT_EQ(0u, unsigned(queue.items[b_position].priority)); queue.SetPriority(b_position, 70, current_order); current_order = queue.PositionToOrder(current_position); - CPPUNIT_ASSERT_EQUAL(3u, current_order); + EXPECT_EQ(3u, current_order); b_order = queue.PositionToOrder(b_position); - CPPUNIT_ASSERT_EQUAL(4u, b_order); + EXPECT_EQ(4u, b_order); check_descending_priority(&queue, current_order + 1); /* move the prio=20 item back */ a_order = queue.PositionToOrder(a_position); - CPPUNIT_ASSERT_EQUAL(5u, a_order); - CPPUNIT_ASSERT_EQUAL(20u, unsigned(queue.items[a_position].priority)); + EXPECT_EQ(5u, a_order); + EXPECT_EQ(20u, unsigned(queue.items[a_position].priority)); queue.SetPriority(a_position, 5, current_order); current_order = queue.PositionToOrder(current_position); - CPPUNIT_ASSERT_EQUAL(3u, current_order); + EXPECT_EQ(3u, current_order); a_order = queue.PositionToOrder(a_position); - CPPUNIT_ASSERT_EQUAL(6u, a_order); -} - -CPPUNIT_TEST_SUITE_REGISTRATION(QueuePriorityTest); - -int -main(gcc_unused int argc, gcc_unused char **argv) -{ - CppUnit::TextUi::TestRunner runner; - auto ®istry = CppUnit::TestFactoryRegistry::getRegistry(); - runner.addTest(registry.makeTest()); - return runner.run() ? EXIT_SUCCESS : EXIT_FAILURE; + EXPECT_EQ(6u, a_order); } diff --git a/test/test_rewind.cxx b/test/test_rewind.cxx deleted file mode 100644 index db03b2b55..000000000 --- a/test/test_rewind.cxx +++ /dev/null @@ -1,149 +0,0 @@ -/* - * Unit tests for class RewindInputStream. - */ - -#include "config.h" -#include "input/RewindInputStream.hxx" -#include "input/InputStream.hxx" -#include "thread/Mutex.hxx" - -#include -#include -#include -#include - -#include - -#include -#include - -class StringInputStream final : public InputStream { - const char *data; - size_t remaining; - -public: - StringInputStream(const char *_uri, - Mutex &_mutex, - const char *_data) - :InputStream(_uri, _mutex), - data(_data), remaining(strlen(data)) { - SetReady(); - } - - /* virtual methods from InputStream */ - bool IsEOF() noexcept override { - return remaining == 0; - } - - size_t Read(void *ptr, size_t read_size) override { - size_t nbytes = std::min(remaining, read_size); - memcpy(ptr, data, nbytes); - data += nbytes; - remaining -= nbytes; - offset += nbytes; - return nbytes; - } -}; - -class RewindTest : public CppUnit::TestFixture { - CPPUNIT_TEST_SUITE(RewindTest); - CPPUNIT_TEST(TestRewind); - CPPUNIT_TEST_SUITE_END(); - -public: - void TestRewind() { - Mutex mutex; - - StringInputStream *sis = - new StringInputStream("foo://", mutex, - "foo bar"); - CPPUNIT_ASSERT(sis->IsReady()); - - auto ris = input_rewind_open(InputStreamPtr(sis)); - CPPUNIT_ASSERT(ris.get() != sis); - CPPUNIT_ASSERT(ris != nullptr); - - const std::lock_guard protect(mutex); - - ris->Update(); - CPPUNIT_ASSERT(ris->IsReady()); - CPPUNIT_ASSERT(!ris->KnownSize()); - CPPUNIT_ASSERT_EQUAL(offset_type(0), ris->GetOffset()); - - char buffer[16]; - size_t nbytes = ris->Read(buffer, 2); - CPPUNIT_ASSERT_EQUAL(size_t(2), nbytes); - CPPUNIT_ASSERT_EQUAL('f', buffer[0]); - CPPUNIT_ASSERT_EQUAL('o', buffer[1]); - CPPUNIT_ASSERT_EQUAL(offset_type(2), ris->GetOffset()); - CPPUNIT_ASSERT(!ris->IsEOF()); - - nbytes = ris->Read(buffer, 2); - CPPUNIT_ASSERT_EQUAL(size_t(2), nbytes); - CPPUNIT_ASSERT_EQUAL('o', buffer[0]); - CPPUNIT_ASSERT_EQUAL(' ', buffer[1]); - CPPUNIT_ASSERT_EQUAL(offset_type(4), ris->GetOffset()); - CPPUNIT_ASSERT(!ris->IsEOF()); - - ris->Seek(1); - CPPUNIT_ASSERT_EQUAL(offset_type(1), ris->GetOffset()); - CPPUNIT_ASSERT(!ris->IsEOF()); - - nbytes = ris->Read(buffer, 2); - CPPUNIT_ASSERT_EQUAL(size_t(2), nbytes); - CPPUNIT_ASSERT_EQUAL('o', buffer[0]); - CPPUNIT_ASSERT_EQUAL('o', buffer[1]); - CPPUNIT_ASSERT_EQUAL(offset_type(3), ris->GetOffset()); - CPPUNIT_ASSERT(!ris->IsEOF()); - - ris->Seek(0); - CPPUNIT_ASSERT_EQUAL(offset_type(0), ris->GetOffset()); - CPPUNIT_ASSERT(!ris->IsEOF()); - - nbytes = ris->Read(buffer, 2); - CPPUNIT_ASSERT_EQUAL(size_t(2), nbytes); - CPPUNIT_ASSERT_EQUAL('f', buffer[0]); - CPPUNIT_ASSERT_EQUAL('o', buffer[1]); - CPPUNIT_ASSERT_EQUAL(offset_type(2), ris->GetOffset()); - CPPUNIT_ASSERT(!ris->IsEOF()); - - nbytes = ris->Read(buffer, sizeof(buffer)); - CPPUNIT_ASSERT_EQUAL(size_t(2), nbytes); - CPPUNIT_ASSERT_EQUAL('o', buffer[0]); - CPPUNIT_ASSERT_EQUAL(' ', buffer[1]); - CPPUNIT_ASSERT_EQUAL(offset_type(4), ris->GetOffset()); - CPPUNIT_ASSERT(!ris->IsEOF()); - - nbytes = ris->Read(buffer, sizeof(buffer)); - CPPUNIT_ASSERT_EQUAL(size_t(3), nbytes); - CPPUNIT_ASSERT_EQUAL('b', buffer[0]); - CPPUNIT_ASSERT_EQUAL('a', buffer[1]); - CPPUNIT_ASSERT_EQUAL('r', buffer[2]); - CPPUNIT_ASSERT_EQUAL(offset_type(7), ris->GetOffset()); - CPPUNIT_ASSERT(ris->IsEOF()); - - ris->Seek(3); - CPPUNIT_ASSERT_EQUAL(offset_type(3), ris->GetOffset()); - CPPUNIT_ASSERT(!ris->IsEOF()); - - nbytes = ris->Read(buffer, sizeof(buffer)); - CPPUNIT_ASSERT_EQUAL(size_t(4), nbytes); - CPPUNIT_ASSERT_EQUAL(' ', buffer[0]); - CPPUNIT_ASSERT_EQUAL('b', buffer[1]); - CPPUNIT_ASSERT_EQUAL('a', buffer[2]); - CPPUNIT_ASSERT_EQUAL('r', buffer[3]); - CPPUNIT_ASSERT_EQUAL(offset_type(7), ris->GetOffset()); - CPPUNIT_ASSERT(ris->IsEOF()); - } -}; - -CPPUNIT_TEST_SUITE_REGISTRATION(RewindTest); - -int -main(gcc_unused int argc, gcc_unused char **argv) -{ - CppUnit::TextUi::TestRunner runner; - auto ®istry = CppUnit::TestFactoryRegistry::getRegistry(); - runner.addTest(registry.makeTest()); - return runner.run() ? EXIT_SUCCESS : EXIT_FAILURE; -} diff --git a/test/test_translate_song.cxx b/test/test_translate_song.cxx index 10c1acc61..7784a2e24 100644 --- a/test/test_translate_song.cxx +++ b/test/test_translate_song.cxx @@ -19,10 +19,7 @@ #include "Mapper.hxx" #include "util/ChronoUtil.hxx" -#include -#include -#include -#include +#include #include #include @@ -196,113 +193,108 @@ ToString(const DetachedSong &song) return result; } -class TranslateSongTest : public CppUnit::TestFixture { - CPPUNIT_TEST_SUITE(TranslateSongTest); - CPPUNIT_TEST(TestAbsoluteURI); - CPPUNIT_TEST(TestInsecure); - CPPUNIT_TEST(TestSecure); - CPPUNIT_TEST(TestInDatabase); - CPPUNIT_TEST(TestRelative); - CPPUNIT_TEST_SUITE_END(); +class TranslateSongTest : public ::testing::Test { + std::unique_ptr _storage; - void TestAbsoluteURI() { - DetachedSong song1("http://example.com/foo.ogg"); - auto se = ToString(song1); - const SongLoader loader(nullptr, nullptr); - CPPUNIT_ASSERT(playlist_check_translate_song(song1, "/ignored", - loader)); - CPPUNIT_ASSERT_EQUAL(se, ToString(song1)); +protected: + void SetUp() override { + _storage = CreateLocalStorage(Path::FromFS(music_directory)); + storage = _storage.get(); } - void TestInsecure() { - /* illegal because secure=false */ - DetachedSong song1 (uri1); - const SongLoader loader(*reinterpret_cast(1)); - CPPUNIT_ASSERT(!playlist_check_translate_song(song1, nullptr, - loader)); - } - - void TestSecure() { - DetachedSong song1(uri1, MakeTag1b()); - auto s1 = ToString(song1); - auto se = ToString(DetachedSong(uri1, MakeTag1c())); - - const SongLoader loader(nullptr, nullptr); - CPPUNIT_ASSERT(playlist_check_translate_song(song1, "/ignored", - loader)); - CPPUNIT_ASSERT_EQUAL(se, ToString(song1)); - } - - void TestInDatabase() { - const SongLoader loader(reinterpret_cast(1), - storage); - - DetachedSong song1("doesntexist"); - CPPUNIT_ASSERT(!playlist_check_translate_song(song1, nullptr, - loader)); - - DetachedSong song2(uri2, MakeTag2b()); - auto s1 = ToString(song2); - auto se = ToString(DetachedSong(uri2, MakeTag2c())); - CPPUNIT_ASSERT(playlist_check_translate_song(song2, nullptr, - loader)); - CPPUNIT_ASSERT_EQUAL(se, ToString(song2)); - - DetachedSong song3("/music/foo/bar.ogg", MakeTag2b()); - s1 = ToString(song3); - se = ToString(DetachedSong(uri2, MakeTag2c())); - CPPUNIT_ASSERT(playlist_check_translate_song(song3, nullptr, - loader)); - CPPUNIT_ASSERT_EQUAL(se, ToString(song3)); - } - - void TestRelative() { - const Database &db = *reinterpret_cast(1); - const SongLoader secure_loader(&db, storage); - const SongLoader insecure_loader(*reinterpret_cast(1), - &db, storage); - - /* map to music_directory */ - DetachedSong song1("bar.ogg", MakeTag2b()); - auto s1 = ToString(song1); - auto se = ToString(DetachedSong(uri2, MakeTag2c())); - CPPUNIT_ASSERT(playlist_check_translate_song(song1, "/music/foo", - insecure_loader)); - CPPUNIT_ASSERT_EQUAL(se, ToString(song1)); - - /* illegal because secure=false */ - DetachedSong song2("bar.ogg", MakeTag2b()); - CPPUNIT_ASSERT(!playlist_check_translate_song(song1, "/foo", - insecure_loader)); - - /* legal because secure=true */ - DetachedSong song3("bar.ogg", MakeTag1b()); - s1 = ToString(song3); - se = ToString(DetachedSong(uri1, MakeTag1c())); - CPPUNIT_ASSERT(playlist_check_translate_song(song3, "/foo", - secure_loader)); - CPPUNIT_ASSERT_EQUAL(se, ToString(song3)); - - /* relative to http:// */ - DetachedSong song4("bar.ogg", MakeTag2a()); - s1 = ToString(song4); - se = ToString(DetachedSong("http://example.com/foo/bar.ogg", MakeTag2a())); - CPPUNIT_ASSERT(playlist_check_translate_song(song4, "http://example.com/foo", - insecure_loader)); - CPPUNIT_ASSERT_EQUAL(se, ToString(song4)); + void TearDown() override { + _storage.reset(); } }; -CPPUNIT_TEST_SUITE_REGISTRATION(TranslateSongTest); - -int -main(gcc_unused int argc, gcc_unused char **argv) +TEST_F(TranslateSongTest, AbsoluteURI) { - auto _storage = CreateLocalStorage(Path::FromFS(music_directory)); - storage = _storage.get(); - - CppUnit::TextUi::TestRunner runner; - auto ®istry = CppUnit::TestFactoryRegistry::getRegistry(); - runner.addTest(registry.makeTest()); - return runner.run() ? EXIT_SUCCESS : EXIT_FAILURE; + DetachedSong song1("http://example.com/foo.ogg"); + auto se = ToString(song1); + const SongLoader loader(nullptr, nullptr); + EXPECT_TRUE(playlist_check_translate_song(song1, "/ignored", + loader)); + EXPECT_EQ(se, ToString(song1)); +} + +TEST_F(TranslateSongTest, Insecure) +{ + /* illegal because secure=false */ + DetachedSong song1 (uri1); + const SongLoader loader(*reinterpret_cast(1)); + EXPECT_FALSE(playlist_check_translate_song(song1, nullptr, + loader)); +} + +TEST_F(TranslateSongTest, Secure) +{ + DetachedSong song1(uri1, MakeTag1b()); + auto s1 = ToString(song1); + auto se = ToString(DetachedSong(uri1, MakeTag1c())); + + const SongLoader loader(nullptr, nullptr); + EXPECT_TRUE(playlist_check_translate_song(song1, "/ignored", + loader)); + EXPECT_EQ(se, ToString(song1)); +} + +TEST_F(TranslateSongTest, InDatabase) +{ + const SongLoader loader(reinterpret_cast(1), + storage); + + DetachedSong song1("doesntexist"); + EXPECT_FALSE(playlist_check_translate_song(song1, nullptr, + loader)); + + DetachedSong song2(uri2, MakeTag2b()); + auto s1 = ToString(song2); + auto se = ToString(DetachedSong(uri2, MakeTag2c())); + EXPECT_TRUE(playlist_check_translate_song(song2, nullptr, + loader)); + EXPECT_EQ(se, ToString(song2)); + + DetachedSong song3("/music/foo/bar.ogg", MakeTag2b()); + s1 = ToString(song3); + se = ToString(DetachedSong(uri2, MakeTag2c())); + EXPECT_TRUE(playlist_check_translate_song(song3, nullptr, + loader)); + EXPECT_EQ(se, ToString(song3)); +} + +TEST_F(TranslateSongTest, Relative) +{ + const Database &db = *reinterpret_cast(1); + const SongLoader secure_loader(&db, storage); + const SongLoader insecure_loader(*reinterpret_cast(1), + &db, storage); + + /* map to music_directory */ + DetachedSong song1("bar.ogg", MakeTag2b()); + auto s1 = ToString(song1); + auto se = ToString(DetachedSong(uri2, MakeTag2c())); + EXPECT_TRUE(playlist_check_translate_song(song1, "/music/foo", + insecure_loader)); + EXPECT_EQ(se, ToString(song1)); + + /* illegal because secure=false */ + DetachedSong song2("bar.ogg", MakeTag2b()); + EXPECT_FALSE(playlist_check_translate_song(song1, "/foo", + insecure_loader)); + + /* legal because secure=true */ + DetachedSong song3("bar.ogg", MakeTag1b()); + s1 = ToString(song3); + se = ToString(DetachedSong(uri1, MakeTag1c())); + EXPECT_TRUE(playlist_check_translate_song(song3, "/foo", + secure_loader)); + EXPECT_EQ(se, ToString(song3)); + + /* relative to http:// */ + DetachedSong song4("bar.ogg", MakeTag2a()); + s1 = ToString(song4); + se = ToString(DetachedSong("http://example.com/foo/bar.ogg", MakeTag2a())); + EXPECT_TRUE(playlist_check_translate_song(song4, "http://example.com/foo", + insecure_loader)); + EXPECT_EQ(se, ToString(song4)); } diff --git a/test/test_util.cxx b/test/test_util.cxx deleted file mode 100644 index 37e352678..000000000 --- a/test/test_util.cxx +++ /dev/null @@ -1,32 +0,0 @@ -/* - * Unit tests for src/util/ - */ - -#include "config.h" -#include "DivideStringTest.hxx" -#include "SplitStringTest.hxx" -#include "UriUtilTest.hxx" -#include "MimeTypeTest.hxx" -#include "TestCircularBuffer.hxx" - -#include -#include -#include -#include - -#include - -CPPUNIT_TEST_SUITE_REGISTRATION(DivideStringTest); -CPPUNIT_TEST_SUITE_REGISTRATION(SplitStringTest); -CPPUNIT_TEST_SUITE_REGISTRATION(UriUtilTest); -CPPUNIT_TEST_SUITE_REGISTRATION(MimeTypeTest); -CPPUNIT_TEST_SUITE_REGISTRATION(TestCircularBuffer); - -int -main(gcc_unused int argc, gcc_unused char **argv) -{ - CppUnit::TextUi::TestRunner runner; - auto ®istry = CppUnit::TestFactoryRegistry::getRegistry(); - runner.addTest(registry.makeTest()); - return runner.run() ? EXIT_SUCCESS : EXIT_FAILURE; -}