|
1 | 1 | package beginner; |
2 | 2 |
|
3 | | -import static com.sandwich.koan.constant.KoanConstants.__; |
4 | | -import static com.sandwich.util.Assert.assertEquals; |
| 3 | +import com.sandwich.koan.Koan; |
5 | 4 |
|
6 | 5 | import java.util.Arrays; |
7 | 6 |
|
8 | | -import com.sandwich.koan.Koan; |
| 7 | +import static com.sandwich.koan.constant.KoanConstants.__; |
| 8 | +import static com.sandwich.util.Assert.assertEquals; |
9 | 9 |
|
10 | 10 | public class AboutArrays { |
11 | 11 |
|
12 | | - @Koan |
13 | | - public void arraysDoNotConsiderElementsWhenEvaluatingEquality(){ |
14 | | - // arrays utilize default object equality (A == {1} B == {1}, though A |
15 | | - // and B contain the same thing, the container is not the same |
16 | | - // referenced array instance... |
17 | | - assertEquals(new int[] {1}.equals(new int[] {1}), __); |
18 | | - } |
19 | | - |
20 | | - @Koan |
21 | | - public void cloneEqualityIsNotRespected(){ //! |
22 | | - int[] original = new int[] { 1 }; |
23 | | - assertEquals(original.equals(original.clone()), __); |
24 | | - } |
25 | | - |
26 | | - @Koan |
27 | | - public void anArraysHashCodeMethodDoesNotConsiderElements(){ |
28 | | - int[] array0 = new int[]{0}; |
29 | | - int[] array1 = new int[]{0}; |
30 | | - assertEquals(Integer.valueOf(array0.hashCode()).equals(array1.hashCode()), __); // not equal! |
31 | | - // TODO: ponder the consequences when arrays are used in Hash Collection implementations. |
32 | | - } |
33 | | - |
34 | | - @Koan |
35 | | - public void arraysHelperClassEqualsMethodConsidersElementsWhenDeterminingEquality(){ |
36 | | - int[] array0 = new int[]{0}; |
37 | | - int[] array1 = new int[]{0}; |
38 | | - assertEquals(Arrays.equals(array0, array1), __); // whew - what most people assume |
39 | | - // about equals in regard to arrays! (logical equality) |
40 | | - } |
| 12 | + @Koan |
| 13 | + public void arraysDoNotConsiderElementsWhenEvaluatingEquality() { |
| 14 | + // arrays utilize default object equality (A == {1} B == {1}, though A |
| 15 | + // and B contain the same thing, the container is not the same |
| 16 | + // referenced array instance... |
| 17 | + assertEquals(new int[]{1}.equals(new int[]{1}), __); |
| 18 | + } |
| 19 | + |
| 20 | + @Koan |
| 21 | + public void cloneEqualityIsNotRespected() { //! |
| 22 | + int[] original = new int[]{1}; |
| 23 | + assertEquals(original.equals(original.clone()), __); |
| 24 | + } |
| 25 | + |
| 26 | + @Koan |
| 27 | + public void anArraysHashCodeMethodDoesNotConsiderElements() { |
| 28 | + int[] array0 = new int[]{0}; |
| 29 | + int[] array1 = new int[]{0}; |
| 30 | + assertEquals(Integer.valueOf(array0.hashCode()).equals(array1.hashCode()), __); // not equal! |
| 31 | + // TODO: ponder the consequences when arrays are used in Hash Collection implementations. |
| 32 | + } |
| 33 | + |
| 34 | + @Koan |
| 35 | + public void arraysHelperClassEqualsMethodConsidersElementsWhenDeterminingEquality() { |
| 36 | + int[] array0 = new int[]{0}; |
| 37 | + int[] array1 = new int[]{0}; |
| 38 | + assertEquals(Arrays.equals(array0, array1), __); // whew - what most people assume |
| 39 | + // about equals in regard to arrays! (logical equality) |
| 40 | + } |
| 41 | + |
| 42 | + @Koan |
| 43 | + public void arraysHelperClassHashCodeMethodConsidersElementsWhenDeterminingHashCode() { |
| 44 | + int[] array0 = new int[]{0}; |
| 45 | + int[] array1 = new int[]{0}; |
| 46 | + // whew - what most people assume about hashCode in regard to arrays! |
| 47 | + assertEquals(Integer.valueOf(Arrays.hashCode(array0)).equals(Arrays.hashCode(array1)), __); |
| 48 | + } |
| 49 | + |
| 50 | + @Koan |
| 51 | + public void arraysAreMutable() { |
| 52 | + final boolean[] oneBoolean = new boolean[]{false}; |
| 53 | + oneBoolean[0] = true; |
| 54 | + assertEquals(oneBoolean[0], __); |
| 55 | + } |
| 56 | + |
| 57 | + @Koan |
| 58 | + public void arraysAreIndexedAtZero() { |
| 59 | + int[] integers = new int[]{1, 2}; |
| 60 | + assertEquals(integers[0], __); |
| 61 | + assertEquals(integers[1], __); |
| 62 | + } |
| 63 | + |
| 64 | + @Koan |
| 65 | + public void arrayIndexOutOfBounds() { |
| 66 | + int[] array = new int[]{1}; |
| 67 | + @SuppressWarnings("unused") |
| 68 | + int meh = array[1]; // remember 0 based indexes, 1 is the 2nd element (which doesn't exist) |
| 69 | + } |
| 70 | + |
| 71 | + @Koan |
| 72 | + public void arrayLengthCanBeChecked() { |
| 73 | + assertEquals(new int[1].length, __); |
| 74 | + } |
41 | 75 |
|
42 | | - @Koan |
43 | | - public void arraysHelperClassHashCodeMethodConsidersElementsWhenDeterminingHashCode(){ |
44 | | - int[] array0 = new int[]{0}; |
45 | | - int[] array1 = new int[]{0}; |
46 | | - // whew - what most people assume about hashCode in regard to arrays! |
47 | | - assertEquals(Integer.valueOf(Arrays.hashCode(array0)).equals(Arrays.hashCode(array1)), __); |
48 | | - } |
49 | | - |
50 | | - @Koan |
51 | | - public void arraysAreMutable(){ |
52 | | - final boolean[] oneBoolean = new boolean[]{false}; |
53 | | - oneBoolean[0] = true; |
54 | | - assertEquals(oneBoolean[0], __); |
55 | | - } |
56 | | - |
57 | | - @Koan |
58 | | - public void arraysAreIndexedAtZero(){ |
59 | | - int[] integers = new int[]{1,2}; |
60 | | - assertEquals(integers[0], __); |
61 | | - assertEquals(integers[1], __); |
62 | | - } |
63 | | - |
64 | | - @Koan |
65 | | - public void arrayIndexOutOfBounds(){ |
66 | | - int[] array = new int[]{1}; |
67 | | - @SuppressWarnings("unused") |
68 | | - int meh = array[1]; // remember 0 based indexes, 1 is the 2nd element (which doesn't exist) |
69 | | - } |
70 | | - |
71 | | - @Koan |
72 | | - public void arrayLengthCanBeChecked(){ |
73 | | - assertEquals(new int[1].length, __); |
74 | | - } |
75 | | - |
76 | 76 | } |
0 commit comments