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
50 changes: 42 additions & 8 deletions src/main/java/algorithms/sprint1/Solution2.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,13 @@
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.nio.charset.StandardCharsets;
import java.util.StringTokenizer;

public class Solution2 {
private static final int MAX_LINE_COUNT = 1_000_000;

public static void solution(Node<String> head) {
StringBuilder output = new StringBuilder();
Node<String> current = head;
Expand All @@ -35,16 +38,47 @@
}

public static void main(String[] args) throws IOException {
StringBuilder outputBuffer = new StringBuilder();
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in, StandardCharsets.UTF_8));
int lineCount = parseInt(reader.readLine());
int lineCount = readLineCount(reader);
PrintWriter writer = new PrintWriter(System.out, false, StandardCharsets.UTF_8);

Check warning on line 43 in src/main/java/algorithms/sprint1/Solution2.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Replace this use of System.out by a logger.

See more on https://sonarcloud.io/project/issues?id=krotname_JavaAlgorithmsShowcase&issues=AZ-4pQ1UlD4Vfl8e7JHW&open=AZ-4pQ1UlD4Vfl8e7JHW&pullRequest=89
for (int i = 0; i < lineCount; ++i) {
StringTokenizer tokenizer = new StringTokenizer(reader.readLine());
int firstValue = parseInt(tokenizer.nextToken());
int secondValue = parseInt(tokenizer.nextToken());
int result = firstValue + secondValue;
outputBuffer.append(result).append("\n");
StringTokenizer tokenizer = new StringTokenizer(readInputLine(reader, i + 1));
int firstValue = parseInt(nextToken(tokenizer, i + 1));
int secondValue = parseInt(nextToken(tokenizer, i + 1));
if (tokenizer.hasMoreTokens()) {
throw new IllegalArgumentException("Line " + (i + 1) + " must contain exactly two integers");
}
int result = Math.addExact(firstValue, secondValue);
writer.println(result);
}
writer.println();
writer.flush();
}

private static int readLineCount(BufferedReader reader) throws IOException {
String countLine = reader.readLine();
if (countLine == null) {
throw new IllegalArgumentException("Missing line count");
}
int lineCount = parseInt(countLine);
if (lineCount < 0 || lineCount > MAX_LINE_COUNT) {
throw new IllegalArgumentException("Line count must be between 0 and " + MAX_LINE_COUNT);
}
return lineCount;
}

private static String readInputLine(BufferedReader reader, int lineNumber) throws IOException {
String inputLine = reader.readLine();
if (inputLine == null) {
throw new IllegalArgumentException("Missing input line " + lineNumber);
}
return inputLine;
}

private static String nextToken(StringTokenizer tokenizer, int lineNumber) {
if (!tokenizer.hasMoreTokens()) {
throw new IllegalArgumentException("Line " + lineNumber + " must contain exactly two integers");
}
System.out.println(outputBuffer);
return tokenizer.nextToken();
}
}
17 changes: 17 additions & 0 deletions src/test/java/algorithms/AlgorithmCliTest.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package algorithms;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.params.provider.Arguments.arguments;

import java.io.ByteArrayInputStream;
Expand Down Expand Up @@ -33,6 +34,22 @@
assertEquals(normalizeLines(expectedOutput), normalizeLines(invokeWithStdio(className, methodName, input)));
}

@Test
void solution2RejectsMissingInputLine() {
assertThrows(IllegalArgumentException.class, () -> invokeWithStdio(

Check warning on line 39 in src/test/java/algorithms/AlgorithmCliTest.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Refactor the code of the lambda to have only one invocation possibly throwing a runtime exception.

See more on https://sonarcloud.io/project/issues?id=krotname_JavaAlgorithmsShowcase&issues=AZ-4pQzHlD4Vfl8e7JHU&open=AZ-4pQzHlD4Vfl8e7JHU&pullRequest=89
"algorithms.sprint1.Solution2",
"main",
"2%n1 2%n".formatted()));
}

@Test
void solution2RejectsOversizedLineCount() {
assertThrows(IllegalArgumentException.class, () -> invokeWithStdio(

Check warning on line 47 in src/test/java/algorithms/AlgorithmCliTest.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Refactor the code of the lambda to have only one invocation possibly throwing a runtime exception.

See more on https://sonarcloud.io/project/issues?id=krotname_JavaAlgorithmsShowcase&issues=AZ-4pQzHlD4Vfl8e7JHV&open=AZ-4pQzHlD4Vfl8e7JHV&pullRequest=89
"algorithms.sprint1.Solution2",
"main",
"1000001%n".formatted()));
}

@Test
void distancesRunReadsAndWritesContestFiles() throws Exception {
Path input = Path.of("input.txt");
Expand Down