Skip to content

Commit 4a97126

Browse files
committed
Add more tests
1 parent 185c8aa commit 4a97126

File tree

2 files changed

+94
-19
lines changed

2 files changed

+94
-19
lines changed

src/test/java/at/favre/lib/bytes/BytesMiscTest.java

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,4 +78,78 @@ public void testCompareTo() throws Exception {
7878
assertTrue(0 == Bytes.from(bOne).compareTo(Bytes.from(bOne)));
7979
}
8080

81+
@Test
82+
public void testLength() throws Exception {
83+
assertEquals(0, Bytes.from(new byte[0]).length());
84+
85+
for (int i = 0; i < 128; i++) {
86+
assertEquals(i, Bytes.from(new byte[i]).length());
87+
assertEquals(i * 8, Bytes.from(new byte[i]).lengthBit());
88+
assertEquals(i, Bytes.allocate(i).length());
89+
}
90+
}
91+
92+
@Test
93+
public void testIsEmpty() throws Exception {
94+
assertTrue(Bytes.from(new byte[0]).isEmpty());
95+
assertTrue(Bytes.allocate(0).isEmpty());
96+
assertFalse(Bytes.from(new byte[1]).isEmpty());
97+
assertFalse(Bytes.allocate(1).isEmpty());
98+
assertFalse(Bytes.from(example_bytes_seven).isEmpty());
99+
}
100+
101+
@Test
102+
public void indexOfByte() throws Exception {
103+
assertEquals(-1, Bytes.allocate(0).indexOf((byte) 0xFD));
104+
assertEquals(0, Bytes.allocate(128).indexOf((byte) 0x00));
105+
assertEquals(2, Bytes.from(example_bytes_seven).indexOf((byte) 0xFD));
106+
assertEquals(5, Bytes.from(example_bytes_seven).indexOf((byte) 0xAF));
107+
assertEquals(-1, Bytes.from(example_bytes_seven).indexOf((byte) 0x00));
108+
}
109+
110+
@Test
111+
public void indexOfArray() throws Exception {
112+
assertEquals(-1, Bytes.allocate(0).indexOf(new byte[]{(byte) 0xFD}));
113+
assertEquals(2, Bytes.from(example_bytes_seven).indexOf(new byte[]{(byte) 0xFD, (byte) 0xFF}));
114+
assertEquals(-1, Bytes.from(example_bytes_seven).indexOf(new byte[]{(byte) 0xFD, (byte) 0x00}));
115+
}
116+
117+
@Test
118+
public void lastIndexOf() throws Exception {
119+
assertEquals(-1, Bytes.allocate(0).lastIndexOf((byte) 0xFD));
120+
assertEquals(127, Bytes.allocate(128).lastIndexOf((byte) 0x00));
121+
assertEquals(2, Bytes.from(example_bytes_seven).lastIndexOf((byte) 0xFD));
122+
assertEquals(5, Bytes.from(example_bytes_seven).lastIndexOf((byte) 0xAF));
123+
assertEquals(-1, Bytes.from(example_bytes_seven).lastIndexOf((byte) 0x00));
124+
}
125+
126+
@Test
127+
public void byteAt() throws Exception {
128+
assertEquals(0, Bytes.allocate(1).byteAt(0));
129+
assertEquals(0, Bytes.allocate(128).byteAt(127));
130+
131+
for (int i = 0; i < example_bytes_twentyfour.length; i++) {
132+
assertEquals(example_bytes_twentyfour[i], Bytes.wrap(example_bytes_twentyfour).byteAt(i));
133+
}
134+
}
135+
136+
@Test
137+
public void count() throws Exception {
138+
assertEquals(0, Bytes.allocate(0).count((byte) 0));
139+
assertEquals(1, Bytes.allocate(1).count((byte) 0));
140+
assertEquals(128, Bytes.allocate(128).count((byte) 0));
141+
assertEquals(3, Bytes.from(example_bytes_twentyfour).count((byte) 0xAA));
142+
assertEquals(1, Bytes.from(example_bytes_seven).count((byte) 0xAF));
143+
}
144+
145+
@Test
146+
public void entropy() throws Exception {
147+
assertEquals(0, Bytes.allocate(0).entropy(), 0.1d);
148+
assertEquals(0, Bytes.allocate(1).entropy(), 0.1d);
149+
assertEquals(0, Bytes.allocate(256).entropy(), 0.1d);
150+
assertEquals(0, Bytes.from(new byte[]{1}).entropy(), 0.1d);
151+
assertTrue(Bytes.from(example_bytes_twentyfour).entropy() > 3.5);
152+
assertTrue(Bytes.from(example_bytes_seven).entropy() > 2.5);
153+
assertTrue(Bytes.from(example_bytes_two).entropy() > 0.5);
154+
}
81155
}

src/test/java/at/favre/lib/bytes/BytesParseAndEncodingTest.java

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,12 @@
2727
import static org.junit.Assert.assertArrayEquals;
2828
import static org.junit.Assert.assertEquals;
2929

30-
public class BytesParseAndEncodingTest {
31-
private final static byte[] example2 = new byte[]{0x4A, (byte) 0x94, (byte) 0xFD, (byte) 0xFF, 0x1E, (byte) 0xAF, (byte) 0xED};
30+
public class BytesParseAndEncodingTest extends ABytesTest {
31+
private byte[] encodingExample;
3232

3333
@Before
3434
public void setUp() throws Exception {
35+
encodingExample = new byte[]{0x4A, (byte) 0x94, (byte) 0xFD, (byte) 0xFF, 0x1E, (byte) 0xAF, (byte) 0xED};
3536
}
3637

3738
@Test
@@ -46,62 +47,62 @@ public void parseHex() throws Exception {
4647
@Test
4748
public void encodeHex() throws Exception {
4849
byte[] defaultArray = new byte[]{(byte) 0xA0, (byte) 0xE1};
49-
assertEquals("a0e1", Bytes.wrap(defaultArray).encodeHex());
50-
assertEquals("A0E1", Bytes.wrap(defaultArray).encodeHex(true));
51-
assertEquals(Bytes.wrap(defaultArray).encodeHex(), Bytes.wrap(defaultArray).encodeHex(false));
52-
assertEquals("4a94fdff1eafed", Bytes.wrap(example2).encodeHex());
50+
assertEquals("a0e1", Bytes.from(defaultArray).encodeHex());
51+
assertEquals("A0E1", Bytes.from(defaultArray).encodeHex(true));
52+
assertEquals(Bytes.from(defaultArray).encodeHex(), Bytes.from(defaultArray).encodeHex(false));
53+
assertEquals("4a94fdff1eafed", Bytes.from(encodingExample).encodeHex());
5354
}
5455

5556
@Test
5657
public void parseBase64() throws Exception {
57-
assertArrayEquals(example2, Bytes.parseBase64("SpT9/x6v7Q==").array());
58+
assertArrayEquals(encodingExample, Bytes.parseBase64("SpT9/x6v7Q==").array());
5859
}
5960

6061
@Test
6162
public void encodeBase64() throws Exception {
62-
assertEquals("SpT9/x6v7Q==", Bytes.wrap(example2).encodeBase64());
63+
assertEquals("SpT9/x6v7Q==", Bytes.from(encodingExample).encodeBase64());
6364
}
6465

6566
@Test
6667
public void encodeBinary() throws Exception {
6768
byte[] defaultArray = new byte[]{(byte) 0xA0, (byte) 0xE1};
68-
assertEquals("1010000011100001", Bytes.wrap(defaultArray).encodeBinary());
69-
assertEquals("1001010100101001111110111111111000111101010111111101101", Bytes.wrap(example2).encodeBinary());
69+
assertEquals("1010000011100001", Bytes.from(defaultArray).encodeBinary());
70+
assertEquals("1001010100101001111110111111111000111101010111111101101", Bytes.from(encodingExample).encodeBinary());
7071
}
7172

7273
@Test
7374
public void parseOctal() throws Exception {
74-
assertArrayEquals(example2, Bytes.parseOctal("1124517677707527755").array());
75+
assertArrayEquals(encodingExample, Bytes.parseOctal("1124517677707527755").array());
7576
}
7677

7778
@Test
7879
public void encodeOctal() throws Exception {
7980
byte[] defaultArray = new byte[]{(byte) 0xA0, (byte) 0xE1};
80-
assertEquals("120341", Bytes.wrap(defaultArray).encodeOctal());
81-
assertEquals("1124517677707527755", Bytes.wrap(example2).encodeOctal());
81+
assertEquals("120341", Bytes.from(defaultArray).encodeOctal());
82+
assertEquals("1124517677707527755", Bytes.from(encodingExample).encodeOctal());
8283
}
8384

8485
@Test
8586
public void parseDec() throws Exception {
86-
assertArrayEquals(example2, Bytes.parseDec("20992966904426477").array());
87+
assertArrayEquals(encodingExample, Bytes.parseDec("20992966904426477").array());
8788
}
8889

8990
@Test
9091
public void encodeDec() throws Exception {
9192
byte[] defaultArray = new byte[]{(byte) 0xA0, (byte) 0xE1};
92-
assertEquals("41185", Bytes.wrap(defaultArray).encodeDec());
93-
assertEquals("20992966904426477", Bytes.wrap(example2).encodeDec());
93+
assertEquals("41185", Bytes.from(defaultArray).encodeDec());
94+
assertEquals("20992966904426477", Bytes.from(encodingExample).encodeDec());
9495
}
9596

9697
@Test
9798
public void parseBase36() throws Exception {
98-
assertArrayEquals(example2, Bytes.parseBase36("5qpdvuwjvu5").array());
99+
assertArrayEquals(encodingExample, Bytes.parseBase36("5qpdvuwjvu5").array());
99100
}
100101

101102
@Test
102103
public void encodeBase36() throws Exception {
103104
byte[] defaultArray = new byte[]{(byte) 0xA0, (byte) 0xE1, (byte) 0x13};
104-
assertEquals("69zbn", Bytes.wrap(defaultArray).encodeBase36());
105-
assertEquals("5qpdvuwjvu5", Bytes.wrap(example2).encodeBase36());
105+
assertEquals("69zbn", Bytes.from(defaultArray).encodeBase36());
106+
assertEquals("5qpdvuwjvu5", Bytes.from(encodingExample).encodeBase36());
106107
}
107108
}

0 commit comments

Comments
 (0)