From 8cd9b2afb2f620bb3c2a0df565f8d9ff483fcfaf Mon Sep 17 00:00:00 2001 From: Samuel Tardieu Date: Sun, 13 Apr 2014 21:21:06 +0200 Subject: buffer lets you split a list into smaller max-size ones --- tests/src/cgeo/geocaching/utils/MiscUtilsTest.java | 54 ++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 tests/src/cgeo/geocaching/utils/MiscUtilsTest.java (limited to 'tests') diff --git a/tests/src/cgeo/geocaching/utils/MiscUtilsTest.java b/tests/src/cgeo/geocaching/utils/MiscUtilsTest.java new file mode 100644 index 0000000..2ee99c4 --- /dev/null +++ b/tests/src/cgeo/geocaching/utils/MiscUtilsTest.java @@ -0,0 +1,54 @@ +package cgeo.geocaching.utils; + +import junit.framework.TestCase; + +import java.util.LinkedList; +import java.util.List; + +public class MiscUtilsTest extends TestCase { + + public static void testBufferEmpty() { + for (final List s: MiscUtils.buffer(new LinkedList(), 10)) { + fail("empty collection should not iterate"); + } + } + + public static void testMultiple() { + final List list = new LinkedList(); + for (int i = 0; i < 50; i++) { + list.add(i); + } + int count = 0; + for (final List subList: MiscUtils.buffer(list, 10)) { + assertEquals("each sublist has the right size", 10, subList.size()); + assertEquals("sublist has the right content", count * 10, (int) subList.get(0)); + count++; + } + assertEquals("there are the right number of sublists", 5, count); + } + + public static void testNonMultiple() { + final List list = new LinkedList(); + for (int i = 0; i < 48; i++) { + list.add(i); + } + int count = 0; + for (final List subList: MiscUtils.buffer(list, 10)) { + assertTrue("each sublist has no more than the allowed number of arguments", subList.size() <= 10); + count += subList.size(); + } + assertEquals("all the elements were seen", 48, count); + } + + public static void testArguments() { + try { + MiscUtils.buffer(new LinkedList(), 0); + fail("an exception should be raised"); + } catch (final IllegalArgumentException e) { + // Ok + } catch (final Exception e) { + fail("bad exception raised: " + e); + } + } + +} -- cgit v1.1