From a4985cb2b8b2f78e9c260f21618c07b761aa63a8 Mon Sep 17 00:00:00 2001 From: "Christian P. MOMON" Date: Mon, 18 Jan 2021 19:36:21 +0100 Subject: [PATCH] Added remove methods and unit tests. --- src/fr/devinsy/strings/StringList.java | 96 +++++++- test/fr/devinsy/strings/StringListTest.java | 258 ++++++++++++++++++++ 2 files changed, 353 insertions(+), 1 deletion(-) diff --git a/src/fr/devinsy/strings/StringList.java b/src/fr/devinsy/strings/StringList.java index 5eebe6c..6548d31 100644 --- a/src/fr/devinsy/strings/StringList.java +++ b/src/fr/devinsy/strings/StringList.java @@ -1767,6 +1767,59 @@ public class StringList extends ArrayList implements CharSequence, Appen return result; } + /** + * Removes the first. + * + * @return the string list + */ + public StringList removeFirst() + { + StringList result; + + if (!isEmpty()) + { + this.remove(0); + } + + result = this; + + // + return result; + } + + /** + * Removes the first. + * + * @param count + * the count + * @return the string list + */ + public StringList removeFirst(final int count) + { + StringList result; + + int counting; + if (count < 0) + { + counting = 0; + } + else if (count <= size()) + { + counting = count; + } + else + { + counting = size(); + } + + removeRange(0, counting); + + result = this; + + // + return result; + } + /** * Removes the last element of the list. * @@ -1776,7 +1829,7 @@ public class StringList extends ArrayList implements CharSequence, Appen { StringList result; - if (this.size() > 0) + if (!isEmpty()) { this.remove(this.size() - 1); } @@ -1787,6 +1840,47 @@ public class StringList extends ArrayList implements CharSequence, Appen return result; } + /** + * Removes the last. + * + * @param count + * the count + * @return the string list + */ + public StringList removeLast(final int count) + { + StringList result; + + int counting; + if (count < 0) + { + counting = 0; + } + else + { + counting = count; + } + + boolean ended = false; + while (!ended) + { + if ((counting == 0) || (isEmpty())) + { + ended = true; + } + else + { + this.remove(this.size() - 1); + counting -= 1; + } + } + + result = this; + + // + return result; + } + /** * Extends the list copying the last element a number of time. * diff --git a/test/fr/devinsy/strings/StringListTest.java b/test/fr/devinsy/strings/StringListTest.java index f258a2d..183e11e 100644 --- a/test/fr/devinsy/strings/StringListTest.java +++ b/test/fr/devinsy/strings/StringListTest.java @@ -775,6 +775,264 @@ public class StringListTest this.logger.debug("===== test done."); } + /** + * Test remove first 01. + */ + @Test + public void testRemoveFirst01() + { + StringList source = new StringList(); + + source.removeFirst(); + Assert.assertTrue(source.isEmpty()); + } + + /** + * Test remove first 02. + */ + @Test + public void testRemoveFirst02() + { + StringList source = new StringList(); + + source.append("un"); + source.removeFirst(); + Assert.assertTrue(source.isEmpty()); + } + + /** + * Test remove first 03. + */ + @Test + public void testRemoveFirst03() + { + StringList source = new StringList(); + + source.append("un", "deux"); + source.removeFirst(); + Assert.assertEquals(1, source.size()); + Assert.assertEquals("deux", source.getLast()); + } + + /** + * Test remove first with count 01. + */ + @Test + public void testRemoveFirstWithCount01() + { + StringList source = new StringList(); + + source.removeFirst(-10); + Assert.assertTrue(source.isEmpty()); + + source.removeFirst(-1); + Assert.assertTrue(source.isEmpty()); + } + + @Test + public void testRemoveFirstWithCount02() + { + StringList source = new StringList(); + + source.append("un", "deux", "trois"); + + source.removeFirst(-10); + Assert.assertEquals(3, source.size()); + + source.removeFirst(-1); + Assert.assertEquals(3, source.size()); + } + + /** + * Test remove first with count 03. + */ + @Test + public void testRemoveFirstWithCount03() + { + StringList source = new StringList(); + + source.removeFirst(0); + Assert.assertTrue(source.isEmpty()); + } + + /** + * Test remove first with count 04. + */ + @Test + public void testRemoveFirstWithCount04() + { + StringList source = new StringList(); + + source.append("un", "deux", "trois"); + source.removeFirst(0); + Assert.assertEquals(3, source.size()); + } + + /** + * Test remove first with count 05. + */ + @Test + public void testRemoveFirstWithCount05() + { + StringList source = new StringList(); + + source.removeFirst(1); + Assert.assertTrue(source.isEmpty()); + + source.removeFirst(10); + Assert.assertTrue(source.isEmpty()); + } + + /** + * Test remove first with count 06. + */ + @Test + public void testRemoveFirstWithCount06() + { + StringList source = new StringList(); + + source.append("un", "deux", "trois", "quatre", "cinq"); + + source.removeFirst(1); + Assert.assertEquals(4, source.size()); + + source.append("cinq", "six", "sept", "huit", "neuf", "dix"); + source.removeFirst(10); + Assert.assertTrue(source.isEmpty()); + } + + /** + * Test remove last 01. + */ + @Test + public void testRemoveLast01() + { + StringList source = new StringList(); + + source.removeLast(); + Assert.assertTrue(source.isEmpty()); + } + + /** + * Test remove last 02. + */ + @Test + public void testRemoveLast02() + { + StringList source = new StringList(); + + source.append("un"); + + source.removeLast(); + Assert.assertTrue(source.isEmpty()); + } + + /** + * Test remove last 02. + */ + @Test + public void testRemoveLast03() + { + StringList source = new StringList(); + + source.append("un", "deux"); + + source.removeLast(); + Assert.assertEquals(1, source.size()); + Assert.assertEquals("un", source.getLast()); + } + + /** + * Test remove last with count 01. + */ + @Test + public void testRemoveLastWithCount01() + { + StringList source = new StringList(); + + source.removeLast(-10); + Assert.assertTrue(source.isEmpty()); + + source.removeLast(-1); + Assert.assertTrue(source.isEmpty()); + } + + /** + * Test remove last with count 02. + */ + @Test + public void testRemoveLastWithCount02() + { + StringList source = new StringList(); + + source.append("un", "deux", "trois"); + + source.removeLast(-10); + Assert.assertEquals(3, source.size()); + + source.removeLast(-1); + Assert.assertEquals(3, source.size()); + } + + /** + * Test remove last with count 03. + */ + @Test + public void testRemoveLastWithCount03() + { + StringList source = new StringList(); + + source.removeLast(0); + Assert.assertTrue(source.isEmpty()); + } + + /** + * Test remove last with count 04. + */ + @Test + public void testRemoveLastWithCount04() + { + StringList source = new StringList(); + + source.append("un", "deux", "trois"); + source.removeLast(0); + Assert.assertEquals(3, source.size()); + } + + /** + * Test remove last with count 05. + */ + @Test + public void testRemoveLastWithCount05() + { + StringList source = new StringList(); + + source.removeLast(1); + Assert.assertTrue(source.isEmpty()); + + source.removeLast(10); + Assert.assertTrue(source.isEmpty()); + } + + /** + * Test remove last with count 06. + */ + @Test + public void testRemoveLastWithCount06() + { + StringList source = new StringList(); + + source.append("un", "deux", "trois", "quatre", "cinq"); + + source.removeLast(1); + Assert.assertEquals(4, source.size()); + Assert.assertEquals("quatre", source.getLast()); + + source.append("cinq", "six", "sept", "huit", "neuf", "dix"); + source.removeLast(10); + Assert.assertTrue(source.isEmpty()); + } + /** * Test shrink 01. */