view test/testzcc.py @ 342:86b02c98a717 devel

Moved target directory
author Windel Bouwman
date Sat, 01 Mar 2014 15:40:31 +0100
parents 4d204f6f7d4e
children 3bb7dcfe5529
line wrap: on
line source

import unittest
import os
import sys

import zcc
from ppci.objectfile import ObjectFile
import ppci
import io
from ppci.target import target_list

# Store testdir for safe switch back to directory:
testdir = os.path.dirname(os.path.abspath(__file__))


class ZccBaseTestCase(unittest.TestCase):
    def callZcc(self, arg_list):
        parser = zcc.make_parser()
        arg_list = ['--log', 'warn'] + arg_list
        args = parser.parse_args(arg_list)
        self.assertEqual(0, zcc.main(args))

    def buildRecipe(self, recipe):
        arg_list = ['recipe', recipe]
        self.callZcc(arg_list)


class ZccTestCase(ZccBaseTestCase):
    """ Tests the compiler driver """
    def setUp(self):
        os.chdir(testdir)

    def tearDown(self):
        os.chdir(testdir)

    def do(self, filenames, imps=[], extra_args=[]):
        basedir = os.path.join('..', 'examples', 'c3')
        arg_list = ['compile']
        arg_list += [os.path.join(basedir, fn) for fn in filenames]
        for fn in imps:
            arg_list.append('-i')
            arg_list.append(os.path.join(basedir, fn))
        arg_list.append('--target')
        arg_list.append('thumb')
        arg_list += extra_args
        self.callZcc(arg_list)


    def testDumpIr(self):
        basedir = os.path.join('..', 'examples', 'c3', 'comments.c3')
        arg_list = ['compile', basedir]
        arg_list.append('--target')
        arg_list.append('thumb')
        self.callZcc(arg_list)

    def testKernel(self):
        """ Build kernel using zcc: """
        recipe = os.path.join(testdir, '..', 'kernel', 'recipe.yaml')
        self.buildRecipe(recipe)

    @unittest.skip('Too difficult to fix')
    def testKernelBuildsEqualTwice(self):
        """ Build kernel two times and check the output is equal """
        recipe = os.path.join(testdir, '..', 'kernel', 'recipe.yaml')
        bin_filename = os.path.join(testdir, '..', 'kernel', 'kernel.bin')
        self.buildRecipe(recipe)
        with open(bin_filename, 'rb') as f:
            a = f.read()
        self.buildRecipe(recipe)
        with open(bin_filename, 'rb') as f:
            b = f.read()
        self.assertSequenceEqual(a, b)

    def testUser(self):
        """ Build userspace using zcc: """
        recipe = os.path.join(testdir, '..', 'user', 'recipe.yaml')
        self.buildRecipe(recipe)

    def testBurn2(self):
        self.do(['burn2.c3'], ['stm32f4xx.c3'])

    def testBurn2_recipe(self):
        recipe = os.path.join(testdir, '..', 'examples', 'c3', 'recipe.yaml')
        self.buildRecipe(recipe)

    @unittest.skip('Skip because of logfile')
    def testBurn2WithLogging(self):
        self.do(['burn2.c3'], ['stm32f4xx.c3'], extra_args=['--report', 'x.rst'])

    def testCommentsExample(self):
        self.do(['comments.c3'])

    def testCast(self):
        self.do(['cast.c3'])

    def testFunctions(self):
        self.do(['functions.c3'])

    @unittest.skip('Revise this test')
    def testSectionAddress(self):
        src = """module tst;
            function void t2() {var int t3; t3 = 2;}
        """
        f = io.StringIO(src)
        out = ObjectFile()
        tg = target_list.armtarget
        tr = ppci.tasks.TaskRunner()
        tr.add_task(ppci.buildtasks.Compile([f], [], tg, out))
        tr.run_tasks()
        code = out.get_section('code')
        self.assertEqual(0x0, code.address)


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