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 :
2009-04-20 19:26:50 +00:00
void test_constructors ( ) {
2008-07-20 16:42:56 +00:00
Common : : String str ( " test-string " ) ;
2009-05-19 11:22:49 +00:00
TS_ASSERT_EQUALS ( str , " test-string " ) ;
2008-07-20 16:42:56 +00:00
str = Common : : String ( str . c_str ( ) + 5 , 3 ) ;
2009-05-19 11:22:49 +00:00
TS_ASSERT_EQUALS ( str , " str " ) ;
2008-07-20 16:42:56 +00:00
str = " test-string " ;
2009-05-19 11:22:49 +00:00
TS_ASSERT_EQUALS ( str , " test-string " ) ;
2008-07-20 16:42:56 +00:00
str = Common : : String ( str . c_str ( ) + 5 , str . c_str ( ) + 8 ) ;
2009-05-19 11:22:49 +00:00
TS_ASSERT_EQUALS ( str , " str " ) ;
2008-07-20 16:42:56 +00:00
}
2009-04-20 19:26:50 +00:00
void test_trim ( ) {
2008-07-22 14:39:26 +00:00
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 ( ) ;
2009-05-19 11:22:49 +00:00
TS_ASSERT_EQUALS ( str , " This is a s tring with spaces " ) ;
TS_ASSERT_EQUALS ( str2 , " This is a s tring with spaces " ) ;
2008-07-22 14:39:26 +00:00
}
2009-04-20 19:26:50 +00:00
void test_empty_clear ( ) {
2003-12-24 16:16:00 +00:00
Common : : String str ( " test " ) ;
2009-05-19 11:22:49 +00:00
TS_ASSERT ( ! str . empty ( ) ) ;
2003-12-24 16:16:00 +00:00
str . clear ( ) ;
2009-05-19 11:22:49 +00:00
TS_ASSERT ( str . empty ( ) ) ;
2003-12-24 16:16:00 +00:00
}
2009-04-20 19:26:50 +00:00
void test_lastChar ( ) {
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
}
2009-04-20 19:26:50 +00:00
void test_concat1 ( ) {
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
}
2009-04-20 19:26:50 +00:00
void test_concat2 ( ) {
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
}
2009-04-20 19:26:50 +00:00
void test_concat3 ( ) {
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
}
2009-04-20 19:26:50 +00:00
void test_refCount ( ) {
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
}
2009-04-20 19:26:50 +00:00
void test_refCount2 ( ) {
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
}
2009-04-20 19:26:50 +00:00
void test_refCount3 ( ) {
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
}
2009-04-20 19:26:50 +00:00
void test_refCount4 ( ) {
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
}
2010-05-30 13:10:23 +00:00
void test_refCount5 ( ) {
2010-05-30 13:42:04 +00:00
// using external storage
2010-05-30 13:10:23 +00:00
Common : : String foo1 ( " HelloHelloHelloHelloAndHi " ) ;
Common : : String foo2 ( foo1 ) ;
for ( Common : : String : : iterator i = foo2 . begin ( ) ; i ! = foo2 . end ( ) ; + + i )
* i = ' h ' ;
TS_ASSERT_EQUALS ( foo1 , " HelloHelloHelloHelloAndHi " ) ;
TS_ASSERT_EQUALS ( foo2 , " hhhhhhhhhhhhhhhhhhhhhhhhh " ) ;
2010-05-30 13:42:04 +00:00
}
2010-05-30 13:10:23 +00:00
2010-05-30 13:42:04 +00:00
void test_refCount6 ( ) {
// using internal storage
Common : : String foo1 ( " Hello " ) ;
Common : : String foo2 ( foo1 ) ;
2010-05-30 13:10:23 +00:00
2010-05-30 13:42:04 +00:00
for ( Common : : String : : iterator i = foo2 . begin ( ) ; i ! = foo2 . end ( ) ; + + i )
2010-05-30 13:10:23 +00:00
* i = ' h ' ;
2010-05-30 13:42:04 +00:00
TS_ASSERT_EQUALS ( foo1 , " Hello " ) ;
TS_ASSERT_EQUALS ( foo2 , " hhhhh " ) ;
2010-05-30 13:10:23 +00:00
}
2009-05-31 22:11:06 +00:00
void test_self_asignment ( ) {
Common : : String foo1 ( " 12345678901234567890123456789012 " ) ;
foo1 = foo1 . c_str ( ) + 2 ;
TS_ASSERT_EQUALS ( foo1 , " 345678901234567890123456789012 " ) ;
Common : : String foo2 ( " 123456789012 " ) ;
foo2 = foo2 . c_str ( ) + 2 ;
TS_ASSERT_EQUALS ( foo2 , " 3456789012 " ) ;
// "foo3" and "foo4" will be using allocated storage from construction on.
Common : : String foo3 ( " 12345678901234567890123456789012 " ) ;
foo3 + = foo3 . c_str ( ) ;
TS_ASSERT_EQUALS ( foo3 , " 12345678901234567890123456789012 " " 12345678901234567890123456789012 " ) ;
Common : : String foo4 ( " 12345678901234567890123456789012 " ) ;
foo4 + = foo4 ;
TS_ASSERT_EQUALS ( foo4 , " 12345678901234567890123456789012 " " 12345678901234567890123456789012 " ) ;
// Based on our current Common::String implementation "foo5" and "foo6" will first use the internal storage,
// and on "operator +=" they will change to allocated memory.
Common : : String foo5 ( " 123456789012 " ) ;
foo5 + = foo5 . c_str ( ) ;
TS_ASSERT_EQUALS ( foo5 , " 123456789012 " " 123456789012 " ) ;
Common : : String foo6 ( " 123456789012 " ) ;
foo6 + = foo6 ;
TS_ASSERT_EQUALS ( foo6 , " 123456789012 " " 123456789012 " ) ;
2009-05-31 22:19:38 +00:00
// "foo7" and "foo8" will purely operate on internal storage.
Common : : String foo7 ( " 1234 " ) ;
foo7 + = foo7 . c_str ( ) ;
TS_ASSERT_EQUALS ( foo7 , " 1234 " " 1234 " ) ;
Common : : String foo8 ( " 1234 " ) ;
foo8 + = foo8 ;
TS_ASSERT_EQUALS ( foo8 , " 1234 " " 1234 " ) ;
Common : : String foo9 ( " 123456789012345678901234567889012 " ) ;
foo9 = foo9 . c_str ( ) ;
TS_ASSERT_EQUALS ( foo9 , " 123456789012345678901234567889012 " ) ;
foo9 = foo9 ;
TS_ASSERT_EQUALS ( foo9 , " 123456789012345678901234567889012 " ) ;
Common : : String foo10 ( " 1234 " ) ;
foo10 = foo10 . c_str ( ) ;
TS_ASSERT_EQUALS ( foo10 , " 1234 " ) ;
foo10 = foo10 ;
TS_ASSERT_EQUALS ( foo10 , " 1234 " ) ;
2009-05-31 22:11:06 +00:00
}
2009-04-20 19:26:50 +00:00
void test_hasPrefix ( ) {
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
}
2009-04-20 19:26:50 +00:00
void test_hasSuffix ( ) {
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
}
2009-04-20 19:26:50 +00:00
void test_contains ( ) {
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 ) ;
2009-06-07 13:04:03 +00:00
TS_ASSERT_EQUALS ( str . contains ( ' / ' ) , true ) ;
TS_ASSERT_EQUALS ( str . contains ( ' x ' ) , false ) ;
2007-04-15 18:27:10 +00:00
}
2009-04-20 19:26:50 +00:00
void test_toLowercase ( ) {
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
}
2009-04-20 19:26:50 +00:00
void test_toUppercase ( ) {
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
}
2008-09-02 11:32:38 +00:00
2009-04-20 19:26:50 +00:00
void test_deleteChar ( ) {
2008-09-05 20:53:30 +00:00
Common : : String str ( " 01234567890123456789012345678901 " ) ;
str . deleteChar ( 10 ) ;
2009-05-19 11:22:49 +00:00
TS_ASSERT_EQUALS ( str , " 0123456789123456789012345678901 " ) ;
2008-09-05 20:53:30 +00:00
str . deleteChar ( 10 ) ;
2009-05-19 11:22:49 +00:00
TS_ASSERT_EQUALS ( str , " 012345678923456789012345678901 " ) ;
2008-09-05 20:53:30 +00:00
}
2009-04-20 19:26:50 +00:00
void test_sharing ( ) {
2008-09-05 20:53:30 +00:00
Common : : String str ( " 01234567890123456789012345678901 " ) ;
Common : : String str2 ( str ) ;
2009-05-19 11:22:49 +00:00
TS_ASSERT_EQUALS ( str2 , " 01234567890123456789012345678901 " ) ;
2008-09-05 20:53:30 +00:00
str . deleteLastChar ( ) ;
2009-05-19 11:22:49 +00:00
TS_ASSERT_EQUALS ( str , " 0123456789012345678901234567890 " ) ;
TS_ASSERT_EQUALS ( str2 , " 01234567890123456789012345678901 " ) ;
2008-09-05 20:53:30 +00:00
}
2009-04-20 19:26:50 +00:00
void test_lastPathComponent ( ) {
2009-05-19 11:22:49 +00:00
TS_ASSERT_EQUALS ( Common : : lastPathComponent ( " / " , ' / ' ) , " " ) ;
TS_ASSERT_EQUALS ( Common : : lastPathComponent ( " /foo/bar " , ' / ' ) , " bar " ) ;
TS_ASSERT_EQUALS ( Common : : lastPathComponent ( " /foo//bar/ " , ' / ' ) , " bar " ) ;
TS_ASSERT_EQUALS ( Common : : lastPathComponent ( " /foo/./bar " , ' / ' ) , " bar " ) ;
TS_ASSERT_EQUALS ( Common : : lastPathComponent ( " /foo//./bar// " , ' / ' ) , " bar " ) ;
TS_ASSERT_EQUALS ( Common : : lastPathComponent ( " /foo//.bar// " , ' / ' ) , " .bar " ) ;
2008-09-02 11:32:38 +00:00
2009-05-19 11:22:49 +00:00
TS_ASSERT_EQUALS ( Common : : lastPathComponent ( " " , ' / ' ) , " " ) ;
TS_ASSERT_EQUALS ( Common : : lastPathComponent ( " foo/bar " , ' / ' ) , " bar " ) ;
TS_ASSERT_EQUALS ( Common : : lastPathComponent ( " foo//bar/ " , ' / ' ) , " bar " ) ;
TS_ASSERT_EQUALS ( Common : : lastPathComponent ( " foo/./bar " , ' / ' ) , " bar " ) ;
TS_ASSERT_EQUALS ( Common : : lastPathComponent ( " foo//./bar// " , ' / ' ) , " bar " ) ;
TS_ASSERT_EQUALS ( Common : : lastPathComponent ( " foo//.bar// " , ' / ' ) , " .bar " ) ;
2010-08-16 16:01:31 +00:00
TS_ASSERT_EQUALS ( Common : : lastPathComponent ( " foo " , ' / ' ) , " foo " ) ;
2008-09-02 11:32:38 +00:00
}
2009-04-20 19:26:50 +00:00
void test_normalizePath ( ) {
2009-05-19 11:22:49 +00:00
TS_ASSERT_EQUALS ( Common : : normalizePath ( " / " , ' / ' ) , " / " ) ;
TS_ASSERT_EQUALS ( Common : : normalizePath ( " /foo/bar " , ' / ' ) , " /foo/bar " ) ;
TS_ASSERT_EQUALS ( Common : : normalizePath ( " /foo//bar/ " , ' / ' ) , " /foo/bar " ) ;
TS_ASSERT_EQUALS ( Common : : normalizePath ( " /foo/./bar " , ' / ' ) , " /foo/bar " ) ;
TS_ASSERT_EQUALS ( Common : : normalizePath ( " /foo//./bar// " , ' / ' ) , " /foo/bar " ) ;
TS_ASSERT_EQUALS ( Common : : normalizePath ( " /foo//.bar// " , ' / ' ) , " /foo/.bar " ) ;
2008-09-02 11:32:38 +00:00
2009-05-19 11:22:49 +00:00
TS_ASSERT_EQUALS ( Common : : normalizePath ( " " , ' / ' ) , " " ) ;
TS_ASSERT_EQUALS ( Common : : normalizePath ( " foo/bar " , ' / ' ) , " foo/bar " ) ;
TS_ASSERT_EQUALS ( Common : : normalizePath ( " foo//bar/ " , ' / ' ) , " foo/bar " ) ;
TS_ASSERT_EQUALS ( Common : : normalizePath ( " foo/./bar " , ' / ' ) , " foo/bar " ) ;
TS_ASSERT_EQUALS ( Common : : normalizePath ( " foo//./bar// " , ' / ' ) , " foo/bar " ) ;
TS_ASSERT_EQUALS ( Common : : normalizePath ( " foo//.bar// " , ' / ' ) , " foo/.bar " ) ;
2011-06-03 15:33:07 +00:00
TS_ASSERT_EQUALS ( Common : : normalizePath ( " .. " , ' / ' ) , " .. " ) ;
TS_ASSERT_EQUALS ( Common : : normalizePath ( " ../ " , ' / ' ) , " .. " ) ;
TS_ASSERT_EQUALS ( Common : : normalizePath ( " /.. " , ' / ' ) , " /.. " ) ;
TS_ASSERT_EQUALS ( Common : : normalizePath ( " ../bar " , ' / ' ) , " ../bar " ) ;
TS_ASSERT_EQUALS ( Common : : normalizePath ( " foo//../ " , ' / ' ) , " " ) ;
TS_ASSERT_EQUALS ( Common : : normalizePath ( " foo/../bar " , ' / ' ) , " bar " ) ;
TS_ASSERT_EQUALS ( Common : : normalizePath ( " foo//../bar// " , ' / ' ) , " bar " ) ;
TS_ASSERT_EQUALS ( Common : : normalizePath ( " foo//..bar// " , ' / ' ) , " foo/..bar " ) ;
TS_ASSERT_EQUALS ( Common : : normalizePath ( " foo/../../bar// " , ' / ' ) , " ../bar " ) ;
TS_ASSERT_EQUALS ( Common : : normalizePath ( " ../foo/../bar " , ' / ' ) , " ../bar " ) ;
TS_ASSERT_EQUALS ( Common : : normalizePath ( " ../../foo/bar/ " , ' / ' ) , " ../../foo/bar " ) ;
2008-09-02 11:32:38 +00:00
}
2008-09-05 20:07:34 +00:00
2009-04-20 19:26:50 +00:00
void test_matchString ( ) {
2009-05-19 11:22:49 +00:00
TS_ASSERT ( Common : : matchString ( " " , " * " ) ) ;
TS_ASSERT ( Common : : matchString ( " a " , " * " ) ) ;
TS_ASSERT ( Common : : matchString ( " monkey.s01 " , " * " ) ) ;
2008-09-05 20:29:03 +00:00
TS_ASSERT ( ! Common : : matchString ( " " , " ? " ) ) ;
2009-05-19 11:22:49 +00:00
TS_ASSERT ( Common : : matchString ( " a " , " ? " ) ) ;
2008-09-05 20:29:03 +00:00
TS_ASSERT ( ! Common : : matchString ( " monkey.s01 " , " ? " ) ) ;
2009-05-19 11:22:49 +00:00
TS_ASSERT ( Common : : matchString ( " monkey.s01 " , " monkey.s?? " ) ) ;
TS_ASSERT ( Common : : matchString ( " monkey.s99 " , " monkey.s?? " ) ) ;
2008-09-05 20:07:34 +00:00
TS_ASSERT ( ! Common : : matchString ( " monkey.s101 " , " monkey.s?? " ) ) ;
2009-05-19 11:22:49 +00:00
TS_ASSERT ( Common : : matchString ( " monkey.s01 " , " monkey.s?1 " ) ) ;
2008-09-05 20:07:34 +00:00
TS_ASSERT ( ! Common : : matchString ( " monkey.s99 " , " monkey.s?1 " ) ) ;
TS_ASSERT ( ! Common : : matchString ( " monkey.s101 " , " monkey.s?1 " ) ) ;
2009-05-19 11:22:49 +00:00
TS_ASSERT ( Common : : matchString ( " monkey.s01 " , " monkey.s* " ) ) ;
TS_ASSERT ( Common : : matchString ( " monkey.s99 " , " monkey.s* " ) ) ;
TS_ASSERT ( Common : : matchString ( " monkey.s101 " , " monkey.s* " ) ) ;
2008-09-05 20:07:34 +00:00
2009-05-19 11:22:49 +00:00
TS_ASSERT ( Common : : matchString ( " monkey.s01 " , " monkey.s*1 " ) ) ;
2008-09-05 20:07:34 +00:00
TS_ASSERT ( ! Common : : matchString ( " monkey.s99 " , " monkey.s*1 " ) ) ;
2009-05-19 11:22:49 +00:00
TS_ASSERT ( Common : : matchString ( " monkey.s101 " , " monkey.s*1 " ) ) ;
2010-09-07 11:40:44 +00:00
TS_ASSERT ( ! Common : : String ( " " ) . matchString ( " *_ " ) ) ;
TS_ASSERT ( Common : : String ( " a " ) . matchString ( " a*** " ) ) ;
2008-09-05 20:07:34 +00:00
}
2009-07-25 10:25:57 +00:00
void test_string_printf ( ) {
2010-11-01 16:02:28 +00:00
TS_ASSERT_EQUALS ( Common : : String : : format ( " " ) , " " ) ;
TS_ASSERT_EQUALS ( Common : : String : : format ( " %s " , " test " ) , " test " ) ;
TS_ASSERT_EQUALS ( Common : : String : : format ( " %s.s%.02d " , " monkey " , 1 ) , " monkey.s01 " ) ;
TS_ASSERT_EQUALS ( Common : : String : : format ( " Some %s to make this string longer than the default built-in %s %d " , " text " , " capacity " , 123456 ) , " Some text to make this string longer than the default built-in capacity 123456 " ) ;
2009-10-12 11:54:32 +00:00
2010-11-01 16:02:28 +00:00
Common : : String s = Common : : String : : format ( " %s%X " , " test " , 1234 ) ;
2010-10-10 14:40:45 +00:00
TS_ASSERT_EQUALS ( s , " test4D2 " ) ;
TS_ASSERT_EQUALS ( s . size ( ) , 7U ) ;
2010-05-05 17:52:59 +00:00
}
void test_strlcpy ( ) {
static const char * const testString = " 1234567890 " ;
char test1 [ 4 ] ;
TS_ASSERT_EQUALS ( Common : : strlcpy ( test1 , testString , 4 ) , strlen ( testString ) ) ;
TS_ASSERT_EQUALS ( strcmp ( test1 , " 123 " ) , 0 ) ;
char test2 [ 12 ] ;
test2 [ 11 ] = ' X ' ;
TS_ASSERT_EQUALS ( Common : : strlcpy ( test2 , testString , 11 ) , strlen ( testString ) ) ;
TS_ASSERT_EQUALS ( strcmp ( test2 , testString ) , 0 ) ;
TS_ASSERT_EQUALS ( test2 [ 11 ] , ' X ' ) ;
char test3 [ 1 ] = { ' X ' } ;
TS_ASSERT_EQUALS ( Common : : strlcpy ( test3 , testString , 0 ) , strlen ( testString ) ) ;
TS_ASSERT_EQUALS ( test3 [ 0 ] , ' X ' ) ;
char test4 [ 12 ] ;
TS_ASSERT_EQUALS ( Common : : strlcpy ( test4 , testString , 12 ) , strlen ( testString ) ) ;
TS_ASSERT_EQUALS ( strcmp ( test4 , testString ) , 0 ) ;
}
2009-10-12 11:54:32 +00:00
2010-05-05 17:54:34 +00:00
void test_strlcat ( ) {
2010-05-05 17:52:59 +00:00
static const char * const initialString = " 123 " ;
static const char * const appendString = " 4567890 " ;
static const char * const resultString = " 1234567890 " ;
char test1 [ 4 ] ;
TS_ASSERT_EQUALS ( Common : : strlcpy ( test1 , initialString , 4 ) , strlen ( initialString ) ) ;
TS_ASSERT_EQUALS ( strcmp ( test1 , initialString ) , 0 ) ;
TS_ASSERT_EQUALS ( Common : : strlcat ( test1 , appendString , 4 ) , strlen ( resultString ) ) ;
TS_ASSERT_EQUALS ( strcmp ( test1 , initialString ) , 0 ) ;
char test2 [ 12 ] ;
test2 [ 11 ] = ' X ' ;
TS_ASSERT_EQUALS ( Common : : strlcpy ( test2 , initialString , 11 ) , strlen ( initialString ) ) ;
TS_ASSERT_EQUALS ( strcmp ( test2 , initialString ) , 0 ) ;
TS_ASSERT_EQUALS ( Common : : strlcat ( test2 , appendString , 11 ) , strlen ( resultString ) ) ;
TS_ASSERT_EQUALS ( strcmp ( test2 , resultString ) , 0 ) ;
TS_ASSERT_EQUALS ( test2 [ 11 ] , ' X ' ) ;
char test3 [ 1 ] ;
test3 [ 0 ] = ' X ' ;
TS_ASSERT_EQUALS ( Common : : strlcat ( test3 , appendString , 0 ) , strlen ( appendString ) ) ;
TS_ASSERT_EQUALS ( test3 [ 0 ] , ' X ' ) ;
char test4 [ 11 ] ;
TS_ASSERT_EQUALS ( Common : : strlcpy ( test4 , initialString , 11 ) , strlen ( initialString ) ) ;
TS_ASSERT_EQUALS ( strcmp ( test4 , initialString ) , 0 ) ;
TS_ASSERT_EQUALS ( Common : : strlcat ( test4 , appendString , 11 ) , strlen ( resultString ) ) ;
TS_ASSERT_EQUALS ( strcmp ( test4 , resultString ) , 0 ) ;
2009-07-25 10:25:57 +00:00
}
2011-05-23 16:32:42 +00:00
void test_scumm_stricmp ( ) {
TS_ASSERT_EQUALS ( scumm_stricmp ( " abCd " , " abCd " ) , 0 ) ;
TS_ASSERT_EQUALS ( scumm_stricmp ( " abCd " , " ABCd " ) , 0 ) ;
TS_ASSERT_LESS_THAN ( scumm_stricmp ( " abCd " , " ABCe " ) , 0 ) ;
TS_ASSERT_LESS_THAN ( scumm_stricmp ( " abCd " , " ABCde " ) , 0 ) ;
}
void test_scumm_strnicmp ( ) {
TS_ASSERT_EQUALS ( scumm_strnicmp ( " abCd " , " abCd " , 3 ) , 0 ) ;
TS_ASSERT_EQUALS ( scumm_strnicmp ( " abCd " , " ABCd " , 4 ) , 0 ) ;
TS_ASSERT_EQUALS ( scumm_strnicmp ( " abCd " , " ABCd " , 5 ) , 0 ) ;
TS_ASSERT_EQUALS ( scumm_strnicmp ( " abCd " , " ABCe " , 3 ) , 0 ) ;
TS_ASSERT_LESS_THAN ( scumm_strnicmp ( " abCd " , " ABCe " , 4 ) , 0 ) ;
TS_ASSERT_EQUALS ( scumm_strnicmp ( " abCd " , " ABCde " , 4 ) , 0 ) ;
TS_ASSERT_LESS_THAN ( scumm_strnicmp ( " abCd " , " ABCde " , 5 ) , 0 ) ;
}
2003-12-24 16:16:00 +00:00
} ;