1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.io.input;
18
19
20 import java.io.ByteArrayInputStream;
21 import java.io.IOException;
22
23 import junit.framework.TestCase;
24
25
26
27
28
29
30
31
32
33 public class SwappedDataInputStreamTest extends TestCase {
34
35 private SwappedDataInputStream sdis;
36 private byte[] bytes;
37
38 public SwappedDataInputStreamTest(String name) {
39 super(name);
40 }
41
42 public void setUp() {
43 bytes = new byte[] {
44 0x01,
45 0x02,
46 0x03,
47 0x04,
48 0x05,
49 0x06,
50 0x07,
51 0x08
52 };
53 ByteArrayInputStream bais = new ByteArrayInputStream( bytes );
54 this.sdis = new SwappedDataInputStream( bais );
55 }
56
57 public void tearDown() {
58 this.sdis = null;
59 }
60
61 public void testReadBoolean() throws IOException {
62 assertEquals( false, this.sdis.readBoolean() );
63 }
64
65 public void testReadByte() throws IOException {
66 assertEquals( 0x01, this.sdis.readByte() );
67 }
68
69 public void testReadChar() throws IOException {
70 assertEquals( (char) 0x0201, this.sdis.readChar() );
71 }
72
73 public void testReadDouble() throws IOException {
74 assertEquals( Double.longBitsToDouble(0x0807060504030201L), this.sdis.readDouble(), 0 );
75 }
76
77 public void testReadFloat() throws IOException {
78 assertEquals( Float.intBitsToFloat(0x04030201), this.sdis.readFloat(), 0 );
79 }
80
81 public void testReadFully() throws IOException {
82 byte[] bytesIn = new byte[8];
83 this.sdis.readFully(bytesIn);
84 for( int i=0; i<8; i++) {
85 assertEquals( bytes[i], bytesIn[i] );
86 }
87 }
88
89 public void testReadInt() throws IOException {
90 assertEquals( (int) 0x04030201, this.sdis.readInt() );
91 }
92
93 public void testReadLine() throws IOException {
94 try {
95 String unexpected = this.sdis.readLine();
96 fail("readLine should be unsupported. ");
97 } catch(UnsupportedOperationException uoe) {
98 }
99 }
100
101 public void testReadLong() throws IOException {
102 assertEquals( 0x0807060504030201L, this.sdis.readLong() );
103 }
104
105 public void testReadShort() throws IOException {
106 assertEquals( (short) 0x0201, this.sdis.readShort() );
107 }
108
109 public void testReadUnsignedByte() throws IOException {
110 assertEquals( 0x01, this.sdis.readUnsignedByte() );
111 }
112
113 public void testReadUnsignedShort() throws IOException {
114 assertEquals( (short) 0x0201, this.sdis.readUnsignedShort() );
115 }
116
117 public void testReadUTF() throws IOException {
118 try {
119 String unexpected = this.sdis.readUTF();
120 fail("readUTF should be unsupported. ");
121 } catch(UnsupportedOperationException uoe) {
122 }
123 }
124
125 public void testSkipBytes() throws IOException {
126 this.sdis.skipBytes(4);
127 assertEquals( (int)0x08070605, this.sdis.readInt() );
128 }
129
130 }