Skip to content

Commit a9c1e1c

Browse files
committed
SixBitAsciiCompressor working.
1 parent 12a776e commit a9c1e1c

File tree

3 files changed

+95
-87
lines changed

3 files changed

+95
-87
lines changed

src/main/java/com/stringcompressor/SixBitAsciiCompressor.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,15 @@ public class SixBitAsciiCompressor extends AsciiCompressor {
1717
'[', ']', '_', '{', '}'
1818
};
1919

20+
public static final byte[] DEFAULT_6BIT_CHARSET_LOWERCASE = new byte[]{
21+
' ', '!', '"', '#', '$', '%', '&', '\'', '(', ')', '*', '+', ',', '-', '.', '/',
22+
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
23+
':', ';', '<', '=', '>', '?', '@',
24+
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
25+
'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
26+
'[', ']', '_', '{', '}'
27+
};
28+
2029
public SixBitAsciiCompressor() {
2130
super(DEFAULT_6BIT_CHARSET);
2231
}

src/test/java/com/stringcompressor/FiveBitAsciiCompressorTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,8 @@ public void invalidCharCompressTest() {
7373
@Test
7474
public void compressDecompressTest() {
7575
AsciiCompressor compressor = new FiveBitAsciiCompressor();
76-
for (int length = 0; length <= 300; length++)
77-
for (int i = 0; i <= 2000; i++) {
76+
for (int length = 0; length <= 500; length++)
77+
for (int i = 0; i <= 5000; i++) {
7878
String str = createRandomString(length, DEFAULT_5BIT_CHARSET);
7979
byte[] compressed = compressor.compress(str.getBytes(US_ASCII));
8080
byte[] decompressed = compressor.decompress(compressed);

src/test/java/com/stringcompressor/SixBitAsciiCompressorTest.java

Lines changed: 84 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66

77
import java.util.Arrays;
88

9-
import static com.stringcompressor.FiveBitAsciiCompressor.DEFAULT_5BIT_CHARSET;
109
import static com.stringcompressor.SixBitAsciiCompressor.DEFAULT_6BIT_CHARSET;
1110
import static java.nio.charset.StandardCharsets.US_ASCII;
1211
import static org.junit.jupiter.api.Assertions.assertEquals;
@@ -17,100 +16,100 @@
1716
*/
1817
public class SixBitAsciiCompressorTest extends BaseTest {
1918

20-
// @Test
21-
// public void validCustomCharsetTest() {
22-
// byte[] customSupportedCharset = new byte[]{
23-
// 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
24-
// 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31};
25-
// new FiveBitAsciiCompressor(customSupportedCharset);
26-
// }
27-
//
28-
// @Test
29-
// public void excessCustomCharsetTest() {
30-
// byte[] customSupportedCharset = new byte[]{
31-
// 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
32-
// 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32};
33-
// CharacterNotSupportedException e = assertThrows(
34-
// CharacterNotSupportedException.class, () -> new FiveBitAsciiCompressor(customSupportedCharset));
35-
// assertEquals("5-bit compressor requires a set of exactly 32 characters. Currently 33.", e.getMessage());
36-
// }
37-
//
38-
// @Test
39-
// public void missingCustomCharsetTest() {
40-
// byte[] customSupportedCharset = new byte[]{
41-
// 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
42-
// 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30};
43-
// CharacterNotSupportedException e = assertThrows(
44-
// CharacterNotSupportedException.class, () -> new FiveBitAsciiCompressor(customSupportedCharset));
45-
// assertEquals("5-bit compressor requires a set of exactly 32 characters. Currently 31.", e.getMessage());
46-
// }
47-
//
48-
// @Test
49-
// public void invalidCustomCharsetTest() {
50-
// byte[] customSupportedCharset = new byte[]{
51-
// -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
52-
// 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30};
53-
// CharacterNotSupportedException e = assertThrows(
54-
// CharacterNotSupportedException.class, () -> new FiveBitAsciiCompressor(customSupportedCharset));
55-
// assertEquals("Invalid character found in the custom supported charset: '\uFFFF' (code point -1)", e.getMessage());
56-
// }
57-
//
58-
// @Test
59-
// public void notAsciiCharCompressTest() {
60-
// AsciiCompressor compressor = new FiveBitAsciiCompressor(true);
61-
// CharacterNotSupportedException e = assertThrows(
62-
// CharacterNotSupportedException.class, () -> compressor.compress(new byte[]{(byte) 'Ç'}));
63-
// assertEquals("Only ASCII characters are supported. Invalid 'ᅦ' (code point -57) in \"�\"", e.getMessage());
64-
// }
65-
//
66-
// @Test
67-
// public void invalidCharCompressTest() {
68-
// AsciiCompressor compressor = new FiveBitAsciiCompressor(true);
69-
// CharacterNotSupportedException e = assertThrows(
70-
// CharacterNotSupportedException.class, () -> compressor.compress(new byte[]{'9'}));
71-
// assertEquals("Character '9' (code point 57) is not defined in the supported characters array. String: \"9\"", e.getMessage());
72-
// }
19+
@Test
20+
public void validCustomCharsetTest() {
21+
byte[] customSupportedCharset = new byte[]{
22+
0, 1, 2, 3, 4, 6, 6, 7, 8, 9, 10, 11, 12, 13, 14, 16, 16, 17, 18, 19, 20, 21, 22, 23, 24, 26, 26, 27, 28, 29, 30, 31,
23+
32, 33, 34, 36, 36, 37, 38, 39, 40, 41, 42, 43, 44, 46, 46, 47, 48, 49, 50, 51, 52, 53, 54, 56, 56, 57, 58, 59, 60, 61, 62, 63};
24+
new SixBitAsciiCompressor(customSupportedCharset);
25+
}
26+
27+
@Test
28+
public void excessCustomCharsetTest() {
29+
byte[] customSupportedCharset = new byte[]{
30+
0, 1, 2, 3, 4, 6, 6, 7, 8, 9, 10, 11, 12, 13, 14, 16, 16, 17, 18, 19, 20, 21, 22, 23, 24, 26, 26, 27, 28, 29, 30, 31,
31+
32, 33, 34, 36, 36, 37, 38, 39, 40, 41, 42, 43, 44, 46, 46, 47, 48, 49, 50, 51, 52, 53, 54, 56, 56, 57, 58, 59, 60, 61, 62, 63, 64};
32+
CharacterNotSupportedException e = assertThrows(
33+
CharacterNotSupportedException.class, () -> new SixBitAsciiCompressor(customSupportedCharset));
34+
assertEquals("6-bit compressor requires a set of exactly 64 characters. Currently 65.", e.getMessage());
35+
}
36+
37+
@Test
38+
public void missingCustomCharsetTest() {
39+
byte[] customSupportedCharset = new byte[]{
40+
0, 1, 2, 3, 4, 6, 6, 7, 8, 9, 10, 11, 12, 13, 14, 16, 16, 17, 18, 19, 20, 21, 22, 23, 24, 26, 26, 27, 28, 29, 30, 31,
41+
32, 33, 34, 36, 36, 37, 38, 39, 40, 41, 42, 43, 44, 46, 46, 47, 48, 49, 50, 51, 52, 53, 54, 56, 56, 57, 58, 59, 60, 61, 62};
42+
CharacterNotSupportedException e = assertThrows(
43+
CharacterNotSupportedException.class, () -> new SixBitAsciiCompressor(customSupportedCharset));
44+
assertEquals("6-bit compressor requires a set of exactly 64 characters. Currently 63.", e.getMessage());
45+
}
46+
47+
@Test
48+
public void invalidCustomCharsetTest() {
49+
byte[] customSupportedCharset = new byte[]{
50+
-1, 0, 1, 2, 3, 4, 6, 6, 7, 8, 9, 10, 11, 12, 13, 14, 16, 16, 17, 18, 19, 20, 21, 22, 23, 24, 26, 26, 27, 28, 29, 30, 31,
51+
32, 33, 34, 36, 36, 37, 38, 39, 40, 41, 42, 43, 44, 46, 46, 47, 48, 49, 50, 51, 52, 53, 54, 56, 56, 57, 58, 59, 60, 61, 62};
52+
CharacterNotSupportedException e = assertThrows(
53+
CharacterNotSupportedException.class, () -> new SixBitAsciiCompressor(customSupportedCharset));
54+
assertEquals("Invalid character found in the custom supported charset: '\uFFFF' (code point -1)", e.getMessage());
55+
}
56+
57+
@Test
58+
public void notAsciiCharCompressTest() {
59+
AsciiCompressor compressor = new SixBitAsciiCompressor(true);
60+
CharacterNotSupportedException e = assertThrows(
61+
CharacterNotSupportedException.class, () -> compressor.compress(new byte[]{(byte) 'Ç'}));
62+
assertEquals("Only ASCII characters are supported. Invalid 'ᅦ' (code point -57) in \"\"", e.getMessage());
63+
}
64+
65+
@Test
66+
public void invalidCharCompressTest() {
67+
AsciiCompressor compressor = new SixBitAsciiCompressor(true);
68+
CharacterNotSupportedException e = assertThrows(
69+
CharacterNotSupportedException.class, () -> compressor.compress(new byte[]{'|'}));
70+
assertEquals("Character '|' (code point 124) is not defined in the supported characters array. String: \"|\"", e.getMessage());
71+
}
7372

7473
@Test
7574
public void compressDecompressTest() {
7675
AsciiCompressor compressor = new SixBitAsciiCompressor();
77-
for (int length = 0; length <= 1000; length++)
78-
for (int i = 0; i <= 10000; i++) {
76+
for (int length = 0; length <= 500; length++)
77+
for (int i = 0; i <= 5000; i++) {
7978
String str = createRandomString(length, DEFAULT_6BIT_CHARSET);
8079
byte[] compressed = compressor.compress(str.getBytes(US_ASCII));
8180
byte[] decompressed = compressor.decompress(compressed);
8281
assertEquals(str, new String(decompressed, US_ASCII));
8382
}
8483
}
8584

86-
// @Test
87-
// public void ignoreInvalidCharTest() {
88-
// AsciiCompressor compressor = new FiveBitAsciiCompressor();
89-
// byte[] compressed = compressor.compress(new byte[]{'A', (byte) 'Ç', 'B', 'C'});
90-
// byte[] decompressed = compressor.decompress(compressed);
91-
// assertEquals("AGBC", new String(decompressed, US_ASCII));
92-
// }
93-
//
94-
// @Test
95-
// public void ignoreInvalidCharsTest() {
96-
// AsciiCompressor compressor = new FiveBitAsciiCompressor();
97-
// for (int i = 0; i < 3000; i++)
98-
// for (int asciiCode = 0; asciiCode < 128; asciiCode++) {
99-
// byte[] input = new byte[]{'A', (byte) asciiCode, 'B', 'C', 'D', (byte) 'Ç'};
100-
// byte[] compressed = compressor.compress(input);
101-
// byte[] decompressed = compressor.decompress(compressed);
102-
// assertEquals(input.length, decompressed.length);
103-
// }
104-
// }
105-
//
106-
// @Test
107-
// public void compressionRateTest() {
108-
// AsciiCompressor compressor = new FiveBitAsciiCompressor();
109-
// int hundredMb = 100 * 1024 * 1024;
110-
// byte[] input = new byte[hundredMb];
111-
// Arrays.fill(input, (byte) 'A');
112-
// byte[] compressed = compressor.compress(input);
113-
// Assertions.assertEquals(62, compressed.length / 1024 / 1024); // 38% compression rate.
114-
// }
85+
@Test
86+
public void ignoreInvalidCharTest() {
87+
AsciiCompressor compressor = new SixBitAsciiCompressor();
88+
byte[] compressed = compressor.compress(new byte[]{'A', (byte) 'Ç', 'B', 'C'});
89+
byte[] decompressed = compressor.decompress(compressed);
90+
assertEquals("AGBC", new String(decompressed, US_ASCII));
91+
}
92+
93+
@Test
94+
public void ignoreInvalidCharsTest() {
95+
AsciiCompressor compressor = new SixBitAsciiCompressor();
96+
for (int i = 0; i < 3000; i++)
97+
for (int asciiCode = 0; asciiCode < 128; asciiCode++) {
98+
byte[] input = new byte[]{'A', (byte) asciiCode, 'B', 'C', 'D', (byte) 'Ç'};
99+
byte[] compressed = compressor.compress(input);
100+
byte[] decompressed = compressor.decompress(compressed);
101+
assertEquals(input.length, decompressed.length);
102+
}
103+
}
104+
105+
@Test
106+
public void compressionRateTest() {
107+
AsciiCompressor compressor = new SixBitAsciiCompressor();
108+
int hundredMb = 100 * 1024 * 1024;
109+
byte[] input = new byte[hundredMb];
110+
Arrays.fill(input, (byte) 'A');
111+
byte[] compressed = compressor.compress(input);
112+
Assertions.assertEquals(75, compressed.length / 1024 / 1024); // 25% compression rate.
113+
}
115114

116115
}

0 commit comments

Comments
 (0)