1+ package array .lucky_integer ;
2+
3+ import org .junit .jupiter .api .BeforeEach ;
4+ import org .junit .jupiter .api .DisplayName ;
5+ import org .junit .jupiter .api .Test ;
6+
7+ import static org .junit .jupiter .api .Assertions .assertEquals ;
8+
9+ /**
10+ * Test suite for the {@link FrequencyMap} class.
11+ */
12+ class FrequencyMapTest {
13+
14+ private FrequencyMap frequencyMap ;
15+
16+ @ BeforeEach
17+ void setUp () {
18+ frequencyMap = new FrequencyMap ();
19+ }
20+
21+ @ Test
22+ @ DisplayName ("Should return the largest lucky integer when multiple exist" )
23+ void findLucky_whenMultipleLuckyIntegersExist_shouldReturnLargest () {
24+ int [] arr = {1 , 2 , 2 , 3 , 3 , 3 };
25+ assertEquals (3 , frequencyMap .findLucky (arr ), "The largest lucky integer is 3, as 1 and 3 are both lucky." );
26+ }
27+
28+ @ Test
29+ @ DisplayName ("Should return a single lucky integer when one exists" )
30+ void findLucky_whenOneLuckyIntegerExists_shouldReturnIt () {
31+ int [] arr = {2 , 2 , 3 , 4 };
32+ assertEquals (2 , frequencyMap .findLucky (arr ), "The only lucky integer is 2." );
33+ }
34+
35+ @ Test
36+ @ DisplayName ("Should return -1 when no lucky integer exists" )
37+ void findLucky_whenNoLuckyIntegerExists_shouldReturnMinusOne () {
38+ int [] arr = {1 , 2 , 2 , 3 , 3 , 2 , 1 };
39+ assertEquals (-1 , frequencyMap .findLucky (arr ), "No number has a frequency equal to its value." );
40+ }
41+
42+ @ Test
43+ @ DisplayName ("Should return -1 for an empty array" )
44+ void findLucky_whenArrayIsEmpty_shouldReturnMinusOne () {
45+ int [] arr = {};
46+ assertEquals (-1 , frequencyMap .findLucky (arr ), "An empty array has no lucky integers." );
47+ }
48+
49+ @ Test
50+ @ DisplayName ("Should handle an array where all elements are the same and form a lucky number" )
51+ void findLucky_whenAllElementsAreSameAndLucky_shouldReturnTheNumber () {
52+ int [] arr = {4 , 4 , 4 , 4 };
53+ assertEquals (4 , frequencyMap .findLucky (arr ), "4 appears 4 times, so it is lucky." );
54+ }
55+
56+ @ Test
57+ @ DisplayName ("Should handle an array where all elements are the same but not lucky" )
58+ void findLucky_whenAllElementsAreSameAndNotLucky_shouldReturnMinusOne () {
59+ int [] arr = {5 , 5 , 5 };
60+ assertEquals (-1 , frequencyMap .findLucky (arr ), "5 appears 3 times, so it is not lucky." );
61+ }
62+
63+ @ Test
64+ @ DisplayName ("Should handle a single element array that is lucky" )
65+ void findLucky_whenSingleElementIsLucky_shouldReturnTheNumber () {
66+ int [] arr = {1 };
67+ assertEquals (1 , frequencyMap .findLucky (arr ), "1 appears 1 time, so it is lucky." );
68+ }
69+
70+ @ Test
71+ @ DisplayName ("Should handle a single element array that is not lucky" )
72+ void findLucky_whenSingleElementIsNotLucky_shouldReturnMinusOne () {
73+ int [] arr = {2 };
74+ assertEquals (-1 , frequencyMap .findLucky (arr ), "2 appears 1 time, so it is not lucky." );
75+ }
76+
77+ @ Test
78+ @ DisplayName ("Should handle a complex case with various numbers" )
79+ void findLucky_withComplexArray_shouldReturnCorrectLargestLucky () {
80+ int [] arr = {7 , 7 , 7 , 7 , 7 , 7 , 7 , 5 , 5 , 5 , 5 , 5 , 2 , 2 , 8 , 9 };
81+ assertEquals (7 , frequencyMap .findLucky (arr ), "Both 5 and 7 are lucky, but 7 is the largest." );
82+ }
83+
84+ @ Test
85+ @ DisplayName ("Should ignore a smaller lucky number after a larger one is found" )
86+ void findLucky_shouldNotBeReplacedBySmallerLuckyNumber () {
87+ // This test explicitly verifies the `num > lucky` part of the condition.
88+ // The map will contain two lucky numbers: 4 (appears 4 times) and 2 (appears 2 times).
89+ // The logic must correctly identify 4 as the largest lucky number,
90+ // even if it processes 2 after 4.
91+ int [] arr = {4 , 4 , 4 , 4 , 2 , 2 };
92+ assertEquals (4 , frequencyMap .findLucky (arr ), "The result should be the largest lucky number (4), not the smaller one (2)." );
93+ }
94+
95+ @ Test
96+ @ DisplayName ("Should correctly identify the largest lucky number when a smaller one is also present" )
97+ void findLucky_shouldHandleMultipleLuckyNumbers () {
98+ // This test ensures that if a smaller lucky number (e.g., 2) is processed
99+ // after a larger one (e.g., 3), the larger one is correctly retained.
100+ // This specifically covers the case where `num == count` is true,
101+ // but `num > lucky` is false.
102+ int [] arr = {2 , 2 , 3 , 3 , 3 };
103+ assertEquals (3 , frequencyMap .findLucky (arr ), "The largest lucky number should be 3, not 2." );
104+ }
105+ }
0 commit comments