-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: coverage for non-generated code (#72)
- Loading branch information
1 parent
5a2b3de
commit a7f5267
Showing
10 changed files
with
213 additions
and
72 deletions.
There are no files selected for viewing
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
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
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
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
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
52 changes: 0 additions & 52 deletions
52
codewars-sdk-client/src/main/java/dev/noid/codewars/client/LazyList.java
This file was deleted.
Oops, something went wrong.
42 changes: 42 additions & 0 deletions
42
codewars-sdk-client/src/main/java/dev/noid/codewars/client/PaginatedList.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package dev.noid.codewars.client; | ||
|
||
import java.util.AbstractList; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.function.IntFunction; | ||
|
||
class PaginatedList<E> extends AbstractList<E> { | ||
|
||
private final IntFunction<List<E>> pageLoader; | ||
private final List<E>[] cache; | ||
private final int totalItems; | ||
private final int pageSize; | ||
|
||
PaginatedList(IntFunction<List<E>> pageLoader, int totalPages, int totalItems, Map<Integer, List<E>> preloaded) { | ||
this.pageLoader = pageLoader; | ||
this.cache = new List[totalPages]; | ||
preloaded.forEach((pageNumber, page) -> cache[pageNumber] = page); | ||
this.totalItems = totalItems; | ||
this.pageSize = (int) Math.ceil((double) totalItems / totalPages); | ||
} | ||
|
||
@Override | ||
public E get(int i) { | ||
if (i < 0 || i >= size()) { | ||
throw new IndexOutOfBoundsException(); | ||
} | ||
|
||
int pageNumber = Math.floorDiv(i, pageSize); | ||
List<E> page = cache[pageNumber]; | ||
if (page == null) { | ||
page = pageLoader.apply(pageNumber); | ||
cache[pageNumber] = page; | ||
} | ||
return page.get(i % pageSize); | ||
} | ||
|
||
@Override | ||
public int size() { | ||
return totalItems; | ||
} | ||
} |
7 changes: 0 additions & 7 deletions
7
codewars-sdk-client/src/test/java/dev/noid/codewars/client/LazyListTest.java
This file was deleted.
Oops, something went wrong.
141 changes: 141 additions & 0 deletions
141
codewars-sdk-client/src/test/java/dev/noid/codewars/client/PaginatedListTest.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
package dev.noid.codewars.client; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertThrows; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
import static org.mockito.ArgumentMatchers.anyInt; | ||
|
||
import java.util.List; | ||
import java.util.Map; | ||
import org.junit.jupiter.api.Test; | ||
import org.mockito.Mockito; | ||
|
||
class PaginatedListTest { | ||
|
||
private final List<List<String>> remotePages = Mockito.spy(List.of( | ||
List.of("a", "b", "c", "d", "e"), | ||
List.of("f", "g", "h", "i", "j"), | ||
List.of("k", "l", "m", "n", "o"), | ||
List.of("p", "q", "r", "s", "t"), | ||
List.of("u", "v", "w", "x", "y"), | ||
List.of("z") | ||
)); | ||
|
||
@Test | ||
void load_all_remote_pages() { | ||
assertEquals(List.of( | ||
"a", "b", "c", "d", "e", | ||
"f", "g", "h", "i", "j", | ||
"k", "l", "m", "n", "o", | ||
"p", "q", "r", "s", "t", | ||
"u", "v", "w", "x", "y", | ||
"z" | ||
), getList(6, 26, Map.of())); | ||
|
||
Mockito.verify(remotePages, Mockito.times(remotePages.size())).get(anyInt()); | ||
for (int page = 0; page < remotePages.size(); page++) { | ||
Mockito.verify(remotePages, Mockito.times(1)).get(page); | ||
} | ||
} | ||
|
||
@Test | ||
void preload_first_page() { | ||
assertEquals(List.of( | ||
"1", "2", "3", "4", "5", | ||
"f", "g", "h", "i", "j", | ||
"k", "l", "m", "n", "o", | ||
"p", "q", "r", "s", "t", | ||
"u", "v", "w", "x", "y", | ||
"z" | ||
), getList(6, 26, Map.of(0, List.of("1", "2", "3", "4", "5")))); | ||
|
||
Mockito.verify(remotePages, Mockito.times(remotePages.size() - 1)).get(anyInt()); | ||
for (int page = 1; page < remotePages.size(); page++) { | ||
Mockito.verify(remotePages, Mockito.times(1)).get(page); | ||
} | ||
} | ||
|
||
@Test | ||
void preload_last_page() { | ||
assertEquals(List.of( | ||
"a", "b", "c", "d", "e", | ||
"f", "g", "h", "i", "j", | ||
"k", "l", "m", "n", "o", | ||
"p", "q", "r", "s", "t", | ||
"u", "v", "w", "x", "y", | ||
"1" | ||
), getList(6, 26, Map.of(5, List.of("1", "2", "3", "4", "5")))); | ||
|
||
Mockito.verify(remotePages, Mockito.times(remotePages.size() - 1)).get(anyInt()); | ||
for (int page = 0; page < remotePages.size() - 1; page++) { | ||
Mockito.verify(remotePages, Mockito.times(1)).get(page); | ||
} | ||
} | ||
|
||
@Test | ||
void load_no_remote_pages() { | ||
Map<Integer, List<String>> preloaded = Map.of( | ||
0, List.of("1", "2", "3", "4", "5"), | ||
1, List.of("6", "7", "8", "9", "0"), | ||
2, List.of("~", "!", "@", "#", "$"), | ||
3, List.of("%", "^", "&", "*", "("), | ||
4, List.of(")", "_", "+", "`", "-"), | ||
5, List.of("=") | ||
); | ||
|
||
assertEquals(List.of( | ||
"1", "2", "3", "4", "5", | ||
"6", "7", "8", "9", "0", | ||
"~", "!", "@", "#", "$", | ||
"%", "^", "&", "*", "(", | ||
")", "_", "+", "`", "-", | ||
"=" | ||
), getList(6, 26, preloaded)); | ||
|
||
Mockito.verify(remotePages, Mockito.times(0)).get(anyInt()); | ||
} | ||
|
||
@Test | ||
void limit_pages_access() { | ||
assertEquals(List.of( | ||
"a", "b", "c", "d", "e", | ||
"f", "g", "h", "i", "j", | ||
"k", "l", "m" | ||
), getList(3, 13, Map.of())); | ||
|
||
Mockito.verify(remotePages, Mockito.times(3)).get(anyInt()); | ||
Mockito.verify(remotePages, Mockito.times(1)).get(0); | ||
Mockito.verify(remotePages, Mockito.times(1)).get(1); | ||
Mockito.verify(remotePages, Mockito.times(1)).get(2); | ||
} | ||
|
||
@Test | ||
void read_from_empty() { | ||
List<String> list = getList(0, 0, Map.of()); | ||
assertTrue(list.isEmpty()); | ||
assertEquals(List.of(), list); | ||
} | ||
|
||
@Test | ||
void constant_size() { | ||
List<String> list = getList(6, 26, Map.of()); | ||
assertEquals(26, list.size()); | ||
for (String ignored : list) { | ||
assertEquals(26, list.size()); | ||
} | ||
assertEquals(26, list.size()); | ||
} | ||
|
||
@Test | ||
void immutability() { | ||
List<String> list = getList(1, 1, Map.of()); | ||
assertThrows(UnsupportedOperationException.class, () -> list.add(0, "!")); | ||
assertThrows(UnsupportedOperationException.class, () -> list.set(0, "!")); | ||
assertThrows(UnsupportedOperationException.class, () -> list.remove(0)); | ||
assertThrows(UnsupportedOperationException.class, () -> list.remove("a")); | ||
} | ||
|
||
private List<String> getList(int totalPages, int totalItems, Map<Integer, List<String>> preloaded) { | ||
return new PaginatedList<>(remotePages::get, totalPages, totalItems, preloaded); | ||
} | ||
} |
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