Back to index

python3.2  3.2.2
Public Member Functions
test.test_decimal.DecimalFormatTest Class Reference

List of all members.

Public Member Functions

def test_formatting
def test_n_format

Detailed Description

Unit tests for the format function.

Definition at line 675 of file test_decimal.py.


Member Function Documentation

Definition at line 677 of file test_decimal.py.

00677 
00678     def test_formatting(self):
00679         # triples giving a format, a Decimal, and the expected result
00680         test_values = [
00681             ('e', '0E-15', '0e-15'),
00682             ('e', '2.3E-15', '2.3e-15'),
00683             ('e', '2.30E+2', '2.30e+2'), # preserve significant zeros
00684             ('e', '2.30000E-15', '2.30000e-15'),
00685             ('e', '1.23456789123456789e40', '1.23456789123456789e+40'),
00686             ('e', '1.5', '1.5e+0'),
00687             ('e', '0.15', '1.5e-1'),
00688             ('e', '0.015', '1.5e-2'),
00689             ('e', '0.0000000000015', '1.5e-12'),
00690             ('e', '15.0', '1.50e+1'),
00691             ('e', '-15', '-1.5e+1'),
00692             ('e', '0', '0e+0'),
00693             ('e', '0E1', '0e+1'),
00694             ('e', '0.0', '0e-1'),
00695             ('e', '0.00', '0e-2'),
00696             ('.6e', '0E-15', '0.000000e-9'),
00697             ('.6e', '0', '0.000000e+6'),
00698             ('.6e', '9.999999', '9.999999e+0'),
00699             ('.6e', '9.9999999', '1.000000e+1'),
00700             ('.6e', '-1.23e5', '-1.230000e+5'),
00701             ('.6e', '1.23456789e-3', '1.234568e-3'),
00702             ('f', '0', '0'),
00703             ('f', '0.0', '0.0'),
00704             ('f', '0E-2', '0.00'),
00705             ('f', '0.00E-8', '0.0000000000'),
00706             ('f', '0E1', '0'), # loses exponent information
00707             ('f', '3.2E1', '32'),
00708             ('f', '3.2E2', '320'),
00709             ('f', '3.20E2', '320'),
00710             ('f', '3.200E2', '320.0'),
00711             ('f', '3.2E-6', '0.0000032'),
00712             ('.6f', '0E-15', '0.000000'), # all zeros treated equally
00713             ('.6f', '0E1', '0.000000'),
00714             ('.6f', '0', '0.000000'),
00715             ('.0f', '0', '0'), # no decimal point
00716             ('.0f', '0e-2', '0'),
00717             ('.0f', '3.14159265', '3'),
00718             ('.1f', '3.14159265', '3.1'),
00719             ('.4f', '3.14159265', '3.1416'),
00720             ('.6f', '3.14159265', '3.141593'),
00721             ('.7f', '3.14159265', '3.1415926'), # round-half-even!
00722             ('.8f', '3.14159265', '3.14159265'),
00723             ('.9f', '3.14159265', '3.141592650'),
00724 
00725             ('g', '0', '0'),
00726             ('g', '0.0', '0.0'),
00727             ('g', '0E1', '0e+1'),
00728             ('G', '0E1', '0E+1'),
00729             ('g', '0E-5', '0.00000'),
00730             ('g', '0E-6', '0.000000'),
00731             ('g', '0E-7', '0e-7'),
00732             ('g', '-0E2', '-0e+2'),
00733             ('.0g', '3.14159265', '3'),  # 0 sig fig -> 1 sig fig
00734             ('.1g', '3.14159265', '3'),
00735             ('.2g', '3.14159265', '3.1'),
00736             ('.5g', '3.14159265', '3.1416'),
00737             ('.7g', '3.14159265', '3.141593'),
00738             ('.8g', '3.14159265', '3.1415926'), # round-half-even!
00739             ('.9g', '3.14159265', '3.14159265'),
00740             ('.10g', '3.14159265', '3.14159265'), # don't pad
00741 
00742             ('%', '0E1', '0%'),
00743             ('%', '0E0', '0%'),
00744             ('%', '0E-1', '0%'),
00745             ('%', '0E-2', '0%'),
00746             ('%', '0E-3', '0.0%'),
00747             ('%', '0E-4', '0.00%'),
00748 
00749             ('.3%', '0', '0.000%'), # all zeros treated equally
00750             ('.3%', '0E10', '0.000%'),
00751             ('.3%', '0E-10', '0.000%'),
00752             ('.3%', '2.34', '234.000%'),
00753             ('.3%', '1.234567', '123.457%'),
00754             ('.0%', '1.23', '123%'),
00755 
00756             ('e', 'NaN', 'NaN'),
00757             ('f', '-NaN123', '-NaN123'),
00758             ('+g', 'NaN456', '+NaN456'),
00759             ('.3e', 'Inf', 'Infinity'),
00760             ('.16f', '-Inf', '-Infinity'),
00761             ('.0g', '-sNaN', '-sNaN'),
00762 
00763             ('', '1.00', '1.00'),
00764 
00765             # test alignment and padding
00766             ('6', '123', '   123'),
00767             ('<6', '123', '123   '),
00768             ('>6', '123', '   123'),
00769             ('^6', '123', ' 123  '),
00770             ('=+6', '123', '+  123'),
00771             ('#<10', 'NaN', 'NaN#######'),
00772             ('#<10', '-4.3', '-4.3######'),
00773             ('#<+10', '0.0130', '+0.0130###'),
00774             ('#< 10', '0.0130', ' 0.0130###'),
00775             ('@>10', '-Inf', '@-Infinity'),
00776             ('#>5', '-Inf', '-Infinity'),
00777             ('?^5', '123', '?123?'),
00778             ('%^6', '123', '%123%%'),
00779             (' ^6', '-45.6', '-45.6 '),
00780             ('/=10', '-45.6', '-/////45.6'),
00781             ('/=+10', '45.6', '+/////45.6'),
00782             ('/= 10', '45.6', ' /////45.6'),
00783 
00784             # thousands separator
00785             (',', '1234567', '1,234,567'),
00786             (',', '123456', '123,456'),
00787             (',', '12345', '12,345'),
00788             (',', '1234', '1,234'),
00789             (',', '123', '123'),
00790             (',', '12', '12'),
00791             (',', '1', '1'),
00792             (',', '0', '0'),
00793             (',', '-1234567', '-1,234,567'),
00794             (',', '-123456', '-123,456'),
00795             ('7,', '123456', '123,456'),
00796             ('8,', '123456', ' 123,456'),
00797             ('08,', '123456', '0,123,456'), # special case: extra 0 needed
00798             ('+08,', '123456', '+123,456'), # but not if there's a sign
00799             (' 08,', '123456', ' 123,456'),
00800             ('08,', '-123456', '-123,456'),
00801             ('+09,', '123456', '+0,123,456'),
00802             # ... with fractional part...
00803             ('07,', '1234.56', '1,234.56'),
00804             ('08,', '1234.56', '1,234.56'),
00805             ('09,', '1234.56', '01,234.56'),
00806             ('010,', '1234.56', '001,234.56'),
00807             ('011,', '1234.56', '0,001,234.56'),
00808             ('012,', '1234.56', '0,001,234.56'),
00809             ('08,.1f', '1234.5', '01,234.5'),
00810             # no thousands separators in fraction part
00811             (',', '1.23456789', '1.23456789'),
00812             (',%', '123.456789', '12,345.6789%'),
00813             (',e', '123456', '1.23456e+5'),
00814             (',E', '123456', '1.23456E+5'),
00815 
00816             # issue 6850
00817             ('a=-7.0', '0.12345', 'aaaa0.1'),
00818 
00819             # Issue 7094: Alternate formatting (specified by #)
00820             ('.0e', '1.0', '1e+0'),
00821             ('#.0e', '1.0', '1.e+0'),
00822             ('.0f', '1.0', '1'),
00823             ('#.0f', '1.0', '1.'),
00824             ('g', '1.1', '1.1'),
00825             ('#g', '1.1', '1.1'),
00826             ('.0g', '1', '1'),
00827             ('#.0g', '1', '1.'),
00828             ('.0%', '1.0', '100%'),
00829             ('#.0%', '1.0', '100.%'),
00830             ]
00831         for fmt, d, result in test_values:
00832             self.assertEqual(format(Decimal(d), fmt), result)

Here is the call graph for this function:

Definition at line 833 of file test_decimal.py.

00833 
00834     def test_n_format(self):
00835         try:
00836             from locale import CHAR_MAX
00837         except ImportError:
00838             return
00839 
00840         # Set up some localeconv-like dictionaries
00841         en_US = {
00842             'decimal_point' : '.',
00843             'grouping' : [3, 3, 0],
00844             'thousands_sep': ','
00845             }
00846 
00847         fr_FR = {
00848             'decimal_point' : ',',
00849             'grouping' : [CHAR_MAX],
00850             'thousands_sep' : ''
00851             }
00852 
00853         ru_RU = {
00854             'decimal_point' : ',',
00855             'grouping' : [3, 3, 0],
00856             'thousands_sep' : ' '
00857             }
00858 
00859         crazy = {
00860             'decimal_point' : '&',
00861             'grouping' : [1, 4, 2, CHAR_MAX],
00862             'thousands_sep' : '-'
00863             }
00864 
00865 
00866         def get_fmt(x, locale, fmt='n'):
00867             return Decimal.__format__(Decimal(x), fmt, _localeconv=locale)
00868 
00869         self.assertEqual(get_fmt(Decimal('12.7'), en_US), '12.7')
00870         self.assertEqual(get_fmt(Decimal('12.7'), fr_FR), '12,7')
00871         self.assertEqual(get_fmt(Decimal('12.7'), ru_RU), '12,7')
00872         self.assertEqual(get_fmt(Decimal('12.7'), crazy), '1-2&7')
00873 
00874         self.assertEqual(get_fmt(123456789, en_US), '123,456,789')
00875         self.assertEqual(get_fmt(123456789, fr_FR), '123456789')
00876         self.assertEqual(get_fmt(123456789, ru_RU), '123 456 789')
00877         self.assertEqual(get_fmt(1234567890123, crazy), '123456-78-9012-3')
00878 
00879         self.assertEqual(get_fmt(123456789, en_US, '.6n'), '1.23457e+8')
00880         self.assertEqual(get_fmt(123456789, fr_FR, '.6n'), '1,23457e+8')
00881         self.assertEqual(get_fmt(123456789, ru_RU, '.6n'), '1,23457e+8')
00882         self.assertEqual(get_fmt(123456789, crazy, '.6n'), '1&23457e+8')
00883 
00884         # zero padding
00885         self.assertEqual(get_fmt(1234, fr_FR, '03n'), '1234')
00886         self.assertEqual(get_fmt(1234, fr_FR, '04n'), '1234')
00887         self.assertEqual(get_fmt(1234, fr_FR, '05n'), '01234')
00888         self.assertEqual(get_fmt(1234, fr_FR, '06n'), '001234')
00889 
00890         self.assertEqual(get_fmt(12345, en_US, '05n'), '12,345')
00891         self.assertEqual(get_fmt(12345, en_US, '06n'), '12,345')
00892         self.assertEqual(get_fmt(12345, en_US, '07n'), '012,345')
00893         self.assertEqual(get_fmt(12345, en_US, '08n'), '0,012,345')
00894         self.assertEqual(get_fmt(12345, en_US, '09n'), '0,012,345')
00895         self.assertEqual(get_fmt(12345, en_US, '010n'), '00,012,345')
00896 
00897         self.assertEqual(get_fmt(123456, crazy, '06n'), '1-2345-6')
00898         self.assertEqual(get_fmt(123456, crazy, '07n'), '1-2345-6')
00899         self.assertEqual(get_fmt(123456, crazy, '08n'), '1-2345-6')
00900         self.assertEqual(get_fmt(123456, crazy, '09n'), '01-2345-6')
00901         self.assertEqual(get_fmt(123456, crazy, '010n'), '0-01-2345-6')
00902         self.assertEqual(get_fmt(123456, crazy, '011n'), '0-01-2345-6')
00903         self.assertEqual(get_fmt(123456, crazy, '012n'), '00-01-2345-6')
00904         self.assertEqual(get_fmt(123456, crazy, '013n'), '000-01-2345-6')
00905 

Here is the call graph for this function:


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