~ubuntu-branches/ubuntu/utopic/dogtail/utopic

« back to all changes in this revision

Viewing changes to dogtail/tc.py

  • Committer: Bazaar Package Importer
  • Author(s): Daniel Holbach
  • Date: 2006-12-21 13:33:47 UTC
  • mfrom: (1.2.1 upstream)
  • mto: This revision was merged to the branch mainline in revision 5.
  • Revision ID: james.westby@ubuntu.com-20061221133347-xo9jg11afp5plcka
Tags: upstream-0.6.1
Import upstream version 0.6.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
12
12
import datetime
13
13
import os.path
14
14
from config import config
15
 
from logging import LogWriter, TimeStamp
 
15
from logging import ResultsLogger, TimeStamp, debugLogger
16
16
from errors import DependencyNotFoundError
17
17
 
18
18
 
19
19
class TC:
20
 
        """
21
 
        The Test Case Superclass
22
 
        """
23
 
        writer = LogWriter()
24
 
        def __init__(self):
25
 
                self.encoding = config.encoding
26
 
                # ascii + unicode. 8 bit extended char has been ripped out
27
 
                self.supportedtypes = ("ascii", "utf-8", "utf-16", "utf-16-be", "utf-16-le", "unicode-escape", "raw-unicode-escape",
28
 
                "big5", "gb18030", "eucJP", "eucKR", "shiftJIS")
29
 
 
30
 
        # String comparison function
31
 
        def compare(self, label, baseline, undertest, encoding=config.encoding):
32
 
                """
33
 
                Compares 2 strings to see if they are the same. The user may specify
34
 
                the encoding to which the two strings are to be normalized for the
35
 
                comparison. Default encoding is the default system encoding. 
36
 
                Normalization to extended 8 bit charactersets is not supported.
37
 
 
38
 
                When the origin of either baseline or undertest is a text file whose 
39
 
                encoding is something other than ASCII, it is necessary to use 
40
 
                codecs.open() instead of open(), so the file's encoding may be 
41
 
                specified.
42
 
                """
43
 
                self.label = label.strip()
44
 
                self.baseline = baseline
45
 
                self.undertest = undertest
46
 
                for string in [self.baseline, self.undertest]:
47
 
                        try: string = unicode(string, 'utf-8')
48
 
                        except TypeError: pass
49
 
                self.encoding = encoding
50
 
 
51
 
                # Normalize the encoding type for the comparaison based on self.encoding
52
 
                if self.encoding in self.supportedtypes:
53
 
                        self.baseline = (self.baseline).encode(self.encoding)
54
 
                        self.undertest = (self.undertest).encode(self.encoding)
55
 
                        # Compare the strings
56
 
                        if self.baseline == self.undertest:
57
 
                                self.result = {self.label: "Passed"}
58
 
                        else:
59
 
                                self.result = {self.label: "Failed - " + self.encoding + " strings do not match. " + self.baseline + " expected: Got " + self.undertest}
60
 
                        # Pass the test result to the LogWriter for writing
61
 
                        TC.writer.writeResult(self.result)
62
 
                        return self.result
63
 
 
64
 
                else:
65
 
                        # We should probably raise an exception here
66
 
                        self.result = {self.label: "ERROR - " + self.encoding + " is not a supported encoding type"}
67
 
                        TC.writer.writeResult(self.result)
68
 
                        return self.result
69
 
 
 
20
    """
 
21
    The Test Case Superclass
 
22
    """
 
23
    logger = ResultsLogger()
 
24
    def __init__(self):
 
25
        self.encoding = config.encoding
 
26
        # ascii + unicode. 8 bit extended char has been ripped out
 
27
        self.supportedtypes = ("ascii", "utf-8", "utf-16", "utf-16-be", "utf-16-le", "unicode-escape", "raw-unicode-escape",
 
28
        "big5", "gb18030", "eucJP", "eucKR", "shiftJIS")
 
29
 
 
30
    # String comparison function
 
31
    def compare(self, label, baseline, undertest, encoding=config.encoding):
 
32
        """
 
33
        Compares 2 strings to see if they are the same. The user may specify
 
34
        the encoding to which the two strings are to be normalized for the
 
35
        comparison. Default encoding is the default system encoding.
 
36
        Normalization to extended 8 bit charactersets is not supported.
 
37
 
 
38
        When the origin of either baseline or undertest is a text file whose
 
39
        encoding is something other than ASCII, it is necessary to use
 
40
        codecs.open() instead of open(), so the file's encoding may be
 
41
        specified.
 
42
        """
 
43
        self.label = label.strip()
 
44
        self.baseline = baseline
 
45
        self.undertest = undertest
 
46
        for string in [self.baseline, self.undertest]:
 
47
            try: string = unicode(string, 'utf-8')
 
48
            except TypeError: pass
 
49
        self.encoding = encoding
 
50
 
 
51
        # Normalize the encoding type for the comparaison based on self.encoding
 
52
        if self.encoding in self.supportedtypes:
 
53
            self.baseline = (self.baseline).encode(self.encoding)
 
54
            self.undertest = (self.undertest).encode(self.encoding)
 
55
            # Compare the strings
 
56
            if self.baseline == self.undertest:
 
57
                self.result = {self.label: "Passed"}
 
58
            else:
 
59
                self.result = {self.label: "Failed - " + self.encoding + " strings do not match. " + self.baseline + " expected: Got " + self.undertest}
 
60
            # Pass the test result to the ResultsLogger for writing
 
61
            TC.logger.log(self.result)
 
62
            return self.result
 
63
 
 
64
        else:
 
65
            # We should probably raise an exception here
 
66
            self.result = {self.label: "ERROR - " + self.encoding + " is not a supported encoding type"}
 
67
            TC.logger.log(self.result)
 
68
            return self.result
 
69
 
70
70
 
71
71
# String Test Case subclass
72
72
class TCString(TC):
73
 
        """
74
 
        String Test Case Class
75
 
        """
76
 
        def __init__(self):
77
 
                TC.__init__(self)
 
73
    """
 
74
    String Test Case Class
 
75
    """
 
76
    def __init__(self):
 
77
        TC.__init__(self)
78
78
 
79
79
# Image test case subclass
80
80
class TCImage(TC):
81
 
        """
82
 
        Image Test Case Class.
83
 
        """
84
 
        IMVersion = ''
85
 
        
86
 
        def __init__(self):
87
 
                TC.__init__(self)
88
 
                self.supportedmetrics = ("MAE", "MSE", "PSE", "PSNR","RMSE", "none")
89
 
                self.scratchDir = config.scratchDir
90
 
                self.deldfile = 0
91
 
 
92
 
                # Get the ImageMagick version by parsing its '-version' output.
93
 
                IMVer = os.popen('compare -version').readline()
94
 
                IMVer = re.match('Version: ImageMagick ([0-9\.]+) .*', IMVer)
95
 
                if IMVer is not None: 
96
 
                        IMVer = IMVer.groups()[0]
97
 
                        TCImage.IMVersion = IMVer
98
 
                else:
99
 
                        raise DependencyNotFoundError, "ImageMagick"
100
 
 
101
 
        # Use ImageMagick to compare 2 files
102
 
        def compare(self, label, baseline, undertest, dfile='default', metric='none', threshold=0.0):
103
 
                """
104
 
                Calls ImageMagick's "compare" program. Default compares are based on
105
 
        size but metric based comparisons are also supported with a threshold
106
 
        determining pass/fail criteria.
107
 
                """
108
 
                self.label = label.strip()
109
 
                self.baseline = baseline.strip()
110
 
                self.undertest = undertest.strip()
111
 
                self.difference = 0.0
112
 
                self.metric = metric.strip()
113
 
                self.threshold = threshold
114
 
 
115
 
                # Create a default filename and flag it for deletion
116
 
                if dfile == "default":
117
 
                        x = TimeStamp()
118
 
                        # Remove all whitespace from the label since IM chokes on it
119
 
                        splabel = label.split(" ")
120
 
                        label = "".join(splabel)
121
 
                        self.dfile = self.scratchDir + x.fileStamp(label)
122
 
                        self.deldfile = 1
123
 
                else: # or use the supplied one with no deletion
124
 
                        self.dfile = dfile
125
 
                        self.deldfile = 0
126
 
 
127
 
                # Check to see if the metric type is supported
128
 
                if self.metric in self.supportedmetrics:
129
 
                        # This is a bit convoluted and will be until IM completes
130
 
                        # implementation of command line chaining. This should be put into
131
 
                        # a try also munge together our IM call chain, if we're not doing
132
 
                        # numeric metrics
133
 
                        if self.metric == "none":
134
 
                                # Build the comparison string. Redirect STDERR to STDOUT;
135
 
                                # IM writes to STDERR and cmd reads STDOUT
136
 
                                cmd = ("compare " + self.baseline + " " + self.undertest + " " + self.dfile + " " + "2>&1")
137
 
                                # os.popen returns a list; if it is empty then we have passed
138
 
                                answer = os.popen(cmd).readlines()
139
 
                                if answer == []:
140
 
                                        self.result = {self.label: "Passed - Images are the same size"}
141
 
                                else:
142
 
                                        fanswer = answer[0].strip()
143
 
                                        self.result = {self.label: "Failed - " + fanswer}
144
 
                                TC.writer.writeResult(self.result)
145
 
                                return self.result
146
 
                        else: # otherwise run the metric code
147
 
                                # Build the cmd
148
 
                                cmd = ("compare -metric " + self.metric + " " + self.baseline + " " + self.undertest + " " + self.dfile + " " + "2>&1")
149
 
                                answer = os.popen(cmd).readlines()
150
 
 
151
 
                                # We need to check if the metric comparison failed. Unfortunately we
152
 
                                # can only tell this by checking the length of the output of the 
153
 
                                # command. More unfortunately, ImageMagic changed the length of the
154
 
                                # output at version 6.2.4, so we have to work around that.
155
 
                                metricFailed = True
156
 
                                IMVersion = TCImage.IMVersion
157
 
                                if IMVersion <= '6.2.3' and len(answer) == 1: metricFailed = False
158
 
                                if IMVersion >= '6.2.4' and len(answer) != 1: metricFailed = False
159
 
                                if metricFailed:
160
 
                                        fanswer = answer[0]
161
 
                                        self.result = {self.label: "Failed - " + fanswer}
162
 
                                else: # grab the metric from answer and convert it to a number
163
 
                                        fanswer = answer[0].strip()
164
 
                                        fanswer = fanswer.split(" ")
165
 
                                        fanswer = fanswer[0]
166
 
                                        fanswer = float(fanswer)
167
 
 
168
 
                                        if fanswer == float("inf"): #same under PSNR returns inf dB:
169
 
                                                self.result = {self.label: "Passed - " + "compare results: " + str(fanswer) + " dB"}
170
 
                                        elif fanswer > self.threshold:
171
 
                                                excess = fanswer - self.threshold
172
 
                                                self.result = {self.label: "Failed - " + "compare result exceeds threshold by: " + str(excess) + " dB"}
173
 
                                        else:
174
 
                                                under = self.threshold - fanswer
175
 
                                                self.result = {self.label: "Passed - " + "compare results under threshold by: " + str(under) + " dB"}
176
 
                                TC.writer.writeResult(self.result)
177
 
                                return self.result
178
 
 
179
 
                        # delete the composite image file if self.dfile is default
180
 
                        if self.deldfile == 1:
181
 
                                try:
182
 
                                        os.remove(self.dfile)
183
 
                                except IOError:
184
 
                                        print "Could not delete tempfile " + self.dfile
185
 
 
186
 
                else: # unsupported metric given
187
 
                        self.result = {self.label: "Failed - " + self.metric + " is not in the list of supported metrics"}
188
 
                        TC.writer.writeResult(self.result)
189
 
                        return self.result
 
81
    """
 
82
    Image Test Case Class.
 
83
    """
 
84
    IMVersion = ''
 
85
 
 
86
    def __init__(self):
 
87
        TC.__init__(self)
 
88
        self.supportedmetrics = ("MAE", "MSE", "PSE", "PSNR","RMSE", "none")
 
89
        self.scratchDir = config.scratchDir
 
90
        self.deldfile = 0
 
91
 
 
92
        # Get the ImageMagick version by parsing its '-version' output.
 
93
        IMVer = os.popen('compare -version').readline()
 
94
        IMVer = re.match('Version: ImageMagick ([0-9\.]+) .*', IMVer)
 
95
        if IMVer is not None:
 
96
            IMVer = IMVer.groups()[0]
 
97
            TCImage.IMVersion = IMVer
 
98
        else:
 
99
            raise DependencyNotFoundError, "ImageMagick"
 
100
 
 
101
    # Use ImageMagick to compare 2 files
 
102
    def compare(self, label, baseline, undertest, dfile='default', metric='none', threshold=0.0):
 
103
        """
 
104
        Calls ImageMagick's "compare" program. Default compares are based on
 
105
size but metric based comparisons are also supported with a threshold
 
106
determining pass/fail criteria.
 
107
        """
 
108
        self.label = label.strip()
 
109
        self.baseline = baseline.strip()
 
110
        self.undertest = undertest.strip()
 
111
        self.difference = 0.0
 
112
        self.metric = metric.strip()
 
113
        self.threshold = threshold
 
114
 
 
115
        # Create a default filename and flag it for deletion
 
116
        if dfile == "default":
 
117
            x = TimeStamp()
 
118
            # Remove all whitespace from the label since IM chokes on it
 
119
            splabel = label.split(" ")
 
120
            label = "".join(splabel)
 
121
            self.dfile = self.scratchDir + x.fileStamp(label)
 
122
            self.deldfile = 1
 
123
        else: # or use the supplied one with no deletion
 
124
            self.dfile = dfile
 
125
            self.deldfile = 0
 
126
 
 
127
        # Check to see if the metric type is supported
 
128
        if self.metric in self.supportedmetrics:
 
129
            # This is a bit convoluted and will be until IM completes
 
130
            # implementation of command line chaining. This should be put into
 
131
            # a try also munge together our IM call chain, if we're not doing
 
132
            # numeric metrics
 
133
            if self.metric == "none":
 
134
                # Build the comparison string. Redirect STDERR to STDOUT;
 
135
                # IM writes to STDERR and cmd reads STDOUT
 
136
                cmd = ("compare " + self.baseline + " " + self.undertest + " " + self.dfile + " " + "2>&1")
 
137
                # os.popen returns a list; if it is empty then we have passed
 
138
                answer = os.popen(cmd).readlines()
 
139
                if answer == []:
 
140
                    self.result = {self.label: "Passed - Images are the same size"}
 
141
                else:
 
142
                    fanswer = answer[0].strip()
 
143
                    self.result = {self.label: "Failed - " + fanswer}
 
144
                TC.logger.log(self.result)
 
145
                return self.result
 
146
            else: # otherwise run the metric code
 
147
                # Build the cmd
 
148
                cmd = ("compare -metric " + self.metric + " " + self.baseline + " " + self.undertest + " " + self.dfile + " " + "2>&1")
 
149
                answer = os.popen(cmd).readlines()
 
150
 
 
151
                # We need to check if the metric comparison failed. Unfortunately we
 
152
                # can only tell this by checking the length of the output of the
 
153
                # command. More unfortunately, ImageMagic changed the length of the
 
154
                # output at version 6.2.4, so we have to work around that.
 
155
                metricFailed = True
 
156
                IMVersion = TCImage.IMVersion
 
157
                if IMVersion <= '6.2.3' and len(answer) == 1: metricFailed = False
 
158
                if IMVersion >= '6.2.4' and len(answer) != 1: metricFailed = False
 
159
                if metricFailed:
 
160
                    fanswer = answer[0]
 
161
                    self.result = {self.label: "Failed - " + fanswer}
 
162
                else: # grab the metric from answer and convert it to a number
 
163
                    fanswer = answer[0].strip()
 
164
                    fanswer = fanswer.split(" ")
 
165
                    fanswer = fanswer[0]
 
166
                    fanswer = float(fanswer)
 
167
 
 
168
                    if fanswer == float("inf"): #same under PSNR returns inf dB:
 
169
                        self.result = {self.label: "Passed - " + "compare results: " + str(fanswer) + " dB"}
 
170
                    elif fanswer > self.threshold:
 
171
                        excess = fanswer - self.threshold
 
172
                        self.result = {self.label: "Failed - " + "compare result exceeds threshold by: " + str(excess) + " dB"}
 
173
                    else:
 
174
                        under = self.threshold - fanswer
 
175
                        self.result = {self.label: "Passed - " + "compare results under threshold by: " + str(under) + " dB"}
 
176
                TC.logger.log(self.result)
 
177
                return self.result
 
178
 
 
179
            # delete the composite image file if self.dfile is default
 
180
            if self.deldfile == 1:
 
181
                try:
 
182
                    os.remove(self.dfile)
 
183
                except IOError:
 
184
                    debugLogger.log("Could not delete tempfile " + self.dfile)
 
185
 
 
186
        else: # unsupported metric given
 
187
            self.result = {self.label: "Failed - " + self.metric + " is not in the list of supported metrics"}
 
188
            TC.logger.log(self.result)
 
189
            return self.result
190
190
 
191
191
 
192
192
 
193
193
class TCNumber(TC):
194
 
        """
195
 
        Number Comparaison Test Case Class
196
 
        """
197
 
        def __init__(self):
198
 
                TC.__init__(self)
199
 
                self.supportedtypes = ("int", "long", "float", "complex", "oct", "hex")
200
 
 
201
 
        # Compare 2 numbers by the type provided in the type arg
202
 
        def compare(self, label, baseline, undertest, type):
203
 
                """
204
 
                Compares 2 numbers to see if they are the same. The user may specify
205
 
                how to normalize mixed type comparisons via the type argument.
206
 
                """
207
 
                self.label = label.strip()
208
 
                self.baseline = baseline
209
 
                self.undertest = undertest
210
 
                self.type = type.strip()
211
 
 
212
 
                # If we get a valid type, convert to that type and compare
213
 
                if self.type in self.supportedtypes:
214
 
                        # Normalize for comparison
215
 
                        if self.type == "int":
216
 
                                self.baseline = int(self.baseline)
217
 
                                self.undertest = int(self.undertest)
218
 
                        elif self.type == "long":
219
 
                                self.baseline = long(self.baseline)
220
 
                                self.undertest = long(self.undertest)
221
 
                        elif self.type == "float":
222
 
                                self.baseline = float(self.baseline)
223
 
                                self.undertest = float(self.undertest)
224
 
                        else:
225
 
                                self.baseline = complex(self.baseline)
226
 
                                self.undertest = complex(self.undertest)
227
 
 
228
 
                        #compare 
229
 
                        if self.baseline == self.undertest:
230
 
                                self.result = {self.label: "Passed - numbers are the same"}
231
 
                        else:
232
 
                                self.result = {self.label: "Failed - " + str(self.baseline) + " expected: Got " + str(self.undertest)}
233
 
                        TC.writer.writeResult(self.result)
234
 
                        return self.result
235
 
                else:
236
 
                        self.result = {self.label: "Failed - " + self.type + " is not in list of supported types"}
237
 
                        TC.writer.writeResult(self.result)
238
 
                        return self.result
 
194
    """
 
195
    Number Comparaison Test Case Class
 
196
    """
 
197
    def __init__(self):
 
198
        TC.__init__(self)
 
199
        self.supportedtypes = ("int", "long", "float", "complex", "oct", "hex")
 
200
 
 
201
    # Compare 2 numbers by the type provided in the type arg
 
202
    def compare(self, label, baseline, undertest, type):
 
203
        """
 
204
        Compares 2 numbers to see if they are the same. The user may specify
 
205
        how to normalize mixed type comparisons via the type argument.
 
206
        """
 
207
        self.label = label.strip()
 
208
        self.baseline = baseline
 
209
        self.undertest = undertest
 
210
        self.type = type.strip()
 
211
 
 
212
        # If we get a valid type, convert to that type and compare
 
213
        if self.type in self.supportedtypes:
 
214
            # Normalize for comparison
 
215
            if self.type == "int":
 
216
                self.baseline = int(self.baseline)
 
217
                self.undertest = int(self.undertest)
 
218
            elif self.type == "long":
 
219
                self.baseline = long(self.baseline)
 
220
                self.undertest = long(self.undertest)
 
221
            elif self.type == "float":
 
222
                self.baseline = float(self.baseline)
 
223
                self.undertest = float(self.undertest)
 
224
            else:
 
225
                self.baseline = complex(self.baseline)
 
226
                self.undertest = complex(self.undertest)
 
227
 
 
228
            #compare
 
229
            if self.baseline == self.undertest:
 
230
                self.result = {self.label: "Passed - numbers are the same"}
 
231
            else:
 
232
                self.result = {self.label: "Failed - " + str(self.baseline) + " expected: Got " + str(self.undertest)}
 
233
            TC.logger.log(self.result)
 
234
            return self.result
 
235
        else:
 
236
            self.result = {self.label: "Failed - " + self.type + " is not in list of supported types"}
 
237
            TC.logger.log(self.result)
 
238
            return self.result
239
239
 
240
240
 
241
241
if __name__ == '__main__':
242
 
        # import the test modules
243
 
        import codecs
244
 
        from utils import *
245
 
 
246
 
        # Set up vars to use to test TC class
247
 
        baseline = "test"
248
 
        undertest = "test"
249
 
        label = "unit test case 1.0"
250
 
        encoding = "utf-8"
251
 
        result = {}
252
 
 
253
 
        # Create the TC instance
254
 
        case1 = TC()
255
 
 
256
 
        # Fire off the compaison
257
 
        result = case1.compare(label, baseline, undertest, encoding)
258
 
 
259
 
        # Print the result - should be label - passed
260
 
        print(result)
261
 
 
262
 
        # Reset variables for failure path
263
 
        undertest = "testy"
264
 
        encoding = "utf-8"
265
 
        result = {}
266
 
        label = "unit test case 1.1"
267
 
 
268
 
        # Compare again
269
 
        result = case1.compare(label, baseline, undertest, encoding)
270
 
 
271
 
        # Print the result - should be label - failure
272
 
        print(result)
273
 
 
274
 
        # Create a TCString instance
275
 
        case2 = TCString()
276
 
 
277
 
        # Load our variables for this test
278
 
        label = " unit test case 2.0"
279
 
        encoding = "utf-8"
280
 
        baseline = u"groß"
281
 
        undertest = u"gro\xc3\xa1"
282
 
        result = {}
283
 
 
284
 
        # Fire off a UTF-8 compare
285
 
        result = case2.compare(label, baseline, undertest, encoding)
286
 
 
287
 
        # Print the result - should be label - passed
288
 
        print(result)
289
 
 
290
 
        # Fire the test for ASCII converted to UTF-8 testing
291
 
        label = " unit test case 2.1"
292
 
        encoding = "utf-8"
293
 
        baseline = "please work"
294
 
        undertest = "please work"
295
 
        result = {}
296
 
 
297
 
        result = case2.compare(label, baseline, undertest, encoding)
298
 
 
299
 
        # Print the result - should be label - passed
300
 
        print(result)
301
 
 
302
 
        # Reset variables for an out of range encoding type
303
 
        label = " unit test case 2.2"
304
 
        encoding = "swahilli"
305
 
        baseline = "please work"
306
 
        undertest = "please work"
307
 
        result = {}
308
 
 
309
 
        result = case2.compare(label, baseline, undertest, encoding)
310
 
 
311
 
        # Print the result - should be label - Error - not supported
312
 
        print(result)
313
 
 
314
 
        # Reset variables for unmatched utf-8 strings 
315
 
        label = " unit test case 2.3"
316
 
        encoding = "utf-8"
317
 
        baseline = u"groß"
318
 
        undertest = "nomatch"
319
 
        result = {}
320
 
 
321
 
#       result = case2.compare(label, baseline, undertest, encoding)
322
 
 
323
 
        # Print the result - should be label - failure
324
 
        print(result)
325
 
 
326
 
        # Reset variables for inherited TC base compare
327
 
        label = " unit test case 2.4"
328
 
        baseline = "This is inhereited"
329
 
        undertest = "This is inhereited"
330
 
        encoding = "utf-8"
331
 
        result = {}
332
 
 
333
 
        result = case2.compare(label, baseline, undertest, encoding)
334
 
 
335
 
        # Print the result - should be label - Passed
336
 
        print(result)
337
 
 
338
 
 
339
 
        # Include real CJKI (UTF-8) charcters for string compare
340
 
        # For this first test case, we are comparing same JA characters
341
 
        label = " unit test case 2.5"
342
 
        encoding = "utf-8"
343
 
        baseline = u"あか"
344
 
        undertest = u"あか"
345
 
        result = {}
346
 
 
347
 
        result = case2.compare(label, baseline, undertest, encoding)
348
 
 
349
 
        # Print the result - should be label - Passed
350
 
        print(result)
351
 
 
352
 
 
353
 
        # Testing different JA characters
354
 
        label = " unit test case 2.6"
355
 
        encoding = "utf-8"
356
 
        baseline = u"あか"
357
 
        undertest = u"元気"
358
 
        result = {}
359
 
 
360
 
        result = case2.compare(label, baseline, undertest, encoding)
361
 
 
362
 
        # Print the result - should be label - Failed
363
 
        print(result)
364
 
 
365
 
 
366
 
 
367
 
        # Test the timestamper class
368
 
        # Create a new timestamp instance
369
 
        # Print the file format time
370
 
        stamp1 = TimeStamp()
371
 
        presently = stamp1.fileStamp("filestamp")
372
 
 
373
 
        # Print - should be filenameYYYYMMDD with local systems date
374
 
        print presently
375
 
 
376
 
        # Make a stamp entry
377
 
        entry = stamp1.entryStamp()
378
 
 
379
 
        # Print the entrystamp - should be YYYY-MM-DD_HH:MM:SS with local system time
380
 
        print entry
381
 
 
382
 
        # Test to see that image compares work - this a simple compare with defaults
383
 
        # Load our variabes
384
 
        label = "unit test case 3.0"
385
 
        baseline = "../examples/data/GNOME-Street.png"
386
 
        undertest = "../examples/data/GNOME-Street1.png"
387
 
        result = {}
388
 
 
389
 
        # Create a TCImage instance
390
 
        case3 = TCImage()
391
 
 
392
 
        # Fire off the compare
393
 
        result = case3.compare(label, baseline, undertest)
394
 
 
395
 
        # Print the result Should be label - Passed - Images are same size
396
 
        print result
397
 
 
398
 
        # Default compare with different images (the sizes differ)
399
 
        label = "unit test case 3.1"
400
 
        baseline = "../examples/data/GNOME-Street.png"
401
 
        undertest = "../examples/data/g-star.png"
402
 
        result = {}
403
 
 
404
 
        # Fire off the compare
405
 
        result = case3.compare(label, baseline, undertest)
406
 
 
407
 
        # Print the result Should be label - Failied compare:Images differ in size
408
 
        print result
409
 
 
410
 
        # Image compare pass with the metrics option
411
 
        label = "unit test case 3.2"
412
 
        baseline = "../examples/data/GNOME-Street.png"
413
 
        undertest = "../examples/data/GNOME-Street1.png"
414
 
        result = {}
415
 
        metrics = ("MAE", "MSE", "PSE", "PSNR"," RMSE")
416
 
 
417
 
        for i in range(len(metrics)):
418
 
                result = case3.compare(label, baseline, undertest, metric=metrics[i])
419
 
                print metrics[i]
420
 
                print result
421
 
 
422
 
        # Image compare fail metrics
423
 
        label = "unit test case 3.3"
424
 
        baseline = "../examples/data/g-star.png"
425
 
        undertest = "../examples/data/g-star1.png"
426
 
        result = {}
427
 
        metrics = ("MAE", "MSE", "PSE", "PSNR"," RMSE")
428
 
 
429
 
        for i in range(len(metrics)):
430
 
                result = case3.compare(label, baseline, undertest, metric=metrics[i])
431
 
                print metrics[i]
432
 
                print result
433
 
 
434
 
        # Image comapre threshold metrics - only PNSR should pass
435
 
        label = "unit test case 3.4"
436
 
        baseline = "../examples/data/g-star.png"
437
 
        undertest = "../examples/data/g-star1.png"
438
 
        result = {}
439
 
        metrics = ("MAE", "MSE", "PSE", "PSNR"," RMSE")
440
 
        bound = 5
441
 
 
442
 
        for i in range(len(metrics)):
443
 
                result = case3.compare(label, baseline, undertest, metric=metrics[i], threshold=bound)
444
 
                print metrics[i]
445
 
                print result
446
 
 
447
 
        # Bogus metric test
448
 
        label = "unit test case 3.5"
449
 
        baseline = "../examples/data/g-star.png"
450
 
        undertest = "../examples/data/g-star1.png"
451
 
        result = {}
452
 
        metrics = "Guess"
453
 
 
454
 
        result = case3.compare(label, baseline, undertest, metric=metrics)
455
 
 
456
 
        # Should be failed - metric unsupported
457
 
        print result
458
 
 
459
 
        # Number comparison tests
460
 
        label = "unit test case 4.0"
461
 
        baseline = 42
462
 
        undertest = 42
463
 
        type = "int"
464
 
        result = {}
465
 
 
466
 
        # Make a TCNumber instance
467
 
        case4 = TCNumber()
468
 
 
469
 
        # Make a simple int compare
470
 
        result = case4.compare(label, baseline, undertest, type)
471
 
 
472
 
        # Should be Passed
473
 
        print result
474
 
 
475
 
        # Now make the int fail
476
 
        label = "unit test case 4.1"
477
 
        undertest = 999
478
 
        result = {}
479
 
 
480
 
        result = case4.compare(label, baseline, undertest, type)
481
 
 
482
 
        # Should fail
483
 
        print result
484
 
 
485
 
        # Now long pass
486
 
        label = "unit test case 4.2"
487
 
        baseline = 1112223334445556667778889990L
488
 
        undertest = 1112223334445556667778889990L
489
 
        type = "long"
490
 
        result = {}
491
 
 
492
 
        result = case4.compare(label, baseline, undertest, type)
493
 
 
494
 
        # Should pass
495
 
        print result
496
 
 
497
 
        # Float Pass
498
 
        label = "unit test case 4.3"
499
 
        baseline = 99.432670
500
 
        undertest = 99.432670
501
 
        type = "float"
502
 
        result = {}
503
 
 
504
 
        result = case4.compare(label, baseline, undertest, type)
505
 
 
506
 
        # Should pass
507
 
        print result
508
 
 
509
 
        # Complex pass
510
 
        label = "unit test case 4.4"
511
 
        baseline = 67+3j
512
 
        undertest = 67+3j
513
 
        type = "complex"
514
 
        result = {}
515
 
 
516
 
        result = case4.compare(label, baseline, undertest, type)
517
 
 
518
 
        # Should pass
519
 
        print result
520
 
 
521
 
        # Octal pass
522
 
        label = "unit test case 4.5"
523
 
        baseline = 0400
524
 
        undertest = 0400
525
 
        type = "oct"
526
 
        result = {}
527
 
 
528
 
        result = case4.compare(label, baseline, undertest, type)
529
 
 
530
 
        # Should pass
531
 
        print result
532
 
 
533
 
        # Hex pass
534
 
        label = "unit test case 4.6"
535
 
        baseline = 0x100
536
 
        undertest = 0x100
537
 
        type = "hex"
538
 
        result = {}
539
 
 
540
 
        result = case4.compare(label, baseline, undertest, type)
541
 
 
542
 
        # Should pass
543
 
        print result
544
 
 
545
 
        # Conversion pass - pass in equivalent hex and octal but compare as int
546
 
        label = "unit test case 4.7"
547
 
        baseline = 0x100
548
 
        undertest = 0400
549
 
        type = "int"
550
 
        result = {}
551
 
 
552
 
        result = case4.compare(label, baseline, undertest, type)
553
 
 
554
 
        # Should pass
555
 
        print result
556
 
 
557
 
        # Give a bogus type
558
 
        label = "unit test case 4.8"
559
 
        type = "face"
560
 
        result = {}
561
 
 
562
 
        result = case4.compare(label, baseline, undertest, type)
563
 
 
564
 
        # Should fail - unsupported type
565
 
        print result
566
 
 
 
242
    # import the test modules
 
243
    import codecs
 
244
    from utils import *
 
245
 
 
246
    # Set up vars to use to test TC class
 
247
    baseline = "test"
 
248
    undertest = "test"
 
249
    label = "unit test case 1.0"
 
250
    encoding = "utf-8"
 
251
    result = {}
 
252
 
 
253
    # Create the TC instance
 
254
    case1 = TC()
 
255
 
 
256
    # Fire off the compaison
 
257
    result = case1.compare(label, baseline, undertest, encoding)
 
258
 
 
259
    # Print the result - should be label - passed
 
260
    print(result)
 
261
 
 
262
    # Reset variables for failure path
 
263
    undertest = "testy"
 
264
    encoding = "utf-8"
 
265
    result = {}
 
266
    label = "unit test case 1.1"
 
267
 
 
268
    # Compare again
 
269
    result = case1.compare(label, baseline, undertest, encoding)
 
270
 
 
271
    # Print the result - should be label - failure
 
272
    print(result)
 
273
 
 
274
    # Create a TCString instance
 
275
    case2 = TCString()
 
276
 
 
277
    # Load our variables for this test
 
278
    label = " unit test case 2.0"
 
279
    encoding = "utf-8"
 
280
    baseline = u"groß"
 
281
    undertest = u"gro\xc3\xa1"
 
282
    result = {}
 
283
 
 
284
    # Fire off a UTF-8 compare
 
285
    result = case2.compare(label, baseline, undertest, encoding)
 
286
 
 
287
    # Print the result - should be label - passed
 
288
    print(result)
 
289
 
 
290
    # Fire the test for ASCII converted to UTF-8 testing
 
291
    label = " unit test case 2.1"
 
292
    encoding = "utf-8"
 
293
    baseline = "please work"
 
294
    undertest = "please work"
 
295
    result = {}
 
296
 
 
297
    result = case2.compare(label, baseline, undertest, encoding)
 
298
 
 
299
    # Print the result - should be label - passed
 
300
    print(result)
 
301
 
 
302
    # Reset variables for an out of range encoding type
 
303
    label = " unit test case 2.2"
 
304
    encoding = "swahilli"
 
305
    baseline = "please work"
 
306
    undertest = "please work"
 
307
    result = {}
 
308
 
 
309
    result = case2.compare(label, baseline, undertest, encoding)
 
310
 
 
311
    # Print the result - should be label - Error - not supported
 
312
    print(result)
 
313
 
 
314
    # Reset variables for unmatched utf-8 strings
 
315
    label = " unit test case 2.3"
 
316
    encoding = "utf-8"
 
317
    baseline = u"groß"
 
318
    undertest = "nomatch"
 
319
    result = {}
 
320
 
 
321
#       result = case2.compare(label, baseline, undertest, encoding)
 
322
 
 
323
    # Print the result - should be label - failure
 
324
    print(result)
 
325
 
 
326
    # Reset variables for inherited TC base compare
 
327
    label = " unit test case 2.4"
 
328
    baseline = "This is inhereited"
 
329
    undertest = "This is inhereited"
 
330
    encoding = "utf-8"
 
331
    result = {}
 
332
 
 
333
    result = case2.compare(label, baseline, undertest, encoding)
 
334
 
 
335
    # Print the result - should be label - Passed
 
336
    print(result)
 
337
 
 
338
 
 
339
    # Include real CJKI (UTF-8) charcters for string compare
 
340
    # For this first test case, we are comparing same JA characters
 
341
    label = " unit test case 2.5"
 
342
    encoding = "utf-8"
 
343
    baseline = u"あか"
 
344
    undertest = u"あか"
 
345
    result = {}
 
346
 
 
347
    result = case2.compare(label, baseline, undertest, encoding)
 
348
 
 
349
    # Print the result - should be label - Passed
 
350
    print(result)
 
351
 
 
352
 
 
353
    # Testing different JA characters
 
354
    label = " unit test case 2.6"
 
355
    encoding = "utf-8"
 
356
    baseline = u"あか"
 
357
    undertest = u"元気"
 
358
    result = {}
 
359
 
 
360
    result = case2.compare(label, baseline, undertest, encoding)
 
361
 
 
362
    # Print the result - should be label - Failed
 
363
    print(result)
 
364
 
 
365
 
 
366
 
 
367
    # Test the timestamper class
 
368
    # Create a new timestamp instance
 
369
    # Print the file format time
 
370
    stamp1 = TimeStamp()
 
371
    presently = stamp1.fileStamp("filestamp")
 
372
 
 
373
    # Print - should be filenameYYYYMMDD with local systems date
 
374
    print presently
 
375
 
 
376
    # Make a stamp entry
 
377
    entry = stamp1.entryStamp()
 
378
 
 
379
    # Print the entrystamp - should be YYYY-MM-DD_HH:MM:SS with local system time
 
380
    print entry
 
381
 
 
382
    # Test to see that image compares work - this a simple compare with defaults
 
383
    # Load our variabes
 
384
    label = "unit test case 3.0"
 
385
    baseline = "../examples/data/20w.png"
 
386
    undertest = "../examples/data/20b.png"
 
387
    result = {}
 
388
 
 
389
    # Create a TCImage instance
 
390
    case3 = TCImage()
 
391
 
 
392
    # Fire off the compare
 
393
    result = case3.compare(label, baseline, undertest)
 
394
 
 
395
    # Print the result Should be label - Passed - Images are same size
 
396
    print result
 
397
 
 
398
    # Default compare with different images (the sizes differ)
 
399
    label = "unit test case 3.1"
 
400
    baseline = "../examples/data/20w.png"
 
401
    undertest = "../examples/data/10w.png"
 
402
    result = {}
 
403
 
 
404
    # Fire off the compare
 
405
    result = case3.compare(label, baseline, undertest)
 
406
 
 
407
    # Print the result Should be label - Failied compare:Images differ in size
 
408
    print result
 
409
 
 
410
    # Image compare pass with the metrics option
 
411
    label = "unit test case 3.2"
 
412
    baseline = "../examples/data/20w.png"
 
413
    undertest = "../examples/data/20b.png"
 
414
    result = {}
 
415
    metrics = ("MAE", "MSE", "PSE", "PSNR"," RMSE")
 
416
 
 
417
    for i in range(len(metrics)):
 
418
        result = case3.compare(label, baseline, undertest, metric=metrics[i])
 
419
        print metrics[i]
 
420
        print result
 
421
 
 
422
    # Image compare fail metrics
 
423
    label = "unit test case 3.3"
 
424
    baseline = "../examples/data/10w.png"
 
425
    undertest = "../examples/data/10b.png"
 
426
    result = {}
 
427
    metrics = ("MAE", "MSE", "PSE", "PSNR"," RMSE")
 
428
 
 
429
    for i in range(len(metrics)):
 
430
        result = case3.compare(label, baseline, undertest, metric=metrics[i])
 
431
        print metrics[i]
 
432
        print result
 
433
 
 
434
    # Image comapre threshold metrics - only PNSR should pass
 
435
    label = "unit test case 3.4"
 
436
    baseline = "../examples/data/10w.png"
 
437
    undertest = "../examples/data/10b.png"
 
438
    result = {}
 
439
    metrics = ("MAE", "MSE", "PSE", "PSNR"," RMSE")
 
440
    bound = 5
 
441
 
 
442
    for i in range(len(metrics)):
 
443
        result = case3.compare(label, baseline, undertest, metric=metrics[i], threshold=bound)
 
444
        print metrics[i]
 
445
        print result
 
446
 
 
447
    # Bogus metric test
 
448
    label = "unit test case 3.5"
 
449
    baseline = "../examples/data/10w.png"
 
450
    undertest = "../examples/data/10b.png"
 
451
    result = {}
 
452
    metrics = "Guess"
 
453
 
 
454
    result = case3.compare(label, baseline, undertest, metric=metrics)
 
455
 
 
456
    # Should be failed - metric unsupported
 
457
    print result
 
458
 
 
459
    # Number comparison tests
 
460
    label = "unit test case 4.0"
 
461
    baseline = 42
 
462
    undertest = 42
 
463
    type = "int"
 
464
    result = {}
 
465
 
 
466
    # Make a TCNumber instance
 
467
    case4 = TCNumber()
 
468
 
 
469
    # Make a simple int compare
 
470
    result = case4.compare(label, baseline, undertest, type)
 
471
 
 
472
    # Should be Passed
 
473
    print result
 
474
 
 
475
    # Now make the int fail
 
476
    label = "unit test case 4.1"
 
477
    undertest = 999
 
478
    result = {}
 
479
 
 
480
    result = case4.compare(label, baseline, undertest, type)
 
481
 
 
482
    # Should fail
 
483
    print result
 
484
 
 
485
    # Now long pass
 
486
    label = "unit test case 4.2"
 
487
    baseline = 1112223334445556667778889990L
 
488
    undertest = 1112223334445556667778889990L
 
489
    type = "long"
 
490
    result = {}
 
491
 
 
492
    result = case4.compare(label, baseline, undertest, type)
 
493
 
 
494
    # Should pass
 
495
    print result
 
496
 
 
497
    # Float Pass
 
498
    label = "unit test case 4.3"
 
499
    baseline = 99.432670
 
500
    undertest = 99.432670
 
501
    type = "float"
 
502
    result = {}
 
503
 
 
504
    result = case4.compare(label, baseline, undertest, type)
 
505
 
 
506
    # Should pass
 
507
    print result
 
508
 
 
509
    # Complex pass
 
510
    label = "unit test case 4.4"
 
511
    baseline = 67+3j
 
512
    undertest = 67+3j
 
513
    type = "complex"
 
514
    result = {}
 
515
 
 
516
    result = case4.compare(label, baseline, undertest, type)
 
517
 
 
518
    # Should pass
 
519
    print result
 
520
 
 
521
    # Octal pass
 
522
    label = "unit test case 4.5"
 
523
    baseline = 0400
 
524
    undertest = 0400
 
525
    type = "oct"
 
526
    result = {}
 
527
 
 
528
    result = case4.compare(label, baseline, undertest, type)
 
529
 
 
530
    # Should pass
 
531
    print result
 
532
 
 
533
    # Hex pass
 
534
    label = "unit test case 4.6"
 
535
    baseline = 0x100
 
536
    undertest = 0x100
 
537
    type = "hex"
 
538
    result = {}
 
539
 
 
540
    result = case4.compare(label, baseline, undertest, type)
 
541
 
 
542
    # Should pass
 
543
    print result
 
544
 
 
545
    # Conversion pass - pass in equivalent hex and octal but compare as int
 
546
    label = "unit test case 4.7"
 
547
    baseline = 0x100
 
548
    undertest = 0400
 
549
    type = "int"
 
550
    result = {}
 
551
 
 
552
    result = case4.compare(label, baseline, undertest, type)
 
553
 
 
554
    # Should pass
 
555
    print result
 
556
 
 
557
    # Give a bogus type
 
558
    label = "unit test case 4.8"
 
559
    type = "face"
 
560
    result = {}
 
561
 
 
562
    result = case4.compare(label, baseline, undertest, type)
 
563
 
 
564
    # Should fail - unsupported type
 
565
    print result