Skip to content

Commit 3b922ae

Browse files
committed
fix: More separation of Util function
1 parent 6c5dcec commit 3b922ae

File tree

2 files changed

+68
-0
lines changed

2 files changed

+68
-0
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package prog.lzw;
2+
3+
public class LzwUtils {
4+
5+
/*
6+
* =========================================================================
7+
* = byte to int conversion
8+
* =========================================================================
9+
*/
10+
public static int btoi(Byte bt) {
11+
int ret = bt;
12+
if (ret < 0)
13+
ret += 256;
14+
return ret;
15+
16+
}
17+
18+
/** ====================================================================== */
19+
20+
/*
21+
* =========================================================================
22+
* = byte to int conversion
23+
* =========================================================================
24+
*/
25+
public static int stoi(String s) {
26+
int ret = 0, i;
27+
for (i = 0; i < s.length(); i++) {
28+
ret *= 2;
29+
if (s.charAt(i) == '1')
30+
ret++;
31+
}
32+
return ret;
33+
}
34+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package prog.lzw;
2+
3+
import org.junit.jupiter.api.Test;
4+
import static org.junit.jupiter.api.Assertions.*;
5+
6+
public class LzwUtilsTest {
7+
8+
@Test
9+
public void testBtoi() {
10+
// Test positive byte values
11+
assertEquals(0, LzwUtils.btoi((byte) 0));
12+
assertEquals(65, LzwUtils.btoi((byte) 65)); // 'A'
13+
assertEquals(90, LzwUtils.btoi((byte) 90)); // 'Z'
14+
assertEquals(127, LzwUtils.btoi((byte) 127));
15+
16+
// Test negative byte values (should convert to positive values 128-255)
17+
assertEquals(128, LzwUtils.btoi((byte) -128));
18+
assertEquals(200, LzwUtils.btoi((byte) -56));
19+
assertEquals(255, LzwUtils.btoi((byte) -1));
20+
}
21+
22+
@Test
23+
public void testStoi() {
24+
// Test binary string to integer conversion
25+
assertEquals(0, LzwUtils.stoi("0"));
26+
assertEquals(1, LzwUtils.stoi("1"));
27+
assertEquals(2, LzwUtils.stoi("10"));
28+
assertEquals(3, LzwUtils.stoi("11"));
29+
assertEquals(10, LzwUtils.stoi("1010"));
30+
assertEquals(15, LzwUtils.stoi("1111"));
31+
assertEquals(255, LzwUtils.stoi("11111111"));
32+
assertEquals(1023, LzwUtils.stoi("1111111111"));
33+
}
34+
}

0 commit comments

Comments
 (0)