You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

TestLittleEndian.java 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  1. /* ====================================================================
  2. Licensed to the Apache Software Foundation (ASF) under one or more
  3. contributor license agreements. See the NOTICE file distributed with
  4. this work for additional information regarding copyright ownership.
  5. The ASF licenses this file to You under the Apache License, Version 2.0
  6. (the "License"); you may not use this file except in compliance with
  7. the License. You may obtain a copy of the License at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. ==================================================================== */
  15. package org.apache.poi.util;
  16. import static org.junit.jupiter.api.Assertions.assertEquals;
  17. import static org.junit.jupiter.api.Assertions.assertThrows;
  18. import static org.junit.jupiter.api.Assertions.assertTrue;
  19. import java.io.ByteArrayInputStream;
  20. import java.io.IOException;
  21. import java.io.InputStream;
  22. import org.apache.poi.util.LittleEndian.BufferUnderrunException;
  23. import org.junit.jupiter.api.Test;
  24. /**
  25. * Class to test LittleEndian functionality
  26. */
  27. final class TestLittleEndian {
  28. /**
  29. * test the getShort() method
  30. */
  31. @Test
  32. void testGetShort() {
  33. byte[] testdata = new byte[ LittleEndianConsts.SHORT_SIZE + 1 ];
  34. testdata[0] = 0x01;
  35. testdata[1] = (byte) 0xFF;
  36. testdata[2] = 0x02;
  37. short[] expected = new short[2];
  38. expected[0] = ( short ) 0xFF01;
  39. expected[1] = 0x02FF;
  40. assertEquals(expected[0], LittleEndian.getShort(testdata));
  41. assertEquals(expected[1], LittleEndian.getShort(testdata, 1));
  42. }
  43. @Test
  44. void testGetUShort() {
  45. byte[] testdata = {
  46. (byte) 0x01,
  47. (byte) 0xFF,
  48. (byte) 0x02,
  49. };
  50. byte[] testdata2 = {
  51. (byte) 0x0D,
  52. (byte) 0x93,
  53. (byte) 0xFF,
  54. };
  55. int expected0 = 0xFF01;
  56. int expected1 = 0x02FF;
  57. int expected2 = 0x930D;
  58. int expected3 = 0xFF93;
  59. assertEquals(expected0, LittleEndian.getUShort(testdata));
  60. assertEquals(expected1, LittleEndian.getUShort(testdata, 1));
  61. assertEquals(expected2, LittleEndian.getUShort(testdata2));
  62. assertEquals(expected3, LittleEndian.getUShort(testdata2, 1));
  63. byte[] testdata3 = new byte[ LittleEndianConsts.SHORT_SIZE + 1 ];
  64. LittleEndian.putUShort(testdata3, 0, expected2);
  65. LittleEndian.putUShort(testdata3, 1, expected3);
  66. assertEquals(testdata3[0], 0x0D);
  67. assertEquals(testdata3[1], (byte)0x93);
  68. assertEquals(testdata3[2], (byte)0xFF);
  69. assertEquals(expected2, LittleEndian.getUShort(testdata3));
  70. assertEquals(expected3, LittleEndian.getUShort(testdata3, 1));
  71. }
  72. private static final byte[] _double_array =
  73. {
  74. 56, 50, -113, -4, -63, -64, -13, 63, 76, -32, -42, -35, 60, -43, 3, 64
  75. };
  76. /** 0x7ff8000000000000 encoded in little endian order */
  77. private static final byte[] _nan_double_array = HexRead.readFromString("00 00 00 00 00 00 F8 7F");
  78. private static final double[] _doubles =
  79. {
  80. 1.23456, 2.47912, Double.NaN
  81. };
  82. /**
  83. * test the getDouble() method
  84. */
  85. @Test
  86. void testGetDouble() {
  87. assertEquals(_doubles[0], LittleEndian.getDouble(_double_array, 0), 0.000001 );
  88. assertEquals(_doubles[1], LittleEndian.getDouble( _double_array, LittleEndianConsts.DOUBLE_SIZE), 0.000001);
  89. assertTrue(Double.isNaN(LittleEndian.getDouble(_nan_double_array, 0)));
  90. double nan = LittleEndian.getDouble(_nan_double_array, 0);
  91. byte[] data = new byte[8];
  92. LittleEndian.putDouble(data, 0, nan);
  93. for ( int i = 0; i < data.length; i++ ) {
  94. assertEquals(data[i], _nan_double_array[i]);
  95. }
  96. }
  97. /**
  98. * test the getInt() method
  99. */
  100. @Test
  101. void testGetInt() {
  102. // reading 4 byte data from a 5 byte buffer
  103. byte[] testdata = {
  104. (byte) 0x01,
  105. (byte) 0xFF,
  106. (byte) 0xFF,
  107. (byte) 0xFF,
  108. (byte) 0x02,
  109. };
  110. assertEquals(0xFFFFFF01, LittleEndian.getInt(testdata));
  111. assertEquals(0x02FFFFFF, LittleEndian.getInt(testdata, 1));
  112. }
  113. /**
  114. * test the getLong method
  115. */
  116. @Test
  117. void testGetLong() {
  118. // reading 8 byte values from a 9 byte buffer
  119. byte[] testdata = {
  120. (byte) 0x01,
  121. (byte) 0xFF,
  122. (byte) 0xFF,
  123. (byte) 0xFF,
  124. (byte) 0xFF,
  125. (byte) 0xFF,
  126. (byte) 0xFF,
  127. (byte) 0xFF,
  128. (byte) 0x02,
  129. };
  130. assertEquals(0xFFFFFFFFFFFFFF01L, LittleEndian.getLong(testdata, 0));
  131. assertEquals(0x02FFFFFFFFFFFFFFL, LittleEndian.getLong(testdata, 1));
  132. }
  133. /**
  134. * test the PutShort method
  135. */
  136. @Test
  137. void testPutShort() {
  138. byte[] expected = new byte[ LittleEndianConsts.SHORT_SIZE + 1 ];
  139. expected[0] = 0x01;
  140. expected[1] = (byte) 0xFF;
  141. expected[2] = 0x02;
  142. byte[] received = new byte[ LittleEndianConsts.SHORT_SIZE + 1 ];
  143. short[] testdata = new short[2];
  144. testdata[0] = ( short ) 0xFF01;
  145. testdata[1] = 0x02FF;
  146. LittleEndian.putShort(received, 0, testdata[0]);
  147. assertTrue(compareByteArrays(received, expected, 0, LittleEndianConsts.SHORT_SIZE));
  148. LittleEndian.putShort(received, 1, testdata[1]);
  149. assertTrue(compareByteArrays(received, expected, 1, LittleEndianConsts.SHORT_SIZE));
  150. }
  151. /**
  152. * test the putInt method
  153. */
  154. @Test
  155. void testPutInt() {
  156. // writing 4 byte data to a 5 byte buffer
  157. byte[] expected = {
  158. (byte) 0x01,
  159. (byte) 0xFF,
  160. (byte) 0xFF,
  161. (byte) 0xFF,
  162. (byte) 0x02,
  163. };
  164. byte[] received = new byte[ LittleEndianConsts.INT_SIZE + 1 ];
  165. LittleEndian.putInt(received, 0, 0xFFFFFF01);
  166. assertTrue(compareByteArrays(received, expected, 0, LittleEndianConsts.INT_SIZE));
  167. LittleEndian.putInt(received, 1, 0x02FFFFFF);
  168. assertTrue(compareByteArrays(received, expected, 1, LittleEndianConsts.INT_SIZE));
  169. }
  170. /**
  171. * test the putDouble methods
  172. */
  173. @Test
  174. void testPutDouble() {
  175. byte[] received = new byte[ LittleEndianConsts.DOUBLE_SIZE + 1 ];
  176. LittleEndian.putDouble(received, 0, _doubles[0]);
  177. assertTrue(compareByteArrays(received, _double_array, 0, LittleEndianConsts.DOUBLE_SIZE));
  178. LittleEndian.putDouble(received, 1, _doubles[1]);
  179. byte[] expected = new byte[ LittleEndianConsts.DOUBLE_SIZE + 1 ];
  180. System.arraycopy(_double_array, LittleEndianConsts.DOUBLE_SIZE, expected,
  181. 1, LittleEndianConsts.DOUBLE_SIZE);
  182. assertTrue(compareByteArrays(received, expected, 1, LittleEndianConsts.DOUBLE_SIZE));
  183. }
  184. /**
  185. * test the putLong method
  186. */
  187. @Test
  188. void testPutLong() {
  189. // writing 8 byte values to a 9 byte buffer
  190. byte[] expected = {
  191. (byte) 0x01,
  192. (byte) 0xFF,
  193. (byte) 0xFF,
  194. (byte) 0xFF,
  195. (byte) 0xFF,
  196. (byte) 0xFF,
  197. (byte) 0xFF,
  198. (byte) 0xFF,
  199. (byte) 0x02,
  200. };
  201. byte[] received = new byte[ LittleEndianConsts.LONG_SIZE + 1 ];
  202. long testdata0 = 0xFFFFFFFFFFFFFF01L;
  203. long testdata1 = 0x02FFFFFFFFFFFFFFL;
  204. LittleEndian.putLong(received, 0, testdata0);
  205. assertTrue(compareByteArrays(received, expected, 0, LittleEndianConsts.LONG_SIZE));
  206. LittleEndian.putLong(received, 1, testdata1);
  207. assertTrue(compareByteArrays(received, expected, 1, LittleEndianConsts.LONG_SIZE));
  208. }
  209. private static final byte[] _good_array = {
  210. 0x01, 0x02, 0x01, 0x02,
  211. 0x01, 0x02, 0x01, 0x02,
  212. 0x01, 0x02, 0x01, 0x02,
  213. 0x01, 0x02, 0x01, 0x02,
  214. };
  215. private static final byte[] _bad_array = {
  216. 0x01
  217. };
  218. /**
  219. * test the readShort method
  220. */
  221. @Test
  222. void testReadShort() throws IOException {
  223. short expected_value = 0x0201;
  224. InputStream stream = new ByteArrayInputStream(_good_array);
  225. int count = 0;
  226. while (true) {
  227. try {
  228. short value = LittleEndian.readShort(stream);
  229. assertEquals(value, expected_value);
  230. count++;
  231. } catch (BufferUnderrunException e) {
  232. break;
  233. }
  234. }
  235. assertEquals(count,
  236. _good_array.length / LittleEndianConsts.SHORT_SIZE);
  237. ByteArrayInputStream stream2 = new ByteArrayInputStream(_bad_array);
  238. assertThrows(BufferUnderrunException.class, () -> LittleEndian.readShort(stream2));
  239. }
  240. /**
  241. * test the readInt method
  242. */
  243. @Test
  244. void testReadInt() throws IOException {
  245. int expected_value = 0x02010201;
  246. InputStream stream = new ByteArrayInputStream(_good_array);
  247. int count = 0;
  248. while (true) {
  249. try {
  250. int value = LittleEndian.readInt(stream);
  251. assertEquals(value, expected_value);
  252. count++;
  253. } catch (BufferUnderrunException e) {
  254. break;
  255. }
  256. }
  257. assertEquals(count, _good_array.length / LittleEndianConsts.INT_SIZE);
  258. ByteArrayInputStream stream2 = new ByteArrayInputStream(_bad_array);
  259. assertThrows(BufferUnderrunException.class, () -> LittleEndian.readInt(stream2));
  260. }
  261. /**
  262. * test the readLong method
  263. */
  264. @Test
  265. void testReadLong() throws IOException {
  266. long expected_value = 0x0201020102010201L;
  267. InputStream stream = new ByteArrayInputStream(_good_array);
  268. int count = 0;
  269. while (true) {
  270. try {
  271. long value = LittleEndian.readLong(stream);
  272. assertEquals(value, expected_value);
  273. count++;
  274. } catch (BufferUnderrunException e) {
  275. break;
  276. }
  277. }
  278. assertEquals(count, _good_array.length / LittleEndianConsts.LONG_SIZE);
  279. ByteArrayInputStream stream2 = new ByteArrayInputStream(_bad_array);
  280. assertThrows(BufferUnderrunException.class, () -> LittleEndian.readLong(stream2));
  281. }
  282. @Test
  283. void testReadFromStream() throws IOException {
  284. int actual;
  285. actual = LittleEndian.readUShort(new ByteArrayInputStream(new byte[] { 5, -128, }));
  286. assertEquals(32773, actual);
  287. actual = LittleEndian.readUShort(new ByteArrayInputStream(new byte[] { 1, 2, 3, 4, }));
  288. assertEquals(513, actual);
  289. assertThrows(BufferUnderrunException.class, () ->
  290. LittleEndian.readInt(new ByteArrayInputStream(new byte[] { 1, 2, 3, })));
  291. }
  292. @Test
  293. void testUnsignedByteToInt() {
  294. assertEquals(255, LittleEndian.ubyteToInt((byte)255));
  295. }
  296. private static boolean compareByteArrays(byte [] received, byte [] expected,
  297. int offset, int size) {
  298. for (int j = offset; j < offset + size; j++) {
  299. if (received[j] != expected[j]) {
  300. System.out.println("difference at index " + j);
  301. return false;
  302. }
  303. }
  304. return true;
  305. }
  306. @Test
  307. void testUnsignedShort() {
  308. assertEquals(0xffff, LittleEndian.getUShort(new byte[] { (byte)0xff, (byte)0xff }, 0));
  309. }
  310. }