Yotam Harchol | a11f38b | 2013-09-26 15:38:17 -0700 | [diff] [blame] | 1 | package org.projectfloodlight.openflow.types; |
| 2 | |
Andreas Wundsam | 30b359c | 2013-11-21 19:27:21 -0800 | [diff] [blame^] | 3 | import static org.hamcrest.CoreMatchers.equalTo; |
Yotam Harchol | a11f38b | 2013-09-26 15:38:17 -0700 | [diff] [blame] | 4 | import static org.junit.Assert.assertArrayEquals; |
Andreas Wundsam | 30b359c | 2013-11-21 19:27:21 -0800 | [diff] [blame^] | 5 | import static org.junit.Assert.assertThat; |
Yotam Harchol | a11f38b | 2013-09-26 15:38:17 -0700 | [diff] [blame] | 6 | import junit.framework.TestCase; |
| 7 | |
| 8 | import org.junit.Test; |
| 9 | |
Andreas Wundsam | 30b359c | 2013-11-21 19:27:21 -0800 | [diff] [blame^] | 10 | import com.google.common.collect.ImmutableList; |
| 11 | |
Andreas Wundsam | 9667a16 | 2013-10-21 12:58:16 -0700 | [diff] [blame] | 12 | public class OFPortBitMapTest extends TestCase { |
Andreas Wundsam | 30b359c | 2013-11-21 19:27:21 -0800 | [diff] [blame^] | 13 | @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 Harchol | a11f38b | 2013-09-26 15:38:17 -0700 | [diff] [blame] | 36 | |
| 37 | @Test |
Andreas Wundsam | 9667a16 | 2013-10-21 12:58:16 -0700 | [diff] [blame] | 38 | public void testOFPortBitMap() { |
Andreas Wundsam | 28c9975 | 2013-10-22 16:51:25 -0700 | [diff] [blame] | 39 | Boolean[] on = new Boolean[127]; |
| 40 | for (int i = 0; i < 127; i++) { |
Yotam Harchol | a11f38b | 2013-09-26 15:38:17 -0700 | [diff] [blame] | 41 | on[i] = false; |
| 42 | } |
| 43 | |
Andreas Wundsam | 9667a16 | 2013-10-21 12:58:16 -0700 | [diff] [blame] | 44 | OFPortBitMap.Builder builder = new OFPortBitMap.Builder(); |
Yotam Harchol | a11f38b | 2013-09-26 15:38:17 -0700 | [diff] [blame] | 45 | |
Andreas Wundsam | 28c9975 | 2013-10-22 16:51:25 -0700 | [diff] [blame] | 46 | for (int i = 0; i < 127; i += 3) { |
Yotam Harchol | a11f38b | 2013-09-26 15:38:17 -0700 | [diff] [blame] | 47 | 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 Wundsam | 9667a16 | 2013-10-21 12:58:16 -0700 | [diff] [blame] | 53 | OFPortBitMap portmap = builder.build(); |
Yotam Harchol | 595c644 | 2013-09-27 16:29:08 -0700 | [diff] [blame] | 54 | //System.out.println(portmap); |
Andreas Wundsam | 28c9975 | 2013-10-22 16:51:25 -0700 | [diff] [blame] | 55 | Boolean[] actual = new Boolean[127]; |
| 56 | for (int i = 0; i < 127; i++) { |
Yotam Harchol | a11f38b | 2013-09-26 15:38:17 -0700 | [diff] [blame] | 57 | actual[i] = false; |
| 58 | } |
Andreas Wundsam | 28c9975 | 2013-10-22 16:51:25 -0700 | [diff] [blame] | 59 | for (int i = 0; i < 127; i++) { |
Yotam Harchol | a11f38b | 2013-09-26 15:38:17 -0700 | [diff] [blame] | 60 | actual[i] = portmap.isOn(OFPort.of(i)); |
| 61 | } |
| 62 | assertArrayEquals(on, actual); |
| 63 | |
| 64 | // Turn some ports off |
Andreas Wundsam | 28c9975 | 2013-10-22 16:51:25 -0700 | [diff] [blame] | 65 | for (int i = 0; i < 127; i += 7) { |
Yotam Harchol | a11f38b | 2013-09-26 15:38:17 -0700 | [diff] [blame] | 66 | on[i] = false; |
| 67 | builder.unset(OFPort.of(i)); |
| 68 | } |
| 69 | |
| 70 | // Test again |
| 71 | portmap = builder.build(); |
Andreas Wundsam | 28c9975 | 2013-10-22 16:51:25 -0700 | [diff] [blame] | 72 | actual = new Boolean[127]; |
| 73 | for (int i = 0; i < 127; i++) { |
Yotam Harchol | a11f38b | 2013-09-26 15:38:17 -0700 | [diff] [blame] | 74 | actual[i] = false; |
| 75 | } |
Andreas Wundsam | 28c9975 | 2013-10-22 16:51:25 -0700 | [diff] [blame] | 76 | for (int i = 0; i < 127; i++) { |
Yotam Harchol | a11f38b | 2013-09-26 15:38:17 -0700 | [diff] [blame] | 77 | actual[i] = portmap.isOn(OFPort.of(i)); |
| 78 | } |
| 79 | assertArrayEquals(on, actual); |
| 80 | } |
| 81 | } |