| 1 | import os
 | 
|---|
| 2 | 
 | 
|---|
| 3 | from pybin.tools import *
 | 
|---|
| 4 | 
 | 
|---|
| 5 | import pybin.settings
 | 
|---|
| 6 | import datetime
 | 
|---|
| 7 | 
 | 
|---|
| 8 | from string import Template
 | 
|---|
| 9 | 
 | 
|---|
| 10 | class DeltaTemplate(Template):
 | 
|---|
| 11 |     delimiter = "%"
 | 
|---|
| 12 | 
 | 
|---|
| 13 | def strfdelta(tdelta, fmt):
 | 
|---|
| 14 |     d["H"], rem = divmod(tdelta.seconds, 3600)
 | 
|---|
| 15 |     d["M"], d["S"] = divmod(rem, 60)
 | 
|---|
| 16 |     t = DeltaTemplate(fmt)
 | 
|---|
| 17 |     return t.substitute(**d)
 | 
|---|
| 18 | 
 | 
|---|
| 19 | # Test class that defines what a test is
 | 
|---|
| 20 | class Test:
 | 
|---|
| 21 |         def __init__(self):
 | 
|---|
| 22 |                 self.name = ''
 | 
|---|
| 23 |                 self.path = ''
 | 
|---|
| 24 |                 self.arch = ''
 | 
|---|
| 25 | 
 | 
|---|
| 26 |         def toString(self):
 | 
|---|
| 27 |                 return "{:25s} ({:5s} {:s})".format( self.name, self.arch if self.arch else "Any", self.target() )
 | 
|---|
| 28 | 
 | 
|---|
| 29 |         def prepare(self):
 | 
|---|
| 30 |                 mkdir( (self.output_log(), self.error_log(), self.input()            ) )
 | 
|---|
| 31 |                 rm   ( (self.output_log(), self.error_log(), self.target_executable()) )
 | 
|---|
| 32 | 
 | 
|---|
| 33 |         def expect(self):
 | 
|---|
| 34 |                 return os.path.normpath( os.path.join(settings.SRCDIR  , self.path, ".expect", "%s%s.txt" % (self.name,'' if not self.arch else ".%s" % self.arch)) )
 | 
|---|
| 35 | 
 | 
|---|
| 36 |         def error_log(self):
 | 
|---|
| 37 |                 return os.path.normpath( os.path.join(settings.BUILDDIR, self.path, ".err"   , "%s.log" % self.name) )
 | 
|---|
| 38 | 
 | 
|---|
| 39 |         def output_log(self):
 | 
|---|
| 40 |                 return os.path.normpath( os.path.join(settings.BUILDDIR, self.path, ".out"   , "%s.log" % self.name) )
 | 
|---|
| 41 | 
 | 
|---|
| 42 |         def input(self):
 | 
|---|
| 43 |                 return os.path.normpath( os.path.join(settings.SRCDIR  , self.path, ".in"    , "%s.txt" % self.name) )
 | 
|---|
| 44 | 
 | 
|---|
| 45 |         def target_output(self):
 | 
|---|
| 46 |                 return self.output_log() if not settings.generating else self.expect()
 | 
|---|
| 47 | 
 | 
|---|
| 48 |         def target(self):
 | 
|---|
| 49 |                 return os.path.normpath( os.path.join(self.path, self.name) )
 | 
|---|
| 50 | 
 | 
|---|
| 51 |         def target_executable(self):
 | 
|---|
| 52 |                 return os.path.normpath( os.path.join(settings.BUILDDIR, self.path, self.name) )
 | 
|---|
| 53 | 
 | 
|---|
| 54 |         @classmethod
 | 
|---|
| 55 |         def valid_name(_, name):
 | 
|---|
| 56 |                 return not name.endswith( ('.c', '.cc', '.cpp', '.cfa') )
 | 
|---|
| 57 | 
 | 
|---|
| 58 |         @classmethod
 | 
|---|
| 59 |         def from_target(_, target):
 | 
|---|
| 60 |                 test = Test()
 | 
|---|
| 61 |                 test.name = os.path.basename(target)
 | 
|---|
| 62 |                 test.path = os.path.relpath (os.path.dirname(target), settings.SRCDIR)
 | 
|---|
| 63 |                 test.arch = settings.arch.toString() if settings.arch.cross_compile else ''
 | 
|---|
| 64 |                 return test
 | 
|---|
| 65 | 
 | 
|---|
| 66 | 
 | 
|---|
| 67 | class TestResult:
 | 
|---|
| 68 |         SUCCESS = 0
 | 
|---|
| 69 |         FAILURE = 1
 | 
|---|
| 70 |         TIMEOUT = 124
 | 
|---|
| 71 | 
 | 
|---|
| 72 |         @classmethod
 | 
|---|
| 73 |         def toString( cls, retcode, duration ):
 | 
|---|
| 74 |                 if settings.generating :
 | 
|---|
| 75 |                         if   retcode == TestResult.SUCCESS:     text = "Done   "
 | 
|---|
| 76 |                         elif retcode == TestResult.TIMEOUT:     text = "TIMEOUT"
 | 
|---|
| 77 |                         else :                                          text = "ERROR code %d" % retcode
 | 
|---|
| 78 |                 else :
 | 
|---|
| 79 |                         if   retcode == TestResult.SUCCESS:     text = "PASSED "
 | 
|---|
| 80 |                         elif retcode == TestResult.TIMEOUT:     text = "TIMEOUT"
 | 
|---|
| 81 |                         else :                                          text = "FAILED with code %d" % retcode
 | 
|---|
| 82 | 
 | 
|---|
| 83 |                 text += "    C%s - R%s" % (cls.fmtDur(duration[0]), cls.fmtDur(duration[1]))
 | 
|---|
| 84 |                 return text
 | 
|---|
| 85 | 
 | 
|---|
| 86 |         @classmethod
 | 
|---|
| 87 |         def fmtDur( cls, duration ):
 | 
|---|
| 88 |                 if duration :
 | 
|---|
| 89 |                         hours, rem = divmod(duration, 3600)
 | 
|---|
| 90 |                         minutes, rem = divmod(rem, 60)
 | 
|---|
| 91 |                         seconds, millis = divmod(rem, 1)
 | 
|---|
| 92 |                         return "%2d:%02d.%03d" % (minutes, seconds, millis * 1000)
 | 
|---|
| 93 |                 return " n/a"
 | 
|---|