Initial commit of OpenSPARC T2 architecture model.
[OpenSPARC-T2-SAM] / sam-t2 / devtools / amd64 / lib / python2.4 / test / test_locale.py
CommitLineData
920dae64
AT
1from test.test_support import verbose, TestSkipped
2import locale
3import sys
4
5if sys.platform == 'darwin':
6 raise TestSkipped("Locale support on MacOSX is minimal and cannot be tested")
7oldlocale = locale.setlocale(locale.LC_NUMERIC)
8
9if sys.platform.startswith("win"):
10 tlocs = ("en",)
11else:
12 tlocs = ("en_US.UTF-8", "en_US.US-ASCII", "en_US")
13
14for tloc in tlocs:
15 try:
16 locale.setlocale(locale.LC_NUMERIC, tloc)
17 break
18 except locale.Error:
19 continue
20else:
21 raise ImportError, "test locale not supported (tried %s)"%(', '.join(tlocs))
22
23def testformat(formatstr, value, grouping = 0, output=None):
24 if verbose:
25 if output:
26 print "%s %% %s =? %s ..." %\
27 (repr(formatstr), repr(value), repr(output)),
28 else:
29 print "%s %% %s works? ..." % (repr(formatstr), repr(value)),
30 result = locale.format(formatstr, value, grouping = grouping)
31 if output and result != output:
32 if verbose:
33 print 'no'
34 print "%s %% %s == %s != %s" %\
35 (repr(formatstr), repr(value), repr(result), repr(output))
36 else:
37 if verbose:
38 print "yes"
39
40try:
41 testformat("%f", 1024, grouping=1, output='1,024.000000')
42 testformat("%f", 102, grouping=1, output='102.000000')
43 testformat("%f", -42, grouping=1, output='-42.000000')
44 testformat("%+f", -42, grouping=1, output='-42.000000')
45 testformat("%20.f", -42, grouping=1, output=' -42')
46 testformat("%+10.f", -4200, grouping=1, output=' -4,200')
47 testformat("%-10.f", 4200, grouping=1, output='4,200 ')
48 # Invoke getpreferredencoding to make sure it does not cause exceptions,
49 locale.getpreferredencoding()
50finally:
51 locale.setlocale(locale.LC_NUMERIC, oldlocale)
52
53
54# Test BSD Rune locale's bug for isctype functions.
55def teststrop(s, method, output):
56 if verbose:
57 print "%s.%s() =? %s ..." % (repr(s), method, repr(output)),
58 result = getattr(s, method)()
59 if result != output:
60 if verbose:
61 print "no"
62 print "%s.%s() == %s != %s" % (repr(s), method, repr(result),
63 repr(output))
64 elif verbose:
65 print "yes"
66
67try:
68 oldlocale = locale.setlocale(locale.LC_CTYPE)
69 locale.setlocale(locale.LC_CTYPE, 'en_US.UTF-8')
70except locale.Error:
71 pass
72else:
73 try:
74 teststrop('\x20', 'isspace', True)
75 teststrop('\xa0', 'isspace', False)
76 teststrop('\xa1', 'isspace', False)
77 teststrop('\xc0', 'isalpha', False)
78 teststrop('\xc0', 'isalnum', False)
79 teststrop('\xc0', 'isupper', False)
80 teststrop('\xc0', 'islower', False)
81 teststrop('\xec\xa0\xbc', 'split', ['\xec\xa0\xbc'])
82 teststrop('\xed\x95\xa0', 'strip', '\xed\x95\xa0')
83 teststrop('\xcc\x85', 'lower', '\xcc\x85')
84 teststrop('\xed\x95\xa0', 'upper', '\xed\x95\xa0')
85 finally:
86 locale.setlocale(locale.LC_CTYPE, oldlocale)