#QUALITY by mawalch
class: RegressionTests::StringTests
added:7 methods
removed: #test30_indexOf
changed:8 methods
add more tests, enhance existing tests
--- a/RegressionTests__StringTests.st Thu May 12 21:10:32 2016 +0200
+++ b/RegressionTests__StringTests.st Fri May 13 01:47:34 2016 +0200
@@ -149,17 +149,19 @@
test01_access
0 to:33 do:[:l |
- |str|
+ |str|
- str := String new:l.
- str atAllPut:(Character space).
- self assert:( str isBlank ).
+ str := String new:l.
+ str atAllPut:(Character space).
+ self assert:( str isBlank ).
+ self assert:((str size == 0) or: [(str first) == Character space.]).
- 1 to:l do:[:pos |
- str at:pos put:$a.
- self assert:( str isBlank not ).
- str at:pos put:(Character space).
- ].
+ 1 to:l do:[:pos |
+ str at:pos put:$a.
+ self assert:( str isBlank not ).
+ self assert:((str at:pos) == $a).
+ str at:pos put:(Character space).
+ ].
].
self runAccessTestsOnInstancesOf:String
@@ -220,6 +222,120 @@
"
!
+test11_IsEmpty
+ |nul n|
+ nul := 0 asCharacter.
+ n := 1.
+ self assert:('' isEmpty).
+ self assert:(' ' isEmpty not).
+ self assert:(nul asCharacter asString isEmpty not).
+ self assert:(('' , nul) isEmpty not).
+ self assert:((String new:0) isEmpty).
+ self assert:((String new:17 withAll:nul) isEmpty not).
+
+ 32 timesRepeat:[
+ |s|
+
+ s := String new:(n + 17).
+ self assert:(s isEmpty not).
+ n := n * 2.
+ ].
+ self assert:((String new:17 withAll:nul) isEmpty not).
+ self assert:((String new:0 withAll:nul) isEmpty).
+ "
+ self new test10_Contains8BitCharacters"
+!
+
+test12_Comparison
+ 0 to: 255 do: [ :i |
+ |s t|
+
+ 1 to: 9 do: [ :j |
+ s := String new:j withAll:(i asCharacter).
+ t := s copy.
+
+ self assert:(s notNil).
+ self assert:(s = s).
+ self assert:(s ~= s) not.
+ self assert:(s < s) not.
+ self assert:(s > s) not.
+ self assert:(s = (s,s)) not.
+
+ self assert:(t notNil).
+ self assert:(s = t).
+ self assert:(s ~= t) not.
+ self assert:(s < t) not.
+ self assert:(s > t) not.
+
+ self assert:((s = nil) == ((s ~= nil) not)).
+ self assert:((s = '') == ((s ~= '') not)).
+ ].
+
+ s := i asCharacter asString.
+ 0 to: 255 do: [ :j |
+ t := j asCharacter asString.
+ self assert:(t notNil).
+
+ self assert:((s = t) == ((s ~= t) not)).
+ ]
+ ].
+
+ "
+ self new test12_Comparison
+ "
+!
+
+test15_CompareWithCollating
+ | nul |
+
+ nul := 0 asCharacter asString.
+ self assert:('' compareWith:'' collating:true) == 0.
+ self assert:('' compareWith:'' collating:false) == 0.
+ self assert:(nul compareWith:(255 asCharacter asString) collating:false) == -1.
+ self assert:((255 asCharacter asString) compareWith:nul collating:false) == 1.
+ self assert:((('' compareWith:nul collating:false) == 0) == ('' = nul)).
+
+ 0 to: 255 do: [ :i |
+ |s|
+
+ s := i asCharacter asString.
+ self assert:(s notNil).
+
+ self assert:('' compareWith:s collating:false) ~= 0.
+ self assert:('' compareWith:s collating:false) == ((s compareWith:'' collating:false) negated).
+
+ 0 to: 255 do: [ :j |
+ | t |
+ t := j asCharacter asString.
+ self assert:(t notNil).
+
+ self assert:(((s compareWith:t collating:false) == -1) == (s < t)).
+ self assert:(((s compareWith:t collating:false) == 0) == (s = t)).
+ self assert:(((s compareWith:t collating:false) == 1) == (s > t)).
+ self assert:(((s compareWith:t collating:true) == -1) == (s < t)).
+ self assert:(((s compareWith:t collating:true) == 0) == (s = t)).
+ self assert:(((s compareWith:t collating:true) == 1) == (s > t)).
+ ]
+ ].
+
+ 0 to: 9 do: [ :i |
+ 0 to: 255 do: [ :j |
+ |s t|
+ s := String new:i withAll:(j asCharacter).
+ t := s copy.
+
+ self assert: (s compareWith:s collating:false) == 0.
+ self assert: (s compareWith:s collating:true) == 0.
+ self assert: (s compareWith:t collating:false) == 0.
+ self assert: (s compareWith:t collating:true) == 0
+ ]
+ ].
+
+ "
+ self new test15_CompareWithCollating
+ "
+!
+
test20_literalsAreReadonly
|myClass s1 l2 s2 parserFlags compiler|
@@ -264,37 +380,6 @@
"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
- "
-!
-
test30_indexOfSubCollection
|i|
"/ 12345678901
@@ -327,7 +412,7 @@
!
test40_indexOfAny
- |s i|
+ |s i collection|
s := 'Some Sample Generators (74035660-d1f6-11df-9ab3-00ff7b08316c)'.
1 to:s size do:[:start |
@@ -348,6 +433,34 @@
(i == 12) ifTrue:[ self assert:((s at:i) == $4) ].
].
+ collection := #($o, $l, $o).
+ "/ 12345678901
+ i := 'hello world' indexOfAny:collection startingAt:1.
+ self assert:(i == 3).
+ i := 'hello world' indexOfAny:collection startingAt:2.
+ self assert:(i == 3).
+ i := 'hello world' indexOfAny:collection startingAt:3.
+ self assert:(i == 3).
+ i := 'hello world' indexOfAny:collection startingAt:4.
+ self assert:(i == 4).
+ i := 'hello world' indexOfAny:collection startingAt:5.
+ self assert:(i == 5).
+ i := 'hello world' indexOfAny:collection startingAt:6.
+ self assert:(i == 8).
+ i := 'hello world' indexOfAny:collection startingAt:7.
+ self assert:(i == 8).
+ i := 'hello world' indexOfAny:collection startingAt:8.
+ self assert:(i == 8).
+ i := 'hello world' indexOfAny:collection startingAt:9.
+ self assert:(i == 10).
+ i := 'hello world' indexOfAny:collection startingAt:10.
+ self assert:(i == 10).
+ i := 'hello world' indexOfAny:collection startingAt:11.
+ self assert:(i == 0).
+ i := 'hello world' indexOfAny:collection startingAt:12.
+ self assert:(i == 0).
+ i := 'hello world' indexOfAny:collection startingAt:10000.
+ self assert:(i == 0).
"
self new test40_indexOfAny
"
@@ -362,7 +475,7 @@
self assert:(s contains:[:ch | ch == $a]).
self assert:(s contains:[:ch | ch == $A]) not.
self assert:(s contains:[:ch | ch == $1]) not.
-
+ self assert:(s contains:[:ch | ch == (0 asCharacter)]) not.
"
self new test41_contains
"
@@ -438,6 +551,21 @@
self assert:( 'hello world' indexOf:$l startingAt:5 ) == 10.
self assert:( 'hello world' indexOf:$d startingAt:5 ) == 11.
+ "/ 12345678901
+ self assert:(('hello world' indexOf:$o startingAt:1) == 5).
+ self assert:(('hello world' indexOf:$o startingAt:2) == 5).
+ self assert:(('hello world' indexOf:$o startingAt:3) == 5).
+ self assert:(('hello world' indexOf:$o startingAt:4) == 5).
+ self assert:(('hello world' indexOf:$o startingAt:5) == 5).
+ self assert:(('hello world' indexOf:$o startingAt:6) == 8).
+ self assert:(('hello world' indexOf:$o startingAt:7) == 8).
+ self assert:(('hello world' indexOf:$o startingAt:8) == 8).
+ self assert:(('hello world' indexOf:$o startingAt:9) == 0).
+ self assert:(('hello world' indexOf:$o startingAt:10) == 0).
+ self assert:(('hello world' indexOf:$o startingAt:11) == 0).
+ self assert:(('hello world' indexOf:$o startingAt:12) == 0).
+ self assert:(('hello world' indexOf:$o startingAt:10000) == 0).
+
"/ how about 0-bytes in between
self assert:( #[0 0 1 0 0] asString indexOf:(Character value:1) startingAt:1 ) == 3.
self assert:( #[0 0 1 0 0] asString indexOf:(Character value:0) startingAt:3 ) == 4.
@@ -568,7 +696,7 @@
!
test52_indexOfSeparator
- |s|
+ |j s|
self assert:('' indexOfSeparator) == 0.
1 to:20 do:[:n |
@@ -615,18 +743,50 @@
].
].
- s := String new:1000 withAll:$a.
- self assert:(s indexOfSeparatorStartingAt:1) == 0.
- 400 to: 417 do:[:i |
+ s := String new:1000 withAll:$a.
+ self assert:(s indexOfSeparatorStartingAt:1) == 0.
+ 400 to: 417 do:[:i |
s := String new:1000 withAll:$a.
s at:i put:(Character space).
self assert:(s indexOfSeparatorStartingAt:1) == i.
-
+
s := String new:1000 withAll:$a.
s at:i put:(Character return).
self assert:(s indexOfSeparatorStartingAt:1) == i.
- ]
-
+ ].
+
+ "/ 12345678901
+ j := 'hello world' indexOfSeparatorStartingAt:1.
+ self assert:(j == 6).
+ j := 'hello world ' indexOfSeparatorStartingAt:2.
+ self assert:(j == 6).
+ j := 'hello world ' indexOfSeparatorStartingAt:3.
+ self assert:(j == 6).
+ j := 'hello world ' indexOfSeparatorStartingAt:4.
+ self assert:(j == 6).
+ j := 'hello world ' indexOfSeparatorStartingAt:5.
+ self assert:(j == 6).
+ j := 'hello world ' indexOfSeparatorStartingAt:6.
+ self assert:(j == 6).
+ j := 'hello world ' indexOfSeparatorStartingAt:7.
+ self assert:(j == 12).
+ j := 'hello world ' indexOfSeparatorStartingAt:8.
+ self assert:(j == 12).
+ j := 'hello world ' indexOfSeparatorStartingAt:9.
+ self assert:(j == 12).
+ j := 'hello world ' indexOfSeparatorStartingAt:10.
+ self assert:(j == 12).
+ j := 'hello world ' indexOfSeparatorStartingAt:11.
+ self assert:(j == 12).
+ j := 'hello world ' indexOfSeparatorStartingAt:12.
+ self assert:(j == 12).
+ j := 'hello world' indexOfSeparatorStartingAt:12.
+ self assert:(j == 0).
+ j := 'hello world ' indexOfSeparatorStartingAt:13.
+ self assert:(j == 0).
+ j := 'hello world ' indexOfSeparatorStartingAt:10000.
+ self assert:(j == 0).
+
"
self new test52_indexOfSeparator
"
@@ -735,6 +895,170 @@
"Created: / 09-01-2013 / 10:58:22 / Jan Vrany <jan.vrany@fit.cvut.cz>"
!
+test65_concatenation
+ |strA strB|
+
+ 0 to:32 do:[:szA |
+ 0 to:32 do:[:szB |
+ |szAB|
+
+ strA := String new:szA withAll:$a.
+ strB := String new:szB withAll:$b.
+ szAB := szA + szB.
+ self assert:(szA = strA size).
+ self assert:(szB = strB size).
+ 10000 timesRepeat:[
+ |strAB|
+
+ strAB := strA , strB.
+
+ self assert:(szAB == strAB size).
+ ]
+ ]
+ ].
+
+ strA := strB := ''.
+ self assert: ((strA , strB) = '').
+ self assert: ((strA , strA) = '').
+ strA := 'a'.
+ self assert: ((strA , strA) = 'aa').
+ self assert: ((strA , strB) = 'a').
+ strA := ''.
+ strB := 'b'.
+ self assert: ((strA , strB) = 'b').
+ strA := 'b'.
+ strB := 'a'.
+ self assert: ((strB , strA) = 'ab').
+ "
+ self new test65_concatenation
+ "
+!
+
+test66_replace
+ |strA|
+
+ 1 to:64 do:[:szA |
+ strA := String new:szA withAll:$a.
+ 1 to:szA do:[:idx |
+ strA at:idx put:$*.
+
+ strA replaceAll:$* with:$#.
+
+ self assert:(strA at:idx) = $#.
+ self assert:(strA occurrencesOf:$#) = idx.
+ self assert:(strA count:[:ch | ch = $#]) = idx.
+ self assert:(strA occurrencesOf:$*) = 0.
+ self assert:(strA count:[:ch | ch = $*]) = 0.
+ self assert:(strA includes:$#).
+ self assert:(strA includes:$*) not.
+ ]
+ ]
+
+ "
+ self new test66_replace
+ "
+!
+
+test67_concatenationAnd
+ |strA strB strC|
+
+ 0 to:32 do:[:szA |
+ 0 to:32 do:[:szB |
+ 0 to:32 do:[:szC |
+ |szABC|
+ strA := String new:szA withAll:$a.
+ strB := String new:szB withAll:$b.
+ strC := String new:szC withAll:$c.
+
+ szABC := szA + szB + szC.
+ self assert:(szA = strA size).
+ self assert:(szB = strB size).
+ self assert:(szC = strC size).
+
+ 300 timesRepeat:[
+ |strABC|
+
+ strABC := strA concatenate:strB and:strC.
+
+ self assert:(szABC == strABC size).
+ ]
+ ]
+ ]
+ ].
+ strA := strB := strC := ''.
+ self assert: ((strA concatenate:strB and:strC) = '').
+ strA := 'a'.
+ self assert: ((strA concatenate:strB and:strC) = 'a').
+ strA := ''.
+ strB := 'b'.
+ self assert: ((strA concatenate:strB and:strC) = 'b').
+ strB := ''.
+ strC := 'c'.
+ self assert: ((strA concatenate:strB and:strC) = 'c').
+ strA := 'c'.
+ strB := 'b'.
+ strC := 'a'.
+ self assert: ((strC concatenate:strB and:strA) = 'abc').
+ "
+ self new test67_concatenationAnd
+ "
+!
+
+test68_concatenationAndAnd
+ |strA strB strC strD|
+
+ 0 to:32 do:[:szA |
+ strA := String new:szA withAll:$a.
+ self assert:(szA = strA size).
+ 0 to:32 do:[:szB |
+ strB := String new:szB withAll:$b.
+ self assert:(szB = strB size).
+ 0 to:32 do:[:szC |
+ strC := String new:szC withAll:$c.
+ self assert:(szC = strC size).
+ 0 to:32 do:[:szD |
+ |szABCD|
+ strD := String new:szD withAll:$d.
+
+ szABCD := szA + szB + szC + szD.
+
+ self assert:(szD = strD size).
+
+ 5 timesRepeat:[
+ |strABCD|
+
+ strABCD := strA concatenate:strB and:strC and:strD.
+
+ self assert:(szABCD == strABCD size).
+ ]
+ ]
+ ]
+ ]
+ ].
+
+ strA := strB := strC := strD := ''.
+ self assert: ((strA concatenate:strB and:strC and:strD) = '').
+ strA := 'a'.
+ self assert: ((strA concatenate:strB and:strC and:strD) = 'a').
+ strA := ''.
+ strB := 'b'.
+ self assert: ((strA concatenate:strB and:strC and:strD) = 'b').
+ strB := ''.
+ strC := 'c'.
+ self assert: ((strA concatenate:strB and:strC and:strD) = 'c').
+ strC := ''.
+ strD := 'd'.
+ self assert: ((strA concatenate:strB and:strC and:strD) = 'd').
+ strA := 'd'.
+ strB := 'c'.
+ strC := 'b'.
+ strD := 'a'.
+ self assert: ((strD concatenate:strC and:strB and:strA) = 'abcd').
+ "
+ self new test68_concatenationAndAnd
+ "
+!
+
test70_storeString
self assert: 'AAA' storeString = '''AAA'''.