Yotam Harchol | a11f38b | 2013-09-26 15:38:17 -0700 | [diff] [blame] | 1 | package org.projectfloodlight.openflow.types; |
| 2 | |
Rob Vaterlaus | 0bea4e0 | 2013-11-25 12:02:55 -0800 | [diff] [blame] | 3 | import static org.hamcrest.Matchers.contains; |
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 | 9667a16 | 2013-10-21 12:58:16 -0700 | [diff] [blame] | 10 | public class OFPortBitMapTest extends TestCase { |
Andreas Wundsam | 30b359c | 2013-11-21 19:27:21 -0800 | [diff] [blame] | 11 | @Test |
| 12 | public void testCreateAndIterate() { |
| 13 | OFPortBitMap map = OFPortBitMap.ofPorts(OFPort.of(1), OFPort.of(2), OFPort.of(5)); |
| 14 | |
Rob Vaterlaus | 0bea4e0 | 2013-11-25 12:02:55 -0800 | [diff] [blame] | 15 | assertThat(map.getOnPorts(), contains(OFPort.of(1), OFPort.of(2), OFPort.of(5))); |
Andreas Wundsam | 30b359c | 2013-11-21 19:27:21 -0800 | [diff] [blame] | 16 | } |
| 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 Vaterlaus | 0bea4e0 | 2013-11-25 12:02:55 -0800 | [diff] [blame] | 24 | assertThat(map.getOnPorts(), contains(OFPort.of(1), OFPort.of(2), OFPort.of(5))); |
Andreas Wundsam | 30b359c | 2013-11-21 19:27:21 -0800 | [diff] [blame] | 25 | } |
Yotam Harchol | a11f38b | 2013-09-26 15:38:17 -0700 | [diff] [blame] | 26 | |
| 27 | @Test |
Andreas Wundsam | 9667a16 | 2013-10-21 12:58:16 -0700 | [diff] [blame] | 28 | public void testOFPortBitMap() { |
Andreas Wundsam | 28c9975 | 2013-10-22 16:51:25 -0700 | [diff] [blame] | 29 | Boolean[] on = new Boolean[127]; |
| 30 | for (int i = 0; i < 127; i++) { |
Yotam Harchol | a11f38b | 2013-09-26 15:38:17 -0700 | [diff] [blame] | 31 | on[i] = false; |
| 32 | } |
| 33 | |
Andreas Wundsam | 9667a16 | 2013-10-21 12:58:16 -0700 | [diff] [blame] | 34 | OFPortBitMap.Builder builder = new OFPortBitMap.Builder(); |
Yotam Harchol | a11f38b | 2013-09-26 15:38:17 -0700 | [diff] [blame] | 35 | |
Andreas Wundsam | 28c9975 | 2013-10-22 16:51:25 -0700 | [diff] [blame] | 36 | for (int i = 0; i < 127; i += 3) { |
Yotam Harchol | a11f38b | 2013-09-26 15:38:17 -0700 | [diff] [blame] | 37 | 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 Wundsam | 9667a16 | 2013-10-21 12:58:16 -0700 | [diff] [blame] | 43 | OFPortBitMap portmap = builder.build(); |
Yotam Harchol | 595c644 | 2013-09-27 16:29:08 -0700 | [diff] [blame] | 44 | //System.out.println(portmap); |
Andreas Wundsam | 28c9975 | 2013-10-22 16:51:25 -0700 | [diff] [blame] | 45 | Boolean[] actual = new Boolean[127]; |
| 46 | for (int i = 0; i < 127; i++) { |
Yotam Harchol | a11f38b | 2013-09-26 15:38:17 -0700 | [diff] [blame] | 47 | actual[i] = false; |
| 48 | } |
Andreas Wundsam | 28c9975 | 2013-10-22 16:51:25 -0700 | [diff] [blame] | 49 | for (int i = 0; i < 127; i++) { |
Yotam Harchol | a11f38b | 2013-09-26 15:38:17 -0700 | [diff] [blame] | 50 | actual[i] = portmap.isOn(OFPort.of(i)); |
| 51 | } |
| 52 | assertArrayEquals(on, actual); |
| 53 | |
| 54 | // Turn some ports off |
Andreas Wundsam | 28c9975 | 2013-10-22 16:51:25 -0700 | [diff] [blame] | 55 | for (int i = 0; i < 127; i += 7) { |
Yotam Harchol | a11f38b | 2013-09-26 15:38:17 -0700 | [diff] [blame] | 56 | on[i] = false; |
| 57 | builder.unset(OFPort.of(i)); |
| 58 | } |
| 59 | |
| 60 | // Test again |
| 61 | portmap = builder.build(); |
Andreas Wundsam | 28c9975 | 2013-10-22 16:51:25 -0700 | [diff] [blame] | 62 | actual = new Boolean[127]; |
| 63 | for (int i = 0; i < 127; i++) { |
Yotam Harchol | a11f38b | 2013-09-26 15:38:17 -0700 | [diff] [blame] | 64 | actual[i] = false; |
| 65 | } |
Andreas Wundsam | 28c9975 | 2013-10-22 16:51:25 -0700 | [diff] [blame] | 66 | for (int i = 0; i < 127; i++) { |
Yotam Harchol | a11f38b | 2013-09-26 15:38:17 -0700 | [diff] [blame] | 67 | actual[i] = portmap.isOn(OFPort.of(i)); |
| 68 | } |
| 69 | assertArrayEquals(on, actual); |
| 70 | } |
| 71 | } |