On Feb 14, 2007, at 16:18 , Damien Cassou wrote:
Execute the following piece of code:
"-------------------------------------------------------------------" |stream stream2| stream := WriteStream with: 'a test '. stream reset. stream nextPutAll: 'to test'. self assert: [stream contents = 'to test'].
"On the following line, you can remove 'copy'" "the problem is present without. It's here to prevent" " the streams from using the same collection" "because the compiler tries to avoid creating 2 identical" "strings" stream2 := WriteStream with: 'a test ' copy. stream2 nextPutAll: 'to test'.
"This assert passes but this is abnormal" self assert: [stream2 contents = 'to testto test'].
"This assert pass and this is abnormal too" "because the strings MUST be equal !!" self assert: [stream2 contents ~= 'a test to test'] "-------------------------------------------------------------------"
On my image, all the 3 tests pass. This is completely abnormal in my opinion.
It is normal.
You are modifying the 'a test ' literal into 'to test'. This modified string gets copied in the second test.
Lesson: never modify string literals.
- Bert -