001package beginner;
002
003import com.sandwich.koan.Koan;
004
005import java.util.Arrays;
006
007import static com.sandwich.koan.constant.KoanConstants.__;
008import static com.sandwich.util.Assert.assertEquals;
009
010public class AboutArrays {
011
012    @Koan
013    public void arraysDoNotConsiderElementsWhenEvaluatingEquality() {
014        // arrays utilize default object equality (A == {1} B == {1}, though A
015        // and B contain the same thing, the container is not the same
016        // referenced array instance...
017        assertEquals(new int[]{1}.equals(new int[]{1}), __);
018    }
019
020    @Koan
021    public void cloneEqualityIsNotRespected() { //!
022        int[] original = new int[]{1};
023        assertEquals(original.equals(original.clone()), __);
024    }
025
026    @Koan
027    public void anArraysHashCodeMethodDoesNotConsiderElements() {
028        int[] array0 = new int[]{0};
029        int[] array1 = new int[]{0};
030        assertEquals(Integer.valueOf(array0.hashCode()).equals(array1.hashCode()), __); // not equal!
031        // TODO: ponder the consequences when arrays are used in Hash Collection implementations.
032    }
033
034    @Koan
035    public void arraysHelperClassEqualsMethodConsidersElementsWhenDeterminingEquality() {
036        int[] array0 = new int[]{0};
037        int[] array1 = new int[]{0};
038        assertEquals(Arrays.equals(array0, array1), __);    // whew - what most people assume
039        // about equals in regard to arrays! (logical equality)
040    }
041
042    @Koan
043    public void arraysHelperClassHashCodeMethodConsidersElementsWhenDeterminingHashCode() {
044        int[] array0 = new int[]{0};
045        int[] array1 = new int[]{0};
046        // whew - what most people assume about hashCode in regard to arrays!
047        assertEquals(Integer.valueOf(Arrays.hashCode(array0)).equals(Arrays.hashCode(array1)), __);
048    }
049
050    @Koan
051    public void arraysAreMutable() {
052        final boolean[] oneBoolean = new boolean[]{false};
053        oneBoolean[0] = true;
054        assertEquals(oneBoolean[0], __);
055    }
056
057    @Koan
058    public void arraysAreIndexedAtZero() {
059        int[] integers = new int[]{1, 2};
060        assertEquals(integers[0], __);
061        assertEquals(integers[1], __);
062    }
063
064    @Koan
065    public void arrayIndexOutOfBounds() {
066        int[] array = new int[]{1};
067        @SuppressWarnings("unused")
068        int meh = array[1]; // remember 0 based indexes, 1 is the 2nd element (which doesn't exist)
069    }
070
071    @Koan
072    public void arrayLengthCanBeChecked() {
073        assertEquals(new int[1].length, __);
074    }
075
076}