|
6 | 6 |
|
7 | 7 | import java.util.Arrays; |
8 | 8 |
|
9 | | -import static com.stringcompressor.FiveBitAsciiCompressor.DEFAULT_5BIT_CHARSET; |
10 | 9 | import static com.stringcompressor.SixBitAsciiCompressor.DEFAULT_6BIT_CHARSET; |
11 | 10 | import static java.nio.charset.StandardCharsets.US_ASCII; |
12 | 11 | import static org.junit.jupiter.api.Assertions.assertEquals; |
|
17 | 16 | */ |
18 | 17 | public class SixBitAsciiCompressorTest extends BaseTest { |
19 | 18 |
|
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 | + } |
73 | 72 |
|
74 | 73 | @Test |
75 | 74 | public void compressDecompressTest() { |
76 | 75 | 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++) { |
79 | 78 | String str = createRandomString(length, DEFAULT_6BIT_CHARSET); |
80 | 79 | byte[] compressed = compressor.compress(str.getBytes(US_ASCII)); |
81 | 80 | byte[] decompressed = compressor.decompress(compressed); |
82 | 81 | assertEquals(str, new String(decompressed, US_ASCII)); |
83 | 82 | } |
84 | 83 | } |
85 | 84 |
|
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 | + } |
115 | 114 |
|
116 | 115 | } |
0 commit comments