2 * Copyright (C) 2009, Google Inc.
6 * Redistribution and use in source and binary forms, with or
7 * without modification, are permitted provided that the following
10 * - Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
13 * - Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
18 * - Neither the name of the Git Development Community nor the
19 * names of its contributors may be used to endorse or promote
20 * products derived from this software without specific prior
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
24 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
25 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
26 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
28 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
29 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
30 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
31 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
32 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
33 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
35 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38 package org
.spearce
.jgit
.transport
;
40 import junit
.framework
.TestCase
;
42 public class LongMapTest
extends TestCase
{
43 private LongMap
<Long
> map
;
45 protected void setUp() throws Exception
{
47 map
= new LongMap
<Long
>();
50 public void testEmptyMap() {
51 assertFalse(map
.containsKey(0));
52 assertFalse(map
.containsKey(1));
54 assertNull(map
.get(0));
55 assertNull(map
.get(1));
57 assertNull(map
.remove(0));
58 assertNull(map
.remove(1));
61 public void testInsertMinValue() {
62 final Long min
= Long
.valueOf(Long
.MIN_VALUE
);
63 assertNull(map
.put(Long
.MIN_VALUE
, min
));
64 assertTrue(map
.containsKey(Long
.MIN_VALUE
));
65 assertSame(min
, map
.get(Long
.MIN_VALUE
));
66 assertFalse(map
.containsKey(Integer
.MIN_VALUE
));
69 public void testReplaceMaxValue() {
70 final Long min
= Long
.valueOf(Long
.MAX_VALUE
);
71 final Long one
= Long
.valueOf(1);
72 assertNull(map
.put(Long
.MAX_VALUE
, min
));
73 assertSame(min
, map
.get(Long
.MAX_VALUE
));
74 assertSame(min
, map
.put(Long
.MAX_VALUE
, one
));
75 assertSame(one
, map
.get(Long
.MAX_VALUE
));
78 public void testRemoveOne() {
80 assertNull(map
.put(start
, Long
.valueOf(start
)));
81 assertEquals(Long
.valueOf(start
), map
.remove(start
));
82 assertFalse(map
.containsKey(start
));
85 public void testRemoveCollision1() {
86 // This test relies upon the fact that we always >>> 1 the value
87 // to derive an unsigned hash code. Thus, 0 and 1 fall into the
88 // same hash bucket. Further it relies on the fact that we add
89 // the 2nd put at the top of the chain, so removing the 1st will
90 // cause a different code path.
92 assertNull(map
.put(0, Long
.valueOf(0)));
93 assertNull(map
.put(1, Long
.valueOf(1)));
94 assertEquals(Long
.valueOf(0), map
.remove(0));
96 assertFalse(map
.containsKey(0));
97 assertTrue(map
.containsKey(1));
100 public void testRemoveCollision2() {
101 // This test relies upon the fact that we always >>> 1 the value
102 // to derive an unsigned hash code. Thus, 0 and 1 fall into the
103 // same hash bucket. Further it relies on the fact that we add
104 // the 2nd put at the top of the chain, so removing the 2nd will
105 // cause a different code path.
107 assertNull(map
.put(0, Long
.valueOf(0)));
108 assertNull(map
.put(1, Long
.valueOf(1)));
109 assertEquals(Long
.valueOf(1), map
.remove(1));
111 assertTrue(map
.containsKey(0));
112 assertFalse(map
.containsKey(1));
115 public void testSmallMap() {
116 final long start
= 12;
118 for (long i
= start
; i
< start
+ n
; i
++)
119 assertNull(map
.put(i
, Long
.valueOf(i
)));
120 for (long i
= start
; i
< start
+ n
; i
++)
121 assertEquals(Long
.valueOf(i
), map
.get(i
));
124 public void testLargeMap() {
125 final long start
= Integer
.MAX_VALUE
;
126 final long n
= 100000;
127 for (long i
= start
; i
< start
+ n
; i
++)
128 assertNull(map
.put(i
, Long
.valueOf(i
)));
129 for (long i
= start
; i
< start
+ n
; i
++)
130 assertEquals(Long
.valueOf(i
), map
.get(i
));