|
| 1 | +package com.thealgorithms.datastructures.hashmap.hashing; |
| 2 | + |
| 3 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 4 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 5 | + |
| 6 | +import java.util.List; |
| 7 | +import org.junit.jupiter.api.Test; |
| 8 | + |
| 9 | +public class IntersectionTest { |
| 10 | + |
| 11 | + @Test |
| 12 | + void testBasicIntersection() { |
| 13 | + int[] arr1 = {1, 2, 2, 1}; |
| 14 | + int[] arr2 = {2, 2}; |
| 15 | + List<Integer> result = Intersection.intersection(arr1, arr2); |
| 16 | + assertEquals(List.of(2, 2), result, "Intersection should return [2, 2]"); |
| 17 | + } |
| 18 | + |
| 19 | + @Test |
| 20 | + void testNoIntersection() { |
| 21 | + int[] arr1 = {1, 2, 3}; |
| 22 | + int[] arr2 = {4, 5, 6}; |
| 23 | + List<Integer> result = Intersection.intersection(arr1, arr2); |
| 24 | + assertTrue(result.isEmpty(), "Intersection should be empty for disjoint sets"); |
| 25 | + } |
| 26 | + |
| 27 | + @Test |
| 28 | + void testEmptyArray() { |
| 29 | + int[] arr1 = {}; |
| 30 | + int[] arr2 = {1, 2, 3}; |
| 31 | + List<Integer> result = Intersection.intersection(arr1, arr2); |
| 32 | + assertTrue(result.isEmpty(), "Intersection should be empty when first array is empty"); |
| 33 | + |
| 34 | + result = Intersection.intersection(arr2, arr1); |
| 35 | + assertTrue(result.isEmpty(), "Intersection should be empty when second array is empty"); |
| 36 | + } |
| 37 | + |
| 38 | + @Test |
| 39 | + void testNullArray() { |
| 40 | + int[] arr1 = null; |
| 41 | + int[] arr2 = {1, 2, 3}; |
| 42 | + List<Integer> result = Intersection.intersection(arr1, arr2); |
| 43 | + assertTrue(result.isEmpty(), "Intersection should be empty when first array is null"); |
| 44 | + |
| 45 | + result = Intersection.intersection(arr2, arr1); |
| 46 | + assertTrue(result.isEmpty(), "Intersection should be empty when second array is null"); |
| 47 | + } |
| 48 | + |
| 49 | + @Test |
| 50 | + void testMultipleOccurrences() { |
| 51 | + int[] arr1 = {5, 5, 5, 6}; |
| 52 | + int[] arr2 = {5, 5, 6, 6, 6}; |
| 53 | + List<Integer> result = Intersection.intersection(arr1, arr2); |
| 54 | + assertEquals(List.of(5, 5, 6), result, "Intersection should return [5, 5, 6]"); |
| 55 | + } |
| 56 | + |
| 57 | + @Test |
| 58 | + void testSameElements() { |
| 59 | + int[] arr1 = {1, 1, 1}; |
| 60 | + int[] arr2 = {1, 1, 1}; |
| 61 | + List<Integer> result = Intersection.intersection(arr1, arr2); |
| 62 | + assertEquals(List.of(1, 1, 1), result, "Intersection should return [1, 1, 1] for same elements"); |
| 63 | + } |
| 64 | + |
| 65 | + @Test |
| 66 | + void testLargeArrays() { |
| 67 | + int[] arr1 = new int[1000]; |
| 68 | + int[] arr2 = new int[1000]; |
| 69 | + for (int i = 0; i < 1000; i++) { |
| 70 | + arr1[i] = i; |
| 71 | + arr2[i] = i; |
| 72 | + } |
| 73 | + List<Integer> result = Intersection.intersection(arr1, arr2); |
| 74 | + assertEquals(1000, result.size(), "Intersection should return all elements for identical large arrays"); |
| 75 | + } |
| 76 | +} |
0 commit comments