Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 13 additions & 8 deletions src/main/java/algorithms/sprint1/Distances.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,14 @@
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.EOFException;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Arrays;

// https://contest.yandex.ru/contest/22450/run-report/157289807/
public class Distances {
private static final int MAX_INPUT_SIZE = 100_000;

// -------------------- SOLUTION --------------------
static int[] solve(int[] a) {
Expand Down Expand Up @@ -113,18 +112,20 @@ void writeInt(int x) throws IOException {
void flush() throws IOException {
out.write(buf, 0, p);
p = 0;
out.flush();
}
}

// -------------------- INPUT / OUTPUT --------------------
static void run() {

try (InputStream input = new BufferedInputStream(new FileInputStream("input.txt"));
OutputStream output = new BufferedOutputStream(new FileOutputStream("output.txt"))) {
FastIn in = new FastIn(input);
FastOut out = new FastOut(output);
static void run(InputStream input, OutputStream output) {
try {
FastIn in = new FastIn(new BufferedInputStream(input));
FastOut out = new FastOut(new BufferedOutputStream(output));

int n = in.nextInt();
if (n < 1 || n > MAX_INPUT_SIZE) {
throw new IllegalArgumentException("Input size must be between 1 and " + MAX_INPUT_SIZE);
}
int[] a = new int[n];
for (int i = 0; i < n; i++) a[i] = in.nextInt();

Expand All @@ -141,6 +142,10 @@ static void run() {
}
}

static void run() {
run(System.in, System.out);
}

// -------------------- TESTS --------------------
static void assertEq(int[] exp, int[] act, String name) {
if (!Arrays.equals(exp, act)) {
Expand Down
27 changes: 3 additions & 24 deletions src/test/java/algorithms/AlgorithmCliTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,8 @@
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.stream.Stream;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
Expand All @@ -33,23 +30,6 @@ void stdioEntryPointProducesExpectedOutput(
assertEquals(normalizeLines(expectedOutput), normalizeLines(invokeWithStdio(className, methodName, input)));
}

@Test
void distancesRunReadsAndWritesContestFiles() throws Exception {
Path input = Path.of("input.txt");
Path output = Path.of("output.txt");
Files.writeString(input, "5%n0 1 4 9 0%n".formatted(), StandardCharsets.UTF_8);
Files.deleteIfExists(output);

try {
invokeNoArg("algorithms.sprint1.Distances", "run");

assertEquals("0 1 2 1 0\n", normalizeLines(Files.readString(output, StandardCharsets.UTF_8)));
} finally {
Files.deleteIfExists(input);
Files.deleteIfExists(output);
}
}

private static Stream<Arguments> stdioCases() {
return Stream.of(
arguments("algorithms.sprint0.SlidingAverage", "main",
Expand All @@ -61,6 +41,9 @@ private static Stream<Arguments> stdioCases() {
arguments("algorithms.sprint1.SleightOfHand", "run",
"3%n1231%n2..2%n2..2%n2..2%n".formatted(),
"2%n".formatted()),
arguments("algorithms.sprint1.Distances", "run",
"5%n0 1 4 9 0%n".formatted(),
"0 1 2 1 0%n".formatted()),
arguments("algorithms.sprint1.Solution2", "main",
"2%n1 2%n3 4%n".formatted(),
"3%n7%n%n".formatted()),
Expand Down Expand Up @@ -154,10 +137,6 @@ private static String invokeWithStdio(String className, String methodName, Strin
}
}

private static void invokeNoArg(String className, String methodName) throws Exception {
invoke(className, methodName);
}

private static void invoke(String className, String methodName) throws Exception {
Method method = "main".equals(methodName)
? Class.forName(className).getDeclaredMethod(methodName, String[].class)
Expand Down
38 changes: 38 additions & 0 deletions src/test/java/algorithms/sprint1/DistancesTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package algorithms.sprint1;

import org.junit.jupiter.api.Test;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.nio.charset.StandardCharsets;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;

class DistancesTest {

@Test
void readsAndWritesThroughProvidedStreams() {
ByteArrayOutputStream output = new ByteArrayOutputStream();

Distances.run(input("5 0 1 4 9 0"), output);

assertEquals("0 1 2 1 0\n", output.toString(StandardCharsets.UTF_8));
}

@Test
void rejectsNegativeInputSize() {
assertThrows(IllegalArgumentException.class,
() -> Distances.run(input("-1"), new ByteArrayOutputStream()));
}

@Test
void rejectsExcessiveInputSize() {
assertThrows(IllegalArgumentException.class,
() -> Distances.run(input("100001"), new ByteArrayOutputStream()));
}

private static ByteArrayInputStream input(String value) {
return new ByteArrayInputStream(value.getBytes(StandardCharsets.UTF_8));
}
}