2 * Copyright (C) 2008, Robin Rosenberg <robin.rosenberg@dewire.com>
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
.lib
;
40 import java
.util
.Arrays
;
41 import java
.util
.Collection
;
43 import java
.util
.TreeMap
;
45 import junit
.framework
.TestCase
;
48 * Test functionality of ObjectIdMap
50 * The reason this class exists is performance, but those figures
51 * are hard to make stable.
53 public class ObjectIdMapTest
extends TestCase
{
55 ObjectId
[] ids
= new ObjectId
[500];
57 protected void setUp() throws Exception
{
59 for (int i
=0; i
<ids
.length
; ++i
) {
60 byte[] data
= new byte[Constants
.OBJECT_ID_LENGTH
];
61 for (int j
=0; j
<Constants
.OBJECT_ID_LENGTH
; ++j
)
62 data
[j
] = (byte) (b
++^
0xEE);
63 ids
[i
] = ObjectId
.fromRaw(data
);
67 protected void tearDown() throws Exception
{
68 ids
= null; // avoid out of memory
72 * Verify that ObjectIdMap and TreeMap functionally are equivalent.
74 @SuppressWarnings("unchecked")
75 public void testFunc() {
76 Map treeMap
= new TreeMap();
77 for (int i
=0; i
<ids
.length
/100; ++i
)
78 treeMap
.put(ids
[i
],ids
[i
]);
79 Map levelMapWithTree
= new ObjectIdMap(new TreeMap());
80 for (int i
=0; i
<ids
.length
/100; ++i
)
81 levelMapWithTree
.put(ids
[i
],ids
[i
]);
83 assertEquals(treeMap
, levelMapWithTree
);
84 assertEquals(treeMap
.values(), levelMapWithTree
.values());
85 assertEquals(treeMap
.keySet(), levelMapWithTree
.keySet());
87 treeMap
.remove(ids
[30]);
88 levelMapWithTree
.remove(ids
[30]);
89 assertFalse(treeMap
.containsKey(ids
[30]));
90 assertFalse(levelMapWithTree
.containsKey(ids
[30]));
91 assertEquals(treeMap
.values(), levelMapWithTree
.values());
92 assertEquals(treeMap
.keySet(), levelMapWithTree
.keySet());
95 void assertEquals(Collection a
, Collection b
) {
96 Object
[] aa
= a
.toArray();
97 Object
[] ba
= b
.toArray();
100 assertTrue(Arrays
.equals(aa
, ba
));