Skip to content

Commit 2968e2d

Browse files
committed
Add unit tests for FloatList
1 parent 4206874 commit 2968e2d

File tree

1 file changed

+95
-0
lines changed

1 file changed

+95
-0
lines changed
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
package processing.data;
2+
3+
import org.junit.Test;
4+
5+
import java.util.ArrayList;
6+
import java.util.Arrays;
7+
import java.util.List;
8+
9+
import static org.junit.Assert.*;
10+
11+
/**
12+
* Unit tests for {@link FloatList}, mirroring coverage patterns used in {@link IntListTest}.
13+
*/
14+
public class FloatListTest {
15+
16+
@Test
17+
public void testDefaultConstructor() {
18+
FloatList list = new FloatList();
19+
assertEquals(0, list.size());
20+
assertEquals(10, list.data.length);
21+
}
22+
23+
@Test
24+
public void testConstructorWithLength() {
25+
FloatList list = new FloatList(25);
26+
assertEquals(0, list.size());
27+
assertEquals(25, list.data.length);
28+
}
29+
30+
@Test
31+
public void testConstructorWithArray() {
32+
float[] source = {1.5f, 2.5f, -3f};
33+
FloatList list = new FloatList(source);
34+
assertEquals(3, list.size());
35+
assertEquals(1.5f, list.get(0), 0.0001f);
36+
assertEquals(2.5f, list.get(1), 0.0001f);
37+
assertEquals(-3f, list.get(2), 0.0001f);
38+
}
39+
40+
@Test
41+
public void testConstructorWithIterableParsesNumbersAndNullAsNaN() {
42+
List<Object> source = new ArrayList<>(Arrays.asList(1, 2.5f, null, "3.25"));
43+
FloatList list = new FloatList(source);
44+
assertEquals(4, list.size());
45+
assertEquals(1f, list.get(0), 0.0001f);
46+
assertEquals(2.5f, list.get(1), 0.0001f);
47+
assertTrue(Float.isNaN(list.get(2)));
48+
assertEquals(3.25f, list.get(3), 0.0001f);
49+
}
50+
51+
@Test
52+
public void testAppendAndGet() {
53+
FloatList list = new FloatList();
54+
list.append(10f);
55+
list.append(20f);
56+
assertEquals(2, list.size());
57+
assertEquals(10f, list.get(0), 0.0001f);
58+
assertEquals(20f, list.get(1), 0.0001f);
59+
}
60+
61+
@Test
62+
public void testClear() {
63+
FloatList list = new FloatList(new float[] {1f, 2f});
64+
list.clear();
65+
assertEquals(0, list.size());
66+
}
67+
68+
@Test
69+
public void testPopReturnsLastAndRemoves() {
70+
FloatList list = new FloatList(new float[] {1f, 2f, 3f});
71+
assertEquals(3f, list.pop(), 0.0001f);
72+
assertEquals(2, list.size());
73+
assertEquals(2f, list.get(1), 0.0001f);
74+
}
75+
76+
@Test(expected = RuntimeException.class)
77+
public void testPopOnEmptyThrows() {
78+
new FloatList().pop();
79+
}
80+
81+
@Test
82+
public void testValuesReturnsCopyOfUsedRange() {
83+
FloatList list = new FloatList(new float[] {1f, 2f, 3f});
84+
float[] v = list.values();
85+
assertEquals(3, v.length);
86+
assertArrayEquals(new float[] {1f, 2f, 3f}, v, 0.0001f);
87+
v[0] = 99f;
88+
assertEquals(1f, list.get(0), 0.0001f);
89+
}
90+
91+
@Test(expected = ArrayIndexOutOfBoundsException.class)
92+
public void testGetOutOfBoundsThrows() {
93+
new FloatList(new float[] {1f}).get(1);
94+
}
95+
}

0 commit comments

Comments
 (0)