Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import org.junit.Assert;
- import org.junit.Test;
- import java.util.Arrays;
- import java.util.Random;
- public class TheClockwiseSpiralTest {
- TheClockwiseSpiral spiral = new TheClockwiseSpiral();
- private static int[][] check7894(int N) {
- int[][] a = new int[N][N];
- int oldN = N;
- int[] steps = {0, 1, 0, -1};
- int turn = 0;
- int c = 1;
- int i = 0;
- int j = 0;
- int passed = 0;
- while (N > 0) {
- a[i][j] = c;
- if ((c - passed) % N == 0) {
- passed += N;
- if (turn % 2 == 0)
- N--;
- turn++;
- }
- i += steps[(turn + 4) % 4];
- j += steps[(turn + 5) % 4];
- c++;
- }
- N = oldN;
- for(i = 0; i < N/2; i+=2) {
- int[] tmp = Arrays.copyOf(a[i], N);
- a[i] = a[N -i];
- a[N - i] = tmp;
- }
- return a;
- }
- @Test
- public void randomized_30_tests() {
- Random random = new Random();
- for (int i = 0; i < 30; i++) {
- int n = random.nextInt(50);
- int[][] expected = check7894(n);
- Assert.assertArrayEquals(expected, spiral.createSpiral(n));
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement