test: use GTest instead of cppunit

This commit is contained in:
Max Kellermann 2018-10-16 19:01:13 +02:00
parent eefc0f5d80
commit 01b6e1cbf2
39 changed files with 1027 additions and 1644 deletions

View File

@ -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

1
NEWS
View File

@ -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

View File

@ -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

View File

@ -1,54 +0,0 @@
/*
* Unit tests for src/util/
*/
#include "check.h"
#include "util/DivideString.hxx"
#include <cppunit/TestFixture.h>
#include <cppunit/extensions/HelperMacros.h>
#include <string.h>
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());
}
};

View File

@ -1,49 +0,0 @@
/*
* Unit tests for src/util/
*/
#include "check.h"
#include "util/MimeType.hxx"
#include <cppunit/TestFixture.h>
#include <cppunit/extensions/HelperMacros.h>
#include <string.h>
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);
}
};

View File

@ -1,65 +0,0 @@
/*
* Unit tests for src/util/
*/
#include "check.h"
#include "util/SplitString.hxx"
#include "util/Macros.hxx"
#include <cppunit/TestFixture.h>
#include <cppunit/extensions/HelperMacros.h>
#include <string.h>
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());
}
};

View File

@ -18,45 +18,14 @@
*/
#include "config.h"
#include "TestAudioFormat.hxx"
#include "AudioFormat.hxx"
#include "AudioParser.hxx"
#include "util/StringBuffer.hxx"
#include <cppunit/TestAssert.h>
#include <gtest/gtest.h>
#include <string.h>
namespace CppUnit {
template<>
struct assertion_traits<const char *>
{
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<AudioFormat>
{
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));
}

View File

@ -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 <cppunit/TestFixture.h>
#include <cppunit/extensions/HelperMacros.h>
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

151
test/TestCircularBuffer.cxx Normal file
View File

@ -0,0 +1,151 @@
/*
* Unit tests for class CircularBuffer.
*/
#include "config.h"
#include "util/CircularBuffer.hxx"
#include <gtest/gtest.h>
TEST(CircularBuffer, Basic)
{
static size_t N = 8;
int data[N];
CircularBuffer<int> 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);
}

View File

@ -1,158 +0,0 @@
/*
* Unit tests for class CircularBuffer.
*/
#include "check.h"
#include "util/CircularBuffer.hxx"
#include <cppunit/TestFixture.h>
#include <cppunit/extensions/HelperMacros.h>
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<int> 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);
}
};

45
test/TestDivideString.cxx Normal file
View File

@ -0,0 +1,45 @@
/*
* Unit tests for src/util/
*/
#include "config.h"
#include "util/DivideString.hxx"
#include <gtest/gtest.h>
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());
}

View File

@ -5,102 +5,75 @@
#include "config.h"
#include "fs/Glob.hxx"
#include <cppunit/TestFixture.h>
#include <cppunit/extensions/TestFactoryRegistry.h>
#include <cppunit/ui/text/TestRunner.h>
#include <cppunit/extensions/HelperMacros.h>
#include <string.h>
#include <stdlib.h>
#include <gtest/gtest.h>
#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 &registry = 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

View File

@ -5,17 +5,8 @@
#include "config.h"
#include "lib/icu/Converter.hxx"
#include "util/AllocatedString.hxx"
#include "util/StringAPI.hxx"
#include <cppunit/TestFixture.h>
#include <cppunit/extensions/TestFactoryRegistry.h>
#include <cppunit/ui/text/TestRunner.h>
#include <cppunit/extensions/HelperMacros.h>
#include <stdexcept>
#include <string.h>
#include <stdlib.h>
#include <gtest/gtest.h>
#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 (...) {
}
TEST(IcuConverter, InvalidCharset)
{
EXPECT_ANY_THROW(IcuConverter::Create("doesntexist"));
}
void TestLatin1() {
TEST(IcuConverter, Latin1)
{
IcuConverter *const converter =
IcuConverter::Create("iso-8859-1");
CPPUNIT_ASSERT(converter != nullptr);
ASSERT_NE(converter, nullptr);
for (const auto i : invalid_utf8) {
try {
auto f = converter->FromUTF8(i);
CPPUNIT_FAIL("Exception expected");
} catch (...) {
}
EXPECT_ANY_THROW(converter->FromUTF8(i));
}
for (const auto i : latin1_tests) {
auto f = converter->FromUTF8(i.utf8);
CPPUNIT_ASSERT_EQUAL(true, StringIsEqual(f.c_str(),
i.other));
EXPECT_STREQ(f.c_str(), i.other);
auto t = converter->ToUTF8(i.other);
CPPUNIT_ASSERT_EQUAL(true, StringIsEqual(t.c_str(),
i.utf8));
EXPECT_STREQ(t.c_str(), i.utf8);
}
delete converter;
}
};
CPPUNIT_TEST_SUITE_REGISTRATION(TestIcuConverter);
#endif
int
main(gcc_unused int argc, gcc_unused char **argv)
{
#ifdef HAVE_ICU_CONVERTER
CppUnit::TextUi::TestRunner runner;
auto &registry = CppUnit::TestFactoryRegistry::getRegistry();
runner.addTest(registry.makeTest());
return runner.run() ? EXIT_SUCCESS : EXIT_FAILURE;
#else
return EXIT_SUCCESS;
#endif
}

40
test/TestMimeType.cxx Normal file
View File

@ -0,0 +1,40 @@
/*
* Unit tests for src/util/
*/
#include "config.h"
#include "util/MimeType.hxx"
#include <gtest/gtest.h>
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);
}

View File

@ -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 <gtest/gtest.h>
#include <string.h>
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<Mutex> 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());
}

57
test/TestSplitString.cxx Normal file
View File

@ -0,0 +1,57 @@
/*
* Unit tests for src/util/
*/
#include "config.h"
#include "util/SplitString.hxx"
#include "util/Macros.hxx"
#include <gtest/gtest.h>
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());
}

48
test/TestUriUtil.cxx Normal file
View File

@ -0,0 +1,48 @@
/*
* Unit tests for src/util/
*/
#include "config.h"
#include "util/UriUtil.hxx"
#include <gtest/gtest.h>
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/"));
}

View File

@ -1,66 +0,0 @@
/*
* Unit tests for src/util/
*/
#include "check.h"
#include "util/UriUtil.hxx"
#include <cppunit/TestFixture.h>
#include <cppunit/extensions/HelperMacros.h>
#include <string.h>
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/"));
}
};

View File

@ -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,
],
))

View File

@ -2,76 +2,46 @@
#include "archive/ArchiveLookup.hxx"
#include "util/Compiler.h"
#include <cppunit/TestFixture.h>
#include <cppunit/extensions/TestFactoryRegistry.h>
#include <cppunit/ui/text/TestRunner.h>
#include <cppunit/extensions/HelperMacros.h>
#include <gtest/gtest.h>
#include <string.h>
#include <stdlib.h>
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 &registry = CppUnit::TestFactoryRegistry::getRegistry();
runner.addTest(registry.makeTest());
return runner.run() ? EXIT_SUCCESS : EXIT_FAILURE;
}

View File

@ -21,33 +21,12 @@
#include "util/Macros.hxx"
#include "util/Compiler.h"
#include <cppunit/TestFixture.h>
#include <cppunit/extensions/HelperMacros.h>
#include <cppunit/extensions/TestFactoryRegistry.h>
#include <cppunit/ui/text/TestRunner.h>
#include <gtest/gtest.h>
#include <string.h>
#include <stdlib.h>
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 &registry = CppUnit::TestFactoryRegistry::getRegistry();
runner.addTest(registry.makeTest());
return runner.run() ? EXIT_SUCCESS : EXIT_FAILURE;
EXPECT_STREQ(result, (const char *)dest);
}

View File

@ -8,10 +8,7 @@
/* include the .cxx file to get access to internal functions */
#include "IcyMetaDataParser.cxx"
#include <cppunit/TestFixture.h>
#include <cppunit/extensions/TestFactoryRegistry.h>
#include <cppunit/ui/text/TestRunner.h>
#include <cppunit/extensions/HelperMacros.h>
#include <gtest/gtest.h>
#include <string>
@ -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,16 +43,11 @@ 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() {
TEST(IcyMetadataParserTest, Basic)
{
TestIcyParserEmpty("foo=bar;");
TestIcyParserTitle("StreamTitle='foo bar'", "foo bar");
TestIcyParserTitle("StreamTitle='foo bar';", "foo bar");
@ -69,15 +61,3 @@ public:
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)
{
CppUnit::TextUi::TestRunner runner;
auto &registry = CppUnit::TestFactoryRegistry::getRegistry();
runner.addTest(registry.makeTest());
return runner.run() ? EXIT_SUCCESS : EXIT_FAILURE;
}

View File

@ -5,83 +5,63 @@
#include "config.h"
#include "player/CrossFade.cxx"
#include <cppunit/TestFixture.h>
#include <cppunit/extensions/TestFactoryRegistry.h>
#include <cppunit/ui/text/TestRunner.h>
#include <cppunit/extensions/HelperMacros.h>
#include <gtest/gtest.h>
#include <string.h>
class MixRampTest : public CppUnit::TestFixture {
CPPUNIT_TEST_SUITE(MixRampTest);
CPPUNIT_TEST(TestInterpolate);
CPPUNIT_TEST_SUITE_END();
public:
void TestInterpolate() {
TEST(MixRamp, Interpolate)
{
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),
EXPECT_NEAR(double(0),
mixramp_interpolate(foo, 0).count(),
0.05);
free(foo);
foo = strdup(input);
CPPUNIT_ASSERT_DOUBLES_EQUAL(float(0),
EXPECT_NEAR(float(0),
mixramp_interpolate(foo, 1).count(),
0.005);
free(foo);
foo = strdup(input);
CPPUNIT_ASSERT_DOUBLES_EQUAL(float(0.1),
EXPECT_NEAR(float(0.1),
mixramp_interpolate(foo, 3).count(),
0.005);
free(foo);
foo = strdup(input);
CPPUNIT_ASSERT_DOUBLES_EQUAL(float(2.5),
EXPECT_NEAR(float(2.5),
mixramp_interpolate(foo, 6).count(),
0.01);
free(foo);
foo = strdup(input);
CPPUNIT_ASSERT(mixramp_interpolate(foo, 6.1) < FloatDuration::zero());
EXPECT_LT(mixramp_interpolate(foo, 6.1), FloatDuration::zero());
free(foo);
foo = strdup(input);
CPPUNIT_ASSERT_DOUBLES_EQUAL(float(0.05),
EXPECT_NEAR(float(0.05),
mixramp_interpolate(foo, 2).count(),
0.05);
free(foo);
foo = strdup(input);
CPPUNIT_ASSERT_DOUBLES_EQUAL(float(1.3),
EXPECT_NEAR(float(1.3),
mixramp_interpolate(foo, 4.5).count(),
0.05);
free(foo);
foo = strdup(input);
CPPUNIT_ASSERT_DOUBLES_EQUAL(float(0.9),
EXPECT_NEAR(float(0.9),
mixramp_interpolate(foo, 4).count(),
0.05);
free(foo);
foo = strdup(input);
CPPUNIT_ASSERT_DOUBLES_EQUAL(float(1.7),
EXPECT_NEAR(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)
{
CppUnit::TextUi::TestRunner runner;
auto &registry = CppUnit::TestFactoryRegistry::getRegistry();
runner.addTest(registry.makeTest());
return runner.run() ? EXIT_SUCCESS : EXIT_FAILURE;
}

View File

@ -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 <cppunit/TestFixture.h>
#include <cppunit/extensions/HelperMacros.h>
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

View File

@ -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 <gtest/gtest.h>
TEST(PcmTest, Channels16)
{
constexpr size_t N = 509;
const auto src = TestDataBuffer<int16_t, N * 2>();
@ -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),
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<int32_t, N * 2>();
@ -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),
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]);
}
}

View File

@ -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 <gtest/gtest.h>
TEST(PcmTest, Dither24)
{
constexpr unsigned N = 509;
const auto src = TestDataBuffer<int32_t, N>(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<int32_t, N>();
@ -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);
}
}

View File

@ -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 <gtest/gtest.h>
#include <string.h>
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<SampleFormat F, class Traits=SampleTraits<F>>
@ -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<SampleFormat::S16>();
TestAlsaChannelOrder71<SampleFormat::S16>();

View File

@ -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 <gtest/gtest.h>
TEST(PcmTest, Format8To16)
{
constexpr size_t N = 509;
const auto src = TestDataBuffer<int8_t, N>();
@ -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<int16_t, N>();
@ -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<int16_t, N>();
@ -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<int16_t, N>();
@ -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<float *>(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]);
}

View File

@ -18,10 +18,11 @@
*/
#include "config.h"
#include "test_pcm_all.hxx"
#include "pcm/Interleave.hxx"
#include "util/Macros.hxx"
#include <gtest/gtest.h>
#include <algorithm>
template<typename T>
@ -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<uint8_t>();
}
void
PcmInterleaveTest::TestInterleave16()
TEST(PcmTest, Interleave16)
{
TestInterleaveN<uint16_t>();
}
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<uint16_t>();
}
void
PcmInterleaveTest::TestInterleave32()
TEST(PcmTest, Interleave32)
{
TestInterleaveN<uint8_t>();
}
void
PcmInterleaveTest::TestInterleave64()
TEST(PcmTest, Interleave64)
{
TestInterleaveN<uint64_t>();
}

View File

@ -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 <cppunit/extensions/TestFactoryRegistry.h>
#include <cppunit/ui/text/TestRunner.h>
#include <stdlib.h>
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 &registry = CppUnit::TestFactoryRegistry::getRegistry();
runner.addTest(registry.makeTest());
return runner.run() ? EXIT_SUCCESS : EXIT_FAILURE;
}

View File

@ -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 <gtest/gtest.h>
template<typename T, SampleFormat format, typename G=RandomInt<T>>
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<int8_t, SampleFormat::S8>();
}
void
PcmMixTest::TestMix16()
TEST(PcmTest, Mix16)
{
TestPcmMix<int16_t, SampleFormat::S16>();
}
void
PcmMixTest::TestMix24()
TEST(PcmTest, Mix24)
{
TestPcmMix<int32_t, SampleFormat::S24_P32>(RandomInt24());
}
void
PcmMixTest::TestMix32()
TEST(PcmTest, Mix32)
{
TestPcmMix<int32_t, SampleFormat::S32>();
}

View File

@ -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 <gtest/gtest.h>
TEST(PcmTest, Pack24)
{
constexpr unsigned N = 509;
const auto src = TestDataBuffer<int32_t, N>(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<uint8_t, N * 3>();
@ -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<uint8_t, N * 3>();
@ -87,6 +85,6 @@ PcmPackTest::TestUnpack24BE()
if (s & 0x800000)
s |= 0xff000000;
CPPUNIT_ASSERT_EQUAL(s, dest[i]);
EXPECT_EQ(s, dest[i]);
}
}

View File

@ -19,6 +19,8 @@
#include "util/ConstBuffer.hxx"
#include <gtest/gtest.h>
#include <array>
#include <random>
@ -92,13 +94,13 @@ template<typename T>
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;

View File

@ -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 <gtest/gtest.h>
#include <algorithm>
#include <string.h>
@ -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<value_type>::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<SampleFormat::S8>();
}
void
PcmVolumeTest::TestVolume16()
TEST(PcmTest, Volume16)
{
TestVolume<SampleFormat::S16>();
}
void
PcmVolumeTest::TestVolume24()
TEST(PcmTest, Volume24)
{
TestVolume<SampleFormat::S24_P32>(RandomInt24());
}
void
PcmVolumeTest::TestVolume32()
TEST(PcmTest, Volume32)
{
TestVolume<SampleFormat::S32>();
}
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<float>::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();
}

View File

@ -4,52 +4,24 @@
#include "protocol/RangeArg.hxx"
#include "util/Compiler.h"
#include <cppunit/TestFixture.h>
#include <cppunit/extensions/TestFactoryRegistry.h>
#include <cppunit/ui/text/TestRunner.h>
#include <cppunit/extensions/HelperMacros.h>
#include <gtest/gtest.h>
#include <stdlib.h>
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 &registry = CppUnit::TestFactoryRegistry::getRegistry();
runner.addTest(registry.makeTest());
return runner.run() ? EXIT_SUCCESS : EXIT_FAILURE;
EXPECT_THROW(range = ParseCommandArgRange("-2"),
ProtocolError);
}

View File

@ -3,10 +3,7 @@
#include "song/DetachedSong.hxx"
#include "util/Macros.hxx"
#include <cppunit/TestFixture.h>
#include <cppunit/extensions/TestFactoryRegistry.h>
#include <cppunit/ui/text/TestRunner.h>
#include <cppunit/extensions/HelperMacros.h>
#include <gtest/gtest.h>
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 &registry = CppUnit::TestFactoryRegistry::getRegistry();
runner.addTest(registry.makeTest());
return runner.run() ? EXIT_SUCCESS : EXIT_FAILURE;
EXPECT_EQ(6u, a_order);
}

View File

@ -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 <cppunit/TestFixture.h>
#include <cppunit/extensions/TestFactoryRegistry.h>
#include <cppunit/ui/text/TestRunner.h>
#include <cppunit/extensions/HelperMacros.h>
#include <string>
#include <string.h>
#include <stdlib.h>
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<Mutex> 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 &registry = CppUnit::TestFactoryRegistry::getRegistry();
runner.addTest(registry.makeTest());
return runner.run() ? EXIT_SUCCESS : EXIT_FAILURE;
}

View File

@ -19,10 +19,7 @@
#include "Mapper.hxx"
#include "util/ChronoUtil.hxx"
#include <cppunit/TestFixture.h>
#include <cppunit/extensions/TestFactoryRegistry.h>
#include <cppunit/ui/text/TestRunner.h>
#include <cppunit/extensions/HelperMacros.h>
#include <gtest/gtest.h>
#include <string.h>
#include <stdio.h>
@ -196,67 +193,77 @@ 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> _storage;
void TestAbsoluteURI() {
protected:
void SetUp() override {
_storage = CreateLocalStorage(Path::FromFS(music_directory));
storage = _storage.get();
}
void TearDown() override {
_storage.reset();
}
};
TEST_F(TranslateSongTest, AbsoluteURI)
{
DetachedSong song1("http://example.com/foo.ogg");
auto se = ToString(song1);
const SongLoader loader(nullptr, nullptr);
CPPUNIT_ASSERT(playlist_check_translate_song(song1, "/ignored",
EXPECT_TRUE(playlist_check_translate_song(song1, "/ignored",
loader));
CPPUNIT_ASSERT_EQUAL(se, ToString(song1));
EXPECT_EQ(se, ToString(song1));
}
void TestInsecure() {
TEST_F(TranslateSongTest, Insecure)
{
/* illegal because secure=false */
DetachedSong song1 (uri1);
const SongLoader loader(*reinterpret_cast<const Client *>(1));
CPPUNIT_ASSERT(!playlist_check_translate_song(song1, nullptr,
EXPECT_FALSE(playlist_check_translate_song(song1, nullptr,
loader));
}
void TestSecure() {
TEST_F(TranslateSongTest, Secure)
{
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",
EXPECT_TRUE(playlist_check_translate_song(song1, "/ignored",
loader));
CPPUNIT_ASSERT_EQUAL(se, ToString(song1));
EXPECT_EQ(se, ToString(song1));
}
void TestInDatabase() {
TEST_F(TranslateSongTest, InDatabase)
{
const SongLoader loader(reinterpret_cast<const Database *>(1),
storage);
DetachedSong song1("doesntexist");
CPPUNIT_ASSERT(!playlist_check_translate_song(song1, nullptr,
EXPECT_FALSE(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,
EXPECT_TRUE(playlist_check_translate_song(song2, nullptr,
loader));
CPPUNIT_ASSERT_EQUAL(se, ToString(song2));
EXPECT_EQ(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,
EXPECT_TRUE(playlist_check_translate_song(song3, nullptr,
loader));
CPPUNIT_ASSERT_EQUAL(se, ToString(song3));
EXPECT_EQ(se, ToString(song3));
}
void TestRelative() {
TEST_F(TranslateSongTest, Relative)
{
const Database &db = *reinterpret_cast<const Database *>(1);
const SongLoader secure_loader(&db, storage);
const SongLoader insecure_loader(*reinterpret_cast<const Client *>(1),
@ -266,43 +273,28 @@ class TranslateSongTest : public CppUnit::TestFixture {
DetachedSong song1("bar.ogg", MakeTag2b());
auto s1 = ToString(song1);
auto se = ToString(DetachedSong(uri2, MakeTag2c()));
CPPUNIT_ASSERT(playlist_check_translate_song(song1, "/music/foo",
EXPECT_TRUE(playlist_check_translate_song(song1, "/music/foo",
insecure_loader));
CPPUNIT_ASSERT_EQUAL(se, ToString(song1));
EXPECT_EQ(se, ToString(song1));
/* illegal because secure=false */
DetachedSong song2("bar.ogg", MakeTag2b());
CPPUNIT_ASSERT(!playlist_check_translate_song(song1, "/foo",
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()));
CPPUNIT_ASSERT(playlist_check_translate_song(song3, "/foo",
EXPECT_TRUE(playlist_check_translate_song(song3, "/foo",
secure_loader));
CPPUNIT_ASSERT_EQUAL(se, ToString(song3));
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()));
CPPUNIT_ASSERT(playlist_check_translate_song(song4, "http://example.com/foo",
EXPECT_TRUE(playlist_check_translate_song(song4, "http://example.com/foo",
insecure_loader));
CPPUNIT_ASSERT_EQUAL(se, ToString(song4));
}
};
CPPUNIT_TEST_SUITE_REGISTRATION(TranslateSongTest);
int
main(gcc_unused int argc, gcc_unused char **argv)
{
auto _storage = CreateLocalStorage(Path::FromFS(music_directory));
storage = _storage.get();
CppUnit::TextUi::TestRunner runner;
auto &registry = CppUnit::TestFactoryRegistry::getRegistry();
runner.addTest(registry.makeTest());
return runner.run() ? EXIT_SUCCESS : EXIT_FAILURE;
EXPECT_EQ(se, ToString(song4));
}

View File

@ -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 <cppunit/TestFixture.h>
#include <cppunit/extensions/TestFactoryRegistry.h>
#include <cppunit/ui/text/TestRunner.h>
#include <cppunit/extensions/HelperMacros.h>
#include <stdlib.h>
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 &registry = CppUnit::TestFactoryRegistry::getRegistry();
runner.addTest(registry.makeTest());
return runner.run() ? EXIT_SUCCESS : EXIT_FAILURE;
}