Created
July 15, 2019 14:08
-
-
Save kenparker/fb54f9acc466d87b0e8633e128302e1c to your computer and use it in GitHub Desktop.
Cyclops Data Structure: Immutability Test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class ImmutabilityTest { | |
private String a = "Tom"; | |
private String b = "Marc"; | |
private String c = "Lucy"; | |
private String d = "Bert"; | |
@Test | |
public void givenUsingJava8List_whenListIsCreated_thenListIsMutable() { | |
List<String> java8MutableList = new ArrayList<>(Arrays.asList(a, b, c)); | |
java8MutableList.add(d); | |
assertThat(java8MutableList).contains(d); | |
} | |
@Test | |
public void givenUsingListX_whenListIsCreated_thenListXIsMutable() { | |
ListX<String> list = ListX.of(a, b, c); | |
list.add(d); | |
assertThat(list).contains(d); | |
assertThat(list).isInstanceOf(List.class).isInstanceOf(LazyCollectionX.class); | |
} | |
@Test | |
public void givenUsingCyclopsVector_whenVectorIsCreated_thenVectorIsImmutable() { | |
Vector<String> vector = Vector.of(a, b, c); | |
assertThat(vector).isInstanceOf(ImmutableList.class); | |
} | |
@Test | |
public void givenUsingCyclopsVectorX_whenVectorIsCreated_thenVectorIsPersistentAndLazy() { | |
VectorX<String> vectorLazy = VectorX.of(a, b, c); | |
assertThat(vectorLazy).isInstanceOf(PersistentList.class).isInstanceOf(LazyCollectionX.class); | |
} | |
@Test | |
public void givenUsingJava8List_whenUnmodifiableListIsCreated_thenListIsImmutable() { | |
List<String> java8List = new ArrayList<>(Arrays.asList(a, b, c)); | |
List<String> unmodifiableList = Collections.unmodifiableList(java8List); | |
ThrowingCallable throwingCallable = () -> unmodifiableList.add(d); | |
assertThatExceptionOfType(UnsupportedOperationException.class).isThrownBy(throwingCallable); | |
} | |
@Test | |
public void givenUsingJava9List_whenListIsCreated_thenListIsImmutable() { | |
List<String> java9List = List.of(a, b, c); | |
ThrowingCallable throwingCallable = () -> java9List.add(d); | |
assertThatExceptionOfType(UnsupportedOperationException.class).isThrownBy(throwingCallable); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment