Skip to content

Commit 3c8cfaa

Browse files
committed
Utils
1 parent b5239c3 commit 3c8cfaa

5 files changed

Lines changed: 142 additions & 72 deletions

File tree

src/main/java/algorithms/sprint0/AB.java

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,19 @@
11
package algorithms.sprint0;
22

33
import org.junit.jupiter.api.Test;
4+
import org.junit.jupiter.params.ParameterizedTest;
5+
import org.junit.jupiter.params.provider.CsvSource;
46

7+
import java.io.ByteArrayInputStream;
8+
import java.io.ByteArrayOutputStream;
9+
import java.io.InputStream;
10+
import java.io.PrintStream;
11+
import java.nio.charset.StandardCharsets;
12+
import java.util.InputMismatchException;
513
import java.util.Scanner;
614

715
import static org.junit.jupiter.api.Assertions.assertEquals;
16+
import static org.junit.jupiter.api.Assertions.assertThrows;
817

918
public class AB {
1019
public static void main(String[] args) {
@@ -16,12 +25,50 @@ public static void main(String[] args) {
1625
}
1726

1827
public static long sum(int a, int b) {
19-
return a + b;
28+
return (long) a + b;
2029
}
2130

2231
@Test
2332
public void test() {
2433
assertEquals(5, sum(2, 3));
2534
assertEquals(-1, sum(-2, 1));
2635
}
36+
37+
@ParameterizedTest
38+
@CsvSource({
39+
"0,0,0",
40+
"2,3,5",
41+
"-1,4,3",
42+
"2147483647,1,2147483648",
43+
"2000000000,2000000000,4000000000"
44+
})
45+
void sum_works(int a, int b, long expected) {
46+
assertEquals(expected, AB.sum(a, b));
47+
}
48+
49+
@Test
50+
void main_prints_sum_basic() {
51+
String in = "2 3\n";
52+
InputStream oldIn = System.in;
53+
PrintStream oldOut = System.out;
54+
try {
55+
System.setIn(new ByteArrayInputStream(in.getBytes(StandardCharsets.UTF_8)));
56+
ByteArrayOutputStream buf = new ByteArrayOutputStream();
57+
System.setOut(new PrintStream(buf, true, StandardCharsets.UTF_8));
58+
59+
AB.main(new String[0]);
60+
61+
String nl = System.lineSeparator();
62+
assertEquals("5" + nl, buf.toString(StandardCharsets.UTF_8));
63+
} finally {
64+
System.setIn(oldIn);
65+
System.setOut(oldOut);
66+
}
67+
}
68+
69+
@Test
70+
void main_bad_input_throws() {
71+
System.setIn(new ByteArrayInputStream("x y\n".getBytes(StandardCharsets.UTF_8)));
72+
assertThrows(InputMismatchException.class, () -> AB.main(new String[0]));
73+
}
2774
}

src/main/java/algorithms/sprint0/SlidingAverage.java

Lines changed: 9 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44

55
import java.io.*;
66
import java.util.ArrayList;
7-
import java.util.Arrays;
87
import java.util.List;
9-
import java.util.stream.Collectors;
108

9+
import static algorithms.sprint0.Utils.readInt;
10+
import static algorithms.sprint0.Utils.readList;
1111
import static org.junit.jupiter.api.Assertions.assertEquals;
1212

1313
public class SlidingAverage {
@@ -40,18 +40,6 @@ public static void main(String[] args) throws IOException {
4040
}
4141
}
4242

43-
44-
private static int readInt(BufferedReader reader) throws IOException {
45-
return Integer.parseInt(reader.readLine());
46-
}
47-
48-
private static List<Integer> readList(BufferedReader reader) throws IOException {
49-
return Arrays.asList(reader.readLine().split(" "))
50-
.stream()
51-
.map(elem -> Integer.parseInt(elem))
52-
.collect(Collectors.toList());
53-
}
54-
5543
private static void assertListDoubles(List<Double> actual, double... expected) {
5644
assertEquals(expected.length, actual.size(), "size");
5745
for (int i = 0; i < expected.length; i++) {
@@ -67,31 +55,31 @@ void test1() {
6755

6856
@Test
6957
void w1_returnsOriginalValues() {
70-
List<Double> actual = movingAverage(7, List.of(1,2,3,4,5,6,7), 1);
71-
assertListDoubles(actual, 1,2,3,4,5,6,7);
58+
List<Double> actual = movingAverage(7, List.of(1, 2, 3, 4, 5, 6, 7), 1);
59+
assertListDoubles(actual, 1, 2, 3, 4, 5, 6, 7);
7260
}
7361

7462
@Test
7563
void wEqMin_singleAverage() {
76-
List<Double> actual = movingAverage(7, List.of(1,2,3,4,5,6,7), 7);
64+
List<Double> actual = movingAverage(7, List.of(1, 2, 3, 4, 5, 6, 7), 7);
7765
assertListDoubles(actual, 4.0);
7866
}
7967

8068
@Test
8169
void wGreaterThanMin_empty() {
82-
List<Double> actual = movingAverage(5, List.of(1,2,3,4,5,6,7), 6);
70+
List<Double> actual = movingAverage(5, List.of(1, 2, 3, 4, 5, 6, 7), 6);
8371
assertEquals(List.of(), actual);
8472
}
8573

8674
@Test
8775
void wZeroOrNegative_empty() {
88-
assertEquals(List.of(), movingAverage(5, List.of(1,2,3,4,5), 0));
89-
assertEquals(List.of(), movingAverage(5, List.of(1,2,3,4,5), -3));
76+
assertEquals(List.of(), movingAverage(5, List.of(1, 2, 3, 4, 5), 0));
77+
assertEquals(List.of(), movingAverage(5, List.of(1, 2, 3, 4, 5), -3));
9078
}
9179

9280
@Test
9381
void cutByN_truncatesAndAverages() {
94-
List<Double> actual = movingAverage(5, List.of(1,2,3,4,5,6,7), 3);
82+
List<Double> actual = movingAverage(5, List.of(1, 2, 3, 4, 5, 6, 7), 3);
9583
// окна по первым 5 элементам: [1,2,3],[2,3,4],[3,4,5]
9684
assertListDoubles(actual, 2.0, 3.0, 4.0);
9785
}

src/main/java/algorithms/sprint0/Solution.java

Lines changed: 0 additions & 24 deletions
This file was deleted.
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package algorithms.sprint0;
2+
3+
import lombok.experimental.UtilityClass;
4+
5+
import java.io.BufferedReader;
6+
import java.io.IOException;
7+
import java.io.Writer;
8+
import java.util.Arrays;
9+
import java.util.List;
10+
import java.util.stream.Collectors;
11+
12+
@UtilityClass
13+
public class Utils {
14+
15+
public static List<Integer> readList(BufferedReader reader) throws IOException {
16+
return Arrays.stream(reader.readLine().trim().split("\\s+"))
17+
.map(Integer::parseInt)
18+
.collect(Collectors.toList());
19+
}
20+
21+
public static <T> void printList(List<T> list, Writer writer) {
22+
for (T elem : list) {
23+
try {
24+
writer.write(String.valueOf(elem));
25+
writer.write(" ");
26+
} catch (IOException ignored) {
27+
}
28+
}
29+
}
30+
31+
public static int readInt(BufferedReader reader) throws IOException {
32+
return Integer.parseInt(reader.readLine());
33+
}
34+
}

src/main/java/algorithms/sprint0/Zip.java

Lines changed: 51 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,10 @@
66
import java.util.ArrayList;
77
import java.util.Arrays;
88
import java.util.List;
9-
import java.util.stream.Collectors;
109

11-
import static org.junit.jupiter.api.Assertions.assertEquals;
10+
import static algorithms.sprint0.Utils.printList;
11+
import static algorithms.sprint0.Utils.readList;
12+
import static org.junit.jupiter.api.Assertions.*;
1213

1314
public class Zip {
1415

@@ -34,39 +35,63 @@ public static void main(String[] args) throws IOException {
3435
}
3536
}
3637

37-
private static List<Integer> readList(BufferedReader reader) throws IOException {
38-
return Arrays.stream(reader.readLine().trim().split("\\s+"))
39-
.map(Integer::parseInt)
40-
.collect(Collectors.toList());
38+
@Test
39+
void equalLength_nEqualsSize() {
40+
List<Integer> a = Arrays.asList(1, 3, 5);
41+
List<Integer> b = Arrays.asList(2, 4, 6);
42+
assertEquals(Arrays.asList(1, 2, 3, 4, 5, 6), zip(a, b, 3));
4143
}
4244

43-
private static <T> void printList(List<T> list, Writer writer) {
44-
for (T elem : list) {
45-
try {
46-
writer.write(String.valueOf(elem));
47-
writer.write(" ");
48-
} catch (IOException ignored) {
49-
}
50-
}
45+
@Test
46+
void nSmallerThanSizes() {
47+
List<Integer> a = Arrays.asList(10, 30, 50);
48+
List<Integer> b = Arrays.asList(20, 40, 60);
49+
assertEquals(Arrays.asList(10, 20, 30, 40), zip(a, b, 2));
50+
}
51+
52+
@Test
53+
void nZero_returnsEmpty() {
54+
List<Integer> a = Arrays.asList(1, 3, 5);
55+
List<Integer> b = Arrays.asList(2, 4, 6);
56+
assertTrue(zip(a, b, 0).isEmpty());
57+
}
58+
59+
@Test
60+
void nGreaterThanSizes_clampedToMin() {
61+
List<Integer> a = Arrays.asList(1, 3, 5);
62+
List<Integer> b = Arrays.asList(2, 4, 6);
63+
assertEquals(Arrays.asList(1, 2, 3, 4, 5, 6), zip(a, b, 10));
64+
}
65+
66+
@Test
67+
void oneListShorter_minByShorter() {
68+
List<Integer> a = List.of(1);
69+
List<Integer> b = Arrays.asList(2, 4, 6);
70+
assertEquals(Arrays.asList(1, 2), zip(a, b, 3));
5171
}
5272

5373
@Test
54-
void test1() {
55-
List<Integer> integers1 = List.of(1, 5, 6);
56-
List<Integer> integers2 = List.of(7, 8, 9);
57-
List<Integer> actualZip = zip(integers1, integers2, 3);
74+
void emptyLists_anyN_returnsEmpty() {
75+
List<Integer> a = List.of();
76+
List<Integer> b = List.of();
77+
assertTrue(zip(a, b, 5).isEmpty());
78+
}
5879

59-
List<Integer> expected = List.of(1, 7, 5, 8, 6, 9);
60-
assertEquals(expected, actualZip);
80+
@Test
81+
void negativeN_throwsIAE_withMessage() {
82+
IllegalArgumentException ex =
83+
assertThrows(IllegalArgumentException.class,
84+
() -> zip(List.of(1), List.of(2), -1));
85+
assertTrue(ex.getMessage().contains("n >= 0"));
6186
}
6287

6388
@Test
64-
void test2() {
65-
List<Integer> integers1 = List.of(1, 5, 6);
66-
List<Integer> integers2 = List.of(7, 8, 9);
67-
List<Integer> actualZip = zip(integers1, integers2, 0);
89+
void nullA_throwsNPE() {
90+
assertThrows(NullPointerException.class, () -> zip(null, List.of(2), 1));
91+
}
6892

69-
List<Integer> expected = List.of();
70-
assertEquals(expected, actualZip);
93+
@Test
94+
void nullB_throwsNPE() {
95+
assertThrows(NullPointerException.class, () -> zip(List.of(1), null, 1));
7196
}
7297
}

0 commit comments

Comments
 (0)