2 * Copyright (C) 2006 Robin Rosenberg <robin.rosenberg@dewire.com>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License, version 2, as published by the Free Software Foundation.
8 * This library is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 * You should have received a copy of the GNU General Public
14 * License along with this library; if not, write to the Free Software
15 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
17 package org
.spearce
.jgit
.lib
;
19 import java
.util
.Comparator
;
20 import java
.util
.HashMap
;
22 import java
.util
.TreeMap
;
24 import junit
.framework
.TestCase
;
25 import junit
.textui
.TestRunner
;
27 public class ObjectIdMapTest
extends TestCase
{
29 ObjectId
[] ids
= new ObjectId
[1000000];
31 protected void setUp() throws Exception
{
33 for (int i
=0; i
<ids
.length
; ++i
) {
34 byte[] data
= new byte[Constants
.OBJECT_ID_LENGTH
];
35 for (int j
=0; j
<Constants
.OBJECT_ID_LENGTH
; ++j
)
36 data
[j
] = (byte) (b
++^
0xEE);
37 ids
[i
] = new ObjectId(data
);
41 protected void tearDown() throws Exception
{
42 ids
= null; // avoid out of memory
45 public void testBoth() {
53 for (int j
=0; j
<64; ++j
) {
55 ((j
& 1)!=0 ?
1 : 0) |
56 ((j
& 2)!=0 ?
2 : 0) |
57 ((j
& 4)!=0 ?
16 : 0) |
58 ((j
& 8)!=0 ?
32 : 0) |
59 ((j
& 16)!=0 ?
4 : 0) |
60 ((j
& 32)!=0 ?
8 : 0);
63 long t0
= System
.currentTimeMillis();
65 Map treeMap
= new TreeMap();
66 for (int i
=0; i
<ids
.length
; ++i
)
67 treeMap
.put(ids
[i
],ids
[i
]);
69 long t1
= System
.currentTimeMillis();
73 long t0
= System
.currentTimeMillis();
74 Map hashMap
= new HashMap();
75 for (int i
=0; i
<ids
.length
; ++i
)
76 hashMap
.put(ids
[i
],ids
[i
]);
77 long t1
= System
.currentTimeMillis();
82 long t0
= System
.currentTimeMillis();
84 Map levelMapWithTree
= new ObjectIdMap(new TreeMap());
85 for (int i
=0; i
<ids
.length
; ++i
)
86 levelMapWithTree
.put(ids
[i
],ids
[i
]);
88 long t1
= System
.currentTimeMillis();
93 long t0
= System
.currentTimeMillis();
94 Map levelMapWithHash
= new ObjectIdMap(new HashMap());
95 for (int i
=0; i
<ids
.length
; ++i
)
96 levelMapWithHash
.put(ids
[i
],ids
[i
]);
98 long t1
= System
.currentTimeMillis();
104 long t0
= System
.currentTimeMillis();
106 Map levelMapWithTreeAndSpecialCompare
= new ObjectIdMap(new TreeMap(new Comparator() {
108 public int compare(Object arg0
, Object arg1
) {
109 byte[] b0
=((ObjectId
)arg0
).getBytes();
110 byte[] b1
=((ObjectId
)arg1
).getBytes();
111 for (int i
=1; i
<Constants
.OBJECT_ID_LENGTH
; ++i
) {
122 for (int i
=0; i
<ids
.length
; ++i
)
123 levelMapWithTreeAndSpecialCompare
.put(ids
[i
],ids
[i
]);
125 long t1
= System
.currentTimeMillis();
130 long t0
= System
.currentTimeMillis();
132 Map levelMapWithTreeAndSpecialCompare
= new ObjectIdMap(new TreeMap(new Comparator() {
134 public int compare(Object arg0
, Object arg1
) {
135 return ((Comparable
)arg0
).compareTo(arg1
);
139 for (int i
=0; i
<ids
.length
; ++i
)
140 levelMapWithTreeAndSpecialCompare
.put(ids
[i
],ids
[i
]);
142 long t1
= System
.currentTimeMillis();
147 System
.out
.println("TreeMap ="+d1
);
148 System
.out
.println("HashMap ="+d2
);
149 System
.out
.println("Partitioned TreeMap ObjectId.compare ="+d3
);
150 System
.out
.println("Partitioned HashMap ="+d4
);
151 System
.out
.println("Partitioned TreeMap enhanced compare ="+d5
);
152 System
.out
.println("Partitioned TreeMap dummy compare ="+d6
);
153 assertEquals(d5
*10/10000, d2
*8/10000); // d5 is ~20% better
156 public void testFunc() {
157 Map treeMap
= new TreeMap();
158 for (int i
=0; i
<ids
.length
/100; ++i
)
159 treeMap
.put(ids
[i
],ids
[i
]);
160 Map levelMapWithTree
= new ObjectIdMap(new TreeMap());
161 for (int i
=0; i
<ids
.length
/100; ++i
)
162 levelMapWithTree
.put(ids
[i
],ids
[i
]);
164 assertEquals(treeMap
, levelMapWithTree
);
167 public static void main(String
[] args
) {
168 TestRunner
.run(ObjectIdMapTest
.class);