View Javadoc
1   /*
2    * Copyright (C) 2009, Google Inc.
3    * and other copyright owners as documented in the project's IP log.
4    *
5    * This program and the accompanying materials are made available
6    * under the terms of the Eclipse Distribution License v1.0 which
7    * accompanies this distribution, is reproduced below, and is
8    * available at http://www.eclipse.org/org/documents/edl-v10.php
9    *
10   * All rights reserved.
11   *
12   * Redistribution and use in source and binary forms, with or
13   * without modification, are permitted provided that the following
14   * conditions are met:
15   *
16   * - Redistributions of source code must retain the above copyright
17   *   notice, this list of conditions and the following disclaimer.
18   *
19   * - Redistributions in binary form must reproduce the above
20   *   copyright notice, this list of conditions and the following
21   *   disclaimer in the documentation and/or other materials provided
22   *   with the distribution.
23   *
24   * - Neither the name of the Eclipse Foundation, Inc. nor the
25   *   names of its contributors may be used to endorse or promote
26   *   products derived from this software without specific prior
27   *   written permission.
28   *
29   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
30   * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
31   * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
32   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
33   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
34   * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
35   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
36   * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
37   * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
38   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39   * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
40   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
41   * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
42   */
43  
44  package org.eclipse.jgit.util;
45  
46  import static org.junit.Assert.assertEquals;
47  import static org.junit.Assert.assertFalse;
48  import static org.junit.Assert.assertNull;
49  import static org.junit.Assert.assertSame;
50  import static org.junit.Assert.assertTrue;
51  
52  import org.junit.Before;
53  import org.junit.Test;
54  
55  public class LongMapTest {
56  	private LongMap<Long> map;
57  
58  	@Before
59  	public void setUp() throws Exception {
60  		map = new LongMap<>();
61  	}
62  
63  	@Test
64  	public void testEmptyMap() {
65  		assertFalse(map.containsKey(0));
66  		assertFalse(map.containsKey(1));
67  
68  		assertNull(map.get(0));
69  		assertNull(map.get(1));
70  
71  		assertNull(map.remove(0));
72  		assertNull(map.remove(1));
73  	}
74  
75  	@Test
76  	public void testInsertMinValue() {
77  		final Long min = Long.valueOf(Long.MIN_VALUE);
78  		assertNull(map.put(Long.MIN_VALUE, min));
79  		assertTrue(map.containsKey(Long.MIN_VALUE));
80  		assertSame(min, map.get(Long.MIN_VALUE));
81  		assertFalse(map.containsKey(Integer.MIN_VALUE));
82  	}
83  
84  	@Test
85  	public void testReplaceMaxValue() {
86  		final Long min = Long.valueOf(Long.MAX_VALUE);
87  		final Long one = Long.valueOf(1);
88  		assertNull(map.put(Long.MAX_VALUE, min));
89  		assertSame(min, map.get(Long.MAX_VALUE));
90  		assertSame(min, map.put(Long.MAX_VALUE, one));
91  		assertSame(one, map.get(Long.MAX_VALUE));
92  	}
93  
94  	@Test
95  	public void testRemoveOne() {
96  		final long start = 1;
97  		assertNull(map.put(start, Long.valueOf(start)));
98  		assertEquals(Long.valueOf(start), map.remove(start));
99  		assertFalse(map.containsKey(start));
100 	}
101 
102 	@Test
103 	public void testRemoveCollision1() {
104 		// This test relies upon the fact that we always >>> 1 the value
105 		// to derive an unsigned hash code. Thus, 0 and 1 fall into the
106 		// same hash bucket. Further it relies on the fact that we add
107 		// the 2nd put at the top of the chain, so removing the 1st will
108 		// cause a different code path.
109 		//
110 		assertNull(map.put(0, Long.valueOf(0)));
111 		assertNull(map.put(1, Long.valueOf(1)));
112 		assertEquals(Long.valueOf(0), map.remove(0));
113 
114 		assertFalse(map.containsKey(0));
115 		assertTrue(map.containsKey(1));
116 	}
117 
118 	@Test
119 	public void testRemoveCollision2() {
120 		// This test relies upon the fact that we always >>> 1 the value
121 		// to derive an unsigned hash code. Thus, 0 and 1 fall into the
122 		// same hash bucket. Further it relies on the fact that we add
123 		// the 2nd put at the top of the chain, so removing the 2nd will
124 		// cause a different code path.
125 		//
126 		assertNull(map.put(0, Long.valueOf(0)));
127 		assertNull(map.put(1, Long.valueOf(1)));
128 		assertEquals(Long.valueOf(1), map.remove(1));
129 
130 		assertTrue(map.containsKey(0));
131 		assertFalse(map.containsKey(1));
132 	}
133 
134 	@Test
135 	public void testSmallMap() {
136 		final long start = 12;
137 		final long n = 8;
138 		for (long i = start; i < start + n; i++)
139 			assertNull(map.put(i, Long.valueOf(i)));
140 		for (long i = start; i < start + n; i++)
141 			assertEquals(Long.valueOf(i), map.get(i));
142 	}
143 
144 	@Test
145 	public void testLargeMap() {
146 		final long start = Integer.MAX_VALUE;
147 		final long n = 100000;
148 		for (long i = start; i < start + n; i++)
149 			assertNull(map.put(i, Long.valueOf(i)));
150 		for (long i = start; i < start + n; i++)
151 			assertEquals(Long.valueOf(i), map.get(i));
152 	}
153 }