|
18 | 18 |
|
19 | 19 | import static org.assertj.core.api.Assertions.assertThat; |
20 | 20 |
|
| 21 | +import java.nio.ByteBuffer; |
| 22 | + |
21 | 23 | import org.junit.Test; |
22 | 24 |
|
| 25 | +import lombok.val; |
| 26 | + |
23 | 27 | public class BytesUtilTest { |
24 | 28 |
|
25 | 29 | @Test |
@@ -60,4 +64,134 @@ public void align () { |
60 | 64 | assertThat(BytesUtil.align(new byte[] { 1, 2, 3, 4 }, 2)) |
61 | 65 | .isEqualTo(new byte[] { 3, 4 }); |
62 | 66 | } |
| 67 | + |
| 68 | + @Test |
| 69 | + public void asShort () { |
| 70 | + val bytes1 = ByteBuffer.allocate(Short.BYTES) |
| 71 | + .putShort(Short.MAX_VALUE) |
| 72 | + .array(); |
| 73 | + |
| 74 | + assertThat(BytesUtil.asShort(bytes1)) |
| 75 | + .isEqualTo(Short.MAX_VALUE); |
| 76 | + |
| 77 | + val bytes2 = ByteBuffer.allocate(Short.BYTES) |
| 78 | + .putShort(Short.MIN_VALUE) |
| 79 | + .array(); |
| 80 | + |
| 81 | + assertThat(BytesUtil.asShort(bytes2)) |
| 82 | + .isEqualTo(Short.MIN_VALUE); |
| 83 | + |
| 84 | + val bytes3 = ByteBuffer.allocate(Short.BYTES) |
| 85 | + .putShort((short) 42) |
| 86 | + .array(); |
| 87 | + |
| 88 | + assertThat(BytesUtil.asShort(bytes3)) |
| 89 | + .isEqualTo((short) 42); |
| 90 | + } |
| 91 | + |
| 92 | + @Test |
| 93 | + public void asChar () { |
| 94 | + val bytes = ByteBuffer.allocate(Character.BYTES) |
| 95 | + .putChar('z') |
| 96 | + .array(); |
| 97 | + |
| 98 | + assertThat(BytesUtil.asChar(bytes)) |
| 99 | + .isEqualTo('z'); |
| 100 | + } |
| 101 | + |
| 102 | + @Test |
| 103 | + public void asInteger () { |
| 104 | + val bytes1 = ByteBuffer.allocate(Integer.BYTES) |
| 105 | + .putInt(Integer.MAX_VALUE) |
| 106 | + .array(); |
| 107 | + |
| 108 | + assertThat(BytesUtil.asInteger(bytes1)) |
| 109 | + .isEqualTo(Integer.MAX_VALUE); |
| 110 | + |
| 111 | + val bytes2 = ByteBuffer.allocate(Integer.BYTES) |
| 112 | + .putInt(Integer.MIN_VALUE) |
| 113 | + .array(); |
| 114 | + |
| 115 | + assertThat(BytesUtil.asInteger(bytes2)) |
| 116 | + .isEqualTo(Integer.MIN_VALUE); |
| 117 | + |
| 118 | + val bytes3 = ByteBuffer.allocate(Integer.BYTES) |
| 119 | + .putInt(42) |
| 120 | + .array(); |
| 121 | + |
| 122 | + assertThat(BytesUtil.asInteger(bytes3)) |
| 123 | + .isEqualTo(42); |
| 124 | + } |
| 125 | + |
| 126 | + @Test |
| 127 | + public void asLong () { |
| 128 | + val bytes1 = ByteBuffer.allocate(Long.BYTES) |
| 129 | + .putLong(Long.MAX_VALUE) |
| 130 | + .array(); |
| 131 | + |
| 132 | + assertThat(BytesUtil.asLong(bytes1)) |
| 133 | + .isEqualTo(Long.MAX_VALUE); |
| 134 | + |
| 135 | + val bytes2 = ByteBuffer.allocate(Long.BYTES) |
| 136 | + .putLong(Long.MIN_VALUE) |
| 137 | + .array(); |
| 138 | + |
| 139 | + assertThat(BytesUtil.asLong(bytes2)) |
| 140 | + .isEqualTo(Long.MIN_VALUE); |
| 141 | + |
| 142 | + val bytes3 = ByteBuffer.allocate(Long.BYTES) |
| 143 | + .putLong(42L) |
| 144 | + .array(); |
| 145 | + |
| 146 | + assertThat(BytesUtil.asLong(bytes3)) |
| 147 | + .isEqualTo(42L); |
| 148 | + } |
| 149 | + |
| 150 | + @Test |
| 151 | + public void asFloat () { |
| 152 | + val bytes1 = ByteBuffer.allocate(Float.BYTES) |
| 153 | + .putFloat(Float.MAX_VALUE) |
| 154 | + .array(); |
| 155 | + |
| 156 | + assertThat(BytesUtil.asFloat(bytes1)) |
| 157 | + .isEqualTo(Float.MAX_VALUE); |
| 158 | + |
| 159 | + val bytes2 = ByteBuffer.allocate(Float.BYTES) |
| 160 | + .putFloat(Float.MIN_VALUE) |
| 161 | + .array(); |
| 162 | + |
| 163 | + assertThat(BytesUtil.asFloat(bytes2)) |
| 164 | + .isEqualTo(Float.MIN_VALUE); |
| 165 | + |
| 166 | + val bytes3 = ByteBuffer.allocate(Float.BYTES) |
| 167 | + .putFloat(.5F) |
| 168 | + .array(); |
| 169 | + |
| 170 | + assertThat(BytesUtil.asFloat(bytes3)) |
| 171 | + .isEqualTo(.5F); |
| 172 | + } |
| 173 | + |
| 174 | + @Test |
| 175 | + public void asDouble () { |
| 176 | + val bytes1 = ByteBuffer.allocate(Double.BYTES) |
| 177 | + .putDouble(Double.MAX_VALUE) |
| 178 | + .array(); |
| 179 | + |
| 180 | + assertThat(BytesUtil.asDouble(bytes1)) |
| 181 | + .isEqualTo(Double.MAX_VALUE); |
| 182 | + |
| 183 | + val bytes2 = ByteBuffer.allocate(Double.BYTES) |
| 184 | + .putDouble(Double.MIN_VALUE) |
| 185 | + .array(); |
| 186 | + |
| 187 | + assertThat(BytesUtil.asDouble(bytes2)) |
| 188 | + .isEqualTo(Double.MIN_VALUE); |
| 189 | + |
| 190 | + val bytes3 = ByteBuffer.allocate(Double.BYTES) |
| 191 | + .putDouble(.5D) |
| 192 | + .array(); |
| 193 | + |
| 194 | + assertThat(BytesUtil.asDouble(bytes3)) |
| 195 | + .isEqualTo(.5D); |
| 196 | + } |
63 | 197 | } |
0 commit comments