view test/testbintools.py @ 409:8fe299cd2d55 devel

Close devel branch
author Windel Bouwman
date Sat, 21 Feb 2015 12:20:10 +0100
parents 86b02c98a717
children 9667d78ba79e
line wrap: on
line source

import unittest
import sys
from ppci.target.arm.token import ArmToken
from ppci.linker import Linker
from ppci.objectfile import ObjectFile
from ppci import CompilerError
from ppci.tasks import EmptyTask, TaskRunner, TaskError


class TaskTestCase(unittest.TestCase):
    def testCircular(self):
        t1 = EmptyTask('t1')
        t2 = EmptyTask('t2')
        t1.add_dependency(t2)
        with self.assertRaises(TaskError):
            t2.add_dependency(t1)

    def testCircularDeeper(self):
        t1 = EmptyTask('t1')
        t2 = EmptyTask('t2')
        t3 = EmptyTask('t3')
        t1.add_dependency(t2)
        t2.add_dependency(t3)
        with self.assertRaises(TaskError):
            t3.add_dependency(t1)

    def testSort(self):
        t1 = EmptyTask('t1')
        t2 = EmptyTask('t2')
        runner = TaskRunner()
        t1.add_dependency(t2)
        runner.add_task(t1)
        runner.add_task(t2)
        runner.run_tasks()


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()