move sections
[python/dscho.git] / Lib / test / test_normalization.py
blob2c4972098ab81f6658d8f2f3b4d6646bf901f92a
1 from test.test_support import run_unittest, open_urlresource
2 import unittest
4 from httplib import HTTPException
5 import sys
6 import os
7 from unicodedata import normalize, unidata_version
9 TESTDATAFILE = "NormalizationTest.txt"
10 TESTDATAURL = "http://www.unicode.org/Public/" + unidata_version + "/ucd/" + TESTDATAFILE
12 def check_version(testfile):
13 hdr = testfile.readline()
14 return unidata_version in hdr
16 class RangeError(Exception):
17 pass
19 def NFC(str):
20 return normalize("NFC", str)
22 def NFKC(str):
23 return normalize("NFKC", str)
25 def NFD(str):
26 return normalize("NFD", str)
28 def NFKD(str):
29 return normalize("NFKD", str)
31 def unistr(data):
32 data = [int(x, 16) for x in data.split(" ")]
33 for x in data:
34 if x > sys.maxunicode:
35 raise RangeError
36 return u"".join([unichr(x) for x in data])
38 class NormalizationTest(unittest.TestCase):
39 def test_main(self):
40 part = None
41 part1_data = {}
42 # Hit the exception early
43 try:
44 testdata = open_urlresource(TESTDATAURL, check_version)
45 except (IOError, HTTPException):
46 self.skipTest("Could not retrieve " + TESTDATAURL)
47 for line in testdata:
48 if '#' in line:
49 line = line.split('#')[0]
50 line = line.strip()
51 if not line:
52 continue
53 if line.startswith("@Part"):
54 part = line.split()[0]
55 continue
56 if part == "@Part3":
57 # XXX we don't support PRI #29 yet, so skip these tests for now
58 continue
59 try:
60 c1,c2,c3,c4,c5 = [unistr(x) for x in line.split(';')[:-1]]
61 except RangeError:
62 # Skip unsupported characters;
63 # try atleast adding c1 if we are in part1
64 if part == "@Part1":
65 try:
66 c1 = unistr(line.split(';')[0])
67 except RangeError:
68 pass
69 else:
70 part1_data[c1] = 1
71 continue
73 # Perform tests
74 self.assertTrue(c2 == NFC(c1) == NFC(c2) == NFC(c3), line)
75 self.assertTrue(c4 == NFC(c4) == NFC(c5), line)
76 self.assertTrue(c3 == NFD(c1) == NFD(c2) == NFD(c3), line)
77 self.assertTrue(c5 == NFD(c4) == NFD(c5), line)
78 self.assertTrue(c4 == NFKC(c1) == NFKC(c2) == \
79 NFKC(c3) == NFKC(c4) == NFKC(c5),
80 line)
81 self.assertTrue(c5 == NFKD(c1) == NFKD(c2) == \
82 NFKD(c3) == NFKD(c4) == NFKD(c5),
83 line)
85 # Record part 1 data
86 if part == "@Part1":
87 part1_data[c1] = 1
89 # Perform tests for all other data
90 for c in range(sys.maxunicode+1):
91 X = unichr(c)
92 if X in part1_data:
93 continue
94 self.assertTrue(X == NFC(X) == NFD(X) == NFKC(X) == NFKD(X), c)
96 def test_bug_834676(self):
97 # Check for bug 834676
98 normalize('NFC', u'\ud55c\uae00')
101 def test_main():
102 run_unittest(NormalizationTest)
104 if __name__ == "__main__":
105 test_main()