|
1 | 1 | package com.thealgorithms.slidingwindow;
|
2 | 2 |
|
3 |
| -import static org.junit.jupiter.api.Assertions.assertEquals; |
4 |
| - |
| 3 | +import static org.junit.jupiter.api.Assertions.assertArrayEquals; |
| 4 | +import java.util.Arrays; |
5 | 5 | import org.junit.jupiter.api.Test;
|
6 | 6 |
|
7 | 7 | /**
|
|
12 | 12 | public class ShortestCoprimeSegmentTest {
|
13 | 13 | @Test
|
14 | 14 | public void testShortestCoprimeSegment() {
|
15 |
| - assertEquals(3, ShortestCoprimeSegment.shortestCoprimeSegment(5, new long[] {4, 6, 9, 3, 6})); |
16 |
| - assertEquals(2, ShortestCoprimeSegment.shortestCoprimeSegment(5, new long[] {4, 5, 9, 3, 6})); |
17 |
| - assertEquals(2, ShortestCoprimeSegment.shortestCoprimeSegment(2, new long[] {3, 2})); |
18 |
| - assertEquals(2, ShortestCoprimeSegment.shortestCoprimeSegment(5, new long[] {3, 9, 9, 9, 10})); |
19 |
| - assertEquals(4, ShortestCoprimeSegment.shortestCoprimeSegment(4, new long[] {3 * 7, 7 * 5, 5 * 7 * 3, 3 * 5})); |
20 |
| - assertEquals(4, ShortestCoprimeSegment.shortestCoprimeSegment(4, new long[] {3 * 11, 11 * 7, 11 * 7 * 3, 3 * 7})); |
21 |
| - assertEquals(5, ShortestCoprimeSegment.shortestCoprimeSegment(5, new long[] {3 * 11, 11 * 7, 11 * 7 * 3, 11 * 7 * 3 * 5, 5 * 7})); |
22 |
| - assertEquals(6, ShortestCoprimeSegment.shortestCoprimeSegment(6, new long[] {3 * 11, 11 * 7, 11 * 7 * 3, 11 * 7 * 3 * 5, 11 * 7 * 3 * 5 * 13, 7 * 13})); |
23 |
| - assertEquals(6, ShortestCoprimeSegment.shortestCoprimeSegment(7, new long[] {3 * 11, 11 * 7, 11 * 7 * 3, 11 * 7 * 3 * 5, 11 * 7 * 3 * 5 * 13, 7 * 13, 11 * 7 * 3 * 5 * 13})); |
24 |
| - assertEquals(10, ShortestCoprimeSegment.shortestCoprimeSegment(10, new long[] {3 * 11, 7 * 11, 3 * 7 * 11, 3 * 5 * 7 * 11, 3 * 5 * 7 * 11 * 13, 2 * 3 * 5 * 7 * 11 * 13, 2 * 3 * 5 * 7 * 11 * 13 * 17, 2 * 3 * 5 * 7 * 11 * 13 * 17 * 19, 2 * 3 * 5 * 7 * 11 * 13 * 17 * 19 * 23, 7 * 13})); |
| 15 | + assertArrayEquals(new long[] {4, 6, 9}, ShortestCoprimeSegment.shortestCoprimeSegment(new long[] {4, 6, 9, 3, 6})); |
| 16 | + assertArrayEquals(new long[] {4, 5}, ShortestCoprimeSegment.shortestCoprimeSegment(new long[] {4, 5, 9, 3, 6})); |
| 17 | + assertArrayEquals(new long[] {3, 2}, ShortestCoprimeSegment.shortestCoprimeSegment(new long[] {3, 2})); |
| 18 | + assertArrayEquals(new long[] {9, 10}, ShortestCoprimeSegment.shortestCoprimeSegment(new long[] {3, 9, 9, 9, 10})); |
| 19 | + |
| 20 | + long[] test5 = new long[] {3 * 11, 11 * 7, 11 * 7 * 3, 11 * 7 * 3 * 5, 11 * 7 * 3 * 5 * 13, 7 * 13, 11 * 7 * 3 * 5 * 13}; |
| 21 | + long[] answer5 = Arrays.copyOfRange(test5, 0, test5.length - 1); |
| 22 | + assertArrayEquals(answer5, ShortestCoprimeSegment.shortestCoprimeSegment(test5)); |
| 23 | + |
| 24 | + // Test suite, when the entire array needs to be taken |
| 25 | + long[] test6 = new long[] {3 * 7, 7 * 5, 5 * 7 * 3, 3 * 5}; |
| 26 | + assertArrayEquals(test6, ShortestCoprimeSegment.shortestCoprimeSegment(test6)); |
| 27 | + |
| 28 | + long[] test7 = new long[] {3 * 11, 11 * 7, 11 * 7 * 3, 3 * 7}; |
| 29 | + assertArrayEquals(test7, ShortestCoprimeSegment.shortestCoprimeSegment(test7)); |
| 30 | + |
| 31 | + long[] test8 = new long[] {3 * 11, 11 * 7, 11 * 7 * 3, 11 * 7 * 3 * 5, 5 * 7}; |
| 32 | + assertArrayEquals(test8, ShortestCoprimeSegment.shortestCoprimeSegment(test8)); |
| 33 | + |
| 34 | + long[] test9 = new long[] {3 * 11, 11 * 7, 11 * 7 * 3, 11 * 7 * 3 * 5, 11 * 7 * 3 * 5 * 13, 7 * 13}; |
| 35 | + assertArrayEquals(test9, ShortestCoprimeSegment.shortestCoprimeSegment(test9)); |
| 36 | + |
| 37 | + long[] test10 = new long[] {3 * 11, 7 * 11, 3 * 7 * 11, 3 * 5 * 7 * 11, 3 * 5 * 7 * 11 * 13, 2 * 3 * 5 * 7 * 11 * 13, 2 * 3 * 5 * 7 * 11 * 13 * 17, 2 * 3 * 5 * 7 * 11 * 13 * 17 * 19, 2 * 3 * 5 * 7 * 11 * 13 * 17 * 19 * 23, 7 * 13}; |
| 38 | + assertArrayEquals(test10, ShortestCoprimeSegment.shortestCoprimeSegment(test10)); |
| 39 | + |
25 | 40 | // Segment can consist of one element
|
26 |
| - assertEquals(1, ShortestCoprimeSegment.shortestCoprimeSegment(5, new long[] {4, 6, 1, 3, 6})); |
27 |
| - assertEquals(1, ShortestCoprimeSegment.shortestCoprimeSegment(1, new long[] {1})); |
| 41 | + long[] test11 = new long[] {1}; |
| 42 | + assertArrayEquals(test11, ShortestCoprimeSegment.shortestCoprimeSegment(new long[] {4, 6, 1, 3, 6})); |
| 43 | + long[] test12 = new long[] {1}; |
| 44 | + assertArrayEquals(test12, ShortestCoprimeSegment.shortestCoprimeSegment(new long[] {1})); |
| 45 | + } |
| 46 | + @Test |
| 47 | + public void testShortestCoprimeSegment2() { |
| 48 | + assertArrayEquals(new long[] {2 * 3, 2 * 3 * 5, 2 * 3 * 5 * 7, 5 * 7}, ShortestCoprimeSegment.shortestCoprimeSegment(new long[] {2 * 3, 2 * 3 * 5, 2 * 3 * 5 * 7, 5 * 7, 2 * 3 * 5 * 7})); |
| 49 | + assertArrayEquals(new long[] {5 * 7, 2}, ShortestCoprimeSegment.shortestCoprimeSegment(new long[] {2 * 3, 2 * 3 * 5, 2 * 3 * 5 * 7, 5 * 7, 2})); |
| 50 | + assertArrayEquals(new long[] {5 * 7, 2 * 5 * 7, 2 * 11}, ShortestCoprimeSegment.shortestCoprimeSegment(new long[] {2 * 3, 2 * 3 * 5, 2 * 3 * 5 * 7, 5 * 7, 2 * 5 * 7, 2 * 11})); |
| 51 | + assertArrayEquals(new long[] {3 * 5 * 7, 2 * 3, 2}, ShortestCoprimeSegment.shortestCoprimeSegment(new long[] {2, 2 * 3, 2 * 3 * 5, 3 * 5 * 7, 2 * 3, 2})); |
28 | 52 | }
|
29 |
| - |
30 | 53 | @Test
|
31 | 54 | public void testNoCoprimeSegment() {
|
32 | 55 | // There may not be a coprime segment
|
33 |
| - assertEquals(-1, ShortestCoprimeSegment.shortestCoprimeSegment(5, new long[] {4, 6, 8, 12, 8})); |
34 |
| - assertEquals(-1, ShortestCoprimeSegment.shortestCoprimeSegment(10, new long[] {4, 4, 4, 4, 10, 4, 6, 8, 12, 8})); |
35 |
| - assertEquals(-1, ShortestCoprimeSegment.shortestCoprimeSegment(1, new long[] {100})); |
36 |
| - assertEquals(-1, ShortestCoprimeSegment.shortestCoprimeSegment(3, new long[] {2, 2, 2})); |
| 56 | + long[] empty = new long[] {}; |
| 57 | + assertArrayEquals(empty, ShortestCoprimeSegment.shortestCoprimeSegment(null)); |
| 58 | + assertArrayEquals(empty, ShortestCoprimeSegment.shortestCoprimeSegment(empty)); |
| 59 | + assertArrayEquals(empty, ShortestCoprimeSegment.shortestCoprimeSegment(new long[] {4, 6, 8, 12, 8})); |
| 60 | + assertArrayEquals(empty, ShortestCoprimeSegment.shortestCoprimeSegment(new long[] {4, 4, 4, 4, 10, 4, 6, 8, 12, 8})); |
| 61 | + assertArrayEquals(empty, ShortestCoprimeSegment.shortestCoprimeSegment(new long[] {100})); |
| 62 | + assertArrayEquals(empty, ShortestCoprimeSegment.shortestCoprimeSegment(new long[] {2, 2, 2})); |
37 | 63 | }
|
38 | 64 | }
|
0 commit comments