Direct-BT v3.3.0-1-gc2d430c
Direct-BT - Direct Bluetooth Programming.
TestEUI48.java
Go to the documentation of this file.
1/**
2 * Copyright 2015 JogAmp Community. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without modification, are
5 * permitted provided that the following conditions are met:
6 *
7 * 1. Redistributions of source code must retain the above copyright notice, this list of
8 * conditions and the following disclaimer.
9 *
10 * 2. Redistributions in binary form must reproduce the above copyright notice, this list
11 * of conditions and the following disclaimer in the documentation and/or other materials
12 * provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY JogAmp Community ``AS IS'' AND ANY EXPRESS OR IMPLIED
15 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
16 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JogAmp Community OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
19 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
20 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
21 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
22 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23 *
24 * The views and conclusions contained in the software and documentation are those of the
25 * authors and should not be interpreted as representing official policies, either expressed
26 * or implied, of JogAmp Community.
27 */
28
29package jau.test.net;
30
31import java.io.IOException;
32import java.nio.ByteOrder;
33import java.util.Arrays;
34import java.util.Iterator;
35import java.util.List;
36
37import org.jau.net.EUI48;
38import org.jau.net.EUI48Sub;
39
40import jau.test.junit.util.JunitTracer;
41
42import org.junit.Assert;
43import org.junit.FixMethodOrder;
44import org.junit.Test;
45import org.junit.runners.MethodSorters;
46
47/**
48 * Test basic EUI48 functionality
49 */
50@FixMethodOrder(MethodSorters.NAME_ASCENDING)
51public class TestEUI48 extends JunitTracer {
52
53 static void test_sub01(final ByteOrder byte_order, final String mac_str, final List<String> mac_sub_strs, final List<Integer> indices) {
54 final EUI48 mac = new EUI48(mac_str);
55
56 System.out.printf("Test EUI48 mac: '%s' -> '%s'\n", mac_str, mac.toString());
57 Assert.assertEquals(mac_str, mac.toString());
58
59 int i=0;
60 for(final Iterator<String> iter=mac_sub_strs.iterator(); iter.hasNext(); ++i) {
61 final String mac_sub_str = iter.next();
62 final EUI48Sub mac_sub = new EUI48Sub(mac_sub_str);
63 System.out.printf("EUI48Sub mac02_sub: '%s' -> '%s'\n", mac_sub_str, mac_sub.toString());
64 // cut-off pre- and post-colon in test string, but leave single colon
65 String sub_str = new String(mac_sub_str);
66 if( sub_str.isEmpty() ) {
67 sub_str = ":";
68 } else if( !sub_str.equals(":") ) {
69 if( sub_str.length() > 0 && sub_str.charAt(0) == ':' ) {
70 sub_str = sub_str.substring(1, sub_str.length());
71 }
72 if( sub_str.length() > 0 && sub_str.charAt(sub_str.length()-1) == ':' ) {
73 sub_str = sub_str.substring(0, sub_str.length()-1);
74 }
75 }
76 Assert.assertEquals(sub_str, mac_sub.toString());
77
78 final int idx = mac.indexOf(mac_sub, byte_order);
79 Assert.assertEquals( idx, indices.get(i).intValue());
80 if( idx >= 0 ) {
81 Assert.assertTrue( mac.contains(mac_sub) );
82 } else {
83 Assert.assertFalse( mac.contains(mac_sub) );
84 }
85 }
86 }
87 static void test_sub02(final String mac_sub_str_exp, final String mac_sub_str, final boolean expected_result) {
88 final StringBuilder errmsg = new StringBuilder();
89 final EUI48Sub mac_sub = new EUI48Sub ();
90 final boolean res = EUI48Sub.scanEUI48Sub(mac_sub_str, mac_sub, errmsg);
91 if( res ) {
92 System.out.printf("EUI48Sub mac_sub: '%s' -> '%s'\n", mac_sub_str, mac_sub.toString());
93 if( expected_result ) {
94 Assert.assertEquals(mac_sub_str_exp, mac_sub.toString());
95 }
96 } else {
97 System.out.printf("EUI48Sub mac_sub: '%s' -> Error '%s'\n", mac_sub_str, errmsg.toString());
98 }
99 Assert.assertEquals(expected_result, res);
100 }
101
102 @Test
103 public void test01_EUI48AndSub() {
104 {
105 // index [high=5 ... low=0]
106 final String mac02_str = "C0:10:22:A0:10:00";
107 final String[] mac02_sub_strs = { "C0", "C0:10", ":10:22", "10:22", ":10:22:", "10:22:", "10", "10:00", "00", ":", "", "00:10", mac02_str};
108 final Integer[] mac02_sub_idxs_le = { 5, 4, 3, 3, 3, 3, 1, 0, 0, 0, 0, -1, 0};
109 final Integer[] mac02_sub_idxs_be = { 0, 0, 1, 1, 1, 1, 4, 4, 5, 0, 0, -1, 0};
110 test_sub01(ByteOrder.LITTLE_ENDIAN, mac02_str, Arrays.asList(mac02_sub_strs), Arrays.asList(mac02_sub_idxs_le));
111 test_sub01(ByteOrder.BIG_ENDIAN, mac02_str, Arrays.asList(mac02_sub_strs), Arrays.asList(mac02_sub_idxs_be));
112 }
113
114 {
115 // index [high=5 ... low=0]
116 final String mac03_str = "01:02:03:04:05:06";
117 final String[] mac03_sub_strs = { "01", "01:02", ":03:04", "03:04", ":04:05:", "04:05:", "04", "05:06", "06", ":", "", "06:05", mac03_str};
118 final Integer[] mac03_sub_idxs_le = { 5, 4, 2, 2, 1, 1, 2, 0, 0, 0, 0, -1, 0};
119 final Integer[] mac03_sub_idxs_be = { 0, 0, 2, 2, 3, 3, 3, 4, 5, 0, 0, -1, 0};
120 test_sub01(ByteOrder.LITTLE_ENDIAN, mac03_str, Arrays.asList(mac03_sub_strs), Arrays.asList(mac03_sub_idxs_le));
121 test_sub01(ByteOrder.BIG_ENDIAN, mac03_str, Arrays.asList(mac03_sub_strs), Arrays.asList(mac03_sub_idxs_be));
122 }
123 {
124 final String mac_sub_str = "C0:10:22:A0:10:00";
125 test_sub02(mac_sub_str, mac_sub_str, true /* expected_result */);
126 }
127 {
128 final String mac_sub_str = "0600106";
129 test_sub02(null, mac_sub_str, false /* expected_result */);
130 }
131 {
132 final EUI48 h = new EUI48("01:02:03:04:05:06");
133 final EUI48Sub n = new EUI48Sub("01:02");
134 Assert.assertEquals(0, h.indexOf(n, ByteOrder.BIG_ENDIAN));
135 Assert.assertEquals(4, h.indexOf(n, ByteOrder.LITTLE_ENDIAN));
136 }
137 {
138 final EUI48 h = new EUI48("01:02:03:04:05:06");
139 final EUI48Sub n = new EUI48Sub("05:06");
140 Assert.assertEquals(4, h.indexOf(n, ByteOrder.BIG_ENDIAN));
141 Assert.assertEquals(0, h.indexOf(n, ByteOrder.LITTLE_ENDIAN));
142 }
143 }
144
145 public static void main(final String args[]) throws IOException {
146 final String tstname = TestEUI48.class.getName();
147 org.junit.runner.JUnitCore.main(tstname);
148 }
149
150}
Test basic EUI48 functionality.
Definition: TestEUI48.java:51
static void main(final String args[])
Definition: TestEUI48.java:145