1
2
3
4
5
6
7
8
9
10
11 package org.eclipse.jgit.util;
12
13 import static org.junit.Assert.assertEquals;
14 import static org.junit.Assert.assertFalse;
15 import static org.junit.Assert.assertNull;
16 import static org.junit.Assert.assertSame;
17 import static org.junit.Assert.assertTrue;
18
19 import org.junit.Before;
20 import org.junit.Test;
21
22 public class LongMapTest {
23 private LongMap<Long> map;
24
25 @Before
26 public void setUp() throws Exception {
27 map = new LongMap<>();
28 }
29
30 @Test
31 public void testEmptyMap() {
32 assertFalse(map.containsKey(0));
33 assertFalse(map.containsKey(1));
34
35 assertNull(map.get(0));
36 assertNull(map.get(1));
37
38 assertNull(map.remove(0));
39 assertNull(map.remove(1));
40 }
41
42 @Test
43 public void testInsertMinValue() {
44 final Long min = Long.valueOf(Long.MIN_VALUE);
45 assertNull(map.put(Long.MIN_VALUE, min));
46 assertTrue(map.containsKey(Long.MIN_VALUE));
47 assertSame(min, map.get(Long.MIN_VALUE));
48 assertFalse(map.containsKey(Integer.MIN_VALUE));
49 }
50
51 @Test
52 public void testReplaceMaxValue() {
53 final Long min = Long.valueOf(Long.MAX_VALUE);
54 final Long one = Long.valueOf(1);
55 assertNull(map.put(Long.MAX_VALUE, min));
56 assertSame(min, map.get(Long.MAX_VALUE));
57 assertSame(min, map.put(Long.MAX_VALUE, one));
58 assertSame(one, map.get(Long.MAX_VALUE));
59 }
60
61 @Test
62 public void testRemoveOne() {
63 final long start = 1;
64 assertNull(map.put(start, Long.valueOf(start)));
65 assertEquals(Long.valueOf(start), map.remove(start));
66 assertFalse(map.containsKey(start));
67 }
68
69 @Test
70 public void testRemoveCollision1() {
71
72
73
74
75
76
77 assertNull(map.put(0, Long.valueOf(0)));
78 assertNull(map.put(1, Long.valueOf(1)));
79 assertEquals(Long.valueOf(0), map.remove(0));
80
81 assertFalse(map.containsKey(0));
82 assertTrue(map.containsKey(1));
83 }
84
85 @Test
86 public void testRemoveCollision2() {
87
88
89
90
91
92
93 assertNull(map.put(0, Long.valueOf(0)));
94 assertNull(map.put(1, Long.valueOf(1)));
95 assertEquals(Long.valueOf(1), map.remove(1));
96
97 assertTrue(map.containsKey(0));
98 assertFalse(map.containsKey(1));
99 }
100
101 @Test
102 public void testSmallMap() {
103 final long start = 12;
104 final long n = 8;
105 for (long i = start; i < start + n; i++)
106 assertNull(map.put(i, Long.valueOf(i)));
107 for (long i = start; i < start + n; i++)
108 assertEquals(Long.valueOf(i), map.get(i));
109 }
110
111 @Test
112 public void testLargeMap() {
113 final long start = Integer.MAX_VALUE;
114 final long n = 100000;
115 for (long i = start; i < start + n; i++)
116 assertNull(map.put(i, Long.valueOf(i)));
117 for (long i = start; i < start + n; i++)
118 assertEquals(Long.valueOf(i), map.get(i));
119 }
120 }