2003-12-24 16:16:00 +00:00
|
|
|
#include <cxxtest/TestSuite.h>
|
|
|
|
|
|
|
|
#include "common/str.h"
|
|
|
|
|
2005-07-30 21:11:48 +00:00
|
|
|
class StringTestSuite : public CxxTest::TestSuite
|
2003-12-24 16:16:00 +00:00
|
|
|
{
|
|
|
|
public:
|
2008-07-20 16:42:56 +00:00
|
|
|
void test_constructors(void) {
|
|
|
|
Common::String str("test-string");
|
2008-07-23 09:53:29 +00:00
|
|
|
TS_ASSERT( str == "test-string");
|
2008-07-20 16:42:56 +00:00
|
|
|
str = Common::String(str.c_str()+5, 3);
|
2008-07-23 09:53:29 +00:00
|
|
|
TS_ASSERT( str == "str");
|
2008-07-20 16:42:56 +00:00
|
|
|
str = "test-string";
|
2008-07-23 09:53:29 +00:00
|
|
|
TS_ASSERT( str == "test-string");
|
2008-07-20 16:42:56 +00:00
|
|
|
str = Common::String(str.c_str()+5, str.c_str()+8);
|
2008-07-23 09:53:29 +00:00
|
|
|
TS_ASSERT( str == "str");
|
2008-07-20 16:42:56 +00:00
|
|
|
}
|
|
|
|
|
2008-07-22 14:39:26 +00:00
|
|
|
void test_trim(void) {
|
|
|
|
Common::String str(" This is a s tring with spaces ");
|
2008-07-23 09:53:29 +00:00
|
|
|
Common::String str2 = str;
|
2008-07-22 14:39:26 +00:00
|
|
|
str.trim();
|
2008-07-23 09:53:29 +00:00
|
|
|
TS_ASSERT( str == "This is a s tring with spaces");
|
|
|
|
TS_ASSERT( str2 == " This is a s tring with spaces ");
|
2008-07-22 14:39:26 +00:00
|
|
|
}
|
|
|
|
|
2008-07-20 16:42:56 +00:00
|
|
|
void test_empty_clear(void) {
|
2003-12-24 16:16:00 +00:00
|
|
|
Common::String str("test");
|
2008-07-23 09:53:29 +00:00
|
|
|
TS_ASSERT( !str.empty());
|
2003-12-24 16:16:00 +00:00
|
|
|
str.clear();
|
2008-07-23 09:53:29 +00:00
|
|
|
TS_ASSERT( str.empty());
|
2003-12-24 16:16:00 +00:00
|
|
|
}
|
|
|
|
|
2008-07-20 16:42:56 +00:00
|
|
|
void test_lastChar(void) {
|
2003-12-24 16:16:00 +00:00
|
|
|
Common::String str;
|
2008-07-23 09:53:29 +00:00
|
|
|
TS_ASSERT_EQUALS(str.lastChar(), '\0');
|
2003-12-24 16:16:00 +00:00
|
|
|
str = "test";
|
2008-07-23 09:53:29 +00:00
|
|
|
TS_ASSERT_EQUALS(str.lastChar(), 't');
|
2003-12-24 16:16:00 +00:00
|
|
|
Common::String str2("bar");
|
2008-07-23 09:53:29 +00:00
|
|
|
TS_ASSERT_EQUALS(str2.lastChar(), 'r');
|
2003-12-24 16:16:00 +00:00
|
|
|
}
|
|
|
|
|
2008-07-20 16:42:56 +00:00
|
|
|
void test_concat1(void) {
|
2003-12-24 16:16:00 +00:00
|
|
|
Common::String str("foo");
|
|
|
|
Common::String str2("bar");
|
|
|
|
str += str2;
|
2008-07-23 09:53:29 +00:00
|
|
|
TS_ASSERT_EQUALS(str, "foobar");
|
|
|
|
TS_ASSERT_EQUALS(str2, "bar");
|
2003-12-24 16:16:00 +00:00
|
|
|
}
|
|
|
|
|
2008-07-20 16:42:56 +00:00
|
|
|
void test_concat2(void) {
|
2003-12-24 16:16:00 +00:00
|
|
|
Common::String str("foo");
|
|
|
|
str += "bar";
|
2008-07-23 09:53:29 +00:00
|
|
|
TS_ASSERT_EQUALS(str, "foobar");
|
2003-12-24 16:16:00 +00:00
|
|
|
}
|
|
|
|
|
2008-07-20 16:42:56 +00:00
|
|
|
void test_concat3(void) {
|
2003-12-24 16:16:00 +00:00
|
|
|
Common::String str("foo");
|
|
|
|
str += 'X';
|
2008-07-23 09:53:29 +00:00
|
|
|
TS_ASSERT_EQUALS(str, "fooX");
|
2003-12-24 16:16:00 +00:00
|
|
|
}
|
|
|
|
|
2008-07-20 16:42:56 +00:00
|
|
|
void test_refCount(void) {
|
2008-07-23 16:33:53 +00:00
|
|
|
// using internal storage
|
2006-09-30 18:58:09 +00:00
|
|
|
Common::String foo1("foo");
|
2008-07-23 16:33:53 +00:00
|
|
|
Common::String foo2(foo1);
|
2006-09-30 18:58:09 +00:00
|
|
|
Common::String foo3(foo2);
|
|
|
|
foo3 += 'X';
|
2008-07-23 16:33:53 +00:00
|
|
|
TS_ASSERT_EQUALS(foo1, "foo");
|
2008-07-23 09:53:29 +00:00
|
|
|
TS_ASSERT_EQUALS(foo2, "foo");
|
|
|
|
TS_ASSERT_EQUALS(foo3, "foo""X");
|
2008-07-23 16:33:53 +00:00
|
|
|
foo2 = 'x';
|
|
|
|
TS_ASSERT_EQUALS(foo1, "foo");
|
|
|
|
TS_ASSERT_EQUALS(foo2, "x");
|
|
|
|
TS_ASSERT_EQUALS(foo3, "foo""X");
|
2006-09-30 18:58:09 +00:00
|
|
|
}
|
|
|
|
|
2008-07-20 16:42:56 +00:00
|
|
|
void test_refCount2(void) {
|
2008-07-23 16:33:53 +00:00
|
|
|
// using external storage
|
2006-09-30 18:58:09 +00:00
|
|
|
Common::String foo1("fooasdkadklasdjklasdjlkasjdlkasjdklasjdlkjasdasd");
|
2008-07-23 16:33:53 +00:00
|
|
|
Common::String foo2(foo1);
|
2006-09-30 18:58:09 +00:00
|
|
|
Common::String foo3(foo2);
|
|
|
|
foo3 += 'X';
|
2008-07-23 16:33:53 +00:00
|
|
|
TS_ASSERT_EQUALS(foo1, "fooasdkadklasdjklasdjlkasjdlkasjdklasjdlkjasdasd");
|
2008-07-23 09:53:29 +00:00
|
|
|
TS_ASSERT_EQUALS(foo2, "fooasdkadklasdjklasdjlkasjdlkasjdklasjdlkjasdasd");
|
|
|
|
TS_ASSERT_EQUALS(foo3, "fooasdkadklasdjklasdjlkasjdlkasjdklasjdlkjasdasd""X");
|
2008-07-23 16:33:53 +00:00
|
|
|
foo2 = 'x';
|
|
|
|
TS_ASSERT_EQUALS(foo1, "fooasdkadklasdjklasdjlkasjdlkasjdklasjdlkjasdasd");
|
|
|
|
TS_ASSERT_EQUALS(foo2, "x");
|
|
|
|
TS_ASSERT_EQUALS(foo3, "fooasdkadklasdjklasdjlkasjdlkasjdklasjdlkjasdasd""X");
|
2006-09-30 18:58:09 +00:00
|
|
|
}
|
|
|
|
|
2008-07-20 16:42:56 +00:00
|
|
|
void test_refCount3(void) {
|
2006-09-30 18:58:09 +00:00
|
|
|
Common::String foo1("0123456789abcdefghijk");
|
2008-07-23 16:33:53 +00:00
|
|
|
Common::String foo2(foo1);
|
2006-09-30 18:58:09 +00:00
|
|
|
Common::String foo3(foo2);
|
|
|
|
foo3 += "0123456789abcdefghijk";
|
2008-07-23 16:33:53 +00:00
|
|
|
TS_ASSERT_EQUALS(foo1, foo2);
|
2008-07-23 09:53:29 +00:00
|
|
|
TS_ASSERT_EQUALS(foo2, "0123456789abcdefghijk");
|
|
|
|
TS_ASSERT_EQUALS(foo3, "0123456789abcdefghijk""0123456789abcdefghijk");
|
2008-07-23 16:33:53 +00:00
|
|
|
foo2 = 'x';
|
|
|
|
TS_ASSERT_EQUALS(foo1, "0123456789abcdefghijk");
|
|
|
|
TS_ASSERT_EQUALS(foo2, "x");
|
|
|
|
TS_ASSERT_EQUALS(foo3, "0123456789abcdefghijk""0123456789abcdefghijk");
|
2006-09-30 18:58:09 +00:00
|
|
|
}
|
|
|
|
|
2008-07-20 16:42:56 +00:00
|
|
|
void test_refCount4(void) {
|
2006-09-30 18:58:09 +00:00
|
|
|
Common::String foo1("fooasdkadklasdjklasdjlkasjdlkasjdklasjdlkjasdasd");
|
2008-07-23 16:33:53 +00:00
|
|
|
Common::String foo2(foo1);
|
2006-09-30 18:58:09 +00:00
|
|
|
Common::String foo3(foo2);
|
|
|
|
foo3 += "fooasdkadklasdjklasdjlkasjdlkasjdklasjdlkjasdasd";
|
2008-07-23 16:33:53 +00:00
|
|
|
TS_ASSERT_EQUALS(foo1, foo2);
|
2008-07-23 09:53:29 +00:00
|
|
|
TS_ASSERT_EQUALS(foo2, "fooasdkadklasdjklasdjlkasjdlkasjdklasjdlkjasdasd");
|
|
|
|
TS_ASSERT_EQUALS(foo3, "fooasdkadklasdjklasdjlkasjdlkasjdklasjdlkjasdasd""fooasdkadklasdjklasdjlkasjdlkasjdklasjdlkjasdasd");
|
2008-07-23 16:33:53 +00:00
|
|
|
foo2 = 'x';
|
|
|
|
TS_ASSERT_EQUALS(foo1, "fooasdkadklasdjklasdjlkasjdlkasjdklasjdlkjasdasd");
|
|
|
|
TS_ASSERT_EQUALS(foo2, "x");
|
|
|
|
TS_ASSERT_EQUALS(foo3, "fooasdkadklasdjklasdjlkasjdlkasjdklasjdlkjasdasd""fooasdkadklasdjklasdjlkasjdlkasjdklasjdlkjasdasd");
|
2006-09-30 18:58:09 +00:00
|
|
|
}
|
|
|
|
|
2008-07-20 16:42:56 +00:00
|
|
|
void test_hasPrefix(void) {
|
2005-02-06 19:01:23 +00:00
|
|
|
Common::String str("this/is/a/test, haha");
|
2008-07-23 09:53:29 +00:00
|
|
|
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);
|
2005-02-06 19:01:23 +00:00
|
|
|
}
|
|
|
|
|
2008-07-20 16:42:56 +00:00
|
|
|
void test_hasSuffix(void) {
|
2005-02-06 19:01:23 +00:00
|
|
|
Common::String str("this/is/a/test, haha");
|
2008-07-23 09:53:29 +00:00
|
|
|
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);
|
2005-02-06 19:01:23 +00:00
|
|
|
}
|
|
|
|
|
2008-07-20 16:42:56 +00:00
|
|
|
void test_contains(void) {
|
2007-04-15 18:27:10 +00:00
|
|
|
Common::String str("this/is/a/test, haha");
|
2008-07-23 09:53:29 +00:00
|
|
|
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);
|
2007-04-15 18:27:10 +00:00
|
|
|
}
|
|
|
|
|
2008-07-20 16:42:56 +00:00
|
|
|
void test_toLowercase(void) {
|
2003-12-24 16:16:00 +00:00
|
|
|
Common::String str("Test it, NOW! 42");
|
2008-07-23 09:53:29 +00:00
|
|
|
Common::String str2 = str;
|
2003-12-24 16:16:00 +00:00
|
|
|
str.toLowercase();
|
2008-07-23 09:53:29 +00:00
|
|
|
TS_ASSERT_EQUALS(str, "test it, now! 42");
|
|
|
|
TS_ASSERT_EQUALS(str2, "Test it, NOW! 42");
|
2003-12-24 16:16:00 +00:00
|
|
|
}
|
|
|
|
|
2008-07-20 16:42:56 +00:00
|
|
|
void test_toUppercase(void) {
|
2003-12-24 16:16:00 +00:00
|
|
|
Common::String str("Test it, NOW! 42");
|
2008-07-23 09:53:29 +00:00
|
|
|
Common::String str2 = str;
|
2003-12-24 16:16:00 +00:00
|
|
|
str.toUppercase();
|
2008-07-23 09:53:29 +00:00
|
|
|
TS_ASSERT_EQUALS(str, "TEST IT, NOW! 42");
|
|
|
|
TS_ASSERT_EQUALS(str2, "Test it, NOW! 42");
|
2003-12-24 16:16:00 +00:00
|
|
|
}
|
|
|
|
};
|