blob: aa7122172f3abc1b2047c87219f6a62bd75eecee [file] [log] [blame]
Martin v. Löwisb4cb6642003-04-19 12:57:57 +00001# To fully test this module, we would need a copy of the stringprep tables.
2# Since we don't have them, this test checks only a few codepoints.
3
Guido van Rossumd8faa362007-04-27 19:54:29 +00004import unittest
Benjamin Petersonee8712c2008-05-20 21:35:26 +00005from test import support
Martin v. Löwisb4cb6642003-04-19 12:57:57 +00006
Martin v. Löwisb4cb6642003-04-19 12:57:57 +00007from stringprep import *
8
Guido van Rossumd8faa362007-04-27 19:54:29 +00009class StringprepTests(unittest.TestCase):
10 def test(self):
Benjamin Petersonc9c0f202009-06-30 23:06:06 +000011 self.assertTrue(in_table_a1("\u0221"))
12 self.assertFalse(in_table_a1("\u0222"))
Martin v. Löwisb4cb6642003-04-19 12:57:57 +000013
Benjamin Petersonc9c0f202009-06-30 23:06:06 +000014 self.assertTrue(in_table_b1("\u00ad"))
15 self.assertFalse(in_table_b1("\u00ae"))
Martin v. Löwisb4cb6642003-04-19 12:57:57 +000016
Benjamin Petersonc9c0f202009-06-30 23:06:06 +000017 self.assertTrue(map_table_b2("\u0041"), "\u0061")
18 self.assertTrue(map_table_b2("\u0061"), "\u0061")
Martin v. Löwisb4cb6642003-04-19 12:57:57 +000019
Benjamin Petersonc9c0f202009-06-30 23:06:06 +000020 self.assertTrue(map_table_b3("\u0041"), "\u0061")
21 self.assertTrue(map_table_b3("\u0061"), "\u0061")
Martin v. Löwisb4cb6642003-04-19 12:57:57 +000022
Benjamin Petersonc9c0f202009-06-30 23:06:06 +000023 self.assertTrue(in_table_c11("\u0020"))
24 self.assertFalse(in_table_c11("\u0021"))
Martin v. Löwisb4cb6642003-04-19 12:57:57 +000025
Benjamin Petersonc9c0f202009-06-30 23:06:06 +000026 self.assertTrue(in_table_c12("\u00a0"))
27 self.assertFalse(in_table_c12("\u00a1"))
Martin v. Löwisb4cb6642003-04-19 12:57:57 +000028
Benjamin Petersonc9c0f202009-06-30 23:06:06 +000029 self.assertTrue(in_table_c12("\u00a0"))
30 self.assertFalse(in_table_c12("\u00a1"))
Martin v. Löwisb4cb6642003-04-19 12:57:57 +000031
Benjamin Petersonc9c0f202009-06-30 23:06:06 +000032 self.assertTrue(in_table_c11_c12("\u00a0"))
33 self.assertFalse(in_table_c11_c12("\u00a1"))
Martin v. Löwisb4cb6642003-04-19 12:57:57 +000034
Benjamin Petersonc9c0f202009-06-30 23:06:06 +000035 self.assertTrue(in_table_c21("\u001f"))
36 self.assertFalse(in_table_c21("\u0020"))
Martin v. Löwisb4cb6642003-04-19 12:57:57 +000037
Benjamin Petersonc9c0f202009-06-30 23:06:06 +000038 self.assertTrue(in_table_c22("\u009f"))
39 self.assertFalse(in_table_c22("\u00a0"))
Martin v. Löwisb4cb6642003-04-19 12:57:57 +000040
Benjamin Petersonc9c0f202009-06-30 23:06:06 +000041 self.assertTrue(in_table_c21_c22("\u009f"))
42 self.assertFalse(in_table_c21_c22("\u00a0"))
Martin v. Löwisb4cb6642003-04-19 12:57:57 +000043
Benjamin Petersonc9c0f202009-06-30 23:06:06 +000044 self.assertTrue(in_table_c3("\ue000"))
45 self.assertFalse(in_table_c3("\uf900"))
Martin v. Löwisb4cb6642003-04-19 12:57:57 +000046
Benjamin Petersonc9c0f202009-06-30 23:06:06 +000047 self.assertTrue(in_table_c4("\uffff"))
48 self.assertFalse(in_table_c4("\u0000"))
Martin v. Löwisb4cb6642003-04-19 12:57:57 +000049
Benjamin Petersonc9c0f202009-06-30 23:06:06 +000050 self.assertTrue(in_table_c5("\ud800"))
51 self.assertFalse(in_table_c5("\ud7ff"))
Martin v. Löwisb4cb6642003-04-19 12:57:57 +000052
Benjamin Petersonc9c0f202009-06-30 23:06:06 +000053 self.assertTrue(in_table_c6("\ufff9"))
54 self.assertFalse(in_table_c6("\ufffe"))
Martin v. Löwisb4cb6642003-04-19 12:57:57 +000055
Benjamin Petersonc9c0f202009-06-30 23:06:06 +000056 self.assertTrue(in_table_c7("\u2ff0"))
57 self.assertFalse(in_table_c7("\u2ffc"))
Martin v. Löwisb4cb6642003-04-19 12:57:57 +000058
Benjamin Petersonc9c0f202009-06-30 23:06:06 +000059 self.assertTrue(in_table_c8("\u0340"))
60 self.assertFalse(in_table_c8("\u0342"))
Martin v. Löwisb4cb6642003-04-19 12:57:57 +000061
Guido van Rossumd8faa362007-04-27 19:54:29 +000062 # C.9 is not in the bmp
Benjamin Petersonc9c0f202009-06-30 23:06:06 +000063 # self.assertTrue(in_table_c9(u"\U000E0001"))
64 # self.assertFalse(in_table_c8(u"\U000E0002"))
Martin v. Löwisb4cb6642003-04-19 12:57:57 +000065
Benjamin Petersonc9c0f202009-06-30 23:06:06 +000066 self.assertTrue(in_table_d1("\u05be"))
67 self.assertFalse(in_table_d1("\u05bf"))
Martin v. Löwisb4cb6642003-04-19 12:57:57 +000068
Benjamin Petersonc9c0f202009-06-30 23:06:06 +000069 self.assertTrue(in_table_d2("\u0041"))
70 self.assertFalse(in_table_d2("\u0040"))
Martin v. Löwisb4cb6642003-04-19 12:57:57 +000071
Guido van Rossumd8faa362007-04-27 19:54:29 +000072 # This would generate a hash of all predicates. However, running
73 # it is quite expensive, and only serves to detect changes in the
74 # unicode database. Instead, stringprep.py asserts the version of
75 # the database.
Martin v. Löwisb4cb6642003-04-19 12:57:57 +000076
Guido van Rossumd8faa362007-04-27 19:54:29 +000077 # import hashlib
78 # predicates = [k for k in dir(stringprep) if k.startswith("in_table")]
79 # predicates.sort()
80 # for p in predicates:
81 # f = getattr(stringprep, p)
82 # # Collect all BMP code points
83 # data = ["0"] * 0x10000
84 # for i in range(0x10000):
85 # if f(unichr(i)):
86 # data[i] = "1"
87 # data = "".join(data)
88 # h = hashlib.sha1()
89 # h.update(data)
90 # print p, h.hexdigest()
91
92def test_main():
Benjamin Petersonee8712c2008-05-20 21:35:26 +000093 support.run_unittest(StringprepTests)
Guido van Rossumd8faa362007-04-27 19:54:29 +000094
95if __name__ == '__main__':
96 test_main()