view test/testbintools.py @ 336:d1ecc493384e

Added spiffy armtoken class for bit fiddeling. Added cool test that checks for build repeatability
author Windel Bouwman
date Wed, 19 Feb 2014 22:32:15 +0100
parents
children 86b02c98a717
line wrap: on
line source

import unittest
import sys
from target.arminstructions import ArmToken
from ppci.linker import Linker
from ppci.objectfile import ObjectFile
from ppci import CompilerError


class TokenTestCase(unittest.TestCase):
    def testSetBits(self):
        at = ArmToken()
        at[2:4] = 0b11
        self.assertEqual(0xc, at.bit_value)

    def testSetBits(self):
        at = ArmToken()
        at[4:8] = 0b1100
        self.assertEqual(0xc0, at.bit_value)


class LinkerTestCase(unittest.TestCase):
    def testUndefinedReference(self):
        l = Linker()
        o1 = ObjectFile()
        o1.get_section('.text')
        o1.add_relocation('undefined_sym', 0, 'rel8', '.text')
        o2 = ObjectFile()
        with self.assertRaises(CompilerError):
            o3 = l.link([o1, o2])

    def testDuplicateSymbol(self):
        l = Linker()
        o1 = ObjectFile()
        o1.get_section('.text')
        o1.add_symbol('a', 0, '.text')
        o2 = ObjectFile()
        o2.get_section('.text')
        o2.add_symbol('a', 0, '.text')
        with self.assertRaises(CompilerError):
            o3 = l.link([o1, o2])

    def testRel8Relocation(self):
        l = Linker()
        o1 = ObjectFile()
        o1.get_section('.text').add_data(bytes([0]*100))
        o1.add_relocation('a', 0, 'rel8', '.text')
        o2 = ObjectFile()
        o2.get_section('.text').add_data(bytes([0]*100))
        o2.add_symbol('a', 24, '.text')
        o3 = l.link([o1, o2])

    def testSymbolValues(self):
        l = Linker()
        o1 = ObjectFile()
        o1.get_section('.text').add_data(bytes([0]*108))
        o1.add_symbol('b', 24, '.text')
        o2 = ObjectFile()
        o2.get_section('.text').add_data(bytes([0]*100))
        o2.add_symbol('a', 2, '.text')
        o3 = l.link([o1, o2])
        self.assertEqual(110, o3.find_symbol('a').value)
        self.assertEqual(24, o3.find_symbol('b').value)
        self.assertEqual(208, o3.get_section('.text').Size)

    def testMemoryLayout(self):
        l = Linker()
        memory_layout = {'.text': 0x08000000, '.data':0x20000000}
        o1 = ObjectFile()
        o1.get_section('.text').add_data(bytes([0]*108))
        o1.add_symbol('b', 24, '.text')
        o2 = ObjectFile()
        o2.get_section('.text').add_data(bytes([0]*100))
        o2.get_section('.data').add_data(bytes([0]*100))
        o2.add_symbol('a', 2, '.data')
        o2.add_symbol('c', 2, '.text')
        o3 = l.link([o1, o2], layout=memory_layout)
        self.assertEqual(0x20000000+2, o3.find_symbol('a').value)
        self.assertEqual(0x08000000+24, o3.find_symbol('b').value)
        self.assertEqual(0x08000000+110, o3.find_symbol('c').value)
        self.assertEqual(208, o3.get_section('.text').Size)
        self.assertEqual(100, o3.get_section('.data').Size)


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