RegressionTests__StringTests.st
changeset 1420 d5438e5960ed
parent 1419 cfc3eb59fbcf
child 1421 3236b9c7d993
equal deleted inserted replaced
1419:cfc3eb59fbcf 1420:d5438e5960ed
     1 "{ Package: 'exept:regression' }"
     1 "{ Package: 'exept:regression' }"
     2 
     2 
     3 "{ NameSpace: RegressionTests }"
     3 "{ NameSpace: RegressionTests }"
     4 
     4 
     5 TestCase subclass:#StringTests
     5 TestCase subclass:#StringTests
     6 	instanceVariableNames:''
     6         instanceVariableNames:''
     7 	classVariableNames:''
     7         classVariableNames:''
     8 	poolDictionaries:''
     8         poolDictionaries:''
     9 	category:'tests-Regression-Collections'
     9         category:'tests-Regression-Collections'
    10 !
    10 !
    11 
    11 
    12 
    12 
    13 !StringTests methodsFor:'helpers'!
    13 !StringTests methodsFor:'helpers'!
    14 
    14 
   132 
   132 
   133 !StringTests methodsFor:'tests'!
   133 !StringTests methodsFor:'tests'!
   134 
   134 
   135 test01_access
   135 test01_access
   136     0 to:33 do:[:l |
   136     0 to:33 do:[:l |
   137 	|str|
   137         |str|
   138 
   138 
   139 	str := String new:l.
   139         str := String new:l.
   140 	str atAllPut:(Character space).
   140         str atAllPut:(Character space).
   141 	self assert:( str isBlank ).
   141         self assert:( str isBlank ).
   142 
   142 
   143 	1 to:l do:[:pos |
   143         1 to:l do:[:pos |
   144 	    str at:pos put:$a.
   144             str at:pos put:$a.
   145 	    self assert:( str isBlank not ).
   145             self assert:( str isBlank not ).
   146 	    str at:pos put:(Character space).
   146             str at:pos put:(Character space).
   147 	].
   147         ].
   148     ].
   148     ].
   149     self runAccessTestsOnInstancesOf:String
   149     self runAccessTestsOnInstancesOf:String
   150 
   150 
   151     "
   151     "
   152      self new test01_access
   152      self new test01_access
   155 
   155 
   156 test02_subclassAccess
   156 test02_subclassAccess
   157     |myStringClass s1|
   157     |myStringClass s1|
   158 
   158 
   159     Class withoutUpdatingChangesDo:[
   159     Class withoutUpdatingChangesDo:[
   160 	myStringClass := String
   160         myStringClass := String
   161 			subclass:#'MyString'
   161                         subclass:#'MyString'
   162 			instanceVariableNames:'foo'
   162                         instanceVariableNames:'foo'
   163 			classVariableNames:''
   163                         classVariableNames:''
   164 			poolDictionaries:nil.
   164                         poolDictionaries:nil.
   165 	myStringClass compile:'foo ^foo'.
   165         myStringClass compile:'foo ^foo'.
   166 	myStringClass compile:'foo:arg foo := arg'.
   166         myStringClass compile:'foo:arg foo := arg'.
   167     ].
   167     ].
   168 
   168 
   169     s1 := (myStringClass new:5) replaceFrom:1 with:'hello'.
   169     s1 := (myStringClass new:5) replaceFrom:1 with:'hello'.
   170     self assert:(s1 foo isNil).
   170     self assert:(s1 foo isNil).
   171     self assert:(s1 size == 5).
   171     self assert:(s1 size == 5).
   185     "
   185     "
   186 !
   186 !
   187 
   187 
   188 test10_Contains8BitCharacters
   188 test10_Contains8BitCharacters
   189     0 to:65 do:[:l |
   189     0 to:65 do:[:l |
   190 	|str|
   190         |str|
   191 
   191 
   192 	str := String new:l.
   192         str := String new:l.
   193 	str atAllPut:(Character value:16r7F).
   193         str atAllPut:(Character value:16r7F).
   194 	self assert:( str contains8BitCharacters not ).
   194         self assert:( str contains8BitCharacters not ).
   195 
   195 
   196 	1 to:l do:[:pos |
   196         1 to:l do:[:pos |
   197 	    str at:pos put:(Character value:16r80).
   197             str at:pos put:(Character value:16r80).
   198 	    self assert:( str contains8BitCharacters ).
   198             self assert:( str contains8BitCharacters ).
   199 	    str at:pos put:(Character value:16r7F).
   199             str at:pos put:(Character value:16r7F).
   200 	].
   200         ].
   201     ].
   201     ].
   202 
   202 
   203     "
   203     "
   204      self new test10_Contains8BitCharacters
   204      self new test10_Contains8BitCharacters
   205     "
   205     "
   211     parserFlags := Compiler new parserFlags copy.
   211     parserFlags := Compiler new parserFlags copy.
   212     parserFlags stringsAreImmutable:true.
   212     parserFlags stringsAreImmutable:true.
   213     parserFlags arraysAreImmutable:true.
   213     parserFlags arraysAreImmutable:true.
   214 
   214 
   215     Class withoutUpdatingChangesDo:[
   215     Class withoutUpdatingChangesDo:[
   216 	myClass := Object
   216         myClass := Object
   217 			subclass:#'MyClass'
   217                         subclass:#'MyClass'
   218 			instanceVariableNames:''
   218                         instanceVariableNames:''
   219 			classVariableNames:''
   219                         classVariableNames:''
   220 			poolDictionaries:nil.
   220                         poolDictionaries:nil.
   221 
   221 
   222 	compiler := myClass compilerClass new.
   222         compiler := myClass compilerClass new.
   223 	compiler parserFlags:parserFlags.
   223         compiler parserFlags:parserFlags.
   224 	compiler compile:'lit1 ^''hello''' forClass:myClass install:true.
   224         compiler compile:'lit1 ^''hello''' forClass:myClass install:true.
   225 
   225 
   226 	compiler := myClass compilerClass new.
   226         compiler := myClass compilerClass new.
   227 	compiler parserFlags:parserFlags.
   227         compiler parserFlags:parserFlags.
   228 	compiler compile:'lit2 ^#(''foo'' ''bar'' ''baz'')' forClass:myClass install:true.
   228         compiler compile:'lit2 ^#(''foo'' ''bar'' ''baz'')' forClass:myClass install:true.
   229     ].
   229     ].
   230 
   230 
   231     s1 := myClass new perform:#lit1.
   231     s1 := myClass new perform:#lit1.
   232     self assert:(s1 isString).
   232     self assert:(s1 isString).
   233     self assert:(s1 class == ImmutableString).
   233     self assert:(s1 class == ImmutableString).
   319 
   319 
   320     "
   320     "
   321      self new test41_contains
   321      self new test41_contains
   322     "
   322     "
   323 !
   323 !
       
   324 
       
   325 test42_includesAny
       
   326     |s|
       
   327 
       
   328     1 to:20 do:[:na |
       
   329         s := (String new:na withAll:$a),'bla bla 1234'.
       
   330         self assert:( s includesAny:'12').
       
   331         self assert:( s includesAny:'21').
       
   332         self assert:( s includesAny:'15').
       
   333         self assert:( s includesAny:'51').
       
   334         self assert:( s includesAny:'45').
       
   335         self assert:( s includesAny:'54').
       
   336         self assert:( s includesAny:'56') not.
       
   337     ].
       
   338     
       
   339     "
       
   340      self new test42_includesAny
       
   341     "
   324 
   342 
   325 test42_occurrencesOf
   343 test42_occurrencesOf
   326     |s|
   344     |s|
   327 
   345 
   328     "/             12345678901
   346     "/             12345678901
   502     "
   520     "
   503      self new test51_substrings
   521      self new test51_substrings
   504     "
   522     "
   505 !
   523 !
   506 
   524 
       
   525 test52_indexOfSeparator
       
   526     |s|
       
   527 
       
   528     self assert:('' indexOfSeparator) == 0.
       
   529     1 to:20 do:[:n |
       
   530         s := (String new:n withAll:$a).
       
   531         1 to:20 do:[:start |
       
   532             self assert:(s indexOfSeparatorStartingAt:start) == 0.
       
   533         ].    
       
   534     ].    
       
   535     1 to:20 do:[:n |
       
   536         s := (String new:n withAll:$a),' '.
       
   537         1 to:n do:[:start |
       
   538             self assert:(s indexOfSeparatorStartingAt:start) == (n+1).
       
   539         ].    
       
   540     ].    
       
   541     
       
   542     { 
       
   543         Character space .
       
   544         Character tab .
       
   545         Character return .
       
   546         Character lf .
       
   547     } do:[:sep |
       
   548         1 to:20 do:[:na |
       
   549             1 to:20 do:[:nb |
       
   550                 s := (String new:na withAll:$a),sep,(String new:na withAll:$a).
       
   551                 1 to:na do:[:start |
       
   552                     self assert:(s indexOfSeparatorStartingAt:start) == (na+1).
       
   553                 ]
       
   554             ]
       
   555         ].    
       
   556     ].
       
   557     
       
   558     { 
       
   559         Character esc .
       
   560         Character null .
       
   561         $a .
       
   562     } do:[:nonSep |
       
   563         1 to:20 do:[:na |
       
   564             1 to:20 do:[:nb |
       
   565                 s := (String new:na withAll:$a),nonSep,(String new:na withAll:$a).
       
   566                 1 to:20 do:[:start |
       
   567                     self assert:(s indexOfSeparatorStartingAt:start) == 0.
       
   568                 ]
       
   569             ]
       
   570         ].    
       
   571     ].
       
   572 
       
   573      s := String new:1000 withAll:$a.
       
   574      self assert:(s indexOfSeparatorStartingAt:1) == 0.
       
   575      400 to: 417 do:[:i |
       
   576         s := String new:1000 withAll:$a.
       
   577         s at:i put:(Character space).
       
   578         self assert:(s indexOfSeparatorStartingAt:1) == i.
       
   579         
       
   580         s := String new:1000 withAll:$a.
       
   581         s at:i put:(Character return).
       
   582         self assert:(s indexOfSeparatorStartingAt:1) == i.
       
   583      ] 
       
   584     
       
   585     "
       
   586      self new test52_indexOfSeparator
       
   587     "
       
   588 !
       
   589 
       
   590 test54_occurrencesOf
       
   591     |s|
       
   592 
       
   593     self assert:('' occurrencesOf:$a) == 0.
       
   594     1 to:20 do:[:n |
       
   595         s := (String new:n withAll:$a).
       
   596         self assert:(s occurrencesOf:$a) == n.
       
   597     ].    
       
   598     1 to:20 do:[:na |
       
   599         1 to:20 do:[:nb |
       
   600             s := (String new:na withAll:$a),(String new:nb withAll:$b).
       
   601             self assert:(s occurrencesOf:$a) == na.
       
   602         ].    
       
   603     ].    
       
   604     1 to:40 do:[:n |
       
   605         s := String new:n withAll:$a.
       
   606         1 to:n by:2 do:[:n |
       
   607             s at:n put:$b.
       
   608         ].    
       
   609         self assert:(s occurrencesOf:$a) == (n // 2).
       
   610     ].    
       
   611     
       
   612     s := String new:1000 withAll:$a.
       
   613     self assert:(s occurrencesOf:$a) == 1000.
       
   614     1 to:1000 do:[:i |
       
   615         s at:i put:$b.
       
   616         self assert:(s occurrencesOf:$a) == (1000-i).
       
   617         self assert:(s occurrencesOf:$b) == i.
       
   618     ].
       
   619     
       
   620     "
       
   621      self new test54_occurrencesOf
       
   622     "
       
   623 !
       
   624 
   507 test60_hash
   625 test60_hash
   508     "
   626     "
   509     As of 2013-01-09 for strings of size 7 String & Unicode16String hash
   627     As of 2013-01-09 for strings of size 7 String & Unicode16String hash
   510     returned different values. This test checks this
   628     returned different values. This test checks this
   511     "
   629     "
   630 
   748 
   631     str from:-1 to:-2 put:$G.
   749     str from:-1 to:-2 put:$G.
   632     self assert: str = 'FFF'.
   750     self assert: str = 'FFF'.
   633 
   751 
   634     self should:[
   752     self should:[
   635 	str from:-1 to:-1 put:$x
   753         str from:-1 to:-1 put:$x
   636     ] raise:SubscriptOutOfBoundsError.
   754     ] raise:SubscriptOutOfBoundsError.
   637     self assert: str = 'FFF'.
   755     self assert: str = 'FFF'.
   638 
   756 
   639     self should:[
   757     self should:[
   640 	str from:-1 to:1 put:$x
   758         str from:-1 to:1 put:$x
   641     ] raise:SubscriptOutOfBoundsError.
   759     ] raise:SubscriptOutOfBoundsError.
   642     self assert: str = 'FFF'.
   760     self assert: str = 'FFF'.
   643 !
   761 !
   644 
   762 
   645 test90_hash
   763 test90_hash