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