diff options
author | Nicolas Peugnet <nicolas.peugnet@lip6.fr> | 2024-01-11 16:45:55 +0300 |
---|---|---|
committer | Daniel Lublin <daniel@lublin.se> | 2024-01-11 17:16:44 +0300 |
commit | f9d8e3b23a13308dbeadae28977fd2a30b26a887 (patch) | |
tree | f749bfde3548c203f6eafbd914938b55fcd15479 | |
parent | 02a1f1a7ec7b1bad7e8a1423b7f7cc189a37963e (diff) |
Add test for WhisperTargetList
To assert that it is working as expected.
-rw-r--r-- | src/test/java/se/lublin/humla/test/WhisperTargetListTest.java | 81 |
1 files changed, 81 insertions, 0 deletions
diff --git a/src/test/java/se/lublin/humla/test/WhisperTargetListTest.java b/src/test/java/se/lublin/humla/test/WhisperTargetListTest.java new file mode 100644 index 0000000..adda841 --- /dev/null +++ b/src/test/java/se/lublin/humla/test/WhisperTargetListTest.java @@ -0,0 +1,81 @@ +/* + * Copyright (C) 2024 Nicolas Peugnet <n.peugnet@free.fr> + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +package se.lublin.humla.test; + +import junit.framework.TestCase; +import static org.junit.Assert.assertThrows; + +import se.lublin.humla.model.Channel; +import se.lublin.humla.model.WhisperTarget; +import se.lublin.humla.model.WhisperTargetChannel; +import se.lublin.humla.model.WhisperTargetList; + +/** + * Tests the Whisper target list model. + */ +public class WhisperTargetListTest extends TestCase { + + public void testWhisperTargetAddRemove() { + // Initialisation. + Channel root = new Channel(0, false); + WhisperTarget[] targets = new WhisperTarget[30]; + for (int i = 0; i < 30; i++) { + targets[i] = new WhisperTargetChannel(root, false, false, null); + } + WhisperTargetList list = new WhisperTargetList(); + + // There should be space for 30 whisper targets. + for (int i = 0; i < 30; i++) { + int space = list.spaceRemaining(); + assertEquals(30-i, space); + byte id = list.append(targets[i]); + assertEquals("id should be equal to i + 1", i+1, id); + WhisperTarget target = list.get(id); + assertEquals("get(id) should return the correct target", targets[i], target); + } + + // But not 31. + int space = list.spaceRemaining(); + assertEquals(0, space); + byte id = list.append(targets[0]); + assertEquals("id should be -1 when no space left", -1, id); + + // Freeing one target + list.free((byte) 5); + space = list.spaceRemaining(); + assertEquals("space should be 1 after freeing one slot", 1, space); + WhisperTarget expected = new WhisperTargetChannel(root, false, false, null); + id = list.append(expected); + assertEquals("append should have filled slot 5", 5, id); + WhisperTarget actual = list.get((byte) 5); + assertEquals("slot 5 should contain our target", expected, actual); + + list.clear(); + space = list.spaceRemaining(); + assertEquals("there should be 30 slots remaining after clear", 30, space); + } + + public void testExceptions() { + WhisperTargetList list = new WhisperTargetList(); + assertThrows(IndexOutOfBoundsException.class, () -> list.get((byte) -1)); + assertThrows(IndexOutOfBoundsException.class, () -> list.get((byte) 31)); + assertThrows(IndexOutOfBoundsException.class, () -> list.free((byte) -1)); + assertThrows(IndexOutOfBoundsException.class, () -> list.free((byte) 31)); + } +} + |