2 * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.protocol.util;
10 import io.netty.buffer.ByteBuf;
11 import io.netty.buffer.Unpooled;
12 import org.junit.Assert;
13 import org.junit.Test;
15 public class BitArrayTest {
18 public void testCreateBitArray() {
19 Assert.assertArrayEquals(new byte[1], new BitArray(5).array());
20 Assert.assertArrayEquals(new byte[3], new BitArray(23).array());
21 Assert.assertArrayEquals(new byte[3], new BitArray(24).array());
22 Assert.assertArrayEquals(new byte[4], new BitArray(25).array());
24 final byte[] a = new byte[] {1, 2, 3, 4};
25 Assert.assertArrayEquals(a, BitArray.valueOf(a).array());
28 Assert.assertEquals(b, BitArray.valueOf(b).toByte());
30 final ByteBuf buf = Unpooled.wrappedBuffer(a);
31 Assert.assertArrayEquals(new byte[] {1, 2}, BitArray.valueOf(buf, 12).array());
33 final ByteBuf res = Unpooled.buffer();
34 final BitArray i = BitArray.valueOf(a);
36 Assert.assertArrayEquals(new byte[] {1, 2, 3, 4}, ByteArray.readAllBytes(res));
40 public void testSetAndGet() {
41 final BitArray ba = new BitArray(10);
43 ba.set(1, Boolean.TRUE);
44 ba.set(2, Boolean.FALSE);
45 ba.set(3, Boolean.TRUE);
46 ba.set(7, Boolean.TRUE);
47 ba.set(8, Boolean.TRUE);
48 ba.set(9, Boolean.TRUE);
50 Assert.assertEquals("BitArray [1 1000111]", ba.toString());
52 Assert.assertFalse(ba.get(0));
53 Assert.assertTrue(ba.get(1));
54 Assert.assertFalse(ba.get(2));
55 Assert.assertTrue(ba.get(3));
56 Assert.assertTrue(ba.get(7));
57 Assert.assertTrue(ba.get(8));
58 Assert.assertTrue(ba.get(9));