1
/* -*- tab-width: 4 -*-
3
* Electric(tm) VLSI Design System
7
* Copyright (c) 2009 Sun Microsystems and Static Free Software
9
* Electric(tm) is free software; you can redistribute it and/or modify
10
* it under the terms of the GNU Lesser General Public License as published by
11
* the Free Software Foundation; either version 3 of the License, or
12
* (at your option) any later version.
14
* Electric(tm) is distributed in the hope that it will be useful,
15
* but WITHOUT ANY WARRANTY; without even the implied warranty of
16
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17
* GNU Lesser General Public License for more details.
19
* You should have received a copy of the GNU Lesser General Public License
20
* along with Electric(tm); see the file COPYING. If not, write to
21
* the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
22
* Boston, Mass 02111-1307, USA.
24
package com.sun.electric.database.geometry.btree;
28
import com.sun.electric.database.geometry.btree.unboxed.*;
29
import com.sun.electric.database.geometry.btree.CachingPageStorage.CachedPage;
32
* A simple regression test for the BTree.
34
public class BTreeTest {
35
public static void main(String[] s) throws Exception {
37
System.err.println("");
38
System.err.println("usage: java " + BTree.class.getName() + " <maxsize> <numops> <cachesize> <seed>");
39
System.err.println("");
40
System.err.println(" Creates a BTree and runs random operations on both it and an in-memory TreeMap.");
41
System.err.println(" Reports any disagreements.");
42
System.err.println("");
43
System.err.println(" <maxsize> maximum number of entries in the tree, or 0 for no limit");
44
System.err.println(" <numops> number of operations to perform, or 0 for no limit");
45
System.err.println(" <cachesize> number of pages to cache in memory, or 0 for no cache");
46
System.err.println(" <seed> seed for random number generator, in hex");
47
System.err.println("");
50
Random rand = new Random(Integer.parseInt(s[3], 16));
51
int cachesize = Integer.parseInt(s[2]);
52
int numops = Integer.parseInt(s[1]);
53
int maxsize = Integer.parseInt(s[0]);
55
CachingPageStorage ps = new CachingPageStorageWrapper(FilePageStorage.create(), cachesize, false);
56
BTree<Integer,Integer,Pair<Integer,Integer>> btree =
57
new BTree<Integer,Integer,Pair<Integer,Integer>>(ps, UnboxedInt.instance,
61
TreeMap<Integer,Integer> tm =
62
new TreeMap<Integer,Integer>();
64
int puts=0, gets=0, deletes=0, misses=0, inserts=0;
67
// you can switch one of these off to gather crude performance measurements and compare them to TreeMap
71
for(int i=0; numops==0 || i<numops; i++) {
72
if (System.currentTimeMillis()-lastprint > 200) {
73
lastprint = System.currentTimeMillis();
74
System.out.print("\r puts="+puts+" gets="+(gets-misses)+"/"+gets+" deletes="+deletes);
76
int key = rand.nextInt() % 1000000;
77
switch(rand.nextInt() % 3) {
79
Integer tget = do_tm ? tm.get(key) : null;
80
Integer bget = do_bt ? btree.getValFromKey(key) : null;
83
if (tget==null && bget==null) { misses++; break; }
84
if (tget!=null && bget!=null && tget.equals(bget)) break;
85
System.out.print("\r puts="+puts+" gets="+(gets-misses)+"/"+gets+" deletes="+deletes);
88
throw new RuntimeException(" disagreement on key " + key + ": btree="+bget+", treemap="+tget);
93
case 1: { // get ordinal
94
int sz = do_bt ? btree.size() : tm.size();
95
int ord = sz==0 ? 0 : Math.abs(rand.nextInt()) % sz;
96
Integer tget = do_tm ? (sz==0 ? null : tm.values().toArray(new Integer[0])[ord]) : null;
97
Integer bget = do_bt ? btree.getValFromOrd(ord) : null;
100
if (tget==null && bget==null) break;
101
if (tget!=null && bget!=null && tget.equals(bget)) break;
102
System.out.print("\r puts="+puts+" gets="+(gets-misses)+"/"+gets+" deletes="+deletes);
103
System.out.println();
104
System.out.println();
105
System.out.println("dump:");
106
throw new RuntimeException(" disagreement on ordinal " + ord + ": btree="+bget+", treemap="+tget);
112
int val = rand.nextInt();
113
boolean already_there = false;
115
if (do_bt) already_there = tm.get(key)!=null;
119
if (!do_tm) already_there = btree.getValFromKey(key)!=null;
121
btree.replace(key, val);
123
btree.insert(key, val);
b'\\ No newline at end of file'