View Javadoc
1   /*
2    *  Licensed under the Apache License, Version 2.0 (the "License");
3    *  you may not use this file except in compliance with the License.
4    *  You may obtain a copy of the License at
5    *
6    *       http://www.apache.org/licenses/LICENSE-2.0
7    *
8    *  Unless required by applicable law or agreed to in writing, software
9    *  distributed under the License is distributed on an "AS IS" BASIS,
10   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11   *  See the License for the specific language governing permissions and
12   *  limitations under the License.
13   *  under the License.
14   */
15  
16  package org.apache.commons.imaging.common;
17  
18  import static org.junit.jupiter.api.Assertions.assertEquals;
19  
20  import java.nio.ByteOrder;
21  
22  import org.apache.commons.imaging.AbstractImagingTest;
23  import org.junit.jupiter.api.Test;
24  
25  public class BinaryFileFunctionsTest extends AbstractImagingTest {
26  
27      private static final long TARGET_LONG = 0x01020304_05060708L;
28  
29      @Test
30      public void testDoubleToByteConversion() {
31          final byte[] bytesLE = ByteConversions.toBytes(1.0, ByteOrder.LITTLE_ENDIAN);
32          assertEquals(ByteConversions.toDouble(bytesLE, ByteOrder.LITTLE_ENDIAN), 1.0, 0);
33  
34          final byte[] bytesBE = ByteConversions.toBytes(1.0, ByteOrder.BIG_ENDIAN);
35          assertEquals(ByteConversions.toDouble(bytesBE, ByteOrder.BIG_ENDIAN), 1.0, 0);
36      }
37  
38      @Test
39      public void testFloatToByteConversion() {
40          final byte[] bytesLE = ByteConversions.toBytes(1.0f, ByteOrder.LITTLE_ENDIAN);
41          assertEquals(ByteConversions.toFloat(bytesLE, ByteOrder.LITTLE_ENDIAN), 1.0f, 0f);
42  
43          final byte[] bytesBE = ByteConversions.toBytes(1.0f, ByteOrder.BIG_ENDIAN);
44          assertEquals(ByteConversions.toFloat(bytesBE, ByteOrder.BIG_ENDIAN), 1.0f, 0f);
45      }
46  
47      @Test
48      public void testLongToByteConversion() {
49          final byte[] bytesLE = ByteConversions.toBytes(TARGET_LONG, ByteOrder.LITTLE_ENDIAN);
50          assertEquals(ByteConversions.toLong(bytesLE, ByteOrder.LITTLE_ENDIAN), TARGET_LONG);
51  
52          final byte[] bytesBE = ByteConversions.toBytes(TARGET_LONG, ByteOrder.BIG_ENDIAN);
53          assertEquals(ByteConversions.toLong(bytesBE, ByteOrder.BIG_ENDIAN), TARGET_LONG);
54      }
55  }