File: test_int24.py

package info (click to toggle)
python-pyelftools 0.32-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 68,964 kB
  • sloc: python: 15,903; ansic: 298; asm: 86; makefile: 24; cpp: 18; sh: 4
file content (30 lines) | stat: -rw-r--r-- 974 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#------------------------------------------------------------------------------
# elftools tests
#
# Seva Alekseyev (sevaa@sprynet.com)
# This code is in the public domain
#------------------------------------------------------------------------------
import unittest
import random
from io import BytesIO

from elftools.common.construct_utils import ULInt24, UBInt24
from elftools.common.utils import struct_parse

class TestInt24(unittest.TestCase):
    def test_main(self):
        # Testing parsing and building, both LE and BE
        b = random.randbytes(3)

        n = struct_parse(UBInt24(''), BytesIO(b))
        self.assertEqual(n, (b[0] << 16) | (b[1] << 8) | b[2])
        s = UBInt24('').build(n)
        self.assertEqual(s, b)
        
        n = struct_parse(ULInt24(''), BytesIO(b))
        self.assertEqual(n, b[0] | (b[1] << 8) | (b[2] << 16))
        s = ULInt24('').build(n)
        self.assertEqual(s, b)

if __name__ == '__main__':
    unittest.main()