Advertisement
Guest User

Untitled

a guest
May 27th, 2018
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.29 KB | None | 0 0
  1. import org.junit.Assert;
  2. import org.junit.Test;
  3.  
  4. import java.util.Arrays;
  5. import java.util.Random;
  6.  
  7. public class TheClockwiseSpiralTest {
  8.  
  9. TheClockwiseSpiral spiral = new TheClockwiseSpiral();
  10.  
  11. private static int[][] check7894(int N) {
  12. int[][] a = new int[N][N];
  13. int oldN = N;
  14. int[] steps = {0, 1, 0, -1};
  15. int turn = 0;
  16.  
  17. int c = 1;
  18. int i = 0;
  19. int j = 0;
  20. int passed = 0;
  21.  
  22. while (N > 0) {
  23. a[i][j] = c;
  24.  
  25. if ((c - passed) % N == 0) {
  26. passed += N;
  27. if (turn % 2 == 0)
  28. N--;
  29. turn++;
  30. }
  31.  
  32. i += steps[(turn + 4) % 4];
  33. j += steps[(turn + 5) % 4];
  34. c++;
  35. }
  36. N = oldN;
  37. for(i = 0; i < N/2; i+=2) {
  38. int[] tmp = Arrays.copyOf(a[i], N);
  39. a[i] = a[N -i];
  40. a[N - i] = tmp;
  41. }
  42. return a;
  43. }
  44.  
  45.  
  46. @Test
  47. public void randomized_30_tests() {
  48. Random random = new Random();
  49. for (int i = 0; i < 30; i++) {
  50. int n = random.nextInt(50);
  51. int[][] expected = check7894(n);
  52. Assert.assertArrayEquals(expected, spiral.createSpiral(n));
  53. }
  54. }
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement