blob: d462d3e7ee125bdd50f3ed9939fa381565559230 [file] [log] [blame]
Jonathan Hart8f5f4682013-08-07 22:13:39 +12001package net.onrc.onos.ofcontroller.bgproute;
2
3import static org.junit.Assert.assertFalse;
4import static org.junit.Assert.assertNotNull;
5import static org.junit.Assert.assertNull;
6import static org.junit.Assert.assertTrue;
7import static org.junit.Assert.fail;
8
9import java.util.HashMap;
10import java.util.Iterator;
11import java.util.Map;
12
13import org.junit.After;
14import org.junit.Before;
15import org.junit.Ignore;
16import org.junit.Test;
17
18public class PatriciaTrieTest {
19
20 IPatriciaTrie ptrie;
21 Prefix[] prefixes;
22 Map<Prefix, Rib> mappings;
23
24 @Before
25 public void setUp() throws Exception {
26 ptrie = new PatriciaTrie(32);
27 mappings = new HashMap<Prefix, Rib>();
28
29 prefixes = new Prefix[] {
30 new Prefix("192.168.10.0", 24),
31 new Prefix("192.168.8.0", 23),
32 new Prefix("192.168.8.0", 22),
33 new Prefix("192.0.0.0", 7),
34 new Prefix("192.168.11.0", 24),
35 new Prefix("10.0.23.128", 25),
36 new Prefix("206.17.144.0", 20),
37 new Prefix("9.17.0.0", 12),
38 new Prefix("192.168.0.0", 16)
39 };
40
41 for (int i = 0; i < prefixes.length; i++) {
42 mappings.put(prefixes[i], new Rib("192.168.10.101", "192.168.20." + i, 32));
43 ptrie.put(prefixes[i], new Rib("192.168.10.101", "192.168.20." + i, 32));
44 }
45 }
46
47 @After
48 public void tearDown() throws Exception {
49 }
50
51 @Test
52 public void testPut() {
53 IPatriciaTrie ptrie = new PatriciaTrie(32);
54
55 Prefix p1 = new Prefix("192.168.240.0", 20);
56 Rib r1 = new Rib("192.168.10.101", "192.168.60.2", 20);
57 Rib retval = ptrie.put(p1, r1);
58 assertNull(retval);
59 retval = ptrie.lookup(p1);
60 assertTrue(r1 == retval); //should be the same object
61
62 Prefix p2 = new Prefix("192.160.0.0", 12);
63 Rib r2 = new Rib("192.168.10.101", "192.168.20.1", 12);
64 retval = ptrie.put(p2, r2);
65 assertNull(retval);
66
67 Prefix p3 = new Prefix("192.168.208.0", 20);
68 Rib r3 = new Rib("192.168.10.101", "192.168.30.1", 20);
69 retval = ptrie.put(p3, r3);
70 assertNull(retval);
71
72 //Insert a new Rib entry over a previous one
73 Rib r3new = new Rib("192.168.10.101", "192.168.60.2", 20);
74 retval = ptrie.put(p3, r3new);
75 assertNotNull(retval);
76 assertTrue(retval.equals(r3));
77 assertTrue(retval == r3); //should be the same object
78
79 //Now we have an aggregate node with prefix 192.168.192.0/18.
80 //We will insert a Rib at this prefix
81 Prefix p4 = new Prefix("192.168.192.0", 18);
82 Rib r4 = new Rib("192.168.10.101", "192.168.40.1", 18);
83 retval = ptrie.put(p4, r4);
84 assertNull(retval);
85 retval = ptrie.lookup(p4);
86 assertTrue(retval == r4); //should be the same object
87 }
88
89 @Test
90 public void testLookup() {
91 for (Map.Entry<Prefix, Rib> entry : mappings.entrySet()) {
92 Rib r = ptrie.lookup(entry.getKey());
93 assertTrue(entry.getValue().equals(r));
94 }
95
96 //These are aggregate nodes in the tree. Shouldn't be returned by lookup
97 Prefix p1 = new Prefix("0.0.0.0", 0);
98 Rib retval = ptrie.lookup(p1);
99 assertNull(retval);
100
101 //We'll put a Rib at an aggregate node and check if lookup returns correctly
102 Prefix p2 = new Prefix("192.0.0.0", 4);
103 Rib r2 = new Rib("192.168.10.101", "192.168.60.1", 4);
104 retval = ptrie.put(p2, r2);
105 assertNull(retval);
106 retval = ptrie.lookup(p2);
107 assertTrue(retval.equals(r2));
108 }
109
110 @Ignore
111 @Test
112 public void testMatch() {
113 fail("Not yet implemented");
114 }
115
116 @Test
117 public void testRemove() {
118 Prefix p1 = new Prefix("192.168.8.0", 23);
119 Rib retval = ptrie.lookup(p1);
120 assertNotNull(retval);
121 boolean success = ptrie.remove(p1, retval);
122 assertTrue(success);
123
124 Prefix p2 = new Prefix("192.168.8.0", 22);
125 Prefix p3 = new Prefix("192.168.10.0", 24);
126
127 //Test it does the right thing with null arguments
128 success = ptrie.remove(null, null);
129 assertFalse(success);
130 success = ptrie.remove(p2, null);
131 assertFalse(success);
132
133 //Check other prefixes are still there
134 retval = ptrie.lookup(p2);
135 assertNotNull(retval);
136 retval = ptrie.lookup(p3);
137 assertNotNull(retval);
138
139 Prefix p4 = new Prefix("9.17.0.0", 12);
140 retval = ptrie.lookup(p4);
141 assertNotNull(retval);
142 success = ptrie.remove(p4, retval);
143 assertTrue(success);
144 success = ptrie.remove(p4, retval);
145 assertFalse(success);
146
147 //Check other prefixes are still there
148 retval = ptrie.lookup(p2);
149 assertNotNull(retval);
150 retval = ptrie.lookup(p3);
151 assertNotNull(retval);
152
153 Prefix p5 = new Prefix("192.0.0.0", 7);
154 retval = ptrie.lookup(p5);
155 assertNotNull(retval);
156 success = ptrie.remove(p5, retval);
157 assertTrue(success);
158
159 //Check other prefixes are still there
160 retval = ptrie.lookup(p2);
161 assertNotNull(retval);
162 retval = ptrie.lookup(p3);
163 assertNotNull(retval);
164
165
166 }
167
168 @Test(expected=java.util.NoSuchElementException.class)
169 public void testIterator() {
170 int[] order = new int[] {7, 5, 3, 8, 2, 1, 0, 4, 6};
171
172 Iterator<IPatriciaTrie.Entry> it = ptrie.iterator();
173 int i = 0;
174 assertTrue(it.hasNext());
175 while (it.hasNext()) {
176 IPatriciaTrie.Entry entry = it.next();
177 assertTrue(entry.getPrefix().equals(prefixes[order[i]]));
178 i++;
179 }
180 assertFalse(it.hasNext());
181 assertTrue(i == order.length);
182
183 IPatriciaTrie pt = new PatriciaTrie(32);
184 Iterator<IPatriciaTrie.Entry> it2 = pt.iterator();
185 assertFalse(it2.hasNext());
186 it.next(); //throws NoSuchElementException
187 }
188
189}