2003-12-24 16:16:00 +00:00
# include <cxxtest/TestSuite.h>
# include "common/str.h"
2020-09-02 20:14:07 +01:00
# include "common/ustr.h"
2003-12-24 16:16:00 +00:00
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
}
2019-04-02 14:33:31 +02:00
void test_firstChar ( ) {
Common : : String str ;
TS_ASSERT_EQUALS ( str . firstChar ( ) , ' \0 ' ) ;
str = " first_test " ;
TS_ASSERT_EQUALS ( str . firstChar ( ) , ' f ' ) ;
Common : : String str2 ( " bar " ) ;
TS_ASSERT_EQUALS ( str2 . firstChar ( ) , ' b ' ) ;
}
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
}
2013-06-19 17:27:05 -05:00
void test_erase ( ) {
Common : : String str ( " 01234567890123456789012345678901 " ) ;
str . erase ( 18 ) ;
TS_ASSERT_EQUALS ( str , " 012345678901234567 " ) ;
str . erase ( 7 , 5 ) ;
TS_ASSERT_EQUALS ( str , " 0123456234567 " ) ;
}
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 17:33:07 +02: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
2016-01-26 02:20:52 +01:00
TS_ASSERT ( Common : : matchString ( " monkey.s01 " , " monkey.s## " ) ) ;
TS_ASSERT ( ! Common : : matchString ( " monkey.s01 " , " monkey.### " ) ) ;
2019-02-21 16:36:01 -08:00
TS_ASSERT ( Common : : matchString ( " monkey.s0# " , " monkey.s0 \\ # " ) ) ;
TS_ASSERT ( ! Common : : matchString ( " monkey.s0# " , " monkey.s0# " ) ) ;
TS_ASSERT ( ! Common : : matchString ( " monkey.s01 " , " monkey.s0 \\ # " ) ) ;
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 ( ) {
2017-11-20 21:46:37 -06:00
TS_ASSERT_EQUALS ( Common : : String : : format ( " " ) , " " ) ;
2010-11-01 16:02:28 +00:00
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
}
2020-09-12 11:53:03 +01:00
void test_ustring_printf ( ) {
//Ideally should be the same as above (make String template?)
TS_ASSERT_EQUALS ( Common : : U32String : : format ( " " ) . encode ( ) , " " ) ;
TS_ASSERT_EQUALS ( Common : : U32String : : format ( " %s " , " test " ) . encode ( ) , " test " ) ;
TS_ASSERT_EQUALS ( Common : : U32String : : format ( " %s%c%s " , " Press " , ' X ' , " to win " ) . encode ( ) , " Press X to win " ) ;
TS_ASSERT_EQUALS ( Common : : U32String : : format ( " Some %s to make this string longer than the default built-in %s %d " , " text " , " capacity " , 123456 ) . encode ( ) , " Some text to make this string longer than the default built-in capacity 123456 " ) ;
2022-11-15 10:59:51 +00:00
TS_ASSERT_EQUALS ( Common : : U32String : : format ( " %u " , 0 ) . encode ( ) , " 0 " ) ;
TS_ASSERT_EQUALS ( Common : : U32String : : format ( " %u " , 1234 ) . encode ( ) , " 1234 " ) ;
TS_ASSERT_EQUALS ( Common : : U32String : : format ( " %d " , 0 ) . encode ( ) , " 0 " ) ;
TS_ASSERT_EQUALS ( Common : : U32String : : format ( " %d " , 1234 ) . encode ( ) , " 1234 " ) ;
TS_ASSERT_EQUALS ( Common : : U32String : : format ( " %d " , - 1234 ) . encode ( ) , " -1234 " ) ;
2022-11-15 18:42:29 +00:00
TS_ASSERT_EQUALS ( Common : : U32String : : format ( " %u %% " , 100 ) . encode ( ) , " 100 % " ) ;
2020-09-12 11:53:03 +01:00
}
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 18:32:42 +02:00
2016-11-26 12:56:25 -06:00
void test_strnlen ( ) {
static const char * const testString = " 123 " ;
2017-01-05 22:06:25 +01:00
TS_ASSERT_EQUALS ( Common : : strnlen ( testString , 0 ) , 0u ) ;
TS_ASSERT_EQUALS ( Common : : strnlen ( testString , 1 ) , 1u ) ;
TS_ASSERT_EQUALS ( Common : : strnlen ( testString , 2 ) , 2u ) ;
TS_ASSERT_EQUALS ( Common : : strnlen ( testString , 3 ) , 3u ) ;
TS_ASSERT_EQUALS ( Common : : strnlen ( testString , 4 ) , 3u ) ;
2016-11-26 12:56:25 -06:00
const char testArray [ 4 ] = { ' 1 ' , ' 2 ' , ' 3 ' , ' 4 ' } ;
2017-01-05 22:06:25 +01:00
TS_ASSERT_EQUALS ( Common : : strnlen ( testArray , 0 ) , 0u ) ;
TS_ASSERT_EQUALS ( Common : : strnlen ( testArray , 1 ) , 1u ) ;
TS_ASSERT_EQUALS ( Common : : strnlen ( testArray , 2 ) , 2u ) ;
TS_ASSERT_EQUALS ( Common : : strnlen ( testArray , 3 ) , 3u ) ;
TS_ASSERT_EQUALS ( Common : : strnlen ( testArray , 4 ) , 4u ) ;
2016-11-26 12:56:25 -06:00
const char testArray2 [ 4 ] = { ' 1 ' , ' \0 ' , ' 3 ' , ' 4 ' } ;
2017-01-05 22:06:25 +01:00
TS_ASSERT_EQUALS ( Common : : strnlen ( testArray2 , 0 ) , 0u ) ;
TS_ASSERT_EQUALS ( Common : : strnlen ( testArray2 , 1 ) , 1u ) ;
TS_ASSERT_EQUALS ( Common : : strnlen ( testArray2 , 2 ) , 1u ) ;
TS_ASSERT_EQUALS ( Common : : strnlen ( testArray2 , 3 ) , 1u ) ;
TS_ASSERT_EQUALS ( Common : : strnlen ( testArray2 , 4 ) , 1u ) ;
2016-11-26 12:56:25 -06:00
}
2011-05-23 18:32:42 +02: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 ) ;
}
2016-03-02 17:07:50 +01:00
2017-10-07 22:01:13 -05:00
void test_wordWrap ( ) {
Common : : String testString ( " 123456 " ) ;
testString . wordWrap ( 10 ) ;
TS_ASSERT ( testString = = " 123456 " ) ;
testString . wordWrap ( 2 ) ;
TS_ASSERT ( testString = = " 12 \n 34 \n 56 " ) ;
testString = " 1234 5678 " ;
testString . wordWrap ( 4 ) ;
TS_ASSERT ( testString = = " 1234 \n 5678 " ) ;
testString = " 12 3 45 " ;
testString . wordWrap ( 4 ) ;
TS_ASSERT ( testString = = " 12 3 \n 45 " ) ;
testString = " \n 1 \n 23 45 \n \n " ;
testString . wordWrap ( 3 ) ;
TS_ASSERT ( testString = = " \n 1 \n 23 \n 45 \n \n " ) ;
testString = " 123 " ;
testString . wordWrap ( 4 ) ;
TS_ASSERT ( testString = = " 123 " ) ;
testString . wordWrap ( 3 ) ;
TS_ASSERT ( testString = = " 123 \n " ) ;
}
2016-03-02 17:07:50 +01:00
void test_replace ( ) {
// Tests created with the results of the STL std::string class
// --------------------------
// Tests without displacement
// --------------------------
Common : : String testString = Common : : String ( " This is the original string. " ) ;
// Positions and sizes as parameters, string as replacement
testString . replace ( 12 , 8 , Common : : String ( " newnewne " ) ) ;
TS_ASSERT_EQUALS ( testString , Common : : String ( " This is the newnewne string. " ) ) ;
// The same but with char*
testString . replace ( 0 , 4 , " That " ) ;
TS_ASSERT_EQUALS ( testString , Common : : String ( " That is the newnewne string. " ) ) ;
// Using iterators (also a terribly useless program as a test).
testString . replace ( testString . begin ( ) , testString . end ( ) , " That is the supernew string. " ) ;
2016-10-09 15:02:02 +02:00
TS_ASSERT_EQUALS ( testString , Common : : String ( " That is the supernew string. " ) ) ;
2016-03-02 17:07:50 +01:00
// With sub strings of character arrays.
testString . replace ( 21 , 6 , " That phrase is new. " , 5 , 6 ) ;
TS_ASSERT_EQUALS ( testString , Common : : String ( " That is the supernew phrase. " ) ) ;
2016-10-09 15:02:02 +02:00
// Now with substrings.
2016-03-02 17:07:50 +01:00
testString . replace ( 12 , 2 , Common : : String ( " That hy is new. " ) , 5 , 2 ) ;
TS_ASSERT_EQUALS ( testString , Common : : String ( " That is the hypernew phrase. " ) ) ;
2016-10-09 15:02:02 +02:00
2016-03-02 17:07:50 +01:00
// --------------------------
// Tests with displacement
// --------------------------
testString = Common : : String ( " Hello World " ) ;
// Positions and sizes as parameters, string as replacement
testString . replace ( 6 , 5 , Common : : String ( " friends " ) ) ;
TS_ASSERT_EQUALS ( testString , Common : : String ( " Hello friends " ) ) ;
// The same but with char*
testString . replace ( 0 , 5 , " Good " ) ;
TS_ASSERT_EQUALS ( testString , Common : : String ( " Good friends " ) ) ;
// Using iterators (also a terribly useless program as a test)
testString . replace ( testString . begin ( ) + 4 , testString . begin ( ) + 5 , " coffee " ) ;
2016-10-09 15:02:02 +02:00
TS_ASSERT_EQUALS ( testString , Common : : String ( " Good coffee friends " ) ) ;
2016-03-02 17:07:50 +01:00
// With sub strings of character arrays
testString . replace ( 4 , 0 , " Lorem ipsum expresso dolor sit amet " , 11 , 9 ) ;
TS_ASSERT_EQUALS ( testString , Common : : String ( " Good expresso coffee friends " ) ) ;
2016-10-09 15:02:02 +02:00
// Now with substrings
2016-03-02 17:07:50 +01:00
testString . replace ( 5 , 9 , Common : : String ( " Displaced ristretto string " ) , 10 , 10 ) ;
TS_ASSERT_EQUALS ( testString , Common : : String ( " Good ristretto coffee friends " ) ) ;
2016-10-09 15:02:02 +02:00
2021-04-15 21:20:04 +02:00
// -----------------------
// Deep copy compliance
// -----------------------
// Makes a deep copy without changing the length of the original
Common : : String s1 = " TestTestTestTestTestTestTestTestTestTestTest " ;
Common : : String s2 ( s1 ) ;
TS_ASSERT_EQUALS ( s1 , " TestTestTestTestTestTestTestTestTestTestTest " ) ;
TS_ASSERT_EQUALS ( s2 , " TestTestTestTestTestTestTestTestTestTestTest " ) ;
s1 . replace ( 0 , 4 , " TEST " ) ;
TS_ASSERT_EQUALS ( s1 , " TESTTestTestTestTestTestTestTestTestTestTest " ) ;
TS_ASSERT_EQUALS ( s2 , " TestTestTestTestTestTestTestTestTestTestTest " ) ;
// Makes a deep copy when we shorten the string
Common : : String s3 = " TestTestTestTestTestTestTestTestTestTestTest " ;
2016-03-02 17:07:50 +01:00
Common : : String s4 ( s3 ) ;
s3 . replace ( 0 , 32 , " " ) ;
TS_ASSERT_EQUALS ( s3 , " TestTestTest " ) ;
TS_ASSERT_EQUALS ( s4 , " TestTestTestTestTestTestTestTestTestTestTest " ) ;
}
2019-04-02 14:33:31 +02:00
2022-11-22 22:55:59 +01:00
void test_find ( ) {
Common : : String a ( " 0123012 " ) , b ;
TS_ASSERT_EQUALS ( a . find ( ' 1 ' ) , 1u ) ;
TS_ASSERT_EQUALS ( a . find ( ' 3 ' ) , 3u ) ;
TS_ASSERT_EQUALS ( a . find ( ' 1 ' , 3 ) , 5u ) ;
TS_ASSERT_EQUALS ( b . find ( ' * ' ) , Common : : String : : npos ) ;
TS_ASSERT_EQUALS ( b . find ( ' * ' , 1 ) , Common : : String : : npos ) ;
TS_ASSERT_EQUALS ( a . rfind ( ' 1 ' ) , 5u ) ;
TS_ASSERT_EQUALS ( a . rfind ( ' 3 ' ) , 3u ) ;
TS_ASSERT_EQUALS ( a . rfind ( ' 1 ' , 3 ) , 1u ) ;
TS_ASSERT_EQUALS ( b . rfind ( ' * ' ) , Common : : String : : npos ) ;
TS_ASSERT_EQUALS ( b . rfind ( ' * ' , 1 ) , Common : : String : : npos ) ;
}
2019-04-02 14:33:31 +02:00
void test_setChar ( ) {
Common : : String testString ( " 123456 " ) ;
testString . setChar ( ' 2 ' , 0 ) ;
TS_ASSERT ( testString = = " 223456 " ) ;
testString . setChar ( ' 0 ' , 5 ) ;
TS_ASSERT ( testString = = " 223450 " ) ;
}
void test_insertChar ( ) {
Common : : String testString ( " 123456 " ) ;
testString . insertChar ( ' 2 ' , 0 ) ;
TS_ASSERT ( testString = = " 2123456 " ) ;
testString . insertChar ( ' 0 ' , 5 ) ;
TS_ASSERT ( testString = = " 21234056 " ) ;
}
2020-09-02 20:14:07 +01:00
void test_comparison ( ) {
Common : : String a ( " 0123 " ) , ax ( " 01234 " ) , b ( " 0124 " ) , e ;
TS_ASSERT_EQUALS ( a , a ) ;
TS_ASSERT_EQUALS ( ax , ax ) ;
TS_ASSERT_EQUALS ( b , b ) ;
TS_ASSERT_EQUALS ( e , e ) ;
TS_ASSERT_DIFFERS ( a , ax ) ;
TS_ASSERT_DIFFERS ( a , b ) ;
TS_ASSERT_DIFFERS ( a , e ) ;
TS_ASSERT_DIFFERS ( ax , b ) ;
TS_ASSERT_DIFFERS ( ax , e ) ;
TS_ASSERT_DIFFERS ( b , ax ) ;
TS_ASSERT_DIFFERS ( b , e ) ;
TS_ASSERT_LESS_THAN ( e , a ) ;
TS_ASSERT_LESS_THAN ( e , ax ) ;
TS_ASSERT_LESS_THAN ( e , b ) ;
TS_ASSERT_LESS_THAN ( a , ax ) ;
TS_ASSERT_LESS_THAN ( a , b ) ;
TS_ASSERT_LESS_THAN ( ax , b ) ;
}
void test_ustr_comparison ( ) {
Common : : U32String a ( " abc " ) , b ( " abd " ) ;
TS_ASSERT_EQUALS ( a , a ) ;
TS_ASSERT_EQUALS ( b , b ) ;
TS_ASSERT_DIFFERS ( a , b ) ;
TS_ASSERT_LESS_THAN ( a , b ) ;
TS_ASSERT_LESS_THAN_EQUALS ( a , b ) ;
TS_ASSERT_LESS_THAN_EQUALS ( a , a ) ;
TS_ASSERT_LESS_THAN_EQUALS ( b , b ) ;
//U32String does not define compare, so test both sides
TS_ASSERT ( a > = a ) ;
TS_ASSERT ( b > a ) ;
TS_ASSERT ( b > = b ) ;
TS_ASSERT ( b > = a ) ;
}
2003-12-24 16:16:00 +00:00
} ;