開発環境
- macOS Mojave - Apple (OS)
- Emacs (Text Editor)
- Windows 10 Pro (OS)
- Visual Studio Code (Text Editor)
- Python 3.7 (プログラミング言語)
Programming Bitcoin: Learn How to Program Bitcoin from Scratch (Jimmy Song(著)、O'Reilly Media)のChapter 4(Serialization)、Big- and Little-Endian Redux、Exercises 8(86)の解答を求めてみる。
コード
Python 3
#!/usr/bin/env python3 from unittest import TestCase, main def little_endian_to_int(b: bytes) -> int: return int.from_bytes(b, 'little') def int_to_little_endian(n: int, length: int) -> bytes: return n.to_bytes(length, 'little') class MyTestCase(TestCase): def setUp(self): pass def tearDown(self): pass def test_little_endian_to_int_1(self): self.assertEqual(little_endian_to_int(b'\x01\x00'), 16 ** 0 * 1) def test_little_endian_to_int_2(self): self.assertEqual(little_endian_to_int(b'\x00\x01'), 16 ** 2 * 1) def test_int_to_little_endian_1(self): self.assertEqual(b'\x01', int_to_little_endian(1, 1)) def test_int_to_little_endian_2(self): self.assertEqual(b'\x00\x01', int_to_little_endian(1 * 16 ** 2, 2)) def test_little_endian_to_int_reverse1(self): b = b'\x00\x01' self.assertEqual(b, int_to_little_endian(little_endian_to_int(b), 2)) def test_little_endian_to_int_reverse2(self): b = b'\x01\x00' self.assertEqual(b, int_to_little_endian(little_endian_to_int(b), 2)) def test_int_to_little_endian_reverse1(self): self.assertEqual(1, little_endian_to_int(int_to_little_endian(1, 1))) def test_int_to_little_endian_reverse2(self): n = 16 ** 2 self.assertEqual(n, little_endian_to_int(int_to_little_endian(n, 2))) if __name__ == '__main__': for b in [b'\x10\x00', b'\x00\x10']: for o in [b, little_endian_to_int(b)]: print(o) print() main()
入出力結果(cmd(コマンドプロンプト)、Terminal、Jupyter(IPython))
C:\Users\...>py sample8.py b'\x10\x00' 16 b'\x00\x10' 4096 EE.. ====================================================================== ERROR: test_int_to_little_endian_1 (__main__.MyTestCase) ---------------------------------------------------------------------- Traceback (most recent call last): File "sample8.py", line 28, in test_int_to_little_endian_1 self.assertEqual(int_to_little_endian(1), b'\x01') NameError: name 'int_to_little_endian' is not defined ====================================================================== ERROR: test_int_to_little_endian_2 (__main__.MyTestCase) ---------------------------------------------------------------------- Traceback (most recent call last): File "sample8.py", line 31, in test_int_to_little_endian_2 self.assertEqual(int_to_little_endian(1 * 16 ** 2, b'\x00\x01')) NameError: name 'int_to_little_endian' is not defined ---------------------------------------------------------------------- Ran 4 tests in 0.001s FAILED (errors=2) C:\Users\...>py sample8.py b'\x10\x00' 16 b'\x00\x10' 4096 .E.. ====================================================================== ERROR: test_int_to_little_endian_2 (__main__.MyTestCase) ---------------------------------------------------------------------- Traceback (most recent call last): File "sample8.py", line 31, in test_int_to_little_endian_2 self.assertEqual(int_to_little_endian(1 * 16 ** 2, b'\x00\x01')) TypeError: int_to_little_endian() takes 1 positional argument but 2 were given ---------------------------------------------------------------------- Ran 4 tests in 0.000s FAILED (errors=1) C:\Users\...>py sample8.py b'\x10\x00' 16 b'\x00\x10' 4096 .... ---------------------------------------------------------------------- Ran 4 tests in 0.000s OK C:\Users\...>py sample8.py b'\x10\x00' 16 b'\x00\x10' 4096 .....F ====================================================================== FAIL: test_little_endian_to_int_reverse2 (__main__.MyTestCase) ---------------------------------------------------------------------- Traceback (most recent call last): File "sample8.py", line 39, in test_little_endian_to_int_reverse2 self.assertEqual(int_to_little_endian(little_endian_to_int(b)), b) AssertionError: b'\x01' != b'\x01\x00' ---------------------------------------------------------------------- Ran 6 tests in 0.001s FAILED (failures=1) C:\Users\...>py sample8.py File "sample8.py", line 13 class MyTestCase(TestCase): ^ SyntaxError: invalid syntax C:\Users\...>py sample8.py b'\x10\x00' 16 b'\x00\x10' 4096 ...... ---------------------------------------------------------------------- Ran 6 tests in 0.000s OK C:\Users\...>py sample8.py b'\x10\x00' 16 b'\x00\x10' 4096 ....... ---------------------------------------------------------------------- Ran 7 tests in 0.000s OK C:\Users\...>py sample8.py b'\x10\x00' 16 b'\x00\x10' 4096 ........ ---------------------------------------------------------------------- Ran 8 tests in 0.000s OK C:\Users\...>py sample8.py -v b'\x10\x00' 16 b'\x00\x10' 4096 test_int_to_little_endian_1 (__main__.MyTestCase) ... ok test_int_to_little_endian_2 (__main__.MyTestCase) ... ok test_int_to_little_endian_reverse1 (__main__.MyTestCase) ... ok test_int_to_little_endian_reverse2 (__main__.MyTestCase) ... ok test_little_endian_to_int_1 (__main__.MyTestCase) ... ok test_little_endian_to_int_2 (__main__.MyTestCase) ... ok test_little_endian_to_int_reverse1 (__main__.MyTestCase) ... ok test_little_endian_to_int_reverse2 (__main__.MyTestCase) ... ok ---------------------------------------------------------------------- Ran 8 tests in 0.001s OK C:\Users\...>
0 コメント:
コメントを投稿