blob: b08c5fcb1a7fe02ec3f83c5c46fa63575b4e528a [file] [log] [blame]
Benjamin Peterson90f5ba52010-03-11 22:53:45 +00001#!/usr/bin/env python3
Hye-Shik Chang3e2a3062004-01-17 14:29:29 +00002#
3# test_codecencodings_cn.py
4# Codec encoding tests for PRC encodings.
5#
Hye-Shik Chang3e2a3062004-01-17 14:29:29 +00006
Benjamin Petersonee8712c2008-05-20 21:35:26 +00007from test import support
R David Murray75d9aca2012-04-09 09:37:52 -04008from test import multibytecodec_support
Hye-Shik Chang3e2a3062004-01-17 14:29:29 +00009import unittest
10
R David Murray75d9aca2012-04-09 09:37:52 -040011class Test_GB2312(multibytecodec_support.TestBase, unittest.TestCase):
Hye-Shik Chang3e2a3062004-01-17 14:29:29 +000012 encoding = 'gb2312'
R David Murray75d9aca2012-04-09 09:37:52 -040013 tstring = multibytecodec_support.load_teststring('gb2312')
Hye-Shik Chang3e2a3062004-01-17 14:29:29 +000014 codectests = (
15 # invalid bytes
Guido van Rossum024da5c2007-05-17 23:59:11 +000016 (b"abc\x81\x81\xc1\xc4", "strict", None),
17 (b"abc\xc8", "strict", None),
Victor Stinner2cded9c2011-07-08 01:45:13 +020018 (b"abc\x81\x81\xc1\xc4", "replace", "abc\ufffd\ufffd\u804a"),
19 (b"abc\x81\x81\xc1\xc4\xc8", "replace", "abc\ufffd\ufffd\u804a\ufffd"),
Guido van Rossum024da5c2007-05-17 23:59:11 +000020 (b"abc\x81\x81\xc1\xc4", "ignore", "abc\u804a"),
21 (b"\xc1\x64", "strict", None),
Hye-Shik Chang3e2a3062004-01-17 14:29:29 +000022 )
23
R David Murray75d9aca2012-04-09 09:37:52 -040024class Test_GBK(multibytecodec_support.TestBase, unittest.TestCase):
Hye-Shik Chang3e2a3062004-01-17 14:29:29 +000025 encoding = 'gbk'
R David Murray75d9aca2012-04-09 09:37:52 -040026 tstring = multibytecodec_support.load_teststring('gbk')
Hye-Shik Chang3e2a3062004-01-17 14:29:29 +000027 codectests = (
28 # invalid bytes
Guido van Rossum024da5c2007-05-17 23:59:11 +000029 (b"abc\x80\x80\xc1\xc4", "strict", None),
30 (b"abc\xc8", "strict", None),
Victor Stinner2cded9c2011-07-08 01:45:13 +020031 (b"abc\x80\x80\xc1\xc4", "replace", "abc\ufffd\ufffd\u804a"),
32 (b"abc\x80\x80\xc1\xc4\xc8", "replace", "abc\ufffd\ufffd\u804a\ufffd"),
Guido van Rossum024da5c2007-05-17 23:59:11 +000033 (b"abc\x80\x80\xc1\xc4", "ignore", "abc\u804a"),
34 (b"\x83\x34\x83\x31", "strict", None),
Guido van Rossumef87d6e2007-05-02 19:09:54 +000035 ("\u30fb", "strict", None),
Hye-Shik Chang3e2a3062004-01-17 14:29:29 +000036 )
37
R David Murray75d9aca2012-04-09 09:37:52 -040038class Test_GB18030(multibytecodec_support.TestBase, unittest.TestCase):
Hye-Shik Chang3e2a3062004-01-17 14:29:29 +000039 encoding = 'gb18030'
R David Murray75d9aca2012-04-09 09:37:52 -040040 tstring = multibytecodec_support.load_teststring('gb18030')
Hye-Shik Chang3e2a3062004-01-17 14:29:29 +000041 codectests = (
42 # invalid bytes
Guido van Rossum024da5c2007-05-17 23:59:11 +000043 (b"abc\x80\x80\xc1\xc4", "strict", None),
44 (b"abc\xc8", "strict", None),
Victor Stinner2cded9c2011-07-08 01:45:13 +020045 (b"abc\x80\x80\xc1\xc4", "replace", "abc\ufffd\ufffd\u804a"),
46 (b"abc\x80\x80\xc1\xc4\xc8", "replace", "abc\ufffd\ufffd\u804a\ufffd"),
Guido van Rossum024da5c2007-05-17 23:59:11 +000047 (b"abc\x80\x80\xc1\xc4", "ignore", "abc\u804a"),
Victor Stinner2cded9c2011-07-08 01:45:13 +020048 (b"abc\x84\x39\x84\x39\xc1\xc4", "replace", "abc\ufffd9\ufffd9\u804a"),
Guido van Rossum024da5c2007-05-17 23:59:11 +000049 ("\u30fb", "strict", b"\x819\xa79"),
Victor Stinner2cded9c2011-07-08 01:45:13 +020050 (b"abc\x84\x32\x80\x80def", "replace", 'abc\ufffd2\ufffd\ufffddef'),
51 (b"abc\x81\x30\x81\x30def", "strict", 'abc\x80def'),
52 (b"abc\x86\x30\x81\x30def", "replace", 'abc\ufffd0\ufffd0def'),
Hye-Shik Chang3e2a3062004-01-17 14:29:29 +000053 )
54 has_iso10646 = True
55
R David Murray75d9aca2012-04-09 09:37:52 -040056class Test_HZ(multibytecodec_support.TestBase, unittest.TestCase):
Victor Stinner8fdfc202011-05-25 00:06:51 +020057 encoding = 'hz'
R David Murray75d9aca2012-04-09 09:37:52 -040058 tstring = multibytecodec_support.load_teststring('hz')
Victor Stinner8fdfc202011-05-25 00:06:51 +020059 codectests = (
60 # test '~\n' (3 lines)
61 (b'This sentence is in ASCII.\n'
62 b'The next sentence is in GB.~{<:Ky2;S{#,~}~\n'
63 b'~{NpJ)l6HK!#~}Bye.\n',
64 'strict',
65 'This sentence is in ASCII.\n'
66 'The next sentence is in GB.'
67 '\u5df1\u6240\u4e0d\u6b32\uff0c\u52ff\u65bd\u65bc\u4eba\u3002'
68 'Bye.\n'),
69 # test '~\n' (4 lines)
70 (b'This sentence is in ASCII.\n'
71 b'The next sentence is in GB.~\n'
72 b'~{<:Ky2;S{#,NpJ)l6HK!#~}~\n'
73 b'Bye.\n',
74 'strict',
75 'This sentence is in ASCII.\n'
76 'The next sentence is in GB.'
77 '\u5df1\u6240\u4e0d\u6b32\uff0c\u52ff\u65bd\u65bc\u4eba\u3002'
78 'Bye.\n'),
79 # invalid bytes
Victor Stinner2cded9c2011-07-08 01:45:13 +020080 (b'ab~cd', 'replace', 'ab\uFFFDcd'),
Victor Stinner8fdfc202011-05-25 00:06:51 +020081 (b'ab\xffcd', 'replace', 'ab\uFFFDcd'),
82 (b'ab~{\x81\x81\x41\x44~}cd', 'replace', 'ab\uFFFD\uFFFD\u804Acd'),
Victor Stinner2cded9c2011-07-08 01:45:13 +020083 (b'ab~{\x41\x44~}cd', 'replace', 'ab\u804Acd'),
84 (b"ab~{\x79\x79\x41\x44~}cd", "replace", "ab\ufffd\ufffd\u804acd"),
Victor Stinner8fdfc202011-05-25 00:06:51 +020085 )
86
Hye-Shik Chang3e2a3062004-01-17 14:29:29 +000087def test_main():
Benjamin Petersonee8712c2008-05-20 21:35:26 +000088 support.run_unittest(__name__)
Hye-Shik Chang3e2a3062004-01-17 14:29:29 +000089
90if __name__ == "__main__":
91 test_main()