瀏覽代碼

Added tests and example code for the Series exercise

Dmitry Noranovich 9 年之前
父節點
當前提交
137cb76984

+ 39
- 0
exercises/series/src/example/java/Series.java 查看文件

1
+
2
+import java.util.ArrayList;
3
+import java.util.Arrays;
4
+import java.util.List;
5
+import java.util.stream.Collectors;
6
+
7
+public class Series {
8
+
9
+    private final int digitsSize;
10
+    private List<Integer> digits;
11
+
12
+    public Series(String string) {
13
+        this.digits = Arrays
14
+                .asList(string.split(("")))
15
+                .stream()
16
+                .map(digit -> Integer.parseInt(digit))
17
+                .collect(Collectors.toList());
18
+        this.digitsSize = this.digits.size();
19
+    }
20
+
21
+    public List<List<Integer>> slices(int num) {
22
+        if (num > this.digitsSize) {
23
+            throw new IllegalArgumentException("Slice size is too big.");
24
+        }
25
+        final int limit = this.digitsSize - num + 1;
26
+        List<List<Integer>> result = new ArrayList<>(limit);
27
+        List<Integer> tmp;
28
+        for (int i = 0; i < limit; i++) {
29
+            tmp = this.digits.subList(i, i + num);
30
+            result.add(tmp);
31
+        }
32
+        return result;
33
+    }
34
+
35
+    public List<Integer> getDigits() {
36
+        return digits;
37
+    }
38
+
39
+}

+ 0
- 0
exercises/series/src/main/java/.keep 查看文件


+ 3
- 0
exercises/series/src/main/java/Series.java 查看文件

1
+public class Series {
2
+    
3
+}

+ 0
- 0
exercises/series/src/test/java/.keep 查看文件


+ 144
- 0
exercises/series/src/test/java/SeriesTest.java 查看文件

1
+
2
+import java.util.Arrays;
3
+import java.util.List;
4
+import junit.framework.Assert;
5
+import org.junit.Test;
6
+
7
+import static org.junit.Assert.assertEquals;
8
+import static org.junit.Assert.assertFalse;
9
+import static org.junit.Assert.assertNotNull;
10
+
11
+public class SeriesTest {
12
+
13
+    @Test
14
+    public void hasDigitsShort() {
15
+        Series sut = new Series("01234");
16
+        List<Integer> expected = Arrays.asList(0, 1, 2, 3, 4);
17
+        List<Integer> actual = sut.getDigits();
18
+        assertNotNull(actual);
19
+        assertFalse(actual.isEmpty());
20
+        assertEquals(expected, actual);
21
+    }
22
+
23
+    @Test
24
+    public void hasDigitsLong() {
25
+        Series sut = new Series("0123456789");
26
+        List<Integer> expected = Arrays.asList(0, 1, 2, 3, 4, 5, 6, 7, 8, 9);
27
+        List<Integer> actual = sut.getDigits();
28
+        assertNotNull(actual);
29
+        assertFalse(actual.isEmpty());
30
+        assertEquals(expected, actual);
31
+    }
32
+
33
+    @Test
34
+    public void keepsTheDigitOrderIfReversed() {
35
+        Series sut = new Series("9876543210");
36
+        List<Integer> expected = Arrays.asList(9, 8, 7, 6, 5, 4, 3, 2, 1, 0);
37
+        List<Integer> actual = sut.getDigits();
38
+        assertNotNull(actual);
39
+        assertFalse(actual.isEmpty());
40
+        assertEquals(expected, actual);
41
+    }
42
+
43
+    @Test
44
+    public void keepsArbitraryDigitOrder() {
45
+        Series sut = new Series("936923468");
46
+        List<Integer> expected = Arrays.asList(9, 3, 6, 9, 2, 3, 4, 6, 8);
47
+        List<Integer> actual = sut.getDigits();
48
+        assertNotNull(actual);
49
+        assertFalse(actual.isEmpty());
50
+        assertEquals(expected, actual);
51
+    }
52
+
53
+    @Test
54
+    public void canSliceByOne() {
55
+        Series sut = new Series("01234");
56
+        List<List<Integer>> expected = Arrays.asList(
57
+                Arrays.asList(0),
58
+                Arrays.asList(1),
59
+                Arrays.asList(2),
60
+                Arrays.asList(3),
61
+                Arrays.asList(4)
62
+        );
63
+        List<List<Integer>> actual = sut.slices(1);
64
+        assertNotNull(actual);
65
+        assertFalse(actual.isEmpty());
66
+        assertEquals(expected, actual);
67
+    }
68
+
69
+    @Test
70
+    public void canSliceByTwo() {
71
+        Series sut = new Series("98273463");
72
+        List<List<Integer>> expected = Arrays.asList(
73
+                Arrays.asList(9, 8),
74
+                Arrays.asList(8, 2),
75
+                Arrays.asList(2, 7),
76
+                Arrays.asList(7, 3),
77
+                Arrays.asList(3, 4),
78
+                Arrays.asList(4, 6),
79
+                Arrays.asList(6, 3)
80
+        );
81
+        List<List<Integer>> actual = sut.slices(2);
82
+        assertNotNull(actual);
83
+        assertFalse(actual.isEmpty());
84
+        assertEquals(expected, actual);
85
+    }
86
+
87
+    @Test
88
+    public void canSliceByThree() {
89
+        Series sut = new Series("01234");
90
+        List<List<Integer>> expected = Arrays.asList(
91
+                Arrays.asList(0, 1, 2),
92
+                Arrays.asList(1, 2, 3),
93
+                Arrays.asList(2, 3, 4)
94
+        );
95
+        List<List<Integer>> actual = sut.slices(3);
96
+        assertNotNull(actual);
97
+        assertFalse(actual.isEmpty());
98
+        assertEquals(expected, actual);
99
+    }
100
+
101
+    @Test
102
+    public void canSliceByThreeWithDuplicateDigits() {
103
+        Series sut = new Series("31001");
104
+        List<List<Integer>> expected = Arrays.asList(
105
+                Arrays.asList(3, 1, 0),
106
+                Arrays.asList(1, 0, 0),
107
+                Arrays.asList(0, 0, 1)
108
+        );
109
+        List<List<Integer>> actual = sut.slices(3);
110
+        assertNotNull(actual);
111
+        assertFalse(actual.isEmpty());
112
+        assertEquals(expected, actual);
113
+    }
114
+
115
+    @Test
116
+    public void canSliceByFour() {
117
+        Series sut = new Series("91274");
118
+        List<List<Integer>> expected = Arrays.asList(
119
+                Arrays.asList(9, 1, 2, 7),
120
+                Arrays.asList(1, 2, 7, 4)
121
+        );
122
+        List<List<Integer>> actual = sut.slices(4);
123
+        assertNotNull(actual);
124
+        assertFalse(actual.isEmpty());
125
+        assertEquals(expected, actual);
126
+    }
127
+
128
+    @Test
129
+    public void canSliceByFive() {
130
+        Series sut = new Series("81228");
131
+        List<List<Integer>> expected = Arrays.asList(
132
+                Arrays.asList(8, 1, 2, 2, 8)
133
+        );
134
+        List<List<Integer>> actual = sut.slices(5);
135
+        assertNotNull(actual);
136
+        assertFalse(actual.isEmpty());
137
+        assertEquals(expected, actual);
138
+    }
139
+
140
+    @Test(expected = IllegalArgumentException.class)
141
+    public void throwsAnErrorIfNotEnoughDigitsToSlice() {
142
+        new Series("01032987583").slices(12);
143
+    }
144
+}