Array : very weird behavior

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Array : very weird behavior

abdelghani ALIDRA
Hi everybody,

I am experiencing a very weird behavior with Arrays :

I writed a test case as follows ;

testChange
| bitArray1 |
bitArray1 := #(0 0 0 0).

(1 to: bitArray1 size)
do: [ :index ||temp|
temp := (bitArray1 at: index) + 1.
bitArray1 at: index put: (temp).
].
self assert: bitArray1 = #(1 1 1 1)


The thing is that the test succedes the first time but if I run it again it fails.
Moreover, if  I change the testChange arbitrarily (just add a space and save) the test succeeds the first time then it fails.....etc.

Actually bitArray1 := #(0 0 0 0) changes the value of bitArray1 in the first run but then bitArray1 does not change any more in the next runs.

The problem does not occur if  I replace bitArray1 := #(0 0 0 0). with bitArray1 := Array new:4 withAll: 0 .

Any idea where the problem can be?
Should I open a bug entry? where?

Abdelghani

Reply | Threaded
Open this post in threaded view
|

Re: Array : very weird behavior

Marcus Denker-4
Hello,

You are changing the “literal array” itself. The #(0 0 0 0) creates an array as part of the compiledMethod
and you are changing this. (Yes, many people think that all literals should be immutable…).

If you add a #copy it will work.

        bitArray1 := #(0 0 0 0) copy

or create the array at runtime with {0.0.0.0}

As for bit array (bit field), I wanted to answer your original question which I will do later…

        Marcus


> On 19 Nov 2015, at 16:12, abdelghani ALIDRA <[hidden email]> wrote:
>
> Hi everybody,
>
> I am experiencing a very weird behavior with Arrays :
>
> I writed a test case as follows ;
>
> testChange
> | bitArray1 |
>
>
> bitArray1 := #(0 0 0 0).
>
> (1 to: bitArray1 size)
> do: [ :index ||temp|
> temp := (bitArray1 at: index) + 1.
> bitArray1 at: index put: (temp).
> ].
> self assert: bitArray1 = #(1 1 1 1)
>
>
> The thing is that the test succedes the first time but if I run it again it fails.
> Moreover, if  I change the testChange arbitrarily (just add a space and save) the test succeeds the first time then it fails.....etc.
>
> Actually bitArray1 := #(0 0 0 0) changes the value of bitArray1 in the first run but then bitArray1 does not change any more in the next runs.
>
> The problem does not occur if  I replace bitArray1 := #(0 0 0 0). with bitArray1 := Array new:4 withAll: 0 .
>
> Any idea where the problem can be?
> Should I open a bug entry? where?
>
> Abdelghani
>