forked from bongtrop/hbctool
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create assemble function and update unittest
- Loading branch information
Pongsakorn Sommalai
committed
Jan 11, 2021
1 parent
5a576a8
commit 5e41c59
Showing
6 changed files
with
158 additions
and
104 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,100 @@ | ||
import unittest | ||
from util import * | ||
from hbc.hbc74.test import TestHBC74 | ||
|
||
class ByteIO: | ||
def __init__(self, v=b""): | ||
self.buf = v | ||
|
||
def write(self, b): | ||
self.buf += b | ||
|
||
def read(self, n=-1): | ||
if n==-1: | ||
o = self.buf | ||
self.buf = b"" | ||
return o | ||
|
||
o = self.buf[:n] | ||
self.buf = self.buf[n:] | ||
return o | ||
|
||
class TestFileUtilization(unittest.TestCase): | ||
def test_bit_writer(self): | ||
io = ByteIO() | ||
fw = BitWriter(io) | ||
|
||
offset = 182856 | ||
paramCount = 1 | ||
bytecodeSizeInBytes = 12342 | ||
functionName = 3086 | ||
|
||
write(fw, offset, ["bit", 25, 1]) | ||
write(fw, paramCount, ["bit", 7, 1]) | ||
write(fw, bytecodeSizeInBytes, ["bit", 15, 1]) | ||
write(fw, functionName, ["bit", 17, 1]) | ||
|
||
bs = io.read() | ||
self.assertEqual("48ca020236300706", bs.hex()) | ||
|
||
isUTF16 = 0 | ||
offset = 465 | ||
length = 3 | ||
|
||
write(fw, isUTF16, ["bit", 1, 1]) | ||
write(fw, offset, ["bit", 23, 1]) | ||
write(fw, length, ["bit", 8, 1]) | ||
|
||
bs = io.read() | ||
self.assertEqual("a2030003", bs.hex()) | ||
|
||
def test_bit_reader(self): | ||
io = ByteIO() | ||
fr = BitReader(io) | ||
|
||
io.write(bytes.fromhex("48ca020236300706")) | ||
|
||
offset = read(fr, ["bit", 25, 1]) | ||
paramCount = read(fr, ["bit", 7, 1]) | ||
bytecodeSizeInBytes = read(fr, ["bit", 15, 1]) | ||
functionName = read(fr, ["bit", 17, 1]) | ||
|
||
self.assertEqual(offset, 182856) | ||
self.assertEqual(paramCount, 1) | ||
self.assertEqual(bytecodeSizeInBytes, 12342) | ||
self.assertEqual(functionName, 3086) | ||
|
||
io.write(bytes.fromhex("a2030003")) | ||
|
||
isUTF16 = read(fr, ["bit", 1, 1]) | ||
offset = read(fr, ["bit", 23, 1]) | ||
length = read(fr, ["bit", 8, 1]) | ||
|
||
self.assertEqual(isUTF16, 0) | ||
self.assertEqual(offset, 465) | ||
self.assertEqual(length, 3) | ||
|
||
def test_conversion(self): | ||
|
||
io = ByteIO() | ||
fr = BitReader(io) | ||
fw = BitWriter(io) | ||
|
||
isUTF16 = 1 | ||
offset = 465 | ||
length = 3 | ||
|
||
write(fw, isUTF16, ["bit", 1, 1]) | ||
write(fw, offset, ["bit", 23, 1]) | ||
write(fw, length, ["bit", 8, 1]) | ||
|
||
isUTF16 = read(fr, ["bit", 1, 1]) | ||
offset = read(fr, ["bit", 23, 1]) | ||
length = read(fr, ["bit", 8, 1]) | ||
|
||
self.assertEqual(isUTF16, 1) | ||
self.assertEqual(offset, 465) | ||
self.assertEqual(length, 3) | ||
|
||
if __name__ == "__main__": | ||
unittest.main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters