Skip to content

Commit

Permalink
Improved tests (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
nibix authored Sep 20, 2024
1 parent 1e3d1bb commit 0b79765
Show file tree
Hide file tree
Showing 9 changed files with 265 additions and 117 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,12 @@ static <E, T> T[] copyAsTypedArray(E[] source, T[] target) {
}

@SuppressWarnings("unchecked")
static <E, T> T[] copyAsTypedArray(E[] source, T[] target, int size) {
T[] result = target.length >= size
static <E, T> T[] copyAsTypedArray(E[] source, T[] target, int newSize) {
T[] result = target.length >= newSize
? target
: (T[]) java.lang.reflect.Array.newInstance(target.getClass().getComponentType(), size);
: (T[]) java.lang.reflect.Array.newInstance(target.getClass().getComponentType(), newSize);

System.arraycopy(source, 0, result, 0, size);
System.arraycopy(source, 0, result, 0, Math.min(source.length, newSize));
return result;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,13 @@ public boolean retainAll(Collection<?> arg0) {
throw new UnsupportedOperationException();
}

@Override
public boolean containsAll(Collection<?> collection) {
for (Object o : collection) {
if (!contains(o)) {
return false;
public boolean containsAny(Collection<E> elements) {
for (E e : elements) {
if (contains(e)) {
return true;
}
}
return true;

return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,50 @@ public void mapInPlace_null() {

@Test
public void copyAsObjectArray() {
String [] array = new String [] {"a"};
Object [] copy = GenericArrays.copyAsObjectArray(array);
String[] array = new String[] {"a"};
Object[] copy = GenericArrays.copyAsObjectArray(array);
Assert.assertArrayEquals(new Object[] {"a"}, copy);
}

@Test
public void copyAsObjectArray_size() {
String[] array = new String[] {"a"};
Object[] copy = GenericArrays.copyAsObjectArray(array, 2);
Assert.assertArrayEquals(new Object[] {"a", null}, copy);
}

@Test
public void copyAsTypedArray_emptyTypedArray() {
String[] array = new String[] {"a"};
String[] copy = GenericArrays.copyAsTypedArray(array, new String[0]);
Assert.assertArrayEquals(new String[] {"a"}, copy);
}

@Test
public void copyAsTypedArray_nonEmptyTypedArray() {
String[] array = new String[] {"a"};
String[] copy = GenericArrays.copyAsTypedArray(array, new String[1]);
Assert.assertArrayEquals(new String[] {"a"}, copy);
}

@Test
public void copyAsTypedArray_emptyTypedArray_size() {
String[] array = new String[] {"a"};
String[] copy = GenericArrays.copyAsTypedArray(array, new String[0], 2);
Assert.assertArrayEquals(new String[] {"a", null}, copy);
}

@Test
public void copyAsTypedArray_nonEmptyTypedArray_size() {
String[] array = new String[] {"a"};
String[] copy = GenericArrays.copyAsTypedArray(array, new String[2], 2);
Assert.assertArrayEquals(new String[] {"a", null}, copy);
}

@Test
public void extend() {
Object [] array = new Object [] {"a"};
Object [] extended = GenericArrays.extend(array, 2);
Assert.assertArrayEquals(new Object [] {"a", null}, extended);
Object[] array = new Object[] {"a"};
Object[] extended = GenericArrays.extend(array, 2);
Assert.assertArrayEquals(new Object[] {"a", null}, extended);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

package com.selectivem.collections;

import java.util.Arrays;
import java.util.Iterator;
import java.util.NoSuchElementException;
import org.junit.Assert;
Expand Down Expand Up @@ -49,7 +48,6 @@ public void of1() {
Assert.assertEquals(1, set1.toArray(new String[0]).length);
Assert.assertTrue(set1.containsAll(ImmutableSetImpl.of("a")));
Assert.assertFalse(set1.containsAll(ImmutableSetImpl.of("a", "b")));

}

@Test(expected = NoSuchElementException.class)
Expand Down Expand Up @@ -110,37 +108,5 @@ public void of2_iterator_exhausted() {
iter.next();
iter.next();
}


@Test(expected = UnsupportedOperationException.class)
public void add() {
ImmutableSetImpl.of("a", "b").add("x");
}

@Test(expected = UnsupportedOperationException.class)
public void addAll() {
ImmutableSetImpl.of("a", "b").addAll(Arrays.asList("x"));
}

@Test(expected = UnsupportedOperationException.class)
public void clear() {
ImmutableSetImpl.of("a", "b").clear();
}

@Test(expected = UnsupportedOperationException.class)
public void remove() {
ImmutableSetImpl.of("a", "b").remove("x");
}

@Test(expected = UnsupportedOperationException.class)
public void removeAll() {
ImmutableSetImpl.of("a", "b").removeAll(Arrays.asList("x"));
}

@Test(expected = UnsupportedOperationException.class)
public void retainAll() {
ImmutableSetImpl.of("a", "b").retainAll(Arrays.asList("x"));
}

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
/*
* Copyright 2024 Nils Bandener
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.selectivem.collections;

import java.util.Arrays;
import java.util.Collection;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
import java.util.function.Consumer;
import java.util.stream.Stream;
import org.junit.Assert;
import org.junit.Test;

public class UnmodifiableSetImplTest {
@Test(expected = UnsupportedOperationException.class)
public void add() {
ConcreteUnmodifiableSetImpl.of("a", "b").add("x");
}

@Test(expected = UnsupportedOperationException.class)
public void addAll() {
ConcreteUnmodifiableSetImpl.of("a", "b").addAll(Arrays.asList("x"));
}

@Test(expected = UnsupportedOperationException.class)
public void clear() {
ConcreteUnmodifiableSetImpl.of("a", "b").clear();
}

@Test(expected = UnsupportedOperationException.class)
public void remove() {
ConcreteUnmodifiableSetImpl.of("a", "b").remove("x");
}

@Test(expected = UnsupportedOperationException.class)
public void removeAll() {
ConcreteUnmodifiableSetImpl.of("a", "b").removeAll(Arrays.asList("x"));
}

@Test(expected = UnsupportedOperationException.class)
public void retainAll() {
ConcreteUnmodifiableSetImpl.of("a", "b").retainAll(Arrays.asList("x"));
}

@Test
public void containsAny() {
ConcreteUnmodifiableSetImpl<String> subject = ConcreteUnmodifiableSetImpl.of("a", "b");
Assert.assertTrue(subject.containsAny(Arrays.asList("a")));
Assert.assertTrue(subject.containsAny(Arrays.asList("b", "a")));
Assert.assertTrue(subject.containsAny(Arrays.asList("c", "a")));
Assert.assertFalse(subject.containsAny(Arrays.asList("c")));
}

static class ConcreteUnmodifiableSetImpl<E> extends UnmodifiableSetImpl<E> {
private final Set<E> delegate = new HashSet<>();

ConcreteUnmodifiableSetImpl(Collection<E> elements) {
this.delegate.addAll(elements);
}

@Override
public int size() {
return delegate.size();
}

@Override
public boolean isEmpty() {
return delegate.isEmpty();
}

@Override
public boolean contains(Object o) {
return delegate.contains(o);
}

@Override
public Iterator<E> iterator() {
return delegate.iterator();
}

@Override
public Stream<E> stream() {
return delegate.stream();
}

@Override
public Stream<E> parallelStream() {
return delegate.parallelStream();
}

@Override
public void forEach(Consumer<? super E> action) {
delegate.forEach(action);
}

static <E> ConcreteUnmodifiableSetImpl<E> of(E... elements) {
return new ConcreteUnmodifiableSetImpl<>(Arrays.asList(elements));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
*/
package com.selectivem.collections;

import java.util.Collection;
import java.util.Iterator;
import java.util.NoSuchElementException;

Expand Down Expand Up @@ -312,17 +311,6 @@ public boolean equals(Object o) {
}
}

@Override
public boolean containsAny(Collection<E> elements) {
for (E e : elements) {
if (contains(e)) {
return true;
}
}

return false;
}

static int bitArraySize(int size) {
if (size <= 64) {
return 1;
Expand Down
Loading

0 comments on commit 0b79765

Please sign in to comment.