/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. package com.hazelcast.util;
  17. import org.junit.Before;
  18. import org.junit.Ignore;
  19. import org.junit.Test;
  20. import org.junit.runner.RunWith;
  21. import static org.junit.Assert.assertEquals;
  22. import static org.junit.Assert.assertTrue;
  23. /**
  24. * Unit tests for ByteUtil class.
  25. */
  26. @RunWith(com.hazelcast.util.RandomBlockJUnit4ClassRunner.class)
  27. public class ByteUtilTest {
  28. private byte b;
  29. @Before
  30. public void initByte() {
  31. b = 0;
  32. }
  33. /**
  34. * Test method for {@link com.hazelcast.util.ByteUtil#setTrue(byte, int)}.
  35. */
  36. @Test
  37. public void testSetTrue() {
  38. for (int i = 0; i < 8; i++) {
  39. assertTrue(ByteUtil.isFalse(b, i));
  40. b = ByteUtil.setTrue(b, i);
  41. assertTrue(ByteUtil.isTrue(b, i));
  42. }
  43. }
  44. /**
  45. * Test method for {@link com.hazelcast.util.ByteUtil#setTrue(byte, int)}.
  46. */
  47. @Test(expected = ArrayIndexOutOfBoundsException.class)
  48. public void testSetTrueLowerLimit() {
  49. ByteUtil.setTrue(b, -1);
  50. }
  51. /**
  52. * Test method for {@link com.hazelcast.util.ByteUtil#setTrue(byte, int)}.
  53. */
  54. @Test(expected = ArrayIndexOutOfBoundsException.class)
  55. public void testSetTrueUpperLimit() {
  56. ByteUtil.setTrue(b, 8);
  57. }
  58. /**
  59. * Test method for {@link com.hazelcast.util.ByteUtil#setFalse(byte, int)}.
  60. */
  61. @Test
  62. public void testSetFalse() {
  63. b = ~0;
  64. for (int i = 0; i < 8; i++) {
  65. assertTrue(ByteUtil.isTrue(b, i));
  66. b = ByteUtil.setFalse(b, i);
  67. assertTrue(ByteUtil.isFalse(b, i));
  68. }
  69. }
  70. /**
  71. * Test method for {@link com.hazelcast.util.ByteUtil#isTrue(byte, int)}.
  72. */
  73. @Test
  74. public void testIsTrue() {
  75. for (int i = 0; i < 8; i++) {
  76. b = ByteUtil.setTrue(b, i);
  77. assertTrue(ByteUtil.isTrue(b, i));
  78. }
  79. }
  80. /**
  81. * Test method for {@link com.hazelcast.util.ByteUtil#isFalse(byte, int)}.
  82. */
  83. @Test
  84. public void testIsFalse() {
  85. for (int i = 0; i < 8; i++) {
  86. assertTrue(ByteUtil.isFalse(b, i));
  87. }
  88. }
  89. @Test
  90. public void testToByte() throws Exception {
  91. assertEquals("00000101", ByteUtil.toBinaryString(ByteUtil.toByte(true, false, true, false)));
  92. assertEquals("11111111", ByteUtil.toBinaryString(ByteUtil.toByte(true, true, true, true, true, true, true, true)));
  93. assertEquals("11011011", ByteUtil.toBinaryString(ByteUtil.toByte(true, true, false, true, true, false, true, true)));
  94. assertEquals("01111111", ByteUtil.toBinaryString(ByteUtil.toByte(true, true, true, true, true, true, true, false)));
  95. }
  96. @Ignore
  97. private void checkFromByte(boolean[] b) {
  98. final boolean[] fromByte = ByteUtil.fromByte(ByteUtil.toByte(b));
  99. for (int i = 0; i < b.length; i++) {
  100. assertEquals(b[i], fromByte[i]);
  101. }
  102. }
  103. @Test
  104. public void testFromByte() throws Exception {
  105. checkFromByte(new boolean[]{true, false, true, false});
  106. checkFromByte(new boolean[]{true, true, true, true, true, true, true, true});
  107. checkFromByte(new boolean[]{true, true, false, true, true, false, true, true});
  108. checkFromByte(new boolean[]{true, true, true, true, true, true, true, false});
  109. }
  110. @Test
  111. public void testToBinaryString() throws Exception {
  112. assertEquals("00000000", ByteUtil.toBinaryString(b));
  113. assertEquals("00000001", ByteUtil.toBinaryString((byte) 1));
  114. assertEquals("00000111", ByteUtil.toBinaryString((byte) 7));
  115. assertEquals("10000000", ByteUtil.toBinaryString((byte) (1 << 7)));
  116. }
  117. }