View Javadoc
1   /*
2    * Copyright (C) 2009, Google Inc. and others
3    *
4    * This program and the accompanying materials are made available under the
5    * terms of the Eclipse Distribution License v. 1.0 which is available at
6    * https://www.eclipse.org/org/documents/edl-v10.php.
7    *
8    * SPDX-License-Identifier: BSD-3-Clause
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  		// This test relies upon the fact that we always >>> 1 the value
72  		// to derive an unsigned hash code. Thus, 0 and 1 fall into the
73  		// same hash bucket. Further it relies on the fact that we add
74  		// the 2nd put at the top of the chain, so removing the 1st will
75  		// cause a different code path.
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  		// This test relies upon the fact that we always >>> 1 the value
88  		// to derive an unsigned hash code. Thus, 0 and 1 fall into the
89  		// same hash bucket. Further it relies on the fact that we add
90  		// the 2nd put at the top of the chain, so removing the 2nd will
91  		// cause a different code path.
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 }