blob: 4db84f11004f91d5dad515e326f215c5356c0a64 [file] [log] [blame]
Yotam Harchola11f38b2013-09-26 15:38:17 -07001package org.projectfloodlight.openflow.types;
2
Rob Vaterlaus0bea4e02013-11-25 12:02:55 -08003import static org.hamcrest.Matchers.contains;
Yotam Harchola11f38b2013-09-26 15:38:17 -07004import static org.junit.Assert.assertArrayEquals;
Andreas Wundsam30b359c2013-11-21 19:27:21 -08005import static org.junit.Assert.assertThat;
Yotam Harchola11f38b2013-09-26 15:38:17 -07006import junit.framework.TestCase;
7
8import org.junit.Test;
9
Andreas Wundsam9667a162013-10-21 12:58:16 -070010public class OFPortBitMapTest extends TestCase {
Andreas Wundsam30b359c2013-11-21 19:27:21 -080011 @Test
12 public void testCreateAndIterate() {
13 OFPortBitMap map = OFPortBitMap.ofPorts(OFPort.of(1), OFPort.of(2), OFPort.of(5));
14
Rob Vaterlaus0bea4e02013-11-25 12:02:55 -080015 assertThat(map.getOnPorts(), contains(OFPort.of(1), OFPort.of(2), OFPort.of(5)));
Andreas Wundsam30b359c2013-11-21 19:27:21 -080016 }
17
18 @Test
19 public void testOFBitMap() {
20 OFBitMask128 bitmap = OFBitMask128.of(0xFFFF_FFFF_FFFF_FFFFL, 0xFFFF_FFFF_FFFF_FFD9L);
21
22 OFPortBitMap map = OFPortBitMap.of(bitmap);
23
Rob Vaterlaus0bea4e02013-11-25 12:02:55 -080024 assertThat(map.getOnPorts(), contains(OFPort.of(1), OFPort.of(2), OFPort.of(5)));
Andreas Wundsam30b359c2013-11-21 19:27:21 -080025 }
Yotam Harchola11f38b2013-09-26 15:38:17 -070026
27 @Test
Andreas Wundsam9667a162013-10-21 12:58:16 -070028 public void testOFPortBitMap() {
Andreas Wundsam28c99752013-10-22 16:51:25 -070029 Boolean[] on = new Boolean[127];
30 for (int i = 0; i < 127; i++) {
Yotam Harchola11f38b2013-09-26 15:38:17 -070031 on[i] = false;
32 }
33
Andreas Wundsam9667a162013-10-21 12:58:16 -070034 OFPortBitMap.Builder builder = new OFPortBitMap.Builder();
Yotam Harchola11f38b2013-09-26 15:38:17 -070035
Andreas Wundsam28c99752013-10-22 16:51:25 -070036 for (int i = 0; i < 127; i += 3) {
Yotam Harchola11f38b2013-09-26 15:38:17 -070037 OFPort p = OFPort.of(i);
38 builder.set(p);
39 on[p.getPortNumber()] = true;
40 }
41
42 // Test that all ports that were added are actually on, and all other ports are off
Andreas Wundsam9667a162013-10-21 12:58:16 -070043 OFPortBitMap portmap = builder.build();
Yotam Harchol595c6442013-09-27 16:29:08 -070044 //System.out.println(portmap);
Andreas Wundsam28c99752013-10-22 16:51:25 -070045 Boolean[] actual = new Boolean[127];
46 for (int i = 0; i < 127; i++) {
Yotam Harchola11f38b2013-09-26 15:38:17 -070047 actual[i] = false;
48 }
Andreas Wundsam28c99752013-10-22 16:51:25 -070049 for (int i = 0; i < 127; i++) {
Yotam Harchola11f38b2013-09-26 15:38:17 -070050 actual[i] = portmap.isOn(OFPort.of(i));
51 }
52 assertArrayEquals(on, actual);
53
54 // Turn some ports off
Andreas Wundsam28c99752013-10-22 16:51:25 -070055 for (int i = 0; i < 127; i += 7) {
Yotam Harchola11f38b2013-09-26 15:38:17 -070056 on[i] = false;
57 builder.unset(OFPort.of(i));
58 }
59
60 // Test again
61 portmap = builder.build();
Andreas Wundsam28c99752013-10-22 16:51:25 -070062 actual = new Boolean[127];
63 for (int i = 0; i < 127; i++) {
Yotam Harchola11f38b2013-09-26 15:38:17 -070064 actual[i] = false;
65 }
Andreas Wundsam28c99752013-10-22 16:51:25 -070066 for (int i = 0; i < 127; i++) {
Yotam Harchola11f38b2013-09-26 15:38:17 -070067 actual[i] = portmap.isOn(OFPort.of(i));
68 }
69 assertArrayEquals(on, actual);
70 }
71}