2020-08-17 21:40:52 +02:00
# include <Platform.h> // Only use angled for Platform, else, xcode project won't compile
2020-03-12 15:40:38 +01:00
# include "../cpp_foundation/XString.h"
2020-04-09 17:46:17 +02:00
# include "../cpp_foundation/unicode_conversions.h"
2020-03-12 15:40:38 +01:00
2020-04-23 15:20:48 +02:00
2020-04-30 23:43:44 +02:00
static int nbTest = 0 ;
static int nbTestFailed = 0 ;
static bool displayOnlyFailed = true ;
2020-05-03 17:07:34 +02:00
static bool displayMainTitle = false ;
2020-04-23 15:20:48 +02:00
# define STRINGIFY_(s) #s
# define STRINGIFY(s) STRINGIFY_(s)
# define CONCATENATE(e1, e2) e1 ## e2
# define PREFIX_L(s) CONCATENATE(L, s)
# define PREFIX_u(s) CONCATENATE(u, s)
# define PREFIX_U(s) CONCATENATE(U, s)
template < class _Tp >
struct make_unsigned
2020-03-31 17:59:35 +02:00
{
} ;
2020-03-12 15:40:38 +01:00
2020-04-23 22:43:35 +02:00
template < > struct make_unsigned < char > { typedef unsigned char type ; } ;
template < > struct make_unsigned < signed char > { typedef unsigned char type ; } ;
template < > struct make_unsigned < unsigned char > { typedef unsigned char type ; } ;
template < > struct make_unsigned < signed short > { typedef unsigned short type ; } ;
template < > struct make_unsigned < unsigned short > { typedef unsigned short type ; } ;
template < > struct make_unsigned < signed int > { typedef unsigned int type ; } ;
template < > struct make_unsigned < unsigned int > { typedef unsigned int type ; } ;
template < > struct make_unsigned < signed long > { typedef unsigned long type ; } ;
template < > struct make_unsigned < unsigned long > { typedef unsigned long type ; } ;
2020-04-23 15:20:48 +02:00
template < > struct make_unsigned < signed long long > { typedef unsigned long long type ; } ;
template < > struct make_unsigned < unsigned long long > { typedef unsigned long long type ; } ;
/*
* Set a breakpoint here to catch failed test under debugger
*/
void breakpoint ( )
2020-03-12 15:40:38 +01:00
{
2020-04-23 15:20:48 +02:00
int a ;
( void ) a ;
}
class SimpleString
{
char * data ;
size_t allocatedSize ;
public :
2020-08-11 14:43:53 +02:00
SimpleString ( ) : data ( NULL ) , allocatedSize ( 0 ) { }
SimpleString ( const SimpleString & simpleString ) : data ( NULL ) , allocatedSize ( 0 ) {
2020-04-23 15:20:48 +02:00
allocatedSize = strlen ( simpleString . data ) + 1 ;
data = ( char * ) malloc ( allocatedSize ) ;
strcpy ( data , simpleString . data ) ;
}
2020-08-11 14:43:53 +02:00
SimpleString ( const char * s ) : data ( NULL ) , allocatedSize ( 0 ) {
2020-04-23 15:20:48 +02:00
allocatedSize = strlen ( s ) + 1 ;
data = ( char * ) malloc ( allocatedSize ) ;
strcpy ( data , s ) ;
}
2020-08-11 14:43:53 +02:00
SimpleString ( const char16_t * s ) : data ( NULL ) , allocatedSize ( 0 ) {
2020-04-23 15:20:48 +02:00
# if defined(__GNUC__) && !defined(__clang__)
data = 0 ; // silence warning
# endif
allocatedSize = utf_size_of_utf_string ( data , s ) + 1 ;
data = ( char * ) malloc ( allocatedSize ) ;
utf_string_from_utf_string ( data , allocatedSize , s ) ;
}
2020-08-11 14:43:53 +02:00
SimpleString ( const char32_t * s ) : data ( NULL ) , allocatedSize ( 0 ) {
2020-04-23 15:20:48 +02:00
# if defined(__GNUC__) && !defined(__clang__)
data = 0 ; // silence warning
# endif
allocatedSize = utf_size_of_utf_string ( data , s ) + 1 ;
data = ( char * ) malloc ( allocatedSize ) ;
utf_string_from_utf_string ( data , allocatedSize , s ) ;
}
2020-08-11 14:43:53 +02:00
SimpleString ( const wchar_t * s ) : data ( NULL ) , allocatedSize ( 0 ) {
2020-04-23 15:20:48 +02:00
# if defined(__GNUC__) && !defined(__clang__)
data = 0 ; // silence warning
# endif
allocatedSize = utf_size_of_utf_string ( data , s ) + 1 ;
data = ( char * ) malloc ( allocatedSize ) ;
utf_string_from_utf_string ( data , allocatedSize , s ) ;
}
SimpleString & operator = ( const SimpleString & simpleString )
{
size_t newSize = strlen ( simpleString . data ) + 1 ;
2020-04-24 08:36:29 +02:00
data = ( char * ) Xrealloc ( data , newSize + 1 , allocatedSize ) ;
2020-04-23 15:20:48 +02:00
allocatedSize = newSize + 1 ;
strncpy ( data , simpleString . data , allocatedSize ) ;
return * this ;
}
const char * c_str ( ) const { return data ; }
SimpleString & svprintf ( const char * format , va_list va )
{
VA_LIST va2 ;
VA_COPY ( va2 , va ) ;
size_t size = ( size_t ) vsnprintf ( NULL , 0 , format , va ) ;
2020-04-24 08:36:29 +02:00
data = ( char * ) Xrealloc ( data , size + 1 , allocatedSize ) ;
2020-04-23 15:20:48 +02:00
allocatedSize = size + 1 ;
data [ size ] = 0 ;
vsnprintf ( data , allocatedSize , format , va2 ) ;
VA_END ( va ) ;
return * this ;
}
~ SimpleString ( ) { delete data ; }
} ;
2020-04-24 11:30:09 +02:00
/* ssprintf = SimpleStringprintf */
SimpleString ssprintf ( const char * format , . . . ) __attribute__ ( ( __format__ ( __printf__ , 1 , 2 ) ) ) ;
SimpleString ssprintf ( const char * format , . . . )
2020-04-23 15:20:48 +02:00
{
SimpleString ss ;
va_list va ;
va_start ( va , format ) ;
ss . svprintf ( format , va ) ;
va_end ( va ) ;
return ss ;
}
// ""
// Don't put any "" in test strings or it will break indexOf and rindexOf tests.
# define utf8_1 "Āࠀ𐀀🧊Выход'UTF16'из"
# define utf8_2 "൧൨൩൪൫൬൭൮"
# define utf8_3 "éàùœ°æƒÌÚ®"
# define utf8_4 "ﰨﰩﰪﰫﰬﰭﰮﰯﰰﰱﰲﰳ"
# define utf8_5 "ォオカガキギクグケゲ"
# define utf8_6 "ꇆꇇꇈꇉꇊꇋꇌꇍ"
# define utf8_7 "伻似伽伾伿佀佁佂佃佄"
# define utf8_8 "楔楕楖楗楘楙楚楛楜楝楞楟楠楡"
//#define utf8_9 "abcdefghijklmnopqrstuvwxyzàáâãäåæçèéêëìíîïðñòóôõöøùúûüýþāăąćĉċčďđēĕėęěĝğġģĥħĩīĭįijĵķĺļľŀłńņňŋōŏőœŕŗřśŝşšţťŧũūŭůűųŵŷÿźżžɓƃƅɔƈɖɗƌǝəɛƒɠɣ ɩ ɨƙɯ ɲɵơƣƥƨʃƭʈưʊʋ ƴƶʒƹƽ dždžljljnjnjǎǐǒǔǖǘ"
//#define utf8_10 "ἯἸἹἺἻἼἽἾἿὈὉὊὋὌὍὙὛὝὟὨὩὪὫὬὭὮὯάέήίόύώᾈᾉᾊᾋᾌᾍᾎᾏᾘᾙᾚᾛᾜᾝᾞᾟᾨᾩᾪᾫᾬᾭᾮᾯᾸᾹᾺΆᾼιῈΈῊΉῌΐῘῙῚΊΰῨῩῪΎῬ΅`ῸΌῺΏῼ´ ΩKÅℲⅠⅡⅢⅣⅤⅥⅦⅧⅨⅩⅪⅫⅬⅭⅮⅯↃⒶⒷⒸⒹⒺⒻⒼⒽⒾⒿⓀⓁⓂⓃⓄⓅⓆⓇⓈⓉⓊⓋⓌⓍⓎⓏⰀⰁⰂⰃⰄⰅⰆⰇⰈⰉⰊⰋⰌⰍⰎⰏⰐⰑⰒⰓⰔⰕⰖⰗⰘⰙⰚⰛⰜⰝⰞⰟⰠⰡⰢⰣⰤⰥⰦⰧⰨⰩⰪⰫⰬⰭⰮⱠⱢⱣⱤⱧⱩⱫⱭⱮⱯⱰⱲⱵⱾⱿⲀⲂⲄⲆⲈⲊⲌⲎⲐⲒⲔⲖⲘⲚⲜⲞⲠⲢⲤⲦⲨⲪⲬⲮⲰⲲⲴⲶⲸⲺⲼⲾⳀⳂⳄⳆⳈⳊⳌⳎⳐⳒⳔⳖⳘⳚⳜⳞⳠⳢⳫⳭⳲ〈〉Ꙁ"
//#define utf8_11 "煉璉秊練聯輦蓮連鍊列劣咽烈裂說廉念捻殮簾獵令囹寧嶺怜玲瑩羚聆鈴零靈領例禮醴隸惡了僚寮尿料樂燎療蓼遼龍暈阮劉杻柳流溜琉留硫紐類六戮陸倫崙淪輪律慄栗率隆利吏履易李梨泥理痢罹裏裡里離匿溺吝燐璘藺隣鱗麟林淋臨立笠粒狀炙識什茶刺切度拓糖宅洞暴輻行降見廓兀嗀塚晴凞猪益礼神祥福靖精羽蘒諸逸都飯飼館鶴郞隷侮僧免勉勤卑喝嘆器塀墨層屮悔慨憎懲敏既暑梅海渚漢煮爫琢碑社祉祈祐祖祝禍禎穀突節練縉繁署者臭艹艹著褐視謁謹賓贈辶逸難響頻恵舘並况全侀充冀勇勺喝啕喙嗢塚墳奄奔婢嬨廒廙彩徭惘慎愈憎慠懲戴揄搜摒敖晴朗望杖歹殺流滛滋漢瀞煮瞧爵犯猪"
struct AbstractTestString
{
size_t size ; // size in nb of char (or char16_t, char32_t or wchar_t) not bytes, not including null termiator
size_t utf32_length ; // length in nb of UTF32 char, not including null termiator
//size_t utf32_size; // size == length for UTF32
const char32_t * utf32 ;
AbstractTestString ( size_t _size , size_t _utf32_length , const char32_t * _utf32 )
: size ( _size ) , utf32_length ( _utf32_length ) , utf32 ( _utf32 ) {
}
} ;
template < typename CharType >
struct TestString : public AbstractTestString
{
const CharType * cha ;
TestString ( size_t _size , const CharType * _cha , size_t _utf32_length , const char32_t * _utf32 ) : AbstractTestString ( _size , _utf32_length , _utf32 ) , cha ( _cha ) {
}
2020-04-26 12:12:05 +02:00
size_t getSizeInBytes ( ) const { return size * sizeof ( CharType ) ; }
size_t getSizeInBytesIncludingTerminator ( ) const { return ( size + 1 ) * sizeof ( CharType ) ; }
2020-04-23 15:20:48 +02:00
} ;
struct TestStringMultiCoded
{
TestString < char > utf8 ;
TestString < char16_t > utf16 ;
TestString < char32_t > utf32 ;
TestString < wchar_t > wchar ;
} ;
# define nbchar(s) (sizeof(s) / sizeof(*s)-1)
2020-04-25 13:33:01 +02:00
# define testStringArray_LINE(utf) \
2020-04-23 15:20:48 +02:00
TestString < char > ( nbchar ( utf ) , utf , nbchar ( PREFIX_U ( utf ) ) , PREFIX_U ( utf ) ) , \
TestString < char16_t > ( nbchar ( PREFIX_u ( utf ) ) , PREFIX_u ( utf ) , nbchar ( PREFIX_U ( utf ) ) , PREFIX_U ( utf ) ) , \
TestString < char32_t > ( nbchar ( PREFIX_U ( utf ) ) , PREFIX_U ( utf ) , nbchar ( PREFIX_U ( utf ) ) , PREFIX_U ( utf ) ) , \
TestString < wchar_t > ( nbchar ( PREFIX_L ( utf ) ) , PREFIX_L ( utf ) , nbchar ( PREFIX_U ( utf ) ) , PREFIX_U ( utf ) ) , \
2020-04-25 13:33:01 +02:00
struct TestStringMultiCodedAndExpectedResult
{
TestString < char > utf8 ;
TestString < char16_t > utf16 ;
TestString < char32_t > utf32 ;
TestString < wchar_t > wchar ;
TestString < char > utf8_expectedResult ;
TestString < char16_t > utf16_expectedResult ;
TestString < char32_t > utf32_expectedResult ;
TestString < wchar_t > wchar_expectedResult ;
} ;
# define testStringExpectedArray_LINE(utf, expected) \
testStringArray_LINE ( utf ) testStringArray_LINE ( expected )
2020-04-23 15:20:48 +02:00
//TestString<char> foo("", "utf8", 1, "a", 1, U"a");
const TestStringMultiCoded testStringMultiCodedArray [ ] = {
2020-04-25 13:33:01 +02:00
{ testStringArray_LINE ( " " ) } ,
{ testStringArray_LINE ( " a " ) } ,
2020-04-23 22:43:35 +02:00
# ifndef _MSC_VER
2020-04-25 13:33:01 +02:00
{ testStringArray_LINE ( utf8_1 ) } ,
{ testStringArray_LINE ( utf8_2 ) } ,
{ testStringArray_LINE ( utf8_3 ) } ,
{ testStringArray_LINE ( utf8_4 ) } ,
{ testStringArray_LINE ( utf8_5 ) } ,
{ testStringArray_LINE ( utf8_6 ) } ,
{ testStringArray_LINE ( utf8_7 ) } ,
{ testStringArray_LINE ( utf8_8 ) } ,
// { testStringArray_LINE(utf8_9) },
// { testStringArray_LINE(utf8_10) },
// { testStringArray_LINE(utf8_11) },
2020-04-23 22:43:35 +02:00
# endif
2020-04-23 15:20:48 +02:00
} ;
size_t nbTestStringMultiCoded = * ( & testStringMultiCodedArray + 1 ) - testStringMultiCodedArray ;
const TestStringMultiCoded testStringMultiCoded4CaseArray [ ] = {
2020-04-25 13:33:01 +02:00
{ testStringArray_LINE ( " A൧൨BCギDEизF " ) } ,
{ testStringArray_LINE ( " abc聯輦deFGꇊHIjklmn " ) } ,
2020-04-23 15:20:48 +02:00
} ;
size_t nbTestStringMultiCoded4CaseArray = * ( & testStringMultiCoded4CaseArray + 1 ) - testStringMultiCoded4CaseArray ;
2020-04-25 13:33:01 +02:00
const TestStringMultiCodedAndExpectedResult testStringMultiCoded4TrimArray [ ] = {
{ testStringExpectedArray_LINE ( " A൧൨BCギDEизF " , " A൧൨BCギDEизF " ) } ,
{ testStringExpectedArray_LINE ( " A൧൨BCギDEизF " , " A൧൨BCギDEизF " ) } ,
{ testStringExpectedArray_LINE ( " A൧൨BCギDEизF " , " A൧൨BCギDEизF " ) } ,
{ testStringExpectedArray_LINE ( " \1 \31 abc聯輦deFGꇊHIjklmn \1 \31 " , " abc聯輦deFGꇊHIjklmn " ) } ,
} ;
size_t nbTestStringMultiCoded4TrimArray = * ( & testStringMultiCoded4TrimArray + 1 ) - testStringMultiCoded4TrimArray ;
2020-04-23 15:20:48 +02:00
2020-04-27 11:50:49 +02:00
const TestStringMultiCodedAndExpectedResult testStringMultiCoded4BasenameArray [ ] = {
{ testStringExpectedArray_LINE ( " A൧൨BC/ギDEизF " , " ギDEизF " ) } ,
{ testStringExpectedArray_LINE ( " A൧൨BC//ギDEизF " , " ギDEизF " ) } ,
{ testStringExpectedArray_LINE ( " A൧൨BC \\ \\ ギDEизF " , " ギDEизF " ) } ,
{ testStringExpectedArray_LINE ( " A൧൨BC///ギDEизF " , " ギDEизF " ) } ,
{ testStringExpectedArray_LINE ( " A൧൨BC \\ \\ \\ ギDEизF " , " ギDEизF " ) } ,
{ testStringExpectedArray_LINE ( " A൧൨BC/ギDEизF " , " ギDEизF " ) } ,
{ testStringExpectedArray_LINE ( " /ギDEизF " , " ギDEизF " ) } ,
{ testStringExpectedArray_LINE ( " ギDEизF " , " ギDEизF " ) } ,
{ testStringExpectedArray_LINE ( " " , " . " ) } ,
{ testStringExpectedArray_LINE ( " " , " " ) } ,
{ testStringExpectedArray_LINE ( " abc聯/輦deFG \\ ꇊHIjklmn " , " ꇊHIjklmn " ) } ,
{ testStringExpectedArray_LINE ( " abc聯 \\ 輦deFG/ꇊHIjklmn " , " ꇊHIjklmn " ) } ,
} ;
size_t nbTestStringMultiCoded4BasenameArray = * ( & testStringMultiCoded4BasenameArray + 1 ) - testStringMultiCoded4BasenameArray ;
2020-04-23 15:20:48 +02:00
template < class XStringType >
struct XStringClassInfo
{
typedef XStringType xs_t ;
static const char * xStringClassName ;
} ;
template < >
2020-04-30 08:03:56 +02:00
struct XStringClassInfo < XString8 >
2020-04-23 15:20:48 +02:00
{
typedef char ch_t ;
2020-04-30 08:03:56 +02:00
typedef XString8 xs_t ;
2020-04-23 15:20:48 +02:00
static constexpr const char * prefix = " " ;
static constexpr const char * xStringClassName = " XString " ;
} ;
template < >
struct XStringClassInfo < XString16 >
{
typedef char16_t ch_t ;
typedef XString16 xs_t ;
static constexpr const char * prefix = " u " ;
static constexpr const char * xStringClassName = " XString16 " ;
} ;
template < >
struct XStringClassInfo < XString32 >
{
typedef char32_t ch_t ;
typedef XString32 xs_t ;
static constexpr const char * prefix = " U " ;
static constexpr const char * xStringClassName = " XString32 " ;
} ;
template < >
struct XStringClassInfo < XStringW >
{
typedef wchar_t ch_t ;
typedef XStringW xs_t ;
static constexpr const char * prefix = " L " ;
static constexpr const char * xStringClassName = " XStringW " ;
} ;
template < >
struct XStringClassInfo < char >
{
typedef char ch_t ;
2020-04-30 08:03:56 +02:00
typedef XString8 xs_t ;
2020-04-23 15:20:48 +02:00
static constexpr const char * prefix = " " ;
static constexpr const char * xStringClassName = " XString " ;
} ;
template < >
struct XStringClassInfo < char16_t >
{
typedef char16_t ch_t ;
typedef XString16 xs_t ;
static constexpr const char * prefix = " u " ;
static constexpr const char * xStringClassName = " XString16 " ;
} ;
template < >
struct XStringClassInfo < char32_t >
{
typedef char32_t ch_t ;
typedef XString32 xs_t ;
static constexpr const char * prefix = " U " ;
static constexpr const char * xStringClassName = " XString32 " ;
} ;
template < >
struct XStringClassInfo < wchar_t >
{
typedef wchar_t ch_t ;
typedef XStringW xs_t ;
static constexpr const char * prefix = " L " ;
static constexpr const char * xStringClassName = " XStringW " ;
} ;
template < >
struct XStringClassInfo < TestString < char > >
{
typedef char ch_t ;
2020-04-30 08:03:56 +02:00
typedef XString8 xs_t ;
2020-04-23 15:20:48 +02:00
static constexpr const char * prefix = " " ;
static constexpr const char * xStringClassName = " XString " ;
} ;
template < >
struct XStringClassInfo < TestString < char16_t > >
{
typedef char16_t ch_t ;
typedef XString16 xs_t ;
static constexpr const char * prefix = " u " ;
static constexpr const char * xStringClassName = " XString16 " ;
} ;
template < >
struct XStringClassInfo < TestString < char32_t > >
{
typedef char32_t ch_t ;
typedef XString32 xs_t ;
static constexpr const char * prefix = " U " ;
static constexpr const char * xStringClassName = " XString32 " ;
} ;
template < >
struct XStringClassInfo < TestString < wchar_t > >
{
typedef wchar_t ch_t ;
typedef XStringW xs_t ;
static constexpr const char * prefix = " L " ;
static constexpr const char * xStringClassName = " XStringW " ;
} ;
SimpleString title_tmp ;
bool title_tmp_needs_reprinted ;
bool displayOnlyIfFailed_tmp ;
# define TEST_TITLE(__displayOnlyIfFailed__, title) \
do { \
displayOnlyIfFailed_tmp = __displayOnlyIfFailed__ ; \
title_tmp = title ; \
title_tmp_needs_reprinted = true ; \
if ( ! displayOnlyIfFailed_tmp ) { \
printf ( " %s " , title_tmp . c_str ( ) ) ; \
title_tmp_needs_reprinted = false ; \
} \
} while ( 0 ) ;
# define CHECK_RESULT(condition, okmessage, failedmessage) \
do { \
if ( ! ( condition ) ) { \
2020-08-10 13:06:06 +02:00
if ( title_tmp_needs_reprinted ) { \
printf ( " %s " , title_tmp . c_str ( ) ) ; \
} \
2020-04-23 15:20:48 +02:00
printf ( " : -> " ) ; \
printf ( " %s " , failedmessage . c_str ( ) ) ; \
2020-08-10 13:06:06 +02:00
printf ( " \n " ) ; \
title_tmp_needs_reprinted = true ; \
nbTestFailed + = 1 ; \
2020-04-23 15:20:48 +02:00
} else if ( ! displayOnlyIfFailed_tmp ) { \
2020-08-10 13:06:06 +02:00
if ( title_tmp_needs_reprinted ) { \
printf ( " %s " , title_tmp . c_str ( ) ) ; \
2020-04-23 15:20:48 +02:00
} \
2020-08-10 13:06:06 +02:00
title_tmp_needs_reprinted = true ; \
2020-04-23 15:20:48 +02:00
printf ( " -> OK : %s \n " , okmessage . c_str ( ) ) ; \
} \
nbTest + = 1 ; \
if ( ! ( condition ) ) breakpoint ( ) ; \
} while ( 0 ) ;
# define __TEST0(test, XStringClass, classEncoding) \
test ( XStringClass , classEncoding ) ; \
# define __TEST1(test, XStringClass, classEncoding, encoding1) \
test ( XStringClass , classEncoding , encoding1 ) ; \
# define __TEST2(test, XStringClass, classEncoding, encoding1, encoding2) \
test ( XStringClass , classEncoding , encoding1 , encoding2 ) ; \
# define __TEST_ALL_UTF2(test, XStringClass, classEncoding, encoding1) \
__TEST2 ( test , XStringClass , classEncoding , encoding1 , utf8 ) ; \
__TEST2 ( test , XStringClass , classEncoding , encoding1 , utf16 ) ; \
__TEST2 ( test , XStringClass , classEncoding , encoding1 , utf32 ) ; \
__TEST2 ( test , XStringClass , classEncoding , encoding1 , wchar ) ; \
2020-04-23 22:43:35 +02:00
/* Warning about array indexes of char type is on, so don't test it
TODO disable warning with pragma and uncomment to test with char */
2020-04-23 15:20:48 +02:00
# define TEST_ALL_INTEGRAL(test, XStringClass, classEncoding) \
2020-04-23 22:43:35 +02:00
/*__TEST1(test, XStringClass, classEncoding, char);*/ \
/*__TEST1(test, XStringClass, classEncoding, signed char);*/ \
/*__TEST1(test, XStringClass, classEncoding, unsigned char);*/ \
__TEST1 ( test , XStringClass , classEncoding , short ) ; \
2020-04-23 15:20:48 +02:00
__TEST1 ( test , XStringClass , classEncoding , signed short ) ; \
__TEST1 ( test , XStringClass , classEncoding , unsigned short ) ; \
2020-04-23 22:43:35 +02:00
__TEST1 ( test , XStringClass , classEncoding , int ) ; \
__TEST1 ( test , XStringClass , classEncoding , signed int ) ; \
__TEST1 ( test , XStringClass , classEncoding , unsigned int ) ; \
__TEST1 ( test , XStringClass , classEncoding , long ) ; \
__TEST1 ( test , XStringClass , classEncoding , signed long ) ; \
__TEST1 ( test , XStringClass , classEncoding , unsigned long ) ; \
__TEST1 ( test , XStringClass , classEncoding , long long ) ; \
__TEST1 ( test , XStringClass , classEncoding , signed long long ) ; \
__TEST1 ( test , XStringClass , classEncoding , unsigned long long ) ; \
2020-04-23 15:20:48 +02:00
# define TEST_ALL_UTF(test, XStringClass, classEncoding) \
__TEST1 ( test , XStringClass , classEncoding , utf8 ) ; \
__TEST1 ( test , XStringClass , classEncoding , utf16 ) ; \
__TEST1 ( test , XStringClass , classEncoding , utf32 ) ; \
__TEST1 ( test , XStringClass , classEncoding , wchar ) ; \
# define TEST_ALL_UTF_ALL_UTF(test, XStringClass, classEncoding) \
__TEST_ALL_UTF2 ( test , XStringClass , classEncoding , utf8 ) ; \
__TEST_ALL_UTF2 ( test , XStringClass , classEncoding , utf16 ) ; \
__TEST_ALL_UTF2 ( test , XStringClass , classEncoding , utf32 ) ; \
__TEST_ALL_UTF2 ( test , XStringClass , classEncoding , wchar ) ; \
# define TEST_ALL_CLASSES(test, macro) \
2020-04-30 08:03:56 +02:00
macro ( test , XString8 , utf8 ) ; \
2020-04-23 15:20:48 +02:00
macro ( test , XString16 , utf16 ) ; \
macro ( test , XString32 , utf32 ) ; \
macro ( test , XStringW , wchar ) ; \
/***************************** Default ctor *****************************/
template < class XStringClass >
SimpleString testDefaultCtor_ ( )
{
XStringClass xstr ;
2020-04-24 11:30:09 +02:00
TEST_TITLE ( displayOnlyFailed , ssprintf ( " Test default ctor of %s " , XStringClassInfo < XStringClass > : : xStringClassName ) ) ;
2020-04-23 15:20:48 +02:00
CHECK_RESULT ( xstr . length ( ) = = 0 ,
2020-04-24 11:30:09 +02:00
ssprintf ( " xstr.length() == 0 " ) ,
ssprintf ( " xstr.length() != 0 " )
2020-04-23 15:20:48 +02:00
) ;
CHECK_RESULT ( xstr . sizeInBytes ( ) = = 0 ,
2020-04-24 11:30:09 +02:00
ssprintf ( " xstr.sizeInBytes() == 0 " ) ,
ssprintf ( " xstr.sizeInBytes() != 0 " )
2020-04-23 15:20:48 +02:00
) ;
CHECK_RESULT ( * xstr . s ( ) = = 0 ,
2020-04-24 11:30:09 +02:00
ssprintf ( " *xstr.s() == 0 " ) ,
ssprintf ( " *xstr.s() != 0 " )
2020-04-23 15:20:48 +02:00
) ;
return SimpleString ( ) ;
}
# define testDefaultCtor(XStringClass, classEncoding) \
2020-05-03 17:07:34 +02:00
if ( displayMainTitle ) printf ( " Test %s::testDefaultCtor \n " , STRINGIFY ( XStringClass ) ) ; \
2020-04-23 15:20:48 +02:00
testDefaultCtor_ < XStringClass > ( ) ;
2020-04-26 12:12:05 +02:00
/***************************** Assignement : ctor, strcpy, takeValueFrom *****************************/
2020-04-23 15:20:48 +02:00
2020-04-26 12:12:05 +02:00
# define CHECK_XSTRING_EQUAL_TESTSTRING(xstr, expectedResult) \
/* We don't use XString::operator == to check xstr == xstr because operator == is not tested yet. */ \
CHECK_RESULT ( xstr . sizeInBytes ( ) = = expectedResult . getSizeInBytes ( ) , \
ssprintf ( " xstr.sizeInBytes() == expectedResult.getSizeInBytes() (%zu) " , expectedResult . getSizeInBytes ( ) ) , \
ssprintf ( " xstr.sizeInBytes() != expectedResult.getSizeInBytes() (%zu!=%zu) " , xstr . sizeInBytes ( ) , expectedResult . getSizeInBytes ( ) ) \
) ; \
CHECK_RESULT ( memcmp ( xstr . s ( ) , expectedResult . cha , expectedResult . getSizeInBytesIncludingTerminator ( ) ) = = 0 , \
ssprintf ( " memcmp(xstr.s(), expectedResult.cha, expectedResult.getSizeInBytesIncludingTerminator()) == 0 " ) , \
ssprintf ( " memcmp(xstr.s(), expectedResult.cha, expectedResult.getSizeInBytesIncludingTerminator()) != 0 " ) \
) ; \
2020-04-23 15:20:48 +02:00
template < class XStringClass , class TestStringSrc , class TestStringExpectedResult >
2020-04-26 12:12:05 +02:00
SimpleString testTakeValueFrom_ ( const TestStringSrc & src , const TestStringExpectedResult & expectedResult )
2020-04-23 15:20:48 +02:00
{
2020-04-24 11:30:09 +02:00
TEST_TITLE ( displayOnlyFailed , ssprintf ( " Test %s::testTakeValueFrom(%s \" %s \" ) " , XStringClassInfo < XStringClass > : : xStringClassName , XStringClassInfo < TestStringSrc > : : prefix , SimpleString ( src . cha ) . c_str ( ) ) ) ;
2020-04-26 12:12:05 +02:00
typedef typename XStringClassInfo < XStringClass > : : ch_t ch_t ;
ch_t c ; ( void ) c ; // dummy for call utf function
2020-04-23 15:20:48 +02:00
2020-04-26 12:12:05 +02:00
// takeValueFrom(native char type)
{
XStringClass xstr ;
xstr . takeValueFrom ( src . cha ) ;
CHECK_XSTRING_EQUAL_TESTSTRING ( xstr , expectedResult ) ;
2020-08-10 13:06:06 +02:00
xstr . takeValueFrom ( src . cha ) ;
2020-04-26 12:12:05 +02:00
}
// strcpy(native char type)
{
XStringClass xstr ;
xstr . takeValueFrom ( " blabla " ) ;
xstr . strcpy ( src . cha ) ;
CHECK_XSTRING_EQUAL_TESTSTRING ( xstr , expectedResult ) ;
}
// strcpy one native char
{
if ( src . utf32_length > 0 )
{
XStringClass xstr ;
for ( size_t pos = 0 ; pos < src . utf32_length - 1 ; pos + + )
{
char32_t char32 = get_char32_from_utf_string_at_pos ( src . cha , pos ) ;
xstr . takeValueFrom ( " foobar " ) ;
xstr . strcpy ( char32 ) ;
//printf("%s\n", SimpleString(xstr.s()).c_str());
size_t expectedSize = utf_size_of_utf_string_len ( & c , & char32 , 1 ) * sizeof ( c ) ;
CHECK_RESULT ( xstr . sizeInBytes ( ) = = expectedSize ,
ssprintf ( " xstr.sizeInBytes() == expectedSize (%zu) " , expectedSize ) ,
ssprintf ( " xstr.sizeInBytes() != expectedSize (%zu!=%zu) " , xstr . sizeInBytes ( ) , expectedSize )
) ;
ch_t buf [ 8 ] ;
utf_string_from_utf_string_len ( buf , sizeof ( buf ) / sizeof ( buf [ 0 ] ) , & char32 , 1 ) ;
size_t expectedSizeIncludingTerminator = expectedSize + sizeof ( buf [ 0 ] ) ;
CHECK_RESULT ( memcmp ( xstr . s ( ) , buf , expectedSizeIncludingTerminator ) = = 0 , // +1 char
ssprintf ( " memcmp(xstr.s(), buf, expectedSizeIncludingTerminator) == 0 " ) ,
ssprintf ( " memcmp(xstr.s(), buf, expectedSizeIncludingTerminator) != 0 " )
) ;
//xstr.takeValueFrom("foobar");
//xstr.strcpy(src.cha[0]);
}
}
}
2020-04-23 15:20:48 +02:00
typename XStringClassInfo < TestStringSrc > : : xs_t srcXString ;
srcXString . takeValueFrom ( src . cha ) ;
2020-04-26 12:12:05 +02:00
// takeValueFrom(XString)
2020-04-23 15:20:48 +02:00
{
2020-04-26 12:12:05 +02:00
XStringClass xstr ;
xstr . takeValueFrom ( srcXString ) ;
CHECK_XSTRING_EQUAL_TESTSTRING ( xstr , expectedResult ) ;
2020-04-23 15:20:48 +02:00
}
2020-04-26 12:12:05 +02:00
// ctor XString
2020-04-23 15:20:48 +02:00
{
2020-04-26 12:12:05 +02:00
XStringClass xstr ( srcXString ) ;
CHECK_XSTRING_EQUAL_TESTSTRING ( xstr , expectedResult ) ;
}
// = XString
{
XStringClass xstr ;
xstr = srcXString ;
CHECK_XSTRING_EQUAL_TESTSTRING ( xstr , expectedResult ) ;
2020-04-23 15:20:48 +02:00
}
2020-04-26 12:12:05 +02:00
// TODO test ctor with litteral
// XStringClass xstr;
// xstr = src.cha;
2020-04-23 15:20:48 +02:00
return SimpleString ( ) ;
}
2020-04-26 12:12:05 +02:00
# define testTakeValueFrom(XStringClass, classEncoding, encoding1) \
2020-08-10 13:06:06 +02:00
if ( displayMainTitle ) printf ( " Test %s::testTakeValueFrom, strcpy(%s) \n " , STRINGIFY ( XStringClass ) , STRINGIFY ( encoding1 ) ) ; \
for ( size_t testStringMultiCodedIndex = 0 ; testStringMultiCodedIndex < nbTestStringMultiCoded ; testStringMultiCodedIndex + + ) { \
testTakeValueFrom_ < XStringClass > ( testStringMultiCodedArray [ testStringMultiCodedIndex ] . encoding1 , testStringMultiCodedArray [ testStringMultiCodedIndex ] . classEncoding ) ; \
2020-04-23 15:20:48 +02:00
} \
/***************************** Default isEmpty, SetEmpty *****************************/
template < class XStringClass >
SimpleString testEmpty_ ( )
{
2020-04-24 11:30:09 +02:00
TEST_TITLE ( displayOnlyFailed , ssprintf ( " Test isEmpty(),notEmpty(),setEmpty() of %s " , XStringClassInfo < XStringClass > : : xStringClassName ) ) ;
2020-04-23 15:20:48 +02:00
XStringClass str ;
str . takeValueFrom ( " aa " ) ;
CHECK_RESULT ( str . isEmpty ( ) = = false ,
2020-04-24 11:30:09 +02:00
ssprintf ( " str.isEmpty() == false " ) ,
ssprintf ( " str.isEmpty() != true " )
2020-04-23 15:20:48 +02:00
) ;
CHECK_RESULT ( str . notEmpty ( ) = = true ,
2020-04-24 11:30:09 +02:00
ssprintf ( " str.notEmpty() == true " ) ,
ssprintf ( " str.notEmpty() != false " )
2020-04-23 15:20:48 +02:00
) ;
str . setEmpty ( ) ;
CHECK_RESULT ( str . isEmpty ( ) = = true ,
2020-04-24 11:30:09 +02:00
ssprintf ( " str.isEmpty() == true " ) ,
ssprintf ( " str.isEmpty() != false " )
2020-04-23 15:20:48 +02:00
) ;
CHECK_RESULT ( str . notEmpty ( ) = = false ,
2020-04-24 11:30:09 +02:00
ssprintf ( " str.notEmpty() == false " ) ,
ssprintf ( " str.notEmpty() != true " )
2020-04-23 15:20:48 +02:00
) ;
return SimpleString ( ) ;
}
# define testEmpty(XStringClass, classEncoding) \
2020-05-03 17:07:34 +02:00
if ( displayMainTitle ) printf ( " Test %s::testEmpty \n " , STRINGIFY ( XStringClass ) ) ; \
2020-04-23 15:20:48 +02:00
testEmpty_ < XStringClass > ( ) ; \
2020-04-23 22:43:35 +02:00
/***************************** char32At *****************************/
2020-04-23 15:20:48 +02:00
template < class XStringClass , typename integralType , class InitialValue >
2020-04-23 22:43:35 +02:00
SimpleString testchar32At_ ( const InitialValue & initialValue )
2020-04-23 15:20:48 +02:00
{
2020-04-24 11:30:09 +02:00
TEST_TITLE ( displayOnlyFailed , ssprintf ( " Test %s::char32At_u() " , XStringClassInfo < XStringClass > : : xStringClassName ) ) ;
2020-04-23 15:20:48 +02:00
XStringClass xstr ;
xstr . takeValueFrom ( initialValue . cha ) ;
for ( integralType i = 0 ; ( typename make_unsigned < integralType > : : type ) i < xstr . length ( ) ; i + + )
{
CHECK_RESULT ( xstr [ i ] = = initialValue . utf32 [ i ] ,
2020-04-24 11:30:09 +02:00
ssprintf ( " xstr[i] == dst.cha[i] (%d) " , initialValue . utf32 [ i ] ) ,
ssprintf ( " xstr[i] != dst.cha[i] (%d!=%d) " , xstr [ i ] , initialValue . utf32 [ i ] )
2020-04-23 15:20:48 +02:00
) ;
}
return SimpleString ( ) ;
}
2020-04-23 22:43:35 +02:00
# define testchar32At(XStringClass, classEncoding, integralType) \
2020-05-03 17:07:34 +02:00
if ( displayMainTitle ) printf ( " Test %s::testchar32At \n " , STRINGIFY ( XStringClass ) ) ; \
2020-08-10 13:06:06 +02:00
for ( size_t testStringMultiCodedIndex = 0 ; testStringMultiCodedIndex < nbTestStringMultiCoded ; testStringMultiCodedIndex + + ) { \
testchar32At_ < XStringClass , integralType > ( testStringMultiCodedArray [ testStringMultiCodedIndex ] . classEncoding ) ; \
2020-04-23 15:20:48 +02:00
}
2020-04-23 22:43:35 +02:00
/***************************** dataSized *****************************/
template < class XStringClass , typename integralType >
SimpleString testdataSized_ ( )
{
2020-04-24 11:30:09 +02:00
TEST_TITLE ( displayOnlyFailed , ssprintf ( " Test %s::dataSized() " , XStringClassInfo < XStringClass > : : xStringClassName ) ) ;
2020-04-23 22:43:35 +02:00
XStringClass xstr ;
integralType i = 10 ;
typename XStringClassInfo < XStringClass > : : ch_t * s = xstr . dataSized ( i ) ;
( void ) s ;
CHECK_RESULT ( xstr . allocatedSize ( ) > = 10 ,
2020-04-24 11:30:09 +02:00
ssprintf ( " xstr[i] == dst.cha[i] (%d) " , 10 ) ,
ssprintf ( " xstr[i] != dst.cha[i] (%zu!=%d) " , xstr . allocatedSize ( ) , 10 )
2020-04-23 22:43:35 +02:00
) ;
return SimpleString ( ) ;
}
# define testdataSized(XStringClass, classEncoding, integralType) \
2020-05-03 17:07:34 +02:00
if ( displayMainTitle ) printf ( " Test %s::testdataSized \n " , STRINGIFY ( XStringClass ) ) ; \
2020-04-23 22:43:35 +02:00
testdataSized_ < XStringClass , integralType > ( ) ; \
2020-04-23 15:20:48 +02:00
/***************************** strncpy *****************************/
template < class XStringClass , class TestStringSameAsClass , class TestStringSrc >
SimpleString teststrncpy_ ( const TestStringSameAsClass & encodedSameAsClass , const TestStringSrc & src )
{
2020-04-24 11:30:09 +02:00
TEST_TITLE ( displayOnlyFailed , ssprintf ( " Test %s::strncpy(%s \" %s \" ) " , XStringClassInfo < XStringClass > : : xStringClassName , XStringClassInfo < TestStringSrc > : : prefix , SimpleString ( src . cha ) . c_str ( ) ) ) ;
2020-04-23 15:20:48 +02:00
for ( size_t i = 0 ; i < length_of_utf_string ( src . cha ) + 5 ; i + + )
{
XStringClass xstr ;
xstr . takeValueFrom ( " foobar " ) ;
xstr . strncpy ( src . cha , i ) ;
2020-04-26 12:12:05 +02:00
size_t expectedLength = length_of_utf_string ( encodedSameAsClass . cha ) ;
if ( expectedLength > i ) expectedLength = i ;
CHECK_RESULT ( xstr . length ( ) = = expectedLength ,
ssprintf ( " xstr.length() == expectedLength (%zu) " , expectedLength ) ,
ssprintf ( " xstr.length() != expectedLength (%zu!=%zu) " , xstr . length ( ) , expectedLength )
2020-04-23 15:20:48 +02:00
) ;
2020-08-10 13:06:06 +02:00
xstr . strncpy ( src . cha , i ) ;
2020-04-26 12:12:05 +02:00
CHECK_RESULT ( memcmp ( xstr . s ( ) , encodedSameAsClass . cha , xstr . sizeInBytes ( ) ) = = 0 , // cannot include terminator as xstr is a substring of encodedSameAsClass. But length() test shows that xstr is really a substring.
ssprintf ( " memcmp(xstr.s(), encodedSameAsClass.cha, xstr.sizeInBytes()) == 0 " ) ,
ssprintf ( " memcmp(xstr.s(), encodedSameAsClass.cha, xstr.sizeInBytes()) != 0 " )
2020-04-23 15:20:48 +02:00
) ;
}
return SimpleString ( ) ;
}
# define teststrncpy(XStringClass, classEncoding, encoding1) \
2020-05-03 17:07:34 +02:00
if ( displayMainTitle ) printf ( " Test %s::teststrncpy(%s) \n " , STRINGIFY ( XStringClass ) , STRINGIFY ( encoding1 ) ) ; \
2020-08-10 13:06:06 +02:00
for ( size_t testStringMultiCodedIndex = 0 ; testStringMultiCodedIndex < nbTestStringMultiCoded ; testStringMultiCodedIndex + + ) { \
teststrncpy_ < XStringClass > ( testStringMultiCodedArray [ testStringMultiCodedIndex ] . classEncoding , testStringMultiCodedArray [ testStringMultiCodedIndex ] . encoding1 ) ; \
2020-04-23 15:20:48 +02:00
} \
/***************************** strcat *****************************/
template < class XStringClass , typename ch_t >
static void teststrcatCheckResult ( size_t expectedLength , size_t expectedSize , ch_t * expectedString , XStringClass xstr )
{
CHECK_RESULT ( xstr . length ( ) = = expectedLength ,
2020-04-24 11:30:09 +02:00
ssprintf ( " xstr.length() == expectedLength (%zu) " , expectedLength ) ,
ssprintf ( " xstr.length() != expectedLength (%zu!=%zu) " , xstr . length ( ) , expectedLength )
2020-04-23 15:20:48 +02:00
) ;
//expectedLength = length_of_utf_string(initialValue.cha) + length_of_utf_string(valueToCat.cha);
//xstr.takeValueFrom(initialValue.cha);
//xstr.strcat(valueToCat.cha);
CHECK_RESULT ( xstr . sizeInBytes ( ) = = expectedSize ,
2020-04-24 11:30:09 +02:00
ssprintf ( " xstr.sizeInBytes() == expectedSize (%zu) " , expectedSize ) ,
ssprintf ( " xstr.sizeInBytes() != expectedSize (%zu!=%zu) " , xstr . sizeInBytes ( ) , expectedSize )
2020-04-23 15:20:48 +02:00
) ;
CHECK_RESULT ( memcmp ( xstr . s ( ) , expectedString , expectedSize + sizeof ( ch_t ) ) = = 0 ,
2020-04-24 11:30:09 +02:00
ssprintf ( " memcmp(xstr.s(), dst.cha, dst.size) == 0 " ) ,
ssprintf ( " memcmp(xstr.s(), dst.cha, dst.size) != 0 " )
2020-04-23 15:20:48 +02:00
) ;
}
2020-04-26 12:12:05 +02:00
template < class XStringClass , class InitialValue , typename TestStringCharType >
SimpleString teststrcat_ ( const InitialValue & initialValue , const TestString < TestStringCharType > & valueToCat )
2020-04-23 15:20:48 +02:00
{
2020-04-26 12:12:05 +02:00
TEST_TITLE ( displayOnlyFailed , ssprintf ( " Test %s::strcpy(%s \" %s \" ) strcat(%s \" %s \" ) " , XStringClassInfo < XStringClass > : : xStringClassName , XStringClassInfo < InitialValue > : : prefix , SimpleString ( initialValue . cha ) . c_str ( ) , XStringClassInfo < TestStringCharType > : : prefix , SimpleString ( valueToCat . cha ) . c_str ( ) ) ) ;
2020-04-23 15:20:48 +02:00
typedef typename XStringClassInfo < XStringClass > : : ch_t ch_t ;
ch_t c ; // dummy for call utf function
size_t expectedLength = length_of_utf_string ( initialValue . cha ) + length_of_utf_string ( valueToCat . cha ) ;
size_t expectedSize = ( utf_size_of_utf_string ( & c , initialValue . cha ) + utf_size_of_utf_string ( & c , valueToCat . cha ) ) * sizeof ( ch_t ) ;
ch_t * expectedString = ( ch_t * ) malloc ( expectedSize + sizeof ( ch_t ) ) ;
utf_string_from_utf_string ( expectedString , expectedSize * sizeof ( ch_t ) + 1 , initialValue . cha ) ;
utf_string_from_utf_string ( expectedString + size_of_utf_string ( expectedString ) , expectedSize * sizeof ( ch_t ) + 1 - size_of_utf_string ( expectedString ) , valueToCat . cha ) ;
2020-04-26 12:12:05 +02:00
// strcat(native char)
{
XStringClass xstr ;
xstr . takeValueFrom ( initialValue . cha ) ;
for ( size_t i = 0 ; i < valueToCat . utf32_length ; i + + ) xstr . strcat ( valueToCat . utf32 [ i ] ) ;
teststrcatCheckResult ( expectedLength , expectedSize , expectedString , xstr ) ;
}
// += native char
{
XStringClass xstr ;
xstr . takeValueFrom ( initialValue . cha ) ;
for ( size_t i = 0 ; i < valueToCat . utf32_length ; i + + ) xstr + = valueToCat . utf32 [ i ] ;
teststrcatCheckResult ( expectedLength , expectedSize , expectedString , xstr ) ;
}
// strcat(native char*)
2020-04-23 15:20:48 +02:00
{
XStringClass xstr ;
xstr . takeValueFrom ( initialValue . cha ) ;
xstr . strcat ( valueToCat . cha ) ;
teststrcatCheckResult ( expectedLength , expectedSize , expectedString , xstr ) ;
}
2020-04-26 12:12:05 +02:00
// += native char*
2020-04-23 15:20:48 +02:00
{
XStringClass xstr ;
xstr . takeValueFrom ( initialValue . cha ) ;
xstr + = valueToCat . cha ;
teststrcatCheckResult ( expectedLength , expectedSize , expectedString , xstr ) ;
}
// strcat XString
{
2020-04-26 12:12:05 +02:00
typename XStringClassInfo < TestStringCharType > : : xs_t valueToCatXString ;
2020-04-23 15:20:48 +02:00
valueToCatXString . takeValueFrom ( valueToCat . cha ) ;
XStringClass xstr ;
xstr . takeValueFrom ( initialValue . cha ) ;
xstr + = valueToCatXString ;
teststrcatCheckResult ( expectedLength , expectedSize , expectedString , xstr ) ;
}
2020-04-26 12:12:05 +02:00
// XString + native char*
2020-04-23 15:20:48 +02:00
{
XStringClass xinitialValue ;
xinitialValue . takeValueFrom ( initialValue . cha ) ;
XStringClass xstr ;
xstr = xinitialValue + valueToCat . cha ;
teststrcatCheckResult ( expectedLength , expectedSize , expectedString , xstr ) ;
}
// XString + XString
{
XStringClass xinitialValue ;
xinitialValue . takeValueFrom ( initialValue . cha ) ;
2020-04-26 12:12:05 +02:00
typename XStringClassInfo < TestStringCharType > : : xs_t valueToCatXString ;
2020-04-23 15:20:48 +02:00
valueToCatXString . takeValueFrom ( valueToCat . cha ) ;
XStringClass xstr ;
xstr = xinitialValue + valueToCatXString ;
teststrcatCheckResult ( expectedLength , expectedSize , expectedString , xstr ) ;
}
2020-04-26 12:12:05 +02:00
// native char* + XString
2020-04-23 15:20:48 +02:00
{
2020-04-26 12:12:05 +02:00
typename XStringClassInfo < TestStringCharType > : : xs_t valueToCatXString ;
2020-04-23 15:20:48 +02:00
valueToCatXString . takeValueFrom ( valueToCat . cha ) ;
XStringClass xstr ;
xstr = initialValue . cha + valueToCatXString ;
teststrcatCheckResult ( expectedLength , expectedSize , expectedString , xstr ) ;
}
free ( expectedString ) ;
return SimpleString ( ) ;
}
# define teststrcat(XStringClass, classEncoding, encoding1, encoding2) \
2020-05-03 17:07:34 +02:00
if ( displayMainTitle ) printf ( " Test %s(%s)::teststrcat(%s) \n " , STRINGIFY ( XStringClass ) , STRINGIFY ( encoding1 ) , STRINGIFY ( encoding2 ) ) ; \
2020-08-10 13:06:06 +02:00
for ( size_t testStringMultiCodedIndex = 0 ; testStringMultiCodedIndex < nbTestStringMultiCoded ; testStringMultiCodedIndex + + ) { \
2020-04-23 15:20:48 +02:00
for ( size_t j = 0 ; j < nbTestStringMultiCoded ; j + + ) { \
2020-08-10 13:06:06 +02:00
teststrcat_ < XStringClass > ( testStringMultiCodedArray [ testStringMultiCodedIndex ] . encoding1 , testStringMultiCodedArray [ testStringMultiCodedIndex ] . encoding2 ) ; \
2020-04-23 15:20:48 +02:00
} \
} \
2020-04-25 11:59:07 +02:00
# define Xmin(x,y) ( (x) < (y) ? (x) : (y) )
# define Xmax(x,y) ( (x) > (y) ? (x) : (y) )
2020-04-23 15:20:48 +02:00
/***************************** strncat *****************************/
template < class XStringClass , class InitialValue , class ValueToCat >
SimpleString teststrncat_ ( const InitialValue & initialValue , const ValueToCat & valueToCat )
{
2020-04-24 11:30:09 +02:00
TEST_TITLE ( displayOnlyFailed , ssprintf ( " Test %s::strcpy(%s \" %s \" ) strncat(%s \" %s \" ) " , XStringClassInfo < XStringClass > : : xStringClassName , XStringClassInfo < InitialValue > : : prefix , SimpleString ( initialValue . cha ) . c_str ( ) , XStringClassInfo < ValueToCat > : : prefix , SimpleString ( valueToCat . cha ) . c_str ( ) ) ) ;
2020-04-23 15:20:48 +02:00
for ( size_t i = 0 ; i < valueToCat . utf32_length + 5 ; i + + )
{
typedef typename XStringClassInfo < XStringClass > : : ch_t ch_t ;
ch_t c ; // dummy for call utf function
XStringClass xstr ;
xstr . takeValueFrom ( initialValue . cha ) ;
xstr . strncat ( valueToCat . cha , i ) ;
2020-04-29 22:34:28 +02:00
size_t expectedLength = length_of_utf_string ( initialValue . cha ) + Xmin ( i , valueToCat . utf32_length ) ;
2020-04-23 15:20:48 +02:00
CHECK_RESULT ( xstr . length ( ) = = expectedLength ,
2020-04-24 11:30:09 +02:00
ssprintf ( " xstr.length() == expectedLength (%zu) " , expectedLength ) ,
ssprintf ( " xstr.length() != expectedLength (%zu!=%zu) " , xstr . length ( ) , expectedLength )
2020-04-23 15:20:48 +02:00
) ;
//expectedLength = length_of_utf_string(initialValue.cha) + min(i, valueToCat.utf32_length);
//xstr.takeValueFrom(initialValue.cha);
//xstr.strncat(valueToCat.cha, i);
2020-04-23 22:43:35 +02:00
size_t expectedSize = ( utf_size_of_utf_string ( & c , initialValue . cha ) + utf_size_of_utf_string_len ( & c , valueToCat . cha , i ) ) ;
CHECK_RESULT ( xstr . sizeInBytes ( ) = = expectedSize * sizeof ( ch_t ) ,
2020-04-24 11:30:09 +02:00
ssprintf ( " xstr.sizeInBytes() == expectedSize (%zu) " , expectedSize * sizeof ( ch_t ) ) ,
ssprintf ( " xstr.sizeInBytes() != expectedSize (%zu!=%zu) " , xstr . sizeInBytes ( ) , expectedSize * sizeof ( ch_t ) )
2020-04-23 15:20:48 +02:00
) ;
2020-04-23 22:43:35 +02:00
ch_t * expectedString = ( ch_t * ) malloc ( ( expectedSize + 1 ) * sizeof ( ch_t ) ) ;
utf_string_from_utf_string ( expectedString , expectedSize + 1 , initialValue . cha ) ;
utf_string_from_utf_string_len ( expectedString + utf_size_of_utf_string ( & c , initialValue . cha ) , expectedSize + 1 - size_of_utf_string ( expectedString ) , valueToCat . cha , i ) ;
2020-04-23 15:20:48 +02:00
CHECK_RESULT ( memcmp ( xstr . s ( ) , expectedString , expectedSize + sizeof ( ch_t ) ) = = 0 ,
2020-04-24 11:30:09 +02:00
ssprintf ( " memcmp(xstr.s(), expectedString, dst.size) == 0 " ) ,
ssprintf ( " memcmp(xstr.s(), expectedString, dst.size) != 0 " )
2020-04-23 15:20:48 +02:00
) ;
//utf_string_from_utf_string(expectedString, expectedSize*sizeof(XStringCharClass) + 1, initialValue.cha);
//utf_string_from_utf_string_len(expectedString + utf_size_of_utf_string(&c, initialValue.cha), expectedSize*sizeof(XStringCharClass) + 1 - size_of_utf_string(expectedString), valueToCat.cha, i);
//expectedLength = length_of_utf_string(initialValue.cha) + min(i, valueToCat.utf32_length);
//xstr.takeValueFrom(initialValue.cha);
//xstr.strncat(valueToCat.cha, i);
free ( expectedString ) ;
}
return SimpleString ( ) ;
}
# define teststrncat(XStringClass, classEncoding, encoding1, encoding2) \
2020-05-03 17:07:34 +02:00
if ( displayMainTitle ) printf ( " Test %s(%s)::teststrncat(%s) \n " , STRINGIFY ( XStringClass ) , STRINGIFY ( encoding1 ) , STRINGIFY ( encoding2 ) ) ; \
2020-08-10 13:06:06 +02:00
for ( size_t testStringMultiCodedIndex = 0 ; testStringMultiCodedIndex < nbTestStringMultiCoded ; testStringMultiCodedIndex + + ) { \
2020-04-23 15:20:48 +02:00
for ( size_t j = 0 ; j < nbTestStringMultiCoded ; j + + ) { \
2020-08-10 13:06:06 +02:00
teststrncat_ < XStringClass > ( testStringMultiCodedArray [ testStringMultiCodedIndex ] . encoding1 , testStringMultiCodedArray [ testStringMultiCodedIndex ] . encoding2 ) ; \
2020-04-23 15:20:48 +02:00
} \
} \
/***************************** subString *****************************/
template < class XStringClass , class InitialValue >
SimpleString testSubString_ ( const InitialValue & initialValue )
{
2020-04-26 12:12:05 +02:00
TEST_TITLE ( displayOnlyFailed , ssprintf ( " Test %s::subString(%s \" %s \" ) " , XStringClassInfo < XStringClass > : : xStringClassName , XStringClassInfo < InitialValue > : : prefix , SimpleString ( initialValue . cha ) . c_str ( ) ) ) ;
2020-04-23 15:20:48 +02:00
typedef typename XStringClassInfo < XStringClass > : : ch_t ch_t ;
ch_t c ; // dummy for call utf function
XStringClass str ;
str . takeValueFrom ( initialValue . cha ) ;
for ( size_t pos = 0 ; pos < initialValue . utf32_length + 3 ; pos + = 1 ) {
for ( size_t count = 0 ; count < initialValue . utf32_length - pos + 3 ; count + = 1 )
{
size_t expectedLength = 0 ;
2020-04-27 11:50:49 +02:00
if ( pos < initialValue . utf32_length ) expectedLength = Xmin ( count , initialValue . utf32_length - pos ) ;
2020-04-23 15:20:48 +02:00
size_t expectedSize = 0 ;
if ( pos < initialValue . utf32_length ) expectedSize = utf_size_of_utf_string_len ( & c , initialValue . cha , pos + count ) - utf_size_of_utf_string_len ( & c , initialValue . cha , pos ) ;
size_t offset ;
if ( pos < initialValue . utf32_length ) offset = utf_size_of_utf_string_len ( & c , initialValue . cha , pos ) ;
else offset = utf_size_of_utf_string ( & c , initialValue . cha ) ;
2020-04-23 22:43:35 +02:00
XStringClass subStr = str . subString ( pos , count ) ;
2020-04-23 15:20:48 +02:00
CHECK_RESULT ( subStr . length ( ) = = expectedLength ,
2020-04-24 11:30:09 +02:00
ssprintf ( " subStr.length() == expectedLength (%zu) " , expectedLength ) ,
ssprintf ( " subStr.length() != expectedLength (%zu!=%zu) " , subStr . length ( ) , expectedLength )
2020-04-23 15:20:48 +02:00
) ;
2020-04-24 11:30:09 +02:00
subStr = str . subString ( pos , count ) ;
2020-04-23 15:20:48 +02:00
CHECK_RESULT ( memcmp ( subStr . s ( ) , initialValue . cha + offset , expectedSize ) = = 0 ,
2020-04-24 11:30:09 +02:00
ssprintf ( " memcmp == 0 " ) ,
ssprintf ( " memcmp != 0) " )
2020-04-23 15:20:48 +02:00
) ;
}
}
return SimpleString ( ) ;
}
# define testSubString(XStringClass, classEncoding) \
2020-05-03 17:07:34 +02:00
if ( displayMainTitle ) printf ( " Test %s::testSubString \n " , STRINGIFY ( XStringClass ) ) ; \
2020-08-10 13:06:06 +02:00
for ( size_t testStringMultiCodedIndex = 0 ; testStringMultiCodedIndex < nbTestStringMultiCoded ; testStringMultiCodedIndex + + ) { \
testSubString_ < XStringClass > ( testStringMultiCodedArray [ testStringMultiCodedIndex ] . classEncoding ) ; \
2020-04-23 15:20:48 +02:00
} \
/***************************** Compare *****************************/
template < typename CharType >
CharType * incrementChar ( const CharType * s , size_t pos , int increment )
{
size_t initialSize = size_of_utf_string ( s ) ;
CharType * buf = ( CharType * ) malloc ( ( initialSize + 1 ) * sizeof ( CharType ) ) ;
size_t dst_max_len = initialSize + 1 ;
char32_t char32 ;
size_t n = 0 ;
CharType * d = buf ;
s = get_char32_from_string ( s , & char32 ) ;
while ( char32 ) {
if ( n = = pos ) {
if ( increment > = 0 ) {
d = store_utf_from_char32 ( d , & dst_max_len , char32 + ( unsigned int ) increment ) ;
} else {
d = store_utf_from_char32 ( d , & dst_max_len , char32 - ( unsigned int ) ( - increment ) ) ; // avoid signedness warning
}
} else {
d = store_utf_from_char32 ( d , & dst_max_len , char32 ) ;
}
s = get_char32_from_string ( s , & char32 ) ;
n + + ;
}
2020-04-24 11:30:09 +02:00
* d = 0 ;
2020-04-23 15:20:48 +02:00
return buf ;
}
template < class XStringClass , class InitialValue >
SimpleString testCompare_ ( const InitialValue & initialValue )
{
2020-04-24 11:30:09 +02:00
TEST_TITLE ( displayOnlyFailed , ssprintf ( " Test %s::strcmp(%s \" %s \" ) " , XStringClassInfo < XStringClass > : : xStringClassName , XStringClassInfo < InitialValue > : : prefix , SimpleString ( initialValue . cha ) . c_str ( ) ) ) ;
2020-04-23 15:20:48 +02:00
// typedef typename XStringClassInfo<XStringClass>::ch_t xs_ch_t;
// ch_t c; // dummy for call utf function
XStringClass xstr ;
xstr . takeValueFrom ( initialValue . cha ) ;
typename XStringClassInfo < InitialValue > : : xs_t xstr2 ;
xstr2 . takeValueFrom ( initialValue . cha ) ;
CHECK_RESULT ( xstr . strcmp ( xstr2 . s ( ) ) = = 0 ,
2020-04-24 11:30:09 +02:00
ssprintf ( " subStr.length() == 0 " ) ,
ssprintf ( " subStr.length() != 0 (%d) " , xstr . strcmp ( xstr2 . s ( ) ) )
2020-04-23 15:20:48 +02:00
) ;
2020-04-24 11:30:09 +02:00
// CHECK_RESULT(xstr == xstr2.s(),
// ssprintf("subStr.length() == 0"),
// ssprintf("subStr.length() != 0 (%d)", xstr.strcmp(xstr2.s()))
// );
2020-04-23 15:20:48 +02:00
CHECK_RESULT ( xstr = = xstr2 ,
2020-04-24 11:30:09 +02:00
ssprintf ( " subStr.length() == 0 " ) ,
ssprintf ( " subStr.length() != 0 (%d) " , xstr . strcmp ( xstr2 . s ( ) ) )
2020-04-23 15:20:48 +02:00
) ;
2020-04-24 11:30:09 +02:00
// CHECK_RESULT(!(xstr != xstr2.s()),
// ssprintf("subStr.length() == 0"),
// ssprintf("subStr.length() != 0 (%d)", xstr.strcmp(xstr2.s()))
// );
2020-04-23 15:20:48 +02:00
CHECK_RESULT ( ! ( xstr ! = xstr2 ) ,
2020-04-24 11:30:09 +02:00
ssprintf ( " subStr.length() == 0 " ) ,
ssprintf ( " subStr.length() != 0 (%d) " , xstr . strcmp ( xstr2 . s ( ) ) )
2020-04-23 15:20:48 +02:00
) ;
2020-04-24 11:30:09 +02:00
// CHECK_RESULT(!(xstr != xstr2.s()),
// ssprintf("subStr.length() == 0"),
// ssprintf("subStr.length() != 0 (%d)", xstr.strcmp(xstr2.s()))
// );
2020-04-23 15:20:48 +02:00
CHECK_RESULT ( ! ( xstr ! = xstr2 ) ,
2020-04-24 11:30:09 +02:00
ssprintf ( " subStr.length() == 0 " ) ,
ssprintf ( " subStr.length() != 0 (%d) " , xstr . strcmp ( xstr2 . s ( ) ) )
2020-04-23 15:20:48 +02:00
) ;
//res = xstr.strcmp(xstr2.s());
typedef typename XStringClassInfo < InitialValue > : : ch_t ch_t ;
for ( size_t pos = 0 ; pos < initialValue . utf32_length ; pos + + ) // this avoid empty string
{
const ch_t * s = incrementChar ( initialValue . cha , pos , 1 ) ;
CHECK_RESULT ( xstr . strcmp ( s ) = = - 1 ,
2020-04-24 11:30:09 +02:00
ssprintf ( " xstr.strcmp(s) == -1 " ) ,
ssprintf ( " xstr.strcmp(s) != -1 (%d) " , xstr . strcmp ( s ) )
2020-04-23 15:20:48 +02:00
) ;
/* operator comparison with native type */
2020-04-24 11:30:09 +02:00
// CHECK_RESULT(!(xstr == s),
// ssprintf("!(xstr == s)"),
// ssprintf("!!(xstr == s)")
// );
// CHECK_RESULT(!(s == xstr),
// ssprintf("!(s == xstr)"),
// ssprintf("!!(s == xstr)")
// );
// CHECK_RESULT(xstr != s,
// ssprintf("xstr != s"),
// ssprintf("!xstr != s")
// );
// CHECK_RESULT(s != xstr,
// ssprintf("s != xstr"),
// ssprintf("!s != xstr")
// );
// CHECK_RESULT(xstr < s,
// ssprintf("xstr < s"),
// ssprintf("!xstr < s")
// );
// CHECK_RESULT(s > xstr,
// ssprintf("s > xstr"),
// ssprintf("!s > xstr")
// );
// CHECK_RESULT(xstr <= s,
// ssprintf("xstr <= s"),
// ssprintf("!xstr <= s")
// );
// CHECK_RESULT(s >= xstr,
// ssprintf("s >= xstr"),
// ssprintf("!s >= xstr")
// );
// CHECK_RESULT(!(xstr > s),
// ssprintf("!(xstr > s)"),
// ssprintf("!!(xstr < s)")
// );
// CHECK_RESULT(!(s < xstr),
// ssprintf("!(s < xstr)"),
// ssprintf("!!(s < xstr)")
// );
// CHECK_RESULT(!(xstr >= s),
// ssprintf("!(xstr >= s)"),
// ssprintf("!!(xstr >= s)")
// );
// CHECK_RESULT(!(s <= xstr),
// ssprintf("!(s <= xstr)"),
// ssprintf("!!(s <= xstr)")
// );
2020-04-23 15:20:48 +02:00
/* operator comparison with other XString */
xstr2 . takeValueFrom ( s ) ;
2020-04-24 11:30:09 +02:00
2020-04-23 15:20:48 +02:00
CHECK_RESULT ( ! ( xstr = = xstr2 ) ,
2020-04-24 11:30:09 +02:00
ssprintf ( " !(xstr == xstr2) " ) ,
ssprintf ( " !!(xstr == xstr2) " )
2020-04-23 15:20:48 +02:00
) ;
CHECK_RESULT ( ! ( xstr2 = = xstr ) ,
2020-04-24 11:30:09 +02:00
ssprintf ( " !(xstr2 == xstr) " ) ,
ssprintf ( " !!(xstr2 == xstr) " )
2020-04-23 15:20:48 +02:00
) ;
CHECK_RESULT ( xstr ! = xstr2 ,
2020-04-24 11:30:09 +02:00
ssprintf ( " xstr != xstr2 " ) ,
ssprintf ( " !xstr != xstr2 " )
2020-04-23 15:20:48 +02:00
) ;
CHECK_RESULT ( xstr2 ! = xstr ,
2020-04-24 11:30:09 +02:00
ssprintf ( " xstr2 != xstr " ) ,
ssprintf ( " !xstr2 != xstr " )
2020-04-23 15:20:48 +02:00
) ;
CHECK_RESULT ( xstr < xstr2 ,
2020-04-24 11:30:09 +02:00
ssprintf ( " xstr < xstr2 " ) ,
ssprintf ( " !xstr < xstr2 " )
2020-04-23 15:20:48 +02:00
) ;
CHECK_RESULT ( xstr2 > xstr ,
2020-04-24 11:30:09 +02:00
ssprintf ( " xstr2 > xstr " ) ,
ssprintf ( " !xstr2 > xstr " )
2020-04-23 15:20:48 +02:00
) ;
CHECK_RESULT ( xstr < = xstr2 ,
2020-04-24 11:30:09 +02:00
ssprintf ( " xstr <= xstr2 " ) ,
ssprintf ( " !xstr <= xstr2 " )
2020-04-23 15:20:48 +02:00
) ;
CHECK_RESULT ( xstr2 > = xstr ,
2020-04-24 11:30:09 +02:00
ssprintf ( " xstr2 >= xstr " ) ,
ssprintf ( " !xstr2 >= xstr " )
2020-04-23 15:20:48 +02:00
) ;
CHECK_RESULT ( ! ( xstr > xstr2 ) ,
2020-04-24 11:30:09 +02:00
ssprintf ( " !(xstr > xstr2) " ) ,
ssprintf ( " !!(xstr < xstr2) " )
2020-04-23 15:20:48 +02:00
) ;
CHECK_RESULT ( ! ( xstr2 < xstr ) ,
2020-04-24 11:30:09 +02:00
ssprintf ( " !(xstr2 < xstr) " ) ,
ssprintf ( " !!(xstr2 < xstr) " )
2020-04-23 15:20:48 +02:00
) ;
CHECK_RESULT ( ! ( xstr > = xstr2 ) ,
2020-04-24 11:30:09 +02:00
ssprintf ( " !(xstr >= xstr2) " ) ,
ssprintf ( " !!(xstr >= xstr2) " )
2020-04-23 15:20:48 +02:00
) ;
CHECK_RESULT ( ! ( xstr2 < = xstr ) ,
2020-04-24 11:30:09 +02:00
ssprintf ( " !(xstr2 <= xstr) " ) ,
ssprintf ( " !!(xstr2 <= xstr) " )
2020-04-23 15:20:48 +02:00
) ;
2020-04-24 11:30:09 +02:00
free ( ( void * ) s ) ;
2020-04-23 15:20:48 +02:00
}
for ( size_t pos = 0 ; pos < initialValue . utf32_length ; pos + + ) // this avoid empty string
{
const ch_t * s = incrementChar ( initialValue . cha , pos , - 1 ) ;
CHECK_RESULT ( xstr . strcmp ( s ) = = 1 ,
2020-04-24 11:30:09 +02:00
ssprintf ( " xstr.strcmp(s) == 1 " ) ,
ssprintf ( " xstr.strcmp(s) != 1 (%d) " , xstr . strcmp ( s ) )
2020-04-23 15:20:48 +02:00
) ;
//const ch_t* s2 = incrementChar(initialValue.cha, pos, 1);
}
return SimpleString ( ) ;
}
# define testCompare(XStringClass, classEncoding, encoding) \
2020-05-03 17:07:34 +02:00
if ( displayMainTitle ) printf ( " Test %s::strcmp(%s) \n " , STRINGIFY ( XStringClass ) , STRINGIFY ( encoding ) ) ; \
2020-08-10 13:06:06 +02:00
for ( size_t testStringMultiCodedIndex = 0 ; testStringMultiCodedIndex < nbTestStringMultiCoded ; testStringMultiCodedIndex + + ) { \
testCompare_ < XStringClass > ( testStringMultiCodedArray [ testStringMultiCodedIndex ] . encoding ) ; \
2020-04-23 15:20:48 +02:00
} \
/***************************** indexOf, rindexOf *****************************/
template < class XStringClass , typename ch_t >
2020-04-23 22:43:35 +02:00
static void testindexOf__ ( XStringClass subStr , bool ignoreCase ,
2020-04-23 15:20:48 +02:00
size_t ( XStringClass : : * indexOfChar ) ( char32_t , size_t ) const ,
size_t ( XStringClass : : * indexOfString ) ( const ch_t * , size_t ) const ,
size_t ( XStringClass : : * rindexOfChar ) ( char32_t , size_t ) const ,
size_t ( XStringClass : : * rindexOfString ) ( const ch_t * , size_t ) const
)
{
XStringClass testStr ;
testStr = subStr ;
if ( ignoreCase ) testStr . lowerAscii ( ) ;
CHECK_RESULT ( ( testStr . * indexOfString ) ( subStr . s ( ) , 0 ) = = 0 ,
2020-04-24 11:30:09 +02:00
ssprintf ( " testStr.indexOf(subStr.s(), 0) == 0 " ) ,
ssprintf ( " testStr.indexOf(subStr.s(), 0) != 0 (%zu) " , ( testStr . * indexOfString ) ( subStr . s ( ) , 0 ) )
2020-04-23 15:20:48 +02:00
) ;
( testStr . * indexOfString ) ( subStr . s ( ) , 0 ) ;
size_t expectedPos = subStr . length ( ) = = 0 ? testStr . length ( ) : 0 ;
CHECK_RESULT ( ( testStr . * rindexOfString ) ( subStr . s ( ) , MAX_XSIZE - 1 ) = = expectedPos ,
2020-04-24 11:30:09 +02:00
ssprintf ( " testStr.indexOf(subStr.s(), 0) == expectedPos (%zu) " , expectedPos ) ,
ssprintf ( " testStr.indexOf(subStr.s(), 0) != 0 (%zu!=%zu) " , ( testStr . * rindexOfString ) ( subStr . s ( ) , MAX_XSIZE - 1 ) , expectedPos )
2020-04-23 15:20:48 +02:00
) ;
( testStr . * rindexOfString ) ( subStr . s ( ) , MAX_XSIZE - 1 ) ;
XStringClass apple ;
apple . takeValueFrom ( " " ) ;
CHECK_RESULT ( ( testStr . * indexOfString ) ( apple . s ( ) , 0 ) = = MAX_XSIZE ,
2020-04-24 11:30:09 +02:00
ssprintf ( " testStr.*indexOfString)( \" \" , 0) == MAX_XSIZE " ) ,
ssprintf ( " testStr.*indexOfString)( \" \" , 0) != MAX_XSIZE (%zu) " , ( testStr . * indexOfString ) ( subStr . s ( ) , 0 ) )
2020-04-23 15:20:48 +02:00
) ;
//(testStr.*indexOfString)("");
CHECK_RESULT ( ( testStr . * rindexOfString ) ( apple . s ( ) , MAX_XSIZE - 1 ) = = MAX_XSIZE ,
2020-04-24 11:30:09 +02:00
ssprintf ( " (testStr.*rindexOfString)( \" \" , MAX_XSIZE-1) == MAX_XSIZE " ) ,
ssprintf ( " (testStr.*rindexOfString)( \" \" , MAX_XSIZE-1) != MAX_XSIZE (%zu) " , ( testStr . * rindexOfString ) ( subStr . s ( ) , MAX_XSIZE - 1 ) )
2020-04-23 15:20:48 +02:00
) ;
if ( subStr . length ( ) > 0 )
{
testStr . takeValueFrom ( " " ) ;
testStr . strcat ( subStr . s ( ) ) ;
if ( ignoreCase ) testStr . lowerAscii ( ) ;
CHECK_RESULT ( ( testStr . * indexOfString ) ( subStr . s ( ) , 0 ) = = 3 ,
2020-04-24 11:30:09 +02:00
ssprintf ( " testStr.indexOf(subStr.s(), 0) == 3 " ) ,
ssprintf ( " testStr.indexOf(subStr.s(), 0) != 3 (%zu) " , ( testStr . * indexOfString ) ( subStr . s ( ) , 0 ) )
2020-04-23 15:20:48 +02:00
) ;
( testStr . * indexOfString ) ( subStr . s ( ) , 0 ) ;
CHECK_RESULT ( ( testStr . * indexOfString ) ( subStr . s ( ) , 0 ) = = 3 ,
2020-04-24 11:30:09 +02:00
ssprintf ( " (testStr.*indexOfString)(subStr.s(), 0) == 3 " ) ,
ssprintf ( " (testStr.*indexOfString)(subStr.s(), 0) != 3 (%zu) " , ( testStr . * indexOfString ) ( subStr . s ( ) , 0 ) )
2020-04-23 15:20:48 +02:00
) ;
CHECK_RESULT ( ( testStr . * indexOfChar ) ( subStr [ 0 ] , 0 ) = = 3 ,
2020-04-24 11:30:09 +02:00
ssprintf ( " (testStr.*indexOfString)(subStr[0]) == 3 " ) ,
ssprintf ( " (testStr.*indexOfString)(subStr[0]) != 3 (%zu) " , ( testStr . * indexOfChar ) ( subStr [ 0 ] , 0 ) )
2020-04-23 15:20:48 +02:00
) ;
CHECK_RESULT ( ( testStr . * rindexOfString ) ( subStr . s ( ) , MAX_XSIZE - 1 ) = = 3 ,
2020-04-24 11:30:09 +02:00
ssprintf ( " testStr.indexOf(subStr.s(), MAX_XSIZE-1) == 3 " ) ,
ssprintf ( " testStr.indexOf(subStr.s(), MAX_XSIZE-1) != 3 (%zu) " , ( testStr . * rindexOfString ) ( subStr . s ( ) , MAX_XSIZE - 1 ) )
2020-04-23 15:20:48 +02:00
) ;
( testStr . * rindexOfString ) ( subStr . s ( ) , MAX_XSIZE - 1 ) ;
CHECK_RESULT ( ( testStr . * rindexOfString ) ( subStr . s ( ) , MAX_XSIZE - 1 ) = = 3 ,
2020-04-24 11:30:09 +02:00
ssprintf ( " (testStr.*rindexOfString)(subStr.s(), 0) == 3 " ) ,
ssprintf ( " (testStr.*rindexOfString)(subStr.s(), 0) != 3 (%zu) " , ( testStr . * rindexOfString ) ( subStr . s ( ) , MAX_XSIZE - 1 ) )
2020-04-23 15:20:48 +02:00
) ;
CHECK_RESULT ( ( testStr . * rindexOfChar ) ( subStr [ subStr . length ( ) - 1 ] , MAX_XSIZE - 1 ) = = 3 + subStr . length ( ) - 1 ,
2020-04-24 11:30:09 +02:00
ssprintf ( " (testStr.*rindexOfString)(subStr[subStr.length()-1]) == 3 + subStr.length() - 1 (%zu) " , 3 + subStr . length ( ) - 1 ) ,
ssprintf ( " (testStr.*rindexOfString)(subStr[subStr.length()-1]) == 3 + subStr.length() - 1 (%zu!=%zu) " , ( testStr . * rindexOfChar ) ( subStr [ subStr . length ( ) - 1 ] , MAX_XSIZE - 1 ) , 3 + subStr . length ( ) - 1 )
2020-04-23 15:20:48 +02:00
) ;
testStr . takeValueFrom ( " " ) ;
testStr . strcat ( subStr . s ( ) ) ;
testStr . strcat ( " " ) ;
if ( ignoreCase ) testStr . lowerAscii ( ) ;
CHECK_RESULT ( ( testStr . * indexOfString ) ( subStr . s ( ) , 0 ) = = 4 ,
2020-04-24 11:30:09 +02:00
ssprintf ( " testStr.indexOf(subStr.s(), 0) == 4 " ) ,
ssprintf ( " testStr.indexOf(subStr.s(), 0) != 4 (%zu) " , ( testStr . * indexOfString ) ( subStr . s ( ) , 0 ) )
2020-04-23 15:20:48 +02:00
) ;
( testStr . * indexOfString ) ( subStr . s ( ) , 0 ) ;
CHECK_RESULT ( ( testStr . * indexOfString ) ( subStr . s ( ) , 0 ) = = 4 ,
2020-04-24 11:30:09 +02:00
ssprintf ( " (testStr.*indexOfString)(subStr.s(), 0) == 4 " ) ,
ssprintf ( " (testStr.*indexOfString)(subStr.s(), 0) != 4 (%zu) " , ( testStr . * indexOfString ) ( subStr . s ( ) , 0 ) )
2020-04-23 15:20:48 +02:00
) ;
CHECK_RESULT ( ( testStr . * indexOfChar ) ( subStr [ 0 ] , 0 ) = = 4 ,
2020-04-24 11:30:09 +02:00
ssprintf ( " (testStr.*indexOfString)(subStr[0]) == 4 " ) ,
ssprintf ( " (testStr.*indexOfString)(subStr[0]) != 4 (%zu) " , ( testStr . * indexOfChar ) ( subStr [ 0 ] , 0 ) )
2020-04-23 15:20:48 +02:00
) ;
CHECK_RESULT ( ( testStr . * rindexOfString ) ( subStr . s ( ) , MAX_XSIZE - 1 ) = = 4 ,
2020-04-24 11:30:09 +02:00
ssprintf ( " testStr.indexOf(subStr.s(), 0) == 4 " ) ,
ssprintf ( " testStr.indexOf(subStr.s(), 0) != 4 (%zu) " , ( testStr . * rindexOfString ) ( subStr . s ( ) , MAX_XSIZE - 1 ) )
2020-04-23 15:20:48 +02:00
) ;
CHECK_RESULT ( ( testStr . * rindexOfString ) ( subStr . s ( ) , MAX_XSIZE - 1 ) = = 4 ,
2020-04-24 11:30:09 +02:00
ssprintf ( " (testStr.*rindexOfString)(subStr.s(), 0) == 4 " ) ,
ssprintf ( " (testStr.*rindexOfString)(subStr.s(), 0) != 4 (%zu) " , ( testStr . * rindexOfString ) ( subStr . s ( ) , MAX_XSIZE - 1 ) )
2020-04-23 15:20:48 +02:00
) ;
CHECK_RESULT ( ( testStr . * rindexOfChar ) ( subStr [ subStr . length ( ) - 1 ] , MAX_XSIZE - 1 ) = = 4 + subStr . length ( ) - 1 ,
2020-04-24 11:30:09 +02:00
ssprintf ( " (testStr.*rindexOfString)(subStr[subStr.length()-1]) == 4 + subStr.length() - 1 (%zu) " , 4 + subStr . length ( ) - 1 ) ,
ssprintf ( " (testStr.*rindexOfString)(subStr[subStr.length()-1]) == 4 + subStr.length() - 1 (%zu!=%zu) " , ( testStr . * rindexOfChar ) ( subStr [ subStr . length ( ) - 1 ] , MAX_XSIZE - 1 ) , 4 + subStr . length ( ) - 1 )
2020-04-23 15:20:48 +02:00
) ;
testStr . takeValueFrom ( " " ) ;
testStr . strcat ( subStr . s ( ) ) ;
testStr . strcat ( " " ) ;
testStr . strcat ( subStr . s ( ) ) ;
testStr . strcat ( " " ) ;
if ( ignoreCase ) testStr . lowerAscii ( ) ;
CHECK_RESULT ( ( testStr . * indexOfString ) ( subStr . s ( ) , 0 ) = = 5 ,
2020-04-24 11:30:09 +02:00
ssprintf ( " testStr.indexOf(subStr.s(), 0) == 5 " ) ,
ssprintf ( " testStr.indexOf(subStr.s(), 0) != 5 (%zu) " , ( testStr . * indexOfString ) ( subStr . s ( ) , 0 ) )
2020-04-23 15:20:48 +02:00
) ;
CHECK_RESULT ( ( testStr . * indexOfString ) ( subStr . s ( ) , 0 ) = = 5 ,
2020-04-24 11:30:09 +02:00
ssprintf ( " (testStr.*indexOfString)(subStr.s(), 0) == 5 " ) ,
ssprintf ( " (testStr.*indexOfString)(subStr.s(), 0) != 5 (%zu) " , ( testStr . * indexOfString ) ( subStr . s ( ) , 0 ) )
2020-04-23 15:20:48 +02:00
) ;
CHECK_RESULT ( ( testStr . * indexOfChar ) ( subStr [ 0 ] , 0 ) = = 5 ,
2020-04-24 11:30:09 +02:00
ssprintf ( " (testStr.*indexOfString)(subStr[0]) == 5 " ) ,
ssprintf ( " (testStr.*indexOfString)(subStr[0]) != 5 (%zu) " , ( testStr . * indexOfChar ) ( subStr [ 0 ] , 0 ) )
2020-04-23 15:20:48 +02:00
) ;
CHECK_RESULT ( ( testStr . * rindexOfString ) ( subStr . s ( ) , MAX_XSIZE - 1 ) = = 5 + subStr . length ( ) + 6 ,
2020-04-24 11:30:09 +02:00
ssprintf ( " testStr.indexOf(subStr.s(), 0) == 5 + subStr.length() + 6 " ) ,
ssprintf ( " testStr.indexOf(subStr.s(), 0) != 5 + subStr.length() + 6 (%zu!=%zu) " , ( testStr . * rindexOfString ) ( subStr . s ( ) , MAX_XSIZE - 1 ) , 5 + subStr . length ( ) + 6 )
2020-04-23 15:20:48 +02:00
) ;
( testStr . * rindexOfString ) ( subStr . s ( ) , MAX_XSIZE - 1 ) ;
CHECK_RESULT ( ( testStr . * rindexOfString ) ( subStr . s ( ) , MAX_XSIZE - 1 ) = = 5 + subStr . length ( ) + 6 ,
2020-04-24 11:30:09 +02:00
ssprintf ( " (testStr.*rindexOfString)(subStr.s(), 0) == 5 + subStr.length() + 6 " ) ,
ssprintf ( " (testStr.*rindexOfString)(subStr.s(), 0) != 5 + subStr.length() + 6 (%zu!=%zu) " , ( testStr . * rindexOfString ) ( subStr . s ( ) , MAX_XSIZE - 1 ) , 5 + subStr . length ( ) + 6 )
2020-04-23 15:20:48 +02:00
) ;
CHECK_RESULT ( ( testStr . * rindexOfChar ) ( subStr [ subStr . length ( ) - 1 ] , MAX_XSIZE - 1 ) = = 5 + subStr . length ( ) + 6 + subStr . length ( ) - 1 ,
2020-04-24 11:30:09 +02:00
ssprintf ( " (testStr.*rindexOfString)(subStr[subStr.length()-1]) == 5 + subStr.length() + 6 + subStr.length() - 1 (%zu) " , 5 + subStr . length ( ) + 6 + subStr . length ( ) - 1 ) ,
ssprintf ( " (testStr.*rindexOfString)(subStr[subStr.length()-1]) == 5 + subStr.length() + 6 + subStr.length() - 1 (%zu!=%zu) " , ( testStr . * rindexOfChar ) ( subStr [ subStr . length ( ) - 1 ] , MAX_XSIZE - 1 ) , 5 + subStr . length ( ) + 6 + subStr . length ( ) - 1 )
2020-04-23 15:20:48 +02:00
) ;
}
}
template < class XStringClass , class InitialValue >
2020-04-23 22:43:35 +02:00
SimpleString testindexOf_ ( const InitialValue & initialValue )
2020-04-23 15:20:48 +02:00
{
2020-04-24 11:30:09 +02:00
TEST_TITLE ( displayOnlyFailed , ssprintf ( " Test %s::idxOf " , XStringClassInfo < XStringClass > : : xStringClassName ) ) ;
2020-04-23 15:20:48 +02:00
typedef typename XStringClassInfo < XStringClass > : : xs_t ixs_t ;
typedef typename XStringClassInfo < XStringClass > : : ch_t ich_t ;
// ch_t c; // dummy for call utf function
ixs_t str ;
str . takeValueFrom ( initialValue . cha ) ;
for ( size_t pos = 0 ; pos < initialValue . utf32_length + 3 ; pos + = 1 ) {
for ( size_t count = 0 ; count < initialValue . utf32_length - pos + 3 ; count + = 1 )
{
2020-04-23 22:43:35 +02:00
ixs_t subStr = str . subString ( pos , count ) ;
2020-04-23 15:20:48 +02:00
// size_t (ixs_t::*p)(const ich_t* S, size_t Pos) const = &ixs_t::template indexOf<ich_t>;
2020-04-23 22:43:35 +02:00
testindexOf__ < ixs_t , ich_t > ( subStr , false , & ixs_t : : indexOf , & ixs_t : : template indexOf , & ixs_t : : rindexOf , & ixs_t : : template rindexOf ) ;
2020-04-23 15:20:48 +02:00
ixs_t subStrLower = subStr ;
2020-04-23 22:43:35 +02:00
testindexOf__ < ixs_t , ich_t > ( subStrLower , true , & ixs_t : : indexOfIC , & ixs_t : : template indexOfIC , & ixs_t : : rindexOfIC , & ixs_t : : template rindexOfIC ) ;
2020-04-23 15:20:48 +02:00
// if ( subStrLower != subStr ) {
// }
}
}
return SimpleString ( ) ;
}
2020-04-23 22:43:35 +02:00
# define testindexOf(XStringClass, classEncoding, encoding) \
2020-08-10 13:06:06 +02:00
if ( displayMainTitle ) printf ( " Test %s::testindexOf(%s) \n " , STRINGIFY ( XStringClass ) , STRINGIFY ( encoding ) ) ; \
for ( size_t testStringMultiCodedIndex = 0 ; testStringMultiCodedIndex < nbTestStringMultiCoded ; testStringMultiCodedIndex + + ) { \
testindexOf_ < XStringClass > ( testStringMultiCodedArray [ testStringMultiCodedIndex ] . encoding ) ; \
2020-04-23 15:20:48 +02:00
} \
2020-08-10 13:06:06 +02:00
for ( size_t testStringMultiCodedIndex = 0 ; testStringMultiCodedIndex < nbTestStringMultiCoded4CaseArray ; testStringMultiCodedIndex + + ) { \
testindexOf_ < XStringClass > ( testStringMultiCoded4CaseArray [ testStringMultiCodedIndex ] . encoding ) ; \
2020-04-23 15:20:48 +02:00
} \
2020-04-25 11:59:07 +02:00
/***************************** lastChar *****************************/
template < class XStringClass , class InitialValue >
SimpleString testlastChar_ ( const InitialValue & initialValue )
{
2020-04-26 12:12:05 +02:00
TEST_TITLE ( displayOnlyFailed , ssprintf ( " Test %s::lastChar(%s \" %s \" ) " , XStringClassInfo < XStringClass > : : xStringClassName , XStringClassInfo < InitialValue > : : prefix , SimpleString ( initialValue . cha ) . c_str ( ) ) ) ;
2020-04-25 11:59:07 +02:00
// typedef typename XStringClassInfo<XStringClass>::ch_t ch_t;
// ch_t c; // dummy for call utf function
XStringClass str ;
str . takeValueFrom ( initialValue . cha ) ;
char32_t expectedChar = 0 ;
if ( initialValue . utf32_length > 0 ) expectedChar = initialValue . utf32 [ initialValue . utf32_length - 1 ] ;
char32_t char32 = str . lastChar ( ) ;
CHECK_RESULT ( char32 = = expectedChar ,
ssprintf ( " char32 == expectedChar (%d) " , expectedChar ) ,
ssprintf ( " char32 == expectedChar (%d!=%d) " , char32 , expectedChar )
) ;
return SimpleString ( ) ;
}
# define testlastChar(XStringClass, classEncoding) \
2020-05-03 17:07:34 +02:00
if ( displayMainTitle ) printf ( " Test %s::testlastChar \n " , STRINGIFY ( XStringClass ) ) ; \
2020-08-10 13:06:06 +02:00
for ( size_t testStringMultiCodedIndex = 0 ; testStringMultiCodedIndex < nbTestStringMultiCoded ; testStringMultiCodedIndex + + ) { \
testlastChar_ < XStringClass > ( testStringMultiCodedArray [ testStringMultiCodedIndex ] . classEncoding ) ; \
2020-04-25 11:59:07 +02:00
} \
2020-04-23 15:20:48 +02:00
2020-04-25 13:33:01 +02:00
/***************************** trim *****************************/
template < class XStringClass , class InitialValue , class ExpectedValue >
SimpleString testtrim_ ( const InitialValue & initialValue , const ExpectedValue & expectedValue )
{
2020-04-26 12:12:05 +02:00
TEST_TITLE ( displayOnlyFailed , ssprintf ( " Test %s::trim(%s \" %s \" ) " , XStringClassInfo < XStringClass > : : xStringClassName , XStringClassInfo < InitialValue > : : prefix , SimpleString ( initialValue . cha ) . c_str ( ) ) ) ;
2020-04-25 13:33:01 +02:00
// typedef typename XStringClassInfo<XStringClass>::ch_t ch_t;
// ch_t c; // dummy for call utf function
XStringClass str ;
str . takeValueFrom ( initialValue . cha ) ;
2021-02-10 13:32:07 +01:00
// char32_t expectedChar = 0;
// if ( initialValue.utf32_length > 0) expectedChar = initialValue.utf32[initialValue.utf32_length-1];
2020-04-25 13:33:01 +02:00
str . trim ( ) ;
CHECK_RESULT ( str . strcmp ( expectedValue . cha ) = = 0 ,
ssprintf ( " str.strcmp(expectedValue.cha) == 0 ( \" %s \" ) " , SimpleString ( expectedValue . cha ) . c_str ( ) ) ,
2020-08-12 17:15:47 +02:00
ssprintf ( " str.strcmp(expectedValue.cha) != 0 ( \" %s \" != \" %s \" ) " , SimpleString ( str . s ( ) ) . c_str ( ) , SimpleString ( expectedValue . cha ) . c_str ( ) )
2020-04-25 13:33:01 +02:00
) ;
//str.takeValueFrom(initialValue.cha);
//str.trim();
return SimpleString ( ) ;
}
# define testtrim(XStringClass, classEncoding) \
2020-05-03 17:07:34 +02:00
if ( displayMainTitle ) printf ( " Test %s::testtrim \n " , STRINGIFY ( XStringClass ) ) ; \
2020-08-10 13:06:06 +02:00
for ( size_t testtrim = 0 ; testtrim < nbTestStringMultiCoded4TrimArray ; testtrim + + ) { \
testtrim_ < XStringClass > ( testStringMultiCoded4TrimArray [ testtrim ] . classEncoding , testStringMultiCoded4TrimArray [ testtrim ] . classEncoding # # _expectedResult ) ; \
2020-04-25 13:33:01 +02:00
} \
2020-04-26 12:12:05 +02:00
/***************************** startWith *****************************/
template < class XStringClass , class InitialValue >
SimpleString teststartWith_ ( const InitialValue & initialValue )
{
TEST_TITLE ( displayOnlyFailed , ssprintf ( " Test %s::startWith(%s \" %s \" ) " , XStringClassInfo < XStringClass > : : xStringClassName , XStringClassInfo < InitialValue > : : prefix , SimpleString ( initialValue . cha ) . c_str ( ) ) ) ;
// typedef typename XStringClassInfo<XStringClass>::ch_t ch_t;
// ch_t c; // dummy for call utf function
2020-04-29 22:34:28 +02:00
XStringClass initia__String ;
initia__String . takeValueFrom ( initialValue . cha ) ;
2020-04-26 12:12:05 +02:00
2021-02-10 13:32:07 +01:00
// char32_t expectedChar = 0;
// if ( initialValue.utf32_length > 0) expectedChar = initialValue.utf32[initialValue.utf32_length-1];
2020-04-26 12:12:05 +02:00
for ( size_t count = 0 ; count < initialValue . utf32_length + 3 ; count + = 1 )
{
2020-04-29 22:34:28 +02:00
XStringClass subStr = initia__String . subString ( 0 , count ) ;
2020-04-26 12:12:05 +02:00
bool expectedResult = true ;
if ( subStr . length ( ) > 0 & & count > = initialValue . utf32_length ) expectedResult = false ;
2020-04-29 22:34:28 +02:00
CHECK_RESULT ( initia__String . startWith ( subStr ) = = expectedResult ,
ssprintf ( " \" %s \" .startWith( \" %s \" ) == %d " , SimpleString ( initia__String . s ( ) ) . c_str ( ) , SimpleString ( subStr . s ( ) ) . c_str ( ) , expectedResult ) ,
ssprintf ( " \" %s \" .startWith( \" %s \" ) != %d " , SimpleString ( initia__String . s ( ) ) . c_str ( ) , SimpleString ( subStr . s ( ) ) . c_str ( ) , expectedResult )
2020-04-26 12:12:05 +02:00
) ;
2020-04-29 22:34:28 +02:00
//initia__String.startWith(subStr);
2020-04-26 12:12:05 +02:00
2020-08-09 17:55:30 +02:00
if ( count > 0 )
{
char32_t c32 = initialValue . utf32 [ count - 1 ] + 1 ;
subStr = initia__String . subString ( 0 , count - 1 ) + c32 ;
expectedResult = false ;
CHECK_RESULT ( initia__String . startWith ( subStr ) = = expectedResult ,
ssprintf ( " \" %s \" .startWith( \" %s \" ) == %d " , SimpleString ( initia__String . s ( ) ) . c_str ( ) , SimpleString ( subStr . s ( ) ) . c_str ( ) , expectedResult ) ,
ssprintf ( " \" %s \" .startWith( \" %s \" ) != %d " , SimpleString ( initia__String . s ( ) ) . c_str ( ) , SimpleString ( subStr . s ( ) ) . c_str ( ) , expectedResult )
) ;
}
2020-04-29 22:34:28 +02:00
//subStr = initia__String.subString(0, count-1);
2020-04-26 12:12:05 +02:00
//subStr = subStr + ((char32_t)(initialValue.utf32[count-1]+1));
2020-04-29 22:34:28 +02:00
//initia__String.startWith(subStr);
2020-04-26 12:12:05 +02:00
}
//str.takeValueFrom(initialValue.cha);
//str.startWith();
return SimpleString ( ) ;
}
# define teststartWith(XStringClass, classEncoding) \
2020-05-03 17:07:34 +02:00
if ( displayMainTitle ) printf ( " Test %s::teststartWith \n " , STRINGIFY ( XStringClass ) ) ; \
2020-08-10 13:06:06 +02:00
for ( size_t testStringMultiCodedIndex = 0 ; testStringMultiCodedIndex < nbTestStringMultiCoded ; testStringMultiCodedIndex + + ) { \
teststartWith_ < XStringClass > ( testStringMultiCodedArray [ testStringMultiCodedIndex ] . classEncoding ) ; \
2020-04-26 12:12:05 +02:00
} \
2020-04-27 11:50:49 +02:00
/***************************** basename *****************************/
template < class XStringClass , class InitialValue , typename ExpectedResult >
SimpleString testbasename_ ( const InitialValue & initialValue , const TestString < ExpectedResult > & expectedResult )
{
TEST_TITLE ( displayOnlyFailed , ssprintf ( " Test %s::basename(%s \" %s \" ) " , XStringClassInfo < XStringClass > : : xStringClassName , XStringClassInfo < InitialValue > : : prefix , SimpleString ( initialValue . cha ) . c_str ( ) ) ) ;
// typedef typename XStringClassInfo<XStringClass>::ch_t ch_t;
// ch_t c; // dummy for call utf function
2020-04-29 22:34:28 +02:00
XStringClass initia__String ;
initia__String . takeValueFrom ( initialValue . cha ) ;
2020-04-27 11:50:49 +02:00
2020-04-29 22:34:28 +02:00
XStringClass xstr = initia__String . basename ( ) ;
2020-04-27 11:50:49 +02:00
CHECK_RESULT ( xstr . strcmp ( expectedResult . cha ) = = 0 ,
2020-04-29 22:34:28 +02:00
ssprintf ( " \" %s \" .basename() == \" %s \" " , SimpleString ( initia__String . s ( ) ) . c_str ( ) , SimpleString ( expectedResult . cha ) . c_str ( ) ) ,
ssprintf ( " \" %s \" .basename() != ( \" %s \" != \" %s \" ) " , SimpleString ( initia__String . s ( ) ) . c_str ( ) , SimpleString ( xstr . s ( ) ) . c_str ( ) , SimpleString ( expectedResult . cha ) . c_str ( ) )
2020-04-27 11:50:49 +02:00
) ;
2020-04-29 22:34:28 +02:00
XStringClass xstr2 = initia__String . basename ( ) ;
2020-04-27 11:50:49 +02:00
return SimpleString ( ) ;
}
# define testbasename(XStringClass, classEncoding) \
2020-05-03 17:07:34 +02:00
if ( displayMainTitle ) printf ( " Test %s::testbasename \n " , STRINGIFY ( XStringClass ) ) ; \
2020-08-10 13:06:06 +02:00
for ( size_t testbasename = 0 ; testbasename < nbTestStringMultiCoded4BasenameArray ; testbasename + + ) { \
testbasename_ < XStringClass > ( testStringMultiCoded4BasenameArray [ testbasename ] . classEncoding , testStringMultiCoded4BasenameArray [ testbasename ] . classEncoding # # _expectedResult ) ; \
2020-04-27 11:50:49 +02:00
} \
2020-04-25 13:33:01 +02:00
2020-04-23 15:20:48 +02:00
/***************************** *****************************/
2020-04-05 14:25:39 +02:00
//
2020-04-23 15:20:48 +02:00
//#include <type_traits>
//#include <typeinfo>
//#include <iostream>
2020-04-27 11:50:49 +02:00
//#include <libgen.h>
2020-04-23 15:20:48 +02:00
//std::is_class
2020-04-29 22:34:28 +02:00
//
//void func_test(XStringW& xsw)
//{
// (void)xsw;
//}
2020-04-23 15:20:48 +02:00
2020-04-29 22:34:28 +02:00
//constexpr LString8 g_xs1 = "foobar";
//constexpr LStringW g_xsw1 = L"foobar";
2020-04-30 08:22:26 +02:00
//XString g_xs2 = "foobar"_XS8;
2020-04-27 11:50:49 +02:00
2020-08-15 15:47:56 +02:00
// U'𐄔' = 0x10114 : this is 2 utf16 chars (codepoint > 0xFFFF)
// U'𐅃' = 0x10143 : this is 2 utf16 chars (codepoint > 0xFFFF)
2021-04-08 17:52:48 +02:00
# include "../cpp_foundation/XBuffer.h"
2020-04-23 22:43:35 +02:00
int XString_tests ( )
2020-04-23 15:20:48 +02:00
{
# ifdef JIEF_DEBUG
2020-04-30 23:43:44 +02:00
// printf("XString_tests -> Enter\n");
2020-04-23 15:20:48 +02:00
# endif
2020-04-30 23:43:44 +02:00
//char c = 'a';
2020-04-29 22:34:28 +02:00
//int ii = sizeof(size_t);
//unsigned long long ull = 1;
//unsigned long long ll = 3;
//xw1.dataSized(c);
//xw1.dataSized(ii);
//xw1.dataSized(ull);
//xw1.dataSized(ll);
//const char* utf8 = "ギ"; (void)utf8;
//size_t utf8_size = sizeof("ギ") - 1; (void)utf8_size; // this char is 6 bytes long !
//const wchar_t* utfw = L"ギ"; (void)utfw;
//size_t utfw_size = sizeof(L"ギ") - 1; (void)utfw_size; // this char is 6 bytes long !
//const char16_t* utf16 = u"ギ"; (void)utf16;
//size_t utf16_size = sizeof(u"ギ") - 1; (void)utf16_size; // this char is 6 bytes long !
//const char32_t* utf32 = U"ギ"; (void)utf32;
//size_t utf32_size = sizeof(U"ギ") - 1; (void)utf32_size; // this char is 6 bytes long !
2020-04-23 15:20:48 +02:00
//size_t size = sizeof("ꇉ")-1; // this char is 3 bytes long
//size_t size = sizeof("伽")-1; // this char is 3 bytes long
//size_t size = sizeof("楘")-1; // this char is 3 bytes long
2020-04-30 08:22:26 +02:00
//XString str = "ギꇉ伽楘"_XS8;
2020-04-23 15:20:48 +02:00
//char* s = str.data(42);
2020-04-23 22:43:35 +02:00
2020-04-25 11:59:07 +02:00
//size_t size1 = sizeof("В ")-1;
//size_t size2 = sizeof("ы")-1;
//size_t size3 = sizeof("х ")-1;
//size_t size4 = sizeof("о ")-1;
//size_t size5 = sizeof("д")-1;
# ifdef _MSC_VER
//SetConsoleOutputCP(65001);
# endif
2020-04-23 22:43:35 +02:00
2021-04-08 17:52:48 +02:00
// {
// NSDate *methodStart = [NSDate date];
//
// XString8 xs8;
// for ( size_t i = 0 ; i < 300000 ; ++i ) {
// xs8.S8Catf("foo");
// }
//
// NSDate *methodFinish = [NSDate date];
// NSTimeInterval executionTime = [methodFinish timeIntervalSinceDate:methodStart];
// NSLog(@"executionTime = %f", executionTime);
// }
// {
// NSDate *methodStart = [NSDate date];
//
// XBuffer<char> xs8;
// for ( size_t i = 0 ; i < 300000 ; ++i ) {
// xs8.S8Catf("foo");
// }
//
// NSDate *methodFinish = [NSDate date];
// NSTimeInterval executionTime = [methodFinish timeIntervalSinceDate:methodStart];
// NSLog(@"executionTime = %f", executionTime);
// }
//exit(1);
2020-04-24 11:30:09 +02:00
//teststrncpy_<XString>("utf8", testStringMultiCodedArray[1].utf8, testStringMultiCodedArray[1].wchar);
//testindexOf(XString, utf8, utf16);
2020-04-25 13:33:01 +02:00
//testCompare(XString, utf8, utf16);
2020-04-24 11:30:09 +02:00
//testindexOf_<XString>(testStringMultiCoded4CaseArray[0].utf8);
2020-04-29 22:34:28 +02:00
//testTakeValueFrom_<XString16>(testStringMultiCodedArray[0].utf16, testStringMultiCodedArray[0].utf16);
2020-04-23 22:43:35 +02:00
2020-08-10 13:06:06 +02:00
// int i = utf8_size_of_utf32_string(U"煉璉秊練");
XString32 xsw = U " éàùœ°æƒÌÚ® " _XS32 ;
// char32_t c1 = xsw[0];
// char32_t c2 = xsw[1];
xsw . insertAtPos ( L ' A ' , 2 ) ;
XStringW xsw2 ;
2021-04-08 17:52:48 +02:00
xsw2 . strncpy ( xsw . s ( ) , 1 ) ;
2020-08-10 13:06:06 +02:00
2020-08-16 11:33:41 +02:00
{
XString8 xs8 = " to TRIM " _XS8 ;
xs8 . trim ( ) ;
if ( xs8 ! = " to TRIM " _XS8 ) {
nbTestFailed + = 1 ;
}
}
{
XString8 xs8 = " Apple Inc. " _XS8 ;
xs8 . trim ( ) ;
if ( xs8 ! = " Apple Inc. " _XS8 ) {
nbTestFailed + = 1 ;
}
}
2020-11-02 14:45:11 +01:00
# ifndef _MSC_VER
2020-08-16 15:21:12 +02:00
{
XString8 xsReplace = " babcbdeb " _XS8 ;
xsReplace . replaceAll ( U ' b ' , U ' 𐅃 ' ) ;
}
{
XString8 xsReplace2 = " 𐄔a𐄔c𐄔de𐄔 " _XS8 ;
xsReplace2 . replaceAll ( U ' 𐄔 ' , U ' x ' ) ;
}
2020-11-02 14:45:11 +01:00
# endif
2020-08-16 15:21:12 +02:00
// {
// XString8 xsReplace = "𐅃𐅃ab"_XS8;
// xsReplace.replaceAll("𐅃𐅃"_XS8, "12"_XS8);
// if ( xsReplace != "12ab"_XS8 ) {
// nbTestFailed += 1;
// }
// }
// TODO proper test
// XSW XS8
{
XString8 xsReplace = " 12ab12cd12ef1212 " _XS8 ;
xsReplace . replaceAll ( L " 12 " _XSW , L " 𐅃𐅃 " _XSW ) ;
if ( xsReplace ! = " 𐅃𐅃ab𐅃𐅃cd𐅃𐅃ef𐅃𐅃𐅃𐅃 " _XS8 ) {
nbTestFailed + = 1 ;
}
}
{
XString8 xsReplace = " 𐅃𐅃ab𐅃𐅃cd𐅃𐅃ef𐅃𐅃𐅃𐅃 " _XS8 ;
xsReplace . replaceAll ( L " 𐅃𐅃 " _XSW , L " 12 " _XSW ) ;
if ( xsReplace ! = " 12ab12cd12ef1212 " _XS8 ) {
nbTestFailed + = 1 ;
}
}
// XS8 XSW
{
XStringW xsReplace = L " 12ab12cd12ef1212 " _XSW ;
xsReplace . replaceAll ( " 12 " _XS8 , " 𐅃𐅃 " _XS8 ) ;
if ( xsReplace ! = " 𐅃𐅃ab𐅃𐅃cd𐅃𐅃ef𐅃𐅃𐅃𐅃 " _XS8 ) {
nbTestFailed + = 1 ;
}
}
{
XStringW xsReplace = L " 𐅃𐅃ab𐅃𐅃cd𐅃𐅃ef𐅃𐅃𐅃𐅃 " _XSW ;
xsReplace . replaceAll ( " 𐅃𐅃 " _XS8 , " 12 " _XS8 ) ;
if ( xsReplace ! = " 12ab12cd12ef1212 " _XS8 ) {
nbTestFailed + = 1 ;
}
}
//XSW XSW
{
XStringW xsReplace = L " 12ab12cd12ef1212 " _XSW ;
xsReplace . replaceAll ( L " 12 " _XSW , L " 𐅃𐅃 " _XSW ) ;
if ( xsReplace ! = L " 𐅃𐅃ab𐅃𐅃cd𐅃𐅃ef𐅃𐅃𐅃𐅃 " _XSW ) {
nbTestFailed + = 1 ;
}
}
{
XStringW xsReplace = L " 𐅃𐅃ab𐅃𐅃cd𐅃𐅃ef𐅃𐅃𐅃𐅃 " _XSW ;
xsReplace . replaceAll ( L " 𐅃𐅃 " _XSW , L " 12 " _XSW ) ;
if ( xsReplace ! = L " 12ab12cd12ef1212 " _XSW ) {
nbTestFailed + = 1 ;
}
}
// XS8 XS8
{
XString8 xsReplace = " 12ab12cd12ef1212 " _XS8 ;
xsReplace . replaceAll ( " 12 " _XS8 , " 𐅃𐅃 " _XS8 ) ;
if ( xsReplace ! = " 𐅃𐅃ab𐅃𐅃cd𐅃𐅃ef𐅃𐅃𐅃𐅃 " _XS8 ) {
nbTestFailed + = 1 ;
}
}
{
XString8 xsReplace = " 𐅃𐅃ab𐅃𐅃cd𐅃𐅃ef𐅃𐅃𐅃𐅃 " _XS8 ;
xsReplace . replaceAll ( " 𐅃𐅃 " _XS8 , " 12 " _XS8 ) ;
if ( xsReplace ! = " 12ab12cd12ef1212 " _XS8 ) {
nbTestFailed + = 1 ;
}
}
//
{
XString8 xsReplace = " ab𐅃𐅃cd𐅃𐅃𐅃𐅃ef " _XS8 ;
xsReplace . replaceAll ( " 𐅃𐅃 " _XS8 , " 12 " _XS8 ) ;
if ( xsReplace ! = " ab12cd1212ef " _XS8 ) {
nbTestFailed + = 1 ;
}
}
{
XString8 xsReplace = " ab𐅃𐅃cd𐅃𐅃𐅃ef " _XS8 ;
xsReplace . replaceAll ( " 𐅃𐅃 " _XS8 , " 12 " _XS8 ) ;
if ( xsReplace ! = " ab12cd12𐅃ef " _XS8 ) {
nbTestFailed + = 1 ;
}
}
2020-08-15 15:47:56 +02:00
2020-08-10 13:06:06 +02:00
// Quick check of stealValueFrom. TOTO proper test
2020-08-09 17:55:30 +02:00
{
char * p = ( char * ) malloc ( 11 ) ;
strcpy ( p , " 0123456789 " ) ;
XString8 t8 ;
t8 . stealValueFrom ( p ) ;
char * p2 = ( char * ) malloc ( 11 ) ;
strcpy ( p2 , " 9876543210 " ) ;
t8 . stealValueFrom ( p2 ) ;
}
{
char16_t * p16 = ( char16_t * ) malloc ( 11 * sizeof ( char16_t ) ) ;
memcpy ( ( char * ) p16 , ( char * ) u " 0123456789 " , 11 * sizeof ( char16_t ) ) ;
XString16 t16 ;
t16 . stealValueFrom ( p16 ) ;
}
2021-01-31 10:50:23 +01:00
{
XString8 xs8 = " 11 " _XS8 ;
bool b = xs8 . startWithOrEqualTo ( " 112 " ) ;
if ( b ) {
nbTestFailed + = 1 ;
}
}
2020-08-09 17:55:30 +02:00
2020-04-29 22:34:28 +02:00
TEST_ALL_CLASSES ( testDefaultCtor , __TEST0 ) ;
TEST_ALL_CLASSES ( testEmpty , __TEST0 ) ;
TEST_ALL_CLASSES ( testTakeValueFrom , TEST_ALL_UTF ) ;
TEST_ALL_CLASSES ( testchar32At , TEST_ALL_INTEGRAL ) ;
TEST_ALL_CLASSES ( testdataSized , TEST_ALL_INTEGRAL ) ;
2020-04-23 15:20:48 +02:00
2020-04-29 22:34:28 +02:00
TEST_ALL_CLASSES ( teststrncpy , TEST_ALL_UTF ) ; // 26944 tests
TEST_ALL_CLASSES ( teststrcat , TEST_ALL_UTF_ALL_UTF ) ;
TEST_ALL_CLASSES ( teststrncat , TEST_ALL_UTF_ALL_UTF ) ; // 2101632 tests
2020-04-23 15:20:48 +02:00
2020-04-27 11:50:49 +02:00
TEST_ALL_CLASSES ( testSubString , __TEST0 ) ;
2020-04-29 22:34:28 +02:00
TEST_ALL_CLASSES ( testCompare , TEST_ALL_UTF ) ;
TEST_ALL_CLASSES ( testindexOf , TEST_ALL_UTF ) ;
TEST_ALL_CLASSES ( testlastChar , __TEST0 ) ;
2020-04-25 13:33:01 +02:00
TEST_ALL_CLASSES ( testtrim , __TEST0 ) ;
2020-04-26 12:12:05 +02:00
TEST_ALL_CLASSES ( teststartWith , __TEST0 ) ;
2020-04-27 11:50:49 +02:00
TEST_ALL_CLASSES ( testbasename , __TEST0 ) ;
2020-04-25 11:59:07 +02:00
2020-04-23 15:20:48 +02:00
2020-04-05 14:25:39 +02:00
//
2020-04-23 15:20:48 +02:00
//// str2.insert(1, str);
2020-04-05 14:25:39 +02:00
//
2020-04-24 11:30:09 +02:00
// str2.ssprintf("%c", 'a'); // signle UTF8 ascii char
2020-04-23 15:20:48 +02:00
// if ( str2 != L"a" ) return
// IMPORTANT : you can't pass a litteral char in a vararg function with Visual Studio (Microsoft strikes again :-).
// At least, you got a warning C4066
// IMPORTANT2 : Litteral string containing UTF16 char are WRONG. And you don't get a warning !!! If litteral is only ascii, it's ok.
// Maybe it's compilation option but I didn't find them.
2020-03-12 15:40:38 +01:00
2020-04-23 15:20:48 +02:00
# ifdef JIEF_DEBUG
2020-05-03 17:07:34 +02:00
if ( displayMainTitle ) {
if ( nbTestFailed = = 0 ) printf ( " All %d XString tests succeeded. \n " , nbTest ) ;
else printf ( " %d XString tests succeeded out of %d. \n " , nbTest - nbTestFailed , nbTest ) ;
}
2020-04-23 15:20:48 +02:00
# endif
2020-04-24 11:30:09 +02:00
return nbTestFailed > 0 ;
2020-03-12 15:40:38 +01:00
}
2020-04-29 22:34:28 +02:00
//const char* p1 = "foo/bar"; // basename returns bar
//const char* p1 = "foo/"; // basename returns foo
//const char* p1 = "foo//"; // basename returns foo
//const char* p1 = "foo///"; // basename returns foo
//const char* p1 = ""; // basename returns "."
//const char* p1 = " foo/bar "; // basename returns "bar "
//const char* p1 = " foo "; // basename returns " foo "
//const char* p1 = " "; // basename returns " "
//const char* p2 = basename((char*)p1);