Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Glavo committed Nov 5, 2024
1 parent 1a5f603 commit c478c51
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/test/template/kala/collection/SeqLikeTestTemplate.java
Original file line number Diff line number Diff line change
Expand Up @@ -426,7 +426,7 @@ default void appendedTest() {
final ArrayList<Integer> al = new ArrayList<>(Arrays.asList(data));
al.add(last);

assertIterableEquals(al, from(data).appended(12345));
assertIterableEquals(al, from(data).appended(last));
}
}

Expand Down
71 changes: 67 additions & 4 deletions src/test/template/kala/collection/SetLikeTestTemplate.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,29 @@
/*
* Copyright 2024 Glavo
*
* 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 kala.collection;

import kala.collection.immutable.ImmutableArray;
import org.junit.jupiter.api.Test;

import java.util.HashSet;
import java.util.List;
import java.util.NoSuchElementException;
import java.util.*;
import java.util.Set;

import static org.junit.jupiter.api.Assertions.*;

public interface SetLikeTestTemplate extends CollectionLikeTestTemplate {
public interface SetLikeTestTemplate extends CollectionLikeTestTemplate {
default void assertSetElements(Iterable<?> expected, SetLike<?> actual) {
int count = 0;
for (Object value : expected) {
Expand Down Expand Up @@ -45,4 +60,52 @@ default void iteratorTest() {
}
assertEquals(expected.size(), n);
}

@Test
default void addedTest() {
assertSetElements(Set.of("str"), this.<String>of().added("str"));
assertSetElements(Set.of("str"), this.of("str").added("str"));
assertSetElements(Collections.singleton(null), this.<String>of().added(null));
assertSetElements(Set.of("str1", "str2"), of("str1").added("str2"));
assertSetElements(Set.of("str1", "str2"), of("str1").added("str2").added("str1"));

final int last = 12345;
for (Integer[] data : data1()) {
final Set<Integer> al = new HashSet<>(Arrays.asList(data));
al.add(last);
assertSetElements(al, from(data).added(last));
}
}

@Test
default void addedAllTest() {
assertSetElements(Set.of(), of().addedAll(List.of()));
assertSetElements(Set.of("str"), of("str").addedAll(List.of()));
assertSetElements(Set.of("str"), of("str").addedAll(new String[0]));
assertSetElements(Set.of("str"), this.<String>of().addedAll(new String[]{"str"}));

for (Integer[] data : data1()) {
assertSetElements(Set.of(data), this.<Integer>of().addedAll(data));
assertSetElements(Set.of(data), this.<Integer>of().addedAll(Arrays.asList(data)));
assertSetElements(Set.of(data), this.<Integer>of().addedAll(ImmutableArray.from(data)));
}

for (int i = 0; i < data1().length - 1; i++) {
final Integer[] data = data1()[i];
final Integer[] data2 = data1()[i + 1];

final Set<Integer> tmp = new HashSet<>(Arrays.asList(data));

assertSetElements(tmp, this.<Integer>of().addedAll(data));
assertSetElements(tmp, this.<Integer>of().addedAll(Arrays.asList(data)));
assertSetElements(tmp, this.<Integer>of().addedAll(ImmutableArray.from(data)));

tmp.addAll(Arrays.asList(data2));

assertSetElements(tmp, from(data).addedAll(data2));
assertSetElements(tmp, from(data).addedAll(Arrays.asList(data2)));
assertSetElements(tmp, from(data).addedAll(ImmutableArray.from(data2)));
}
}

}

0 comments on commit c478c51

Please sign in to comment.