Merge "BUG-2794 : created BitArray class"
[bgpcep.git] / util / src / test / java / org / opendaylight / protocol / util / BitArrayTest.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.protocol.util;
9
10 import io.netty.buffer.ByteBuf;
11 import io.netty.buffer.Unpooled;
12 import org.junit.Assert;
13 import org.junit.Test;
14
15 public class BitArrayTest {
16
17     @Test
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());
23
24         final byte[] a = new byte[] {1, 2, 3, 4};
25         Assert.assertArrayEquals(a, BitArray.valueOf(a).array());
26
27         final byte b = 44;
28         Assert.assertEquals(b, BitArray.valueOf(b).toByte());
29
30         final ByteBuf buf = Unpooled.wrappedBuffer(a);
31         Assert.assertArrayEquals(new byte[] {1, 2}, BitArray.valueOf(buf, 12).array());
32
33         final ByteBuf res = Unpooled.buffer();
34         final BitArray i = BitArray.valueOf(a);
35         i.toByteBuf(res);
36         Assert.assertArrayEquals(new byte[] {1, 2, 3, 4}, ByteArray.readAllBytes(res));
37     }
38
39     @Test
40     public void testSetAndGet() {
41         final BitArray ba = new BitArray(10);
42         ba.set(0, null);
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);
49
50         Assert.assertEquals("BitArray [1 1000111]", ba.toString());
51
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));
59     }
60 }