|
| 1 | +package lec7_lists4.resizeExercise; |
| 2 | + |
| 3 | +import org.junit.jupiter.api.Test; |
| 4 | + |
| 5 | +import static com.google.common.truth.Truth.assertThat; |
| 6 | + |
| 7 | +/** This set of tests is not particularly exhaustive. For example, |
| 8 | + * it doesn't test that you can add items to the list, then delete |
| 9 | + * all of them, then add them back. |
| 10 | + */ |
| 11 | +public class AListTest { |
| 12 | + @Test |
| 13 | + public void testAddAndGet() { |
| 14 | + AList list = new AList(); |
| 15 | + list.addLast(5); |
| 16 | + list.addLast(10); |
| 17 | + |
| 18 | + // List should be [5, 10] |
| 19 | + assertThat(list.get(0)).isEqualTo(5); |
| 20 | + assertThat(list.get(1)).isEqualTo(10); |
| 21 | + } |
| 22 | + |
| 23 | + @Test |
| 24 | + public void testGetLast() { |
| 25 | + AList list = new AList(); |
| 26 | + list.addLast(7); |
| 27 | + list.addLast(14); |
| 28 | + list.addLast(21); |
| 29 | + |
| 30 | + // List should be [7, 14, 21] |
| 31 | + assertThat(list.getLast()).isEqualTo(21); |
| 32 | + } |
| 33 | + |
| 34 | + @Test |
| 35 | + public void testRemoveLast() { |
| 36 | + AList list = new AList(); |
| 37 | + list.addLast(3); |
| 38 | + list.addLast(6); |
| 39 | + list.addLast(9); |
| 40 | + |
| 41 | + // First remove should return 9 |
| 42 | + assertThat(list.removeLast()).isEqualTo(9); |
| 43 | + |
| 44 | + // And the list should be [3, 6] after that remove |
| 45 | + assertThat(list.get(0)).isEqualTo(3); |
| 46 | + assertThat(list.getLast()).isEqualTo(6); |
| 47 | + |
| 48 | + // List should be [3] after second remove, with 6 returned |
| 49 | + assertThat(list.removeLast()).isEqualTo(6); |
| 50 | + assertThat(list.removeLast()).isEqualTo(3); |
| 51 | + } |
| 52 | + |
| 53 | + @Test |
| 54 | + public void add200Items() { |
| 55 | + AList list = new AList(); |
| 56 | + for (int i = 0; i < 200; i += 1) { |
| 57 | + list.addLast(i); |
| 58 | + } |
| 59 | + |
| 60 | + for (int i = 0; i < 200; i += 1) { |
| 61 | + assertThat(list.get(i)).isEqualTo(i); |
| 62 | + } |
| 63 | + } |
| 64 | +} |
0 commit comments