aboutsummaryrefslogtreecommitdiff
path: root/test/common
diff options
context:
space:
mode:
Diffstat (limited to 'test/common')
-rw-r--r--test/common/bufferedreadstream.h27
-rw-r--r--test/common/bufferedseekablereadstream.h65
-rw-r--r--test/common/func.h60
-rw-r--r--test/common/ptr.h3
-rw-r--r--test/common/seekablesubreadstream.h20
-rw-r--r--test/common/str.h116
-rw-r--r--test/common/subreadstream.h15
7 files changed, 242 insertions, 64 deletions
diff --git a/test/common/bufferedreadstream.h b/test/common/bufferedreadstream.h
new file mode 100644
index 0000000000..7733949d9a
--- /dev/null
+++ b/test/common/bufferedreadstream.h
@@ -0,0 +1,27 @@
+#include <cxxtest/TestSuite.h>
+
+#include "common/stream.h"
+
+class BufferedReadStreamTestSuite : public CxxTest::TestSuite {
+ public:
+ void test_traverse(void) {
+ byte contents[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
+ Common::MemoryReadStream ms(contents, 10);
+
+ // Use a buffer size of 4 -- note that 10 % 4 != 0,
+ // so we test what happens if the cache can't be completly
+ // refilled.
+ Common::BufferedReadStream srs(&ms, 4);
+
+ int i;
+ byte b;
+ for (i = 0; i < 10; ++i) {
+ TS_ASSERT( !srs.eos() );
+
+ b = srs.readByte();
+ TS_ASSERT_EQUALS( i, b );
+ }
+
+ TS_ASSERT( srs.eos() );
+ }
+};
diff --git a/test/common/bufferedseekablereadstream.h b/test/common/bufferedseekablereadstream.h
new file mode 100644
index 0000000000..63941904cd
--- /dev/null
+++ b/test/common/bufferedseekablereadstream.h
@@ -0,0 +1,65 @@
+#include <cxxtest/TestSuite.h>
+
+#include "common/stream.h"
+
+class BufferedSeekableReadStreamTestSuite : public CxxTest::TestSuite {
+ public:
+ void test_traverse(void) {
+ byte contents[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
+ Common::MemoryReadStream ms(contents, 10);
+
+ Common::BufferedSeekableReadStream ssrs(&ms, 4);
+
+ int i;
+ byte b;
+ for (i = 0; i < 10; ++i) {
+ TS_ASSERT( !ssrs.eos() );
+
+ TS_ASSERT_EQUALS( i, ssrs.pos() );
+
+ ssrs.read(&b, 1);
+ TS_ASSERT_EQUALS( i, b );
+ }
+
+ TS_ASSERT( ssrs.eos() );
+ }
+
+ void test_seek(void) {
+ byte contents[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
+ Common::MemoryReadStream ms(contents, 10);
+
+ Common::BufferedSeekableReadStream ssrs(&ms, 4);
+ byte b;
+
+ TS_ASSERT_EQUALS( ssrs.pos(), (uint32)0 );
+
+ ssrs.seek(1, SEEK_SET);
+ TS_ASSERT_EQUALS( ssrs.pos(), (uint32)1 );
+ b = ssrs.readByte();
+ TS_ASSERT_EQUALS( b, 1 );
+
+ ssrs.seek(5, SEEK_CUR);
+ TS_ASSERT_EQUALS( ssrs.pos(), (uint32)7 );
+ b = ssrs.readByte();
+ TS_ASSERT_EQUALS( b, 7 );
+
+ ssrs.seek(-3, SEEK_CUR);
+ TS_ASSERT_EQUALS( ssrs.pos(), (uint32)5 );
+ b = ssrs.readByte();
+ TS_ASSERT_EQUALS( b, 5 );
+
+ ssrs.seek(0, SEEK_END);
+ TS_ASSERT_EQUALS( ssrs.pos(), (uint32)10 );
+ TS_ASSERT( ssrs.eos() );
+
+ ssrs.seek(3, SEEK_END);
+ TS_ASSERT_EQUALS( ssrs.pos(), (uint32)7 );
+ b = ssrs.readByte();
+ TS_ASSERT_EQUALS( b, 7 );
+
+ ssrs.seek(8, SEEK_END);
+ TS_ASSERT_EQUALS( ssrs.pos(), (uint32)2 );
+ b = ssrs.readByte();
+ TS_ASSERT_EQUALS( b, 2 );
+ }
+};
diff --git a/test/common/func.h b/test/common/func.h
new file mode 100644
index 0000000000..bf9b2ddff9
--- /dev/null
+++ b/test/common/func.h
@@ -0,0 +1,60 @@
+#include <cxxtest/TestSuite.h>
+
+#include "common/func.h"
+
+void myFunction1(int &dst, const int src) { dst = src; }
+void myFunction2(const int src, int &dst) { dst = src; }
+
+class FuncTestSuite : public CxxTest::TestSuite
+{
+ public:
+ void test_bind1st() {
+ int dst = 0;
+ Common::bind1st(Common::ptr_fun(myFunction1), dst)(1);
+ TS_ASSERT_EQUALS(dst, 1);
+ }
+
+ void test_bind2nd() {
+ int dst = 0;
+ Common::bind2nd(Common::ptr_fun(myFunction2), dst)(1);
+ TS_ASSERT_EQUALS(dst, 1);
+ }
+
+ struct Foo {
+ void fooAdd(int &foo) {
+ ++foo;
+ }
+
+ void fooSub(int &foo) const {
+ --foo;
+ }
+ };
+
+ void test_mem_fun_ref() {
+ Foo myFoos[4];
+ int counter = 0;
+
+ Common::for_each(myFoos, myFoos+4, Common::bind2nd(Common::mem_fun_ref(&Foo::fooAdd), counter));
+ TS_ASSERT_EQUALS(counter, 4);
+
+ Common::for_each(myFoos, myFoos+4, Common::bind2nd(Common::mem_fun_ref(&Foo::fooSub), counter));
+ TS_ASSERT_EQUALS(counter, 0);
+ }
+
+ void test_mem_fun() {
+ Foo *myFoos[4];
+ for (int i = 0; i < 4; ++i)
+ myFoos[i] = new Foo;
+
+ int counter = 0;
+
+ Common::for_each(myFoos, myFoos+4, Common::bind2nd(Common::mem_fun(&Foo::fooAdd), counter));
+ TS_ASSERT_EQUALS(counter, 4);
+
+ Common::for_each(myFoos, myFoos+4, Common::bind2nd(Common::mem_fun(&Foo::fooSub), counter));
+ TS_ASSERT_EQUALS(counter, 0);
+
+ for (int i = 0; i < 4; ++i)
+ delete myFoos[i];
+ }
+};
diff --git a/test/common/ptr.h b/test/common/ptr.h
index 11ed52d4b9..986330057c 100644
--- a/test/common/ptr.h
+++ b/test/common/ptr.h
@@ -61,6 +61,9 @@ class PtrTestSuite : public CxxTest::TestSuite
TS_ASSERT(p1 != 0);
TS_ASSERT(p2 == 0);
+
+ p1.reset();
+ TS_ASSERT(!p1);
}
struct A {
diff --git a/test/common/seekablesubreadstream.h b/test/common/seekablesubreadstream.h
index c4b21667c7..4e517093a5 100644
--- a/test/common/seekablesubreadstream.h
+++ b/test/common/seekablesubreadstream.h
@@ -2,22 +2,19 @@
#include "common/stream.h"
-class SeekableSubReadStreamTestSuite : public CxxTest::TestSuite
-{
+class SeekableSubReadStreamTestSuite : public CxxTest::TestSuite {
public:
- void test_traverse( void )
- {
+ void test_traverse(void) {
byte contents[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
- Common::MemoryReadStream ms = Common::MemoryReadStream(contents, 10);
+ Common::MemoryReadStream ms(contents, 10);
int start = 2, end = 8;
- Common::SeekableSubReadStream ssrs = Common::SeekableSubReadStream(&ms, start, end);
+ Common::SeekableSubReadStream ssrs(&ms, start, end);
int i;
byte b;
- for (i = start; i < end; ++i)
- {
+ for (i = start; i < end; ++i) {
TS_ASSERT( !ssrs.eos() );
TS_ASSERT_EQUALS( uint32(i - start), ssrs.pos() );
@@ -29,12 +26,11 @@ class SeekableSubReadStreamTestSuite : public CxxTest::TestSuite
TS_ASSERT( ssrs.eos() );
}
- void test_seek( void )
- {
+ void test_seek(void) {
byte contents[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
- Common::MemoryReadStream ms = Common::MemoryReadStream(contents, 10);
+ Common::MemoryReadStream ms(contents, 10);
- Common::SeekableSubReadStream ssrs = Common::SeekableSubReadStream(&ms, 1, 9);
+ Common::SeekableSubReadStream ssrs(&ms, 1, 9);
byte b;
TS_ASSERT_EQUALS( ssrs.pos(), (uint32)0 );
diff --git a/test/common/str.h b/test/common/str.h
index 76574ea70f..72d4df6f61 100644
--- a/test/common/str.h
+++ b/test/common/str.h
@@ -7,124 +7,154 @@ class StringTestSuite : public CxxTest::TestSuite
public:
void test_constructors(void) {
Common::String str("test-string");
- TS_ASSERT( str == "test-string" );
+ TS_ASSERT( str == "test-string");
str = Common::String(str.c_str()+5, 3);
- TS_ASSERT( str == "str" );
+ TS_ASSERT( str == "str");
str = "test-string";
- TS_ASSERT( str == "test-string" );
+ TS_ASSERT( str == "test-string");
str = Common::String(str.c_str()+5, str.c_str()+8);
- TS_ASSERT( str == "str" );
+ TS_ASSERT( str == "str");
+ }
+
+ void test_trim(void) {
+ Common::String str(" This is a s tring with spaces ");
+ Common::String str2 = str;
+ str.trim();
+ TS_ASSERT( str == "This is a s tring with spaces");
+ TS_ASSERT( str2 == " This is a s tring with spaces ");
}
void test_empty_clear(void) {
Common::String str("test");
- TS_ASSERT( !str.empty() );
+ TS_ASSERT( !str.empty());
str.clear();
- TS_ASSERT( str.empty() );
+ TS_ASSERT( str.empty());
}
void test_lastChar(void) {
Common::String str;
- TS_ASSERT_EQUALS( str.lastChar(), '\0' );
+ TS_ASSERT_EQUALS(str.lastChar(), '\0');
str = "test";
- TS_ASSERT_EQUALS( str.lastChar(), 't' );
+ TS_ASSERT_EQUALS(str.lastChar(), 't');
Common::String str2("bar");
- TS_ASSERT_EQUALS( str2.lastChar(), 'r' );
+ TS_ASSERT_EQUALS(str2.lastChar(), 'r');
}
void test_concat1(void) {
Common::String str("foo");
Common::String str2("bar");
str += str2;
- TS_ASSERT_EQUALS( str, "foobar" );
- TS_ASSERT_EQUALS( str2, "bar" );
+ TS_ASSERT_EQUALS(str, "foobar");
+ TS_ASSERT_EQUALS(str2, "bar");
}
void test_concat2(void) {
Common::String str("foo");
str += "bar";
- TS_ASSERT_EQUALS( str, "foobar" );
+ TS_ASSERT_EQUALS(str, "foobar");
}
void test_concat3(void) {
Common::String str("foo");
str += 'X';
- TS_ASSERT_EQUALS( str, "fooX" );
+ TS_ASSERT_EQUALS(str, "fooX");
}
void test_refCount(void) {
+ // using internal storage
Common::String foo1("foo");
- Common::String foo2("foo");
+ Common::String foo2(foo1);
Common::String foo3(foo2);
foo3 += 'X';
- TS_ASSERT_EQUALS( foo2, foo1 );
- TS_ASSERT_EQUALS( foo2, "foo" );
- TS_ASSERT_EQUALS( foo3, "foo""X" );
+ TS_ASSERT_EQUALS(foo1, "foo");
+ TS_ASSERT_EQUALS(foo2, "foo");
+ TS_ASSERT_EQUALS(foo3, "foo""X");
+ foo2 = 'x';
+ TS_ASSERT_EQUALS(foo1, "foo");
+ TS_ASSERT_EQUALS(foo2, "x");
+ TS_ASSERT_EQUALS(foo3, "foo""X");
}
void test_refCount2(void) {
+ // using external storage
Common::String foo1("fooasdkadklasdjklasdjlkasjdlkasjdklasjdlkjasdasd");
- Common::String foo2("fooasdkadklasdjklasdjlkasjdlkasjdklasjdlkjasdasd");
+ Common::String foo2(foo1);
Common::String foo3(foo2);
foo3 += 'X';
- TS_ASSERT_EQUALS( foo2, foo1 );
- TS_ASSERT_EQUALS( foo2, "fooasdkadklasdjklasdjlkasjdlkasjdklasjdlkjasdasd" );
- TS_ASSERT_EQUALS( foo3, "fooasdkadklasdjklasdjlkasjdlkasjdklasjdlkjasdasd""X" );
+ TS_ASSERT_EQUALS(foo1, "fooasdkadklasdjklasdjlkasjdlkasjdklasjdlkjasdasd");
+ TS_ASSERT_EQUALS(foo2, "fooasdkadklasdjklasdjlkasjdlkasjdklasjdlkjasdasd");
+ TS_ASSERT_EQUALS(foo3, "fooasdkadklasdjklasdjlkasjdlkasjdklasjdlkjasdasd""X");
+ foo2 = 'x';
+ TS_ASSERT_EQUALS(foo1, "fooasdkadklasdjklasdjlkasjdlkasjdklasjdlkjasdasd");
+ TS_ASSERT_EQUALS(foo2, "x");
+ TS_ASSERT_EQUALS(foo3, "fooasdkadklasdjklasdjlkasjdlkasjdklasjdlkjasdasd""X");
}
void test_refCount3(void) {
Common::String foo1("0123456789abcdefghijk");
- Common::String foo2("0123456789abcdefghijk");
+ Common::String foo2(foo1);
Common::String foo3(foo2);
foo3 += "0123456789abcdefghijk";
- TS_ASSERT_EQUALS( foo2, foo1 );
- TS_ASSERT_EQUALS( foo2, "0123456789abcdefghijk" );
- TS_ASSERT_EQUALS( foo3, "0123456789abcdefghijk""0123456789abcdefghijk" );
+ TS_ASSERT_EQUALS(foo1, foo2);
+ TS_ASSERT_EQUALS(foo2, "0123456789abcdefghijk");
+ TS_ASSERT_EQUALS(foo3, "0123456789abcdefghijk""0123456789abcdefghijk");
+ foo2 = 'x';
+ TS_ASSERT_EQUALS(foo1, "0123456789abcdefghijk");
+ TS_ASSERT_EQUALS(foo2, "x");
+ TS_ASSERT_EQUALS(foo3, "0123456789abcdefghijk""0123456789abcdefghijk");
}
void test_refCount4(void) {
Common::String foo1("fooasdkadklasdjklasdjlkasjdlkasjdklasjdlkjasdasd");
- Common::String foo2("fooasdkadklasdjklasdjlkasjdlkasjdklasjdlkjasdasd");
+ Common::String foo2(foo1);
Common::String foo3(foo2);
foo3 += "fooasdkadklasdjklasdjlkasjdlkasjdklasjdlkjasdasd";
- TS_ASSERT_EQUALS( foo2, foo1 );
- TS_ASSERT_EQUALS( foo2, "fooasdkadklasdjklasdjlkasjdlkasjdklasjdlkjasdasd" );
- TS_ASSERT_EQUALS( foo3, "fooasdkadklasdjklasdjlkasjdlkasjdklasjdlkjasdasd""fooasdkadklasdjklasdjlkasjdlkasjdklasjdlkjasdasd" );
+ TS_ASSERT_EQUALS(foo1, foo2);
+ TS_ASSERT_EQUALS(foo2, "fooasdkadklasdjklasdjlkasjdlkasjdklasjdlkjasdasd");
+ TS_ASSERT_EQUALS(foo3, "fooasdkadklasdjklasdjlkasjdlkasjdklasjdlkjasdasd""fooasdkadklasdjklasdjlkasjdlkasjdklasjdlkjasdasd");
+ foo2 = 'x';
+ TS_ASSERT_EQUALS(foo1, "fooasdkadklasdjklasdjlkasjdlkasjdklasjdlkjasdasd");
+ TS_ASSERT_EQUALS(foo2, "x");
+ TS_ASSERT_EQUALS(foo3, "fooasdkadklasdjklasdjlkasjdlkasjdklasjdlkjasdasd""fooasdkadklasdjklasdjlkasjdlkasjdklasjdlkjasdasd");
}
void test_hasPrefix(void) {
Common::String str("this/is/a/test, haha");
- TS_ASSERT_EQUALS( str.hasPrefix(""), true );
- TS_ASSERT_EQUALS( str.hasPrefix("this"), true );
- TS_ASSERT_EQUALS( str.hasPrefix("thit"), false );
- TS_ASSERT_EQUALS( str.hasPrefix("foo"), false );
+ TS_ASSERT_EQUALS(str.hasPrefix(""), true);
+ TS_ASSERT_EQUALS(str.hasPrefix("this"), true);
+ TS_ASSERT_EQUALS(str.hasPrefix("thit"), false);
+ TS_ASSERT_EQUALS(str.hasPrefix("foo"), false);
}
void test_hasSuffix(void) {
Common::String str("this/is/a/test, haha");
- TS_ASSERT_EQUALS( str.hasSuffix(""), true );
- TS_ASSERT_EQUALS( str.hasSuffix("haha"), true );
- TS_ASSERT_EQUALS( str.hasSuffix("hahb"), false );
- TS_ASSERT_EQUALS( str.hasSuffix("hahah"), false );
+ TS_ASSERT_EQUALS(str.hasSuffix(""), true);
+ TS_ASSERT_EQUALS(str.hasSuffix("haha"), true);
+ TS_ASSERT_EQUALS(str.hasSuffix("hahb"), false);
+ TS_ASSERT_EQUALS(str.hasSuffix("hahah"), false);
}
void test_contains(void) {
Common::String str("this/is/a/test, haha");
- TS_ASSERT_EQUALS( str.contains(""), true );
- TS_ASSERT_EQUALS( str.contains("haha"), true );
- TS_ASSERT_EQUALS( str.contains("hahb"), false );
- TS_ASSERT_EQUALS( str.contains("test"), true );
+ TS_ASSERT_EQUALS(str.contains(""), true);
+ TS_ASSERT_EQUALS(str.contains("haha"), true);
+ TS_ASSERT_EQUALS(str.contains("hahb"), false);
+ TS_ASSERT_EQUALS(str.contains("test"), true);
}
void test_toLowercase(void) {
Common::String str("Test it, NOW! 42");
+ Common::String str2 = str;
str.toLowercase();
- TS_ASSERT_EQUALS( str, "test it, now! 42" );
+ TS_ASSERT_EQUALS(str, "test it, now! 42");
+ TS_ASSERT_EQUALS(str2, "Test it, NOW! 42");
}
void test_toUppercase(void) {
Common::String str("Test it, NOW! 42");
+ Common::String str2 = str;
str.toUppercase();
- TS_ASSERT_EQUALS( str, "TEST IT, NOW! 42" );
+ TS_ASSERT_EQUALS(str, "TEST IT, NOW! 42");
+ TS_ASSERT_EQUALS(str2, "Test it, NOW! 42");
}
};
diff --git a/test/common/subreadstream.h b/test/common/subreadstream.h
index c48f57c3a8..4e14448c06 100644
--- a/test/common/subreadstream.h
+++ b/test/common/subreadstream.h
@@ -2,25 +2,22 @@
#include "common/stream.h"
-class SubReadStreamTestSuite : public CxxTest::TestSuite
-{
+class SubReadStreamTestSuite : public CxxTest::TestSuite {
public:
- void test_traverse( void )
- {
+ void test_traverse(void) {
byte contents[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
- Common::MemoryReadStream ms = Common::MemoryReadStream(contents, 10);
+ Common::MemoryReadStream ms(contents, 10);
int end = 5;
- Common::SubReadStream srs = Common::SubReadStream(&ms, end);
+ Common::SubReadStream srs(&ms, end);
int i;
byte b;
- for (i = 0; i < end; ++i)
- {
+ for (i = 0; i < end; ++i) {
TS_ASSERT( !srs.eos() );
- srs.read(&b, 1);
+ b = srs.readByte();
TS_ASSERT_EQUALS( i, b );
}