1   /*
2    *  TestRBTreeMap.java
3    *
4    *  Copyright (c) 1998-2005, The University of Sheffield.
5    *
6    *  This file is part of GATE (see http://gate.ac.uk/), and is free
7    *  software, licenced under the GNU Library General Public License,
8    *  Version 2, June 1991 (in the distribution as file licence.html,
9    *  and also available at http://gate.ac.uk/gate/licence.html).
10   *  
11   *  Valentin Tablan, 09/02/2000
12   *
13   *  $Id: TestRBTreeMap.java,v 1.7 2005/01/11 13:51:37 ian Exp $
14   */
15  
16  package gate.util;
17  
18  import junit.framework.*;
19  
20  /** Tests for the RBTreeMap class
21    */
22  public class TestRBTreeMap extends TestCase
23  {
24    /** Debug flag */
25    private static final boolean DEBUG = false;
26  
27    /** Construction */
28    public TestRBTreeMap(String name) { super(name); }
29  
30    /** Create a map  with sparse values as keys */
31    public void setUp() {
32      myTree=new RBTreeMap();
33      myTree.put(new Long(10),"Ten");
34      myTree.put(new Long(20),"Twenty");
35      myTree.put(new Long(30),"Thirty");
36      myTree.put(new Long(40),"Forty");
37      myTree.put(new Long(50),"Fifty");
38    } // setUp
39  
40    /** A test test */
41    public void testExact() {
42      Object result;
43      Long key;
44      String expected;
45  
46      //try the first entry
47      key=new Long(10);
48      expected="Ten";
49      result=myTree.get(key);
50      assertEquals(expected,result);
51  
52      //try some entry
53      key=new Long(30);
54      expected="Thirty";
55      result=myTree.get(key);
56      assertEquals(expected,result);
57  
58      //try the last entry
59      key=new Long(50);
60      expected="Fifty";
61      result=myTree.get(key);
62      assertEquals(expected,result);
63  
64      //try the last entry
65      key=new Long(15);
66      result=myTree.get(key);
67      assertNull(result);
68  
69    } // testExact
70  
71    public void testClosestMatch(){
72      Object[] result;
73      Long key;
74      Object[] expected;
75  
76      //try a match
77      key=new Long(10);
78      expected=new Object[]{"Ten","Ten"};
79      result=myTree.getClosestMatch(key);
80      assertEquals("TestCM 1",expected[0],result[0]);
81      assertEquals("TestCM 2",expected[1],result[1]);
82  
83      //try glb=null
84      key=new Long(5);
85      expected=new Object[]{null,"Ten"};
86      result=myTree.getClosestMatch(key);
87      assertNull("TestCM 3",result[0]);
88      assertEquals("TestCM 4",expected[1],result[1]);
89  
90      //the normal case
91      key=new Long(15);
92      expected=new Object[]{"Ten","Twenty"};
93      result=myTree.getClosestMatch(key);
94      assertEquals("TestCM 5",expected[0],result[0]);
95      assertEquals("TestCM 6",expected[1],result[1]);
96  
97      //try lub=null
98      key=new Long(55);
99      expected=new Object[]{"Fifty",null};
100     result=myTree.getClosestMatch(key);
101     assertEquals("TestCM 7",expected[0],result[0]);
102     assertNull("TestCM 8",result[1]);
103 
104     //empty the tree
105     myTree=new RBTreeMap();
106 
107     //try glb=lub=null
108     key=new Long(15);
109     expected=new Object[]{null,null};
110     result=myTree.getClosestMatch(key);
111     assertNull("TestCM 9",result[0]);
112     assertNull("TestCM 10",result[1]);
113   }
114 
115   public void testGetNextOf(){
116     Object result;
117     Long key;
118     String expected;
119 
120     //try the first entry
121     key=new Long(5);
122     expected="Ten";
123     result=myTree.getNextOf(key);
124     assertEquals(expected,result);
125 
126     //try some entry
127     key=new Long(20);
128     expected="Twenty";
129     result=myTree.getNextOf(key);
130     assertEquals(expected,result);
131 
132     //try the "next" case
133     key=new Long(15);
134     expected="Twenty";
135     result=myTree.getNextOf(key);
136     assertEquals(expected,result);
137 
138     //try the last case
139     key=new Long(55);
140     result=myTree.getNextOf(key);
141     assertNull(result);
142 
143     //empty the tree
144     myTree=new RBTreeMap();
145     key=new Long(15);
146     result=myTree.getNextOf(key);
147     assertNull(result);
148   }
149 
150 
151   /** Test suite routine for the test runner */
152   public static Test suite() {
153     return new TestSuite(TestRBTreeMap.class);
154   } // suite
155 
156 
157   private RBTreeMap myTree;
158   
159 } // class TestRBTreeMap
160