blob: fc214e51b09b0f64a3af40e243cd3744b710794b [file] [log] [blame]
Yotam Harchola11f38b2013-09-26 15:38:17 -07001package org.projectfloodlight.openflow.types;
2
Andreas Wundsam30b359c2013-11-21 19:27:21 -08003import static org.hamcrest.CoreMatchers.equalTo;
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 Wundsam30b359c2013-11-21 19:27:21 -080010import com.google.common.collect.ImmutableList;
11
Andreas Wundsam9667a162013-10-21 12:58:16 -070012public class OFPortBitMapTest extends TestCase {
Andreas Wundsam30b359c2013-11-21 19:27:21 -080013 @Test
14 public void testCreateAndIterate() {
15 OFPortBitMap map = OFPortBitMap.ofPorts(OFPort.of(1), OFPort.of(2), OFPort.of(5));
16
17 assertThat(
18 ImmutableList.copyOf(map.getOnPorts()),
19 equalTo(
20 ImmutableList.of(OFPort.of(1), OFPort.of(2), OFPort.of(5))
21 ));
22 }
23
24 @Test
25 public void testOFBitMap() {
26 OFBitMask128 bitmap = OFBitMask128.of(0xFFFF_FFFF_FFFF_FFFFL, 0xFFFF_FFFF_FFFF_FFD9L);
27
28 OFPortBitMap map = OFPortBitMap.of(bitmap);
29
30 assertThat(
31 ImmutableList.copyOf(map.getOnPorts()),
32 equalTo(
33 ImmutableList.of(OFPort.of(1), OFPort.of(2), OFPort.of(5))
34 ));
35 }
Yotam Harchola11f38b2013-09-26 15:38:17 -070036
37 @Test
Andreas Wundsam9667a162013-10-21 12:58:16 -070038 public void testOFPortBitMap() {
Andreas Wundsam28c99752013-10-22 16:51:25 -070039 Boolean[] on = new Boolean[127];
40 for (int i = 0; i < 127; i++) {
Yotam Harchola11f38b2013-09-26 15:38:17 -070041 on[i] = false;
42 }
43
Andreas Wundsam9667a162013-10-21 12:58:16 -070044 OFPortBitMap.Builder builder = new OFPortBitMap.Builder();
Yotam Harchola11f38b2013-09-26 15:38:17 -070045
Andreas Wundsam28c99752013-10-22 16:51:25 -070046 for (int i = 0; i < 127; i += 3) {
Yotam Harchola11f38b2013-09-26 15:38:17 -070047 OFPort p = OFPort.of(i);
48 builder.set(p);
49 on[p.getPortNumber()] = true;
50 }
51
52 // Test that all ports that were added are actually on, and all other ports are off
Andreas Wundsam9667a162013-10-21 12:58:16 -070053 OFPortBitMap portmap = builder.build();
Yotam Harchol595c6442013-09-27 16:29:08 -070054 //System.out.println(portmap);
Andreas Wundsam28c99752013-10-22 16:51:25 -070055 Boolean[] actual = new Boolean[127];
56 for (int i = 0; i < 127; i++) {
Yotam Harchola11f38b2013-09-26 15:38:17 -070057 actual[i] = false;
58 }
Andreas Wundsam28c99752013-10-22 16:51:25 -070059 for (int i = 0; i < 127; i++) {
Yotam Harchola11f38b2013-09-26 15:38:17 -070060 actual[i] = portmap.isOn(OFPort.of(i));
61 }
62 assertArrayEquals(on, actual);
63
64 // Turn some ports off
Andreas Wundsam28c99752013-10-22 16:51:25 -070065 for (int i = 0; i < 127; i += 7) {
Yotam Harchola11f38b2013-09-26 15:38:17 -070066 on[i] = false;
67 builder.unset(OFPort.of(i));
68 }
69
70 // Test again
71 portmap = builder.build();
Andreas Wundsam28c99752013-10-22 16:51:25 -070072 actual = new Boolean[127];
73 for (int i = 0; i < 127; i++) {
Yotam Harchola11f38b2013-09-26 15:38:17 -070074 actual[i] = false;
75 }
Andreas Wundsam28c99752013-10-22 16:51:25 -070076 for (int i = 0; i < 127; i++) {
Yotam Harchola11f38b2013-09-26 15:38:17 -070077 actual[i] = portmap.isOn(OFPort.of(i));
78 }
79 assertArrayEquals(on, actual);
80 }
81}