Skip to content

Commit 3da142c

Browse files
committed
Tweaks.
1 parent 394cdc7 commit 3da142c

File tree

3 files changed

+69
-0
lines changed

3 files changed

+69
-0
lines changed

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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,29 @@
1717
*/
1818
public class FiveBitAsciiCompressorTest extends BaseTest {
1919

20+
@Test
21+
public void usageExample() {
22+
// A string to be compressed. Whenever possible, prefer working directly with byte[] to avoid creating String objects.
23+
byte[] inputStr = "HELLO-COMPRESSOR".getBytes(US_ASCII);
24+
25+
// Creates a compressor with the default supported character set.
26+
AsciiCompressor compressor = new FiveBitAsciiCompressor();
27+
// Creates a compressor with a custom charset (see FiveBitAsciiCompressor.DEFAULT_5BIT_CHARSET)
28+
// AsciiCompressor customCharsetCompressor = new FiveBitAsciiCompressor(new byte[]{/* custom charset */});
29+
30+
// Throws an exception when invalid characters are present; useful for debugging purposes.
31+
// Invalid characters should be silently ignored in production. Default is false.
32+
compressor.throwException = true;
33+
// Compressor overwrites the original string ("inputStr") to reduce memory usage.
34+
// Set to true to prevent this. Default is false.
35+
compressor.preserveOriginal = false;
36+
37+
byte[] compressed = compressor.compress(inputStr);
38+
byte[] decompressed = compressor.decompress(compressed);
39+
40+
assertEquals("HELLO-COMPRESSOR", new String(decompressed, US_ASCII));
41+
}
42+
2043
@Test
2144
public void validCustomCharsetTest() {
2245
byte[] customCharset = new byte[]{

src/test/java/com/stringcompressor/FourBitAsciiCompressorTest.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,29 @@
1717
*/
1818
public class FourBitAsciiCompressorTest extends BaseTest {
1919

20+
@Test
21+
public void usageExample() {
22+
// A string to be compressed. Whenever possible, prefer working directly with byte[] to avoid creating String objects.
23+
byte[] inputStr = "0123456789".getBytes(US_ASCII);
24+
25+
// Creates a compressor with the default supported character set.
26+
AsciiCompressor compressor = new FourBitAsciiCompressor();
27+
// Creates a compressor with a custom charset (see FourBitAsciiCompressor.DEFAULT_4BIT_CHARSET).
28+
// AsciiCompressor customCharsetCompressor = new FourBitAsciiCompressor(new byte[]{/* custom charset */});
29+
30+
// Throws an exception when invalid characters are present; useful for debugging purposes.
31+
// Invalid characters should be silently ignored in production. Default is false.
32+
compressor.throwException = true;
33+
// Compressor overwrites the original string ("inputStr") to reduce memory usage.
34+
// Set to true to prevent this. Default is false.
35+
compressor.preserveOriginal = false;
36+
37+
byte[] compressed = compressor.compress(inputStr);
38+
byte[] decompressed = compressor.decompress(compressed);
39+
40+
assertEquals("0123456789", new String(decompressed, US_ASCII));
41+
}
42+
2043
@Test
2144
public void validCustomCharsetTest() {
2245
byte[] customCharset = new byte[]{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15};

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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,29 @@
1717
*/
1818
public class SixBitAsciiCompressorTest extends BaseTest {
1919

20+
@Test
21+
public void usageExample() {
22+
// A string to be compressed. Whenever possible, prefer working directly with byte[] to avoid creating String objects.
23+
byte[] inputStr = "HELLO, COMPRESSOR".getBytes(US_ASCII);
24+
25+
// Creates a compressor with the default supported character set.
26+
AsciiCompressor compressor = new SixBitAsciiCompressor();
27+
// Creates a compressor with a custom charset (see SixBitAsciiCompressor.DEFAULT_6BIT_CHARSET).
28+
// AsciiCompressor customCharsetCompressor = new SixBitAsciiCompressor(new byte[]{/* custom charset */});
29+
30+
// Throws an exception when invalid characters are present; useful for debugging purposes.
31+
// Invalid characters should be silently ignored in production. Default is false.
32+
compressor.throwException = true;
33+
// Compressor overwrites the original string ("inputStr") to reduce memory usage.
34+
// Set to true to prevent this. Default is false.
35+
compressor.preserveOriginal = false;
36+
37+
byte[] compressed = compressor.compress(inputStr);
38+
byte[] decompressed = compressor.decompress(compressed);
39+
40+
assertEquals("HELLO, COMPRESSOR", new String(decompressed, US_ASCII));
41+
}
42+
2043
@Test
2144
public void validCustomCharsetTest() {
2245
byte[] customCharset = new byte[]{

0 commit comments

Comments
 (0)