PageRenderTime 29ms CodeModel.GetById 1ms app.highlight 22ms RepoModel.GetById 1ms app.codeStats 0ms

/hazelcast/src/test/java/com/hazelcast/util/ByteUtilTest.java

https://bitbucket.org/gabral6_gmailcom/hazelcast
Java | 133 lines | 81 code | 16 blank | 36 comment | 5 complexity | 5b69ab7ca6a68da54dc848d04058b4d2 MD5 | raw file
  1/*
  2 * Copyright (c) 2008-2013, Hazelcast, Inc. All Rights Reserved.
  3 *
  4 * Licensed under the Apache License, Version 2.0 (the "License");
  5 * you may not use this file except in compliance with the License.
  6 * You may obtain a copy of the License at
  7 *
  8 * http://www.apache.org/licenses/LICENSE-2.0
  9 *
 10 * Unless required by applicable law or agreed to in writing, software
 11 * distributed under the License is distributed on an "AS IS" BASIS,
 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 13 * See the License for the specific language governing permissions and
 14 * limitations under the License.
 15 */
 16
 17package com.hazelcast.util;
 18
 19import org.junit.Before;
 20import org.junit.Ignore;
 21import org.junit.Test;
 22import org.junit.runner.RunWith;
 23
 24import static org.junit.Assert.assertEquals;
 25import static org.junit.Assert.assertTrue;
 26
 27/**
 28 * Unit tests for ByteUtil class.
 29 */
 30@RunWith(com.hazelcast.util.RandomBlockJUnit4ClassRunner.class)
 31public class ByteUtilTest {
 32
 33    private byte b;
 34
 35    @Before
 36    public void initByte() {
 37        b = 0;
 38    }
 39
 40    /**
 41     * Test method for {@link com.hazelcast.util.ByteUtil#setTrue(byte, int)}.
 42     */
 43    @Test
 44    public void testSetTrue() {
 45        for (int i = 0; i < 8; i++) {
 46            assertTrue(ByteUtil.isFalse(b, i));
 47            b = ByteUtil.setTrue(b, i);
 48            assertTrue(ByteUtil.isTrue(b, i));
 49        }
 50    }
 51
 52    /**
 53     * Test method for {@link com.hazelcast.util.ByteUtil#setTrue(byte, int)}.
 54     */
 55    @Test(expected = ArrayIndexOutOfBoundsException.class)
 56    public void testSetTrueLowerLimit() {
 57        ByteUtil.setTrue(b, -1);
 58    }
 59
 60    /**
 61     * Test method for {@link com.hazelcast.util.ByteUtil#setTrue(byte, int)}.
 62     */
 63    @Test(expected = ArrayIndexOutOfBoundsException.class)
 64    public void testSetTrueUpperLimit() {
 65        ByteUtil.setTrue(b, 8);
 66    }
 67
 68    /**
 69     * Test method for {@link com.hazelcast.util.ByteUtil#setFalse(byte, int)}.
 70     */
 71    @Test
 72    public void testSetFalse() {
 73        b = ~0;
 74        for (int i = 0; i < 8; i++) {
 75            assertTrue(ByteUtil.isTrue(b, i));
 76            b = ByteUtil.setFalse(b, i);
 77            assertTrue(ByteUtil.isFalse(b, i));
 78        }
 79    }
 80
 81    /**
 82     * Test method for {@link com.hazelcast.util.ByteUtil#isTrue(byte, int)}.
 83     */
 84    @Test
 85    public void testIsTrue() {
 86        for (int i = 0; i < 8; i++) {
 87            b = ByteUtil.setTrue(b, i);
 88            assertTrue(ByteUtil.isTrue(b, i));
 89        }
 90    }
 91
 92    /**
 93     * Test method for {@link com.hazelcast.util.ByteUtil#isFalse(byte, int)}.
 94     */
 95    @Test
 96    public void testIsFalse() {
 97        for (int i = 0; i < 8; i++) {
 98            assertTrue(ByteUtil.isFalse(b, i));
 99        }
100    }
101
102    @Test
103    public void testToByte() throws Exception {
104        assertEquals("00000101", ByteUtil.toBinaryString(ByteUtil.toByte(true, false, true, false)));
105        assertEquals("11111111", ByteUtil.toBinaryString(ByteUtil.toByte(true, true, true, true, true, true, true, true)));
106        assertEquals("11011011", ByteUtil.toBinaryString(ByteUtil.toByte(true, true, false, true, true, false, true, true)));
107        assertEquals("01111111", ByteUtil.toBinaryString(ByteUtil.toByte(true, true, true, true, true, true, true, false)));
108    }
109
110    @Ignore
111    private void checkFromByte(boolean[] b) {
112        final boolean[] fromByte = ByteUtil.fromByte(ByteUtil.toByte(b));
113        for (int i = 0; i < b.length; i++) {
114            assertEquals(b[i], fromByte[i]);
115        }
116    }
117
118    @Test
119    public void testFromByte() throws Exception {
120        checkFromByte(new boolean[]{true, false, true, false});
121        checkFromByte(new boolean[]{true, true, true, true, true, true, true, true});
122        checkFromByte(new boolean[]{true, true, false, true, true, false, true, true});
123        checkFromByte(new boolean[]{true, true, true, true, true, true, true, false});
124    }
125
126    @Test
127    public void testToBinaryString() throws Exception {
128        assertEquals("00000000", ByteUtil.toBinaryString(b));
129        assertEquals("00000001", ByteUtil.toBinaryString((byte) 1));
130        assertEquals("00000111", ByteUtil.toBinaryString((byte) 7));
131        assertEquals("10000000", ByteUtil.toBinaryString((byte) (1 << 7)));
132    }
133}