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
20 changes: 19 additions & 1 deletion src/main/java/algorithms/sprint5/PyramidSort.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
//https://contest.yandex.ru/contest/24810/run-report/160623687/

public class PyramidSort {
private static final int MAX_PARTICIPANTS = 100_000;
private static final int MAX_LOGIN_BYTES = 1_024;

/*
* Принцип работы алгоритма:
Expand Down Expand Up @@ -147,11 +149,21 @@ int nextInt() throws IOException {
if (c == '-') {
sign = -1;
c = read();
if (c <= ' ') {
throw new NumberFormatException("Expected digit after sign");
}
}

int val = 0;
while (c > ' ') {
val = val * 10 + c - '0';
if (c < '0' || c > '9') {
throw new NumberFormatException("Invalid integer input");
}
int digit = c - '0';
if (val > (Integer.MAX_VALUE - digit) / 10) {
throw new NumberFormatException("Integer input is too large");
Comment thread
krotname marked this conversation as resolved.
}
val = val * 10 + digit;
c = read();
}
return val * sign;
Expand All @@ -170,6 +182,9 @@ String next() throws IOException {
int n = 0;

while (c > ' ') {
if (n == MAX_LOGIN_BYTES) {
throw new IOException("Login token is too long");
}
if (n == tmp.length) {
byte[] next = new byte[tmp.length * 2];
System.arraycopy(tmp, 0, next, 0, tmp.length);
Expand Down Expand Up @@ -219,6 +234,9 @@ private static void run() throws Exception {
FastOut out = new FastOut(System.out);

int n = in.nextInt();
if (n < 0 || n > MAX_PARTICIPANTS) {
throw new IllegalArgumentException("Participant count is out of range");
}
Participant[] a = new Participant[n];

for (int i = 0; i < n; i++) {
Expand Down
66 changes: 66 additions & 0 deletions src/test/java/algorithms/sprint5/PyramidSortTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package algorithms.sprint5;

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

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.nio.charset.StandardCharsets;

import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;

@Tag("unit")
class PyramidSortTest {

@Test
void fastInRejectsOverflowingInteger() {
PyramidSort.FastIn in = fastIn("2147483648 ");

assertThrows(NumberFormatException.class, in::nextInt);
}

@Test
void fastInRejectsOversizedLoginToken() {
PyramidSort.FastIn in = fastIn("a".repeat(1_025) + " ");

assertThrows(java.io.IOException.class, in::next);
}

@Test
void runRejectsNegativeParticipantCountBeforeAllocation() throws Exception {
InputStream stdin = System.in;
System.setIn(new ByteArrayInputStream("-1\n".getBytes(StandardCharsets.UTF_8)));
try {
Method run = PyramidSort.class.getDeclaredMethod("run");
run.setAccessible(true);

InvocationTargetException ex = assertThrows(InvocationTargetException.class, () -> run.invoke(null));

assertEquals(IllegalArgumentException.class, ex.getCause().getClass());
} finally {
System.setIn(stdin);
}
}

@Test
void solvePreservesParticipantOrdering() {
PyramidSort.Participant[] participants = new PyramidSort.Participant[] {
new PyramidSort.Participant("alla", 4, 100),
new PyramidSort.Participant("gena", 6, 1000),
new PyramidSort.Participant("timofey", 4, 80)
};

PyramidSort.solve(participants);

assertEquals("gena", participants[0].login);
assertEquals("timofey", participants[1].login);
assertEquals("alla", participants[2].login);
}

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