Switch jgit library to the EDL (3-clause BSD)
[jgit.git] / org.spearce.jgit.test / tst / org / spearce / jgit / lib / ObjectIdMapTest.java
blobf1c1c0c77e94c66a8743804e517e761da8b5f834
1 /*
2 * Copyright (C) 2008, Robin Rosenberg <robin.rosenberg@dewire.com>
4 * All rights reserved.
6 * Redistribution and use in source and binary forms, with or
7 * without modification, are permitted provided that the following
8 * conditions are met:
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
21 * written permission.
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;
42 import java.util.Map;
43 import java.util.TreeMap;
45 import junit.framework.TestCase;
47 /**
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 {
58 int b=0;
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
71 /**
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();
98 Arrays.sort(aa);
99 Arrays.sort(ba);
100 assertTrue(Arrays.equals(aa, ba));