|
| 1 | +/* |
| 2 | + * Copyright 2018 the original author or authors. |
| 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 | + |
| 17 | +package io.appulse.utils; |
| 18 | + |
| 19 | +import static org.assertj.core.api.Assertions.assertThat; |
| 20 | + |
| 21 | +import org.junit.Test; |
| 22 | + |
| 23 | +public class BytesUtilTest { |
| 24 | + |
| 25 | + @Test |
| 26 | + public void asBytes () { |
| 27 | + assertThat(BytesUtil.asBytes('a')) |
| 28 | + .isEqualTo(new byte[] { 0, 97 }); |
| 29 | + |
| 30 | + assertThat(BytesUtil.asBytes((byte) 1)) |
| 31 | + .isEqualTo(new byte[] { 1 }); |
| 32 | + |
| 33 | + assertThat(BytesUtil.asBytes((short) 300)) |
| 34 | + .isEqualTo(new byte[] { 1, 44 }); |
| 35 | + |
| 36 | + assertThat(BytesUtil.asBytes(40_000)) |
| 37 | + .isEqualTo(new byte[] { 0, 0, -100, 64 }); |
| 38 | + |
| 39 | + assertThat(BytesUtil.asBytes(100L)) |
| 40 | + .isEqualTo(new byte[] { 0, 0, 0, 0, 0, 0, 0, 100 }); |
| 41 | + |
| 42 | + assertThat(BytesUtil.asBytes(.5F)) |
| 43 | + .isEqualTo(new byte[] { 63, 0, 0, 0 }); |
| 44 | + |
| 45 | + assertThat(BytesUtil.asBytes(.3D)) |
| 46 | + .isEqualTo(new byte[] { 63, -45, 51, 51, 51, 51, 51, 51 }); |
| 47 | + } |
| 48 | + |
| 49 | + @Test |
| 50 | + public void concatenate () { |
| 51 | + assertThat(BytesUtil.concatenate(new byte[] { 1 }, new byte[] { 2 }, new byte[] { 3 })) |
| 52 | + .isEqualTo(new byte[] { 1, 2, 3 }); |
| 53 | + } |
| 54 | + |
| 55 | + @Test |
| 56 | + public void align () { |
| 57 | + assertThat(BytesUtil.align(new byte[] { 1 }, 4)) |
| 58 | + .isEqualTo(new byte[] { 0, 0, 0, 1 }); |
| 59 | + |
| 60 | + assertThat(BytesUtil.align(new byte[] { 1, 2, 3, 4 }, 2)) |
| 61 | + .isEqualTo(new byte[] { 3, 4 }); |
| 62 | + } |
| 63 | +} |
0 commit comments