DivideStringTest.hxx 1.35 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/*
 * 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);
18
	CPPUNIT_TEST(TestStrip);
19 20 21 22 23 24 25
	CPPUNIT_TEST_SUITE_END();

public:
	void TestBasic() {
		constexpr char input[] = "foo.bar";
		const DivideString ds(input, '.');
		CPPUNIT_ASSERT(ds.IsDefined());
26
		CPPUNIT_ASSERT(!ds.empty());
27 28 29 30 31 32 33 34
		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());
35
		CPPUNIT_ASSERT(ds.empty());
36 37 38 39 40 41 42 43 44
		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());
	}
45 46 47 48 49

	void TestStrip() {
		constexpr char input[] = " foo\t.\nbar\r";
		const DivideString ds(input, '.', true);
		CPPUNIT_ASSERT(ds.IsDefined());
50
		CPPUNIT_ASSERT(!ds.empty());
51 52 53
		CPPUNIT_ASSERT_EQUAL(0, strcmp(ds.GetFirst(), "foo"));
		CPPUNIT_ASSERT_EQUAL(input + 7, ds.GetSecond());
	}
54
};