view test/testzcc.py @ 382:0c44e494ef58

Made lexer more generic
author Windel Bouwman
date Sun, 27 Apr 2014 12:24:21 +0200
parents 9667d78ba79e
children
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__))

def relpath(*args):
    return os.path.join(testdir, *args)


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, targetlist=[]):
        arg_list = ['--buildfile', recipe] + targetlist
        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=[]):
        return
        basedir = relpath('..', '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)

    @unittest.skip('Api change')
    def testDumpIr(self):
        basedir = relpath('..', 'examples', 'c3', 'comments.c3')
        arg_list = ['compile', basedir]
        arg_list.append('--target')
        arg_list.append('thumb')
        self.callZcc(arg_list)

    @unittest.skip('Too hard')
    def testThumbKernel(self):
        """ Build kernel using zcc: """
        recipe = relpath('..', 'kernel', 'thumb.yaml')
        self.buildRecipe(recipe)

    def testArmKernel(self):
        """ Build kernel using zcc: """
        recipe = relpath('..', 'kernel', 'build.xml')
        self.buildRecipe(recipe)

    def testKernelBuildsEqualTwice(self):
        """ Build kernel two times and check the output is equal """
        recipe = relpath('..', 'kernel', 'build.xml')
        bin_filename = relpath('..', 'kernel', 'kernel_arm.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 = relpath('..', 'user', 'build.xml')
        self.buildRecipe(recipe)

    def testBurn2(self):
        recipe = relpath('..', 'examples', 'c3', 'build.xml')
        self.buildRecipe(recipe)

    def test_hello_A9_c3_recipe(self):
        recipe = relpath('..', 'examples', 'qemu_a9_hello', 'build.xml')
        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'])


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