Back to index

python3.2  3.2.2
Public Member Functions | Public Attributes
test.test_decimal.DecimalTest Class Reference

List of all members.

Public Member Functions

def setUp
def eval_file
def eval_line
def eval_directive
def eval_equation
def getexceptions
def change_precision
def change_rounding_method
def change_min_exponent
def change_max_exponent
def change_clamp

Public Attributes

 context
 ignore_list
 ChangeDict

Detailed Description

Class which tests the Decimal class against the test cases.

Changed for unittest.

Definition at line 202 of file test_decimal.py.


Member Function Documentation

def test.test_decimal.DecimalTest.change_clamp (   self,
  clamp 
)

Definition at line 407 of file test_decimal.py.

00407 
00408     def change_clamp(self, clamp):
00409         self.context.clamp = clamp
00410 
00411 
00412 
00413 # The following classes test the behaviour of Decimal according to PEP 327

Definition at line 405 of file test_decimal.py.

00405 
00406     def change_max_exponent(self, exp):
        self.context.Emax = exp

Definition at line 403 of file test_decimal.py.

00403 
00404     def change_min_exponent(self, exp):
        self.context.Emin = exp

Definition at line 399 of file test_decimal.py.

00399 
00400     def change_precision(self, prec):
        self.context.prec = prec

Definition at line 401 of file test_decimal.py.

00401 
00402     def change_rounding_method(self, rounding):
        self.context.rounding = rounding

Definition at line 254 of file test_decimal.py.

00254 
00255     def eval_directive(self, s):
00256         funct, value = (x.strip().lower() for x in s.split(':'))
00257         if funct == 'rounding':
00258             value = RoundingDict[value]
00259         else:
00260             try:
00261                 value = int(value)
00262             except ValueError:
00263                 pass
00264 
00265         funct = self.ChangeDict.get(funct, Nonfunction)
00266         funct(value)

Here is the caller graph for this function:

Definition at line 267 of file test_decimal.py.

00267 
00268     def eval_equation(self, s):
00269         #global DEFAULT_PRECISION
00270         #print DEFAULT_PRECISION
00271 
00272         if not TEST_ALL and random.random() < 0.90:
00273             return
00274 
00275         try:
00276             Sides = s.split('->')
00277             L = Sides[0].strip().split()
00278             id = L[0]
00279             if DEBUG:
00280                 print("Test ", id, end=" ")
00281             funct = L[1].lower()
00282             valstemp = L[2:]
00283             L = Sides[1].strip().split()
00284             ans = L[0]
00285             exceptions = L[1:]
00286         except (TypeError, AttributeError, IndexError):
00287             raise InvalidOperation
00288         def FixQuotes(val):
00289             val = val.replace("''", 'SingleQuote').replace('""', 'DoubleQuote')
00290             val = val.replace("'", '').replace('"', '')
00291             val = val.replace('SingleQuote', "'").replace('DoubleQuote', '"')
00292             return val
00293 
00294         if id in skipped_test_ids:
00295             return
00296 
00297         fname = nameAdapter.get(funct, funct)
00298         if fname == 'rescale':
00299             return
00300         funct = getattr(self.context, fname)
00301         vals = []
00302         conglomerate = ''
00303         quote = 0
00304         theirexceptions = [ErrorNames[x.lower()] for x in exceptions]
00305 
00306         for exception in Signals:
00307             self.context.traps[exception] = 1 #Catch these bugs...
00308         for exception in theirexceptions:
00309             self.context.traps[exception] = 0
00310         for i, val in enumerate(valstemp):
00311             if val.count("'") % 2 == 1:
00312                 quote = 1 - quote
00313             if quote:
00314                 conglomerate = conglomerate + ' ' + val
00315                 continue
00316             else:
00317                 val = conglomerate + val
00318                 conglomerate = ''
00319             v = FixQuotes(val)
00320             if fname in ('to_sci_string', 'to_eng_string'):
00321                 if EXTENDEDERRORTEST:
00322                     for error in theirexceptions:
00323                         self.context.traps[error] = 1
00324                         try:
00325                             funct(self.context.create_decimal(v))
00326                         except error:
00327                             pass
00328                         except Signals as e:
00329                             self.fail("Raised %s in %s when %s disabled" % \
00330                                       (e, s, error))
00331                         else:
00332                             self.fail("Did not raise %s in %s" % (error, s))
00333                         self.context.traps[error] = 0
00334                 v = self.context.create_decimal(v)
00335             else:
00336                 v = Decimal(v, self.context)
00337             vals.append(v)
00338 
00339         ans = FixQuotes(ans)
00340 
00341         if EXTENDEDERRORTEST and fname not in ('to_sci_string', 'to_eng_string'):
00342             for error in theirexceptions:
00343                 self.context.traps[error] = 1
00344                 try:
00345                     funct(*vals)
00346                 except error:
00347                     pass
00348                 except Signals as e:
00349                     self.fail("Raised %s in %s when %s disabled" % \
00350                               (e, s, error))
00351                 else:
00352                     self.fail("Did not raise %s in %s" % (error, s))
00353                 self.context.traps[error] = 0
00354 
00355             # as above, but add traps cumulatively, to check precedence
00356             ordered_errors = [e for e in OrderedSignals if e in theirexceptions]
00357             for error in ordered_errors:
00358                 self.context.traps[error] = 1
00359                 try:
00360                     funct(*vals)
00361                 except error:
00362                     pass
00363                 except Signals as e:
00364                     self.fail("Raised %s in %s; expected %s" %
00365                               (type(e), s, error))
00366                 else:
00367                     self.fail("Did not raise %s in %s" % (error, s))
00368             # reset traps
00369             for error in ordered_errors:
00370                 self.context.traps[error] = 0
00371 
00372 
00373         if DEBUG:
00374             print("--", self.context)
00375         try:
00376             result = str(funct(*vals))
00377             if fname in LOGICAL_FUNCTIONS:
00378                 result = str(int(eval(result))) # 'True', 'False' -> '1', '0'
00379         except Signals as error:
00380             self.fail("Raised %s in %s" % (error, s))
00381         except: #Catch any error long enough to state the test case.
00382             print("ERROR:", s)
00383             raise
00384 
00385         myexceptions = self.getexceptions()
00386         self.context.clear_flags()
00387 
00388         myexceptions.sort(key=repr)
00389         theirexceptions.sort(key=repr)
00390 
00391         self.assertEqual(result, ans,
00392                          'Incorrect answer for ' + s + ' -- got ' + result)
00393         self.assertEqual(myexceptions, theirexceptions,
00394               'Incorrect flags set in ' + s + ' -- got ' + str(myexceptions))
00395         return

Here is the call graph for this function:

Here is the caller graph for this function:

def test.test_decimal.DecimalTest.eval_file (   self,
  file 
)

Definition at line 219 of file test_decimal.py.

00219 
00220     def eval_file(self, file):
00221         global skip_expected
00222         if skip_expected:
00223             raise unittest.SkipTest
00224             return
00225         with open(file) as f:
00226             for line in f:
00227                 line = line.replace('\r\n', '').replace('\n', '')
00228                 #print line
00229                 try:
00230                     t = self.eval_line(line)
00231                 except DecimalException as exception:
00232                     #Exception raised where there shouldn't have been one.
00233                     self.fail('Exception "'+exception.__class__.__name__ + '" raised on line '+line)
00234 
00235         return

Here is the call graph for this function:

def test.test_decimal.DecimalTest.eval_line (   self,
  s 
)

Definition at line 236 of file test_decimal.py.

00236 
00237     def eval_line(self, s):
00238         if s.find(' -> ') >= 0 and s[:2] != '--' and not s.startswith('  --'):
00239             s = (s.split('->')[0] + '->' +
00240                  s.split('->')[1].split('--')[0]).strip()
00241         else:
00242             s = s.split('--')[0].strip()
00243 
00244         for ignore in self.ignore_list:
00245             if s.find(ignore) >= 0:
00246                 #print s.split()[0], 'NotImplemented--', ignore
00247                 return
00248         if not s:
00249             return
00250         elif ':' in s:
00251             return self.eval_directive(s)
00252         else:
00253             return self.eval_equation(s)

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 396 of file test_decimal.py.

00396 
00397     def getexceptions(self):
00398         return [e for e in Signals if self.context.flags[e]]

Here is the caller graph for this function:

Definition at line 207 of file test_decimal.py.

00207 
00208     def setUp(self):
00209         self.context = Context()
00210         self.ignore_list = ['#']
00211         # Basically, a # means return NaN InvalidOperation.
00212         # Different from a sNaN in trim
00213 
00214         self.ChangeDict = {'precision' : self.change_precision,
00215                       'rounding' : self.change_rounding_method,
00216                       'maxexponent' : self.change_max_exponent,
00217                       'minexponent' : self.change_min_exponent,
00218                       'clamp' : self.change_clamp}

Here is the caller graph for this function:


Member Data Documentation

Definition at line 213 of file test_decimal.py.

Definition at line 208 of file test_decimal.py.

Definition at line 209 of file test_decimal.py.


The documentation for this class was generated from the following file: