1   /*--------------------------------------------------------------------------
2    *  Copyright 2009 utgenome.org
3    *
4    *  Licensed under the Apache License, Version 2.0 (the "License");
5    *  you may not use this file except in compliance with the License.
6    *  You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   *  Unless required by applicable law or agreed to in writing, software
11   *  distributed under the License is distributed on an "AS IS" BASIS,
12   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *  See the License for the specific language governing permissions and
14   *  limitations under the License.
15   *--------------------------------------------------------------------------*/
16  //--------------------------------------
17  // utgb-core Project
18  //
19  // PrioritySearchTreeTest.java
20  // Since: Dec 4, 2009
21  //
22  // $URL$ 
23  // $Author$
24  //--------------------------------------
25  package org.utgenome.gwt.utgb;
26  
27  import static org.junit.Assert.assertEquals;
28  import static org.junit.Assert.assertFalse;
29  import static org.junit.Assert.assertTrue;
30  
31  import java.util.HashSet;
32  import java.util.List;
33  
34  import org.junit.After;
35  import org.junit.Before;
36  import org.junit.Test;
37  import org.utgenome.gwt.utgb.client.canvas.PrioritySearchTree;
38  
39  public class PrioritySearchTreeTest {
40  
41  	PrioritySearchTree<String> p;
42  
43  	@Before
44  	public void setUp() throws Exception {
45  		p = new PrioritySearchTree<String>();
46  		p.insert("A", 1, 2);
47  		p.insert("B", 2, 4);
48  		p.insert("C", 1, 8);
49  		p.insert("D", 3, 3);
50  		p.insert("E", 5, 6);
51  	}
52  
53  	@After
54  	public void tearDown() throws Exception {
55  	}
56  
57  	@Test
58  	public void testRangeQuery() {
59  
60  		List<String> result = p.rangeQuery(1, 2, 6);
61  		assertEquals(2, result.size());
62  		assertTrue(result.contains("A"));
63  		assertTrue(result.contains("B"));
64  
65  		result = p.rangeQuery(2, 5, 4);
66  		assertEquals(2, result.size());
67  		assertTrue(result.contains("B"));
68  		assertTrue(result.contains("D"));
69  
70  		result = p.rangeQuery(2, 5, 10);
71  		assertEquals(3, result.size());
72  		assertTrue(result.contains("B"));
73  		assertTrue(result.contains("E"));
74  		assertTrue(result.contains("D"));
75  
76  	}
77  
78  	@Test
79  	public void testRemove() {
80  		p.remove("B", 2, 4);
81  
82  		List<String> result = p.rangeQuery(1, 2, 6);
83  		assertEquals(1, result.size());
84  		assertTrue(result.contains("A"));
85  		assertFalse(result.contains("B"));
86  
87  		result = p.rangeQuery(2, 5, 4);
88  		assertEquals(1, result.size());
89  		assertTrue(result.contains("D"));
90  
91  	}
92  
93  	@Test
94  	public void iterator() throws Exception {
95  
96  		HashSet<String> nodeSet = new HashSet<String>();
97  
98  		int nodeCount = 0;
99  		for (String each : p) {
100 			nodeCount++;
101 			nodeSet.add(each);
102 		}
103 
104 		assertEquals(5, nodeCount);
105 		assertTrue(nodeSet.contains("A"));
106 		assertTrue(nodeSet.contains("B"));
107 		assertTrue(nodeSet.contains("C"));
108 		assertTrue(nodeSet.contains("D"));
109 		assertTrue(nodeSet.contains("E"));
110 	}
111 
112 }