RegressionTests__StringTests.st
author Claus Gittinger <cg@exept.de>
Sat, 01 Mar 2014 14:53:56 +0100
changeset 1060 ae418384712d
parent 962 2fca937dc59b
child 1103 373c0f5e539d
permissions -rw-r--r--
category

"{ Package: 'exept:regression' }"

"{ NameSpace: RegressionTests }"

TestCase subclass:#StringTests
	instanceVariableNames:''
	classVariableNames:''
	poolDictionaries:''
	category:'tests-Regression-Collections'
!


!StringTests methodsFor:'helpers'!

runAccessTestsOnInstancesOf:aStringClass
    |str s0 s1 s2 s3 s4 t|

    0 to:33 do:[:l |
        str := aStringClass new:l.
        str atAllPut:(Character space).
        self assert:( str isBlank ).

        1 to:l do:[:pos |
            str at:pos put:$a.
            self assert:( str isBlank not ).
            str at:pos put:(Character space).
        ].
    ].

    s0 := aStringClass new:0.
    self assert:s0 isEmpty.
    self assert:s0 isEmptyOrNil.
    self assert:s0 size == 0.

    s1 := (aStringClass new:5) replaceFrom:1 with:'hello'.
    s2 := (aStringClass new:6) replaceFrom:1 with:' world'.
    self assert:(s1 , s2) size == 11.
    self assert:(s1 , s2) class == aStringClass.
    self assert:(s1 = 'hello').
    self assert:(s2 = ' world').
    self assert:(s1 size == 5).
    self assert:(s2 size == 6).

    self assert:(s1 = 'hello').
    self assert:(s1 asSymbol == #'hello').
    self assert:(s1 copyFrom:1) = 'hello'.
    self assert:(s1 copyFrom:1) class == aStringClass.
    self assert:(s1 copyFrom:1 to:3) = 'hel'.
    self assert:(s1 copyFrom:1 to:3) class == aStringClass.
    self assert:(s1 copyWith:$X) = 'helloX'.
    self assert:(s1 copyWith:$X) class == aStringClass.
    self assert:(s1 endsWith:'llo').
    self assert:(s1 startsWith:'hel').

    self assert:(t := s1 identityIndexOf:$h) == 1 description:('identityIndex is %1 (should be 1)' bindWith:t).
    self assert:(t := s1 identityIndexOf:$l) == 3 description:('identityIndex is %1 (should be 3)' bindWith:t).
    self assert:(t := s1 identityIndexOf:$L) == 0 description:('identityIndex is %1 (should be 0)' bindWith:t).

    self assert:(s1 identityIndexOf:$h startingAt:1) == 1.
    self assert:(s1 identityIndexOf:$h startingAt:2) == 0.
    self assert:(s1 identityIndexOf:$e startingAt:1) == 2.
    self assert:(s1 identityIndexOf:$e startingAt:2) == 2.
    self assert:(s1 identityIndexOf:$l startingAt:1) == 3.
    self assert:(s1 identityIndexOf:$l startingAt:2) == 3.
    self assert:(s1 identityIndexOf:$l startingAt:3) == 3.
    self assert:(s1 identityIndexOf:$l startingAt:4) == 4.
    self assert:(s1 identityIndexOf:$l startingAt:5) == 0.
    self assert:(s1 identityIndexOf:$l startingAt:6) == 0.

    self assert:(s1 indexOf:$h) == 1.
    self assert:(s1 indexOf:$l) == 3.
    self assert:(s1 indexOf:$L) == 0.
    self assert:(s1 indexOf:$A) == 0.

    self assert:(s1 indexOf:$h startingAt:1) == 1.
    self assert:(s1 indexOf:$h startingAt:2) == 0.
    self assert:(s1 indexOf:$e startingAt:1) == 2.
    self assert:(s1 indexOf:$e startingAt:2) == 2.
    self assert:(s1 indexOf:$l startingAt:1) == 3.
    self assert:(s1 indexOf:$l startingAt:2) == 3.
    self assert:(s1 indexOf:$l startingAt:3) == 3.
    self assert:(s1 indexOf:$l startingAt:4) == 4.
    self assert:(s1 indexOf:$l startingAt:5) == 0.
    self assert:(s1 indexOf:$l startingAt:6) == 0.

    self assert:(s1 includes:$l).
    self assert:(s1 includes:$L) not.

    self assert:(s1 includesAny:'a') not.
    self assert:(s1 includesAny:'ab') not.
    self assert:(s1 includesAny:'abc') not.
    self assert:(s1 includesAny:'abcd') not.
    self assert:(s1 includesAny:'abcde').
    self assert:(s1 includesAny:'e').
    self assert:(s1 includesAny:'ae').
    self assert:(s1 includesAny:'ea').
    self assert:(s1 includesAny:'abe').
    self assert:(s1 includesAny:'eab').
    self assert:(s1 includesAny:'aeb').
    self assert:(s1 includesAny:'abce').
    self assert:(s1 includesAny:'eabc').
    self assert:(s1 includesAny:'aebc').
    self assert:(s1 includesAny:'abec').
    self assert:(s1 includesAny:'abcde').
                                                  " 12345678901234567890 "
    s3 := (aStringClass new:20) replaceFrom:1 with:'12 45,78;01.34-67+90'.
    t := s3 asCollectionOfSubstringsSeparatedBy:$,.
    self assert:(t size = 2).
    self assert:(t first = '12 45').
    self assert:(t second = '78;01.34-67+90').

    t := s3 asCollectionOfSubstringsSeparatedByAny:',;. '.
    self assert:(t size = 5).
    self assert:(t first = '12').
    self assert:(t second = '45').
    self assert:(t third = '78').
    self assert:(t fourth = '01').
    self assert:(t fifth = '34-67+90').

    s4 := (aStringClass new:10) replaceFrom:1 with:'123',Character tab,'567',Character cr,'90'.
    self assert:(s4 size = 10).
    self assert:(s4 indexOfSeparatorStartingAt:1) = 4.
    self assert:(s4 indexOfSeparatorStartingAt:3) = 4.
    self assert:(s4 indexOfSeparatorStartingAt:4) = 4.
    self assert:(s4 indexOfSeparatorStartingAt:5) = 8.
    self assert:(s4 indexOfControlCharacterStartingAt:1) = 4.
    self assert:(s4 indexOfNonSeparatorStartingAt:1) = 1.
    self assert:(s4 indexOfNonSeparatorStartingAt:3) = 3.
    self assert:(s4 indexOfNonSeparatorStartingAt:4) = 5.
    self assert:(s4 indexOfNonSeparatorStartingAt:5) = 5.
! !

!StringTests methodsFor:'tests'!

test01_access
    0 to:33 do:[:l |
	|str|

	str := String new:l.
	str atAllPut:(Character space).
	self assert:( str isBlank ).

	1 to:l do:[:pos |
	    str at:pos put:$a.
	    self assert:( str isBlank not ).
	    str at:pos put:(Character space).
	].
    ].
    self runAccessTestsOnInstancesOf:String

    "
     self new test01_access
    "
!

test02_subclassAccess
    |myStringClass s1|

    Class withoutUpdatingChangesDo:[
        myStringClass := String
                        subclass:#'MyString'
                        instanceVariableNames:'foo'
                        classVariableNames:''
                        poolDictionaries:nil.
        myStringClass compile:'foo ^foo'.
        myStringClass compile:'foo:arg foo := arg'.
    ].

    s1 := (myStringClass new:5) replaceFrom:1 with:'hello'.
    self assert:(s1 foo isNil).
    self assert:(s1 size == 5).
    s1 foo:16r11413344.         "/ ensure there is a $A inside (hex 41)
    self assert:(s1 foo == 16r11413344).
    self assert:(s1 = 'hello').
    self assert:(s1 deepCopy foo == 16r11413344).

    self assert:((s1 indexOf:$A) == 0).                 "/ should not find the A
    self assert:((s1 identityIndexOf:$A) == 0).         "/ should not find the A
    self assert:((s1 findFirst:[:ch | ch == $A]) == 0). "/ should not find the A

    self runAccessTestsOnInstancesOf:myStringClass.

    "
     self new test02_subclassAccess
    "
!

test10_Contains8BitCharacters
    0 to:65 do:[:l |
	|str|

	str := String new:l.
	str atAllPut:(Character value:16r7F).
	self assert:( str contains8BitCharacters not ).

	1 to:l do:[:pos |
	    str at:pos put:(Character value:16r80).
	    self assert:( str contains8BitCharacters ).
	    str at:pos put:(Character value:16r7F).
	].
    ].

    "
     self new test10_Contains8BitCharacters
    "
!

test20_literalsAreReadonly
    |myClass s1 l2 s2 parserFlags compiler|

    parserFlags := Compiler new parserFlags copy.
    parserFlags stringsAreImmutable:true.
    parserFlags arraysAreImmutable:true.

    Class withoutUpdatingChangesDo:[
        myClass := Object
                        subclass:#'MyClass'
                        instanceVariableNames:''
                        classVariableNames:''
                        poolDictionaries:nil.

        compiler := myClass compilerClass new.
        compiler parserFlags:parserFlags.
        compiler compile:'lit1 ^''hello''' forClass:myClass install:true.

        compiler := myClass compilerClass new.
        compiler parserFlags:parserFlags.
        compiler compile:'lit2 ^#(''foo'' ''bar'' ''baz'')' forClass:myClass install:true.
    ].

    s1 := myClass new perform:#lit1.
    self assert:(s1 isString).
    self assert:(s1 class == ImmutableString).
    self should:[ s1 at:1 put:$H ] raise:Error.

    l2 := myClass new perform:#lit2.
    self assert:(l2 isArray).
    self assert:(l2 class == ImmutableArray).
    self should:[ l2 at:1 put:$H ] raise:Error.
    s2 := l2 at:1.
    self assert:(s2 isString).
    self assert:(s2 class == ImmutableString).
    self should:[ s2 at:1 put:$H ] raise:Error.

    "
     self new test20_literalsAreReadonly
    "

    "Modified: / 02-08-2011 / 19:30:53 / cg"
!

test30_indexOf
    |i|
      "/  12345678901
    i := 'hello world' indexOfSubCollection:'world' startingAt:1 ifAbsent:0 caseSensitive:true.
    self assert:(i == 7).
    i := 'hello wOrLd' indexOfSubCollection:'world' startingAt:1 ifAbsent:0 caseSensitive:true.
    self assert:(i == 0).
    i := 'hello wOrLd' indexOfSubCollection:'world' startingAt:1 ifAbsent:0 caseSensitive:false.
    self assert:(i == 7).
    i := 'hello wOrLd' indexOfSubCollection:'ll' startingAt:1 ifAbsent:0 caseSensitive:false.
    self assert:(i == 3).

    i := 'hello wOrLd yellow' indexOfSubCollection:'ll' startingAt:1 ifAbsent:0 caseSensitive:false.
    self assert:(i == 3).
    i := 'hello wOrLd yellow' indexOfSubCollection:'ll' startingAt:3 ifAbsent:0 caseSensitive:false.
    self assert:(i == 3).
    i := 'hello wOrLd yellow' indexOfSubCollection:'ll' startingAt:4 ifAbsent:0 caseSensitive:false.
    self assert:(i == 15).

    i := 'hello wOrLd yellow' indexOfSubCollection:'low' startingAt:1 ifAbsent:0 caseSensitive:false.
    self assert:(i == 16).
    i := 'hello wOrLd yellow' indexOfSubCollection:'low' startingAt:17 ifAbsent:0 caseSensitive:false.
    self assert:(i == 0).
    i := 'hello wOrLd yellow' indexOfSubCollection:'low' startingAt:18 ifAbsent:0 caseSensitive:false.
    self assert:(i == 0).

    "
     self new test30_indexOf
    "
!

test40_indexOfAny
    |s i|

    s := 'Some Sample Generators (74035660-d1f6-11df-9ab3-00ff7b08316c)'.
    1 to:s size do:[:start |
        i := s indexOfAny:'-' startingAt:start.
        self assert:(i == 0 or:[ i >= start]).
    ].

    "
     self new test40_indexOfAny
    "

    "Created: / 29-10-2010 / 14:58:21 / cg"
!

test50_indexOf
    |s i|

    s := 'Some Sample Generators (74035660-d1f6-11df-9ab3-00ff7b08316c)'.
    1 to:s size do:[:start |
        i := s indexOf:$- startingAt:start.
        self assert:(i == 0 or:[ i >= start]).
    ].

    "
     self new test50_indexOf
    "

    "Created: / 29-10-2010 / 14:58:21 / cg"
!

test60_hash
    "
    As of 2013-01-09 for strings of size 7 String & Unicode16String hash
    returned different values. This test checks this
    "

    | tester |

    tester := [:s|
        self assert: s hash == s asUnicode16String hash 
             description: 'String and Unicode16String hashes differ!!'.
        self assert: s hash == s asUnicode32String hash 
             description: 'String and Unicode32String hashes differ!!'.
        self assert: s asUnicode16String hash == s asUnicode32String hash 
             description: 'Unicode16String and Unicode32String hashes differ!!'.
    ].

    tester value:'a'.
    tester value:'12345678'.
    tester value:'1234567'.
    tester value:'123456'.
    tester value:'boolean'.

    String allInstancesDo:[:each| tester value:each].
    Symbol allInstancesDo:[:each| tester value:each].

    "Created: / 09-01-2013 / 10:58:22 / Jan Vrany <jan.vrany@fit.cvut.cz>"
!

test70_storeString

    self assert: 'AAA' storeString = '''AAA'''.
    self assert: 'A''A''A' storeString = '''A''''A''''A'''.

    self assert: 'AAA' asImmutableString storeString = '''AAA'''.
    self assert: 'A''A''A' asImmutableString storeString = '''A''''A''''A'''.

    "Created: / 14-07-2013 / 19:17:33 / Jan Vrany <jan.vrany@fit.cvut.cz>"
! !

!StringTests class methodsFor:'documentation'!

version
    ^ '$Header$'
!

version_CVS
    ^ '$Header$'
! !