summaryrefslogtreecommitdiff
path: root/Examples/test-suite/python/python_strict_unicode_runme.py
blob: ba0e7d965aa781aced98177aa416aa1b0eec35da (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import python_strict_unicode
import sys

test_bytes   = b"hello \x01world\x99"
BYTES        = b"BYTES"

if sys.version_info[0:2] < (3, 0):
    test_unicode = u"h\udce9llo w\u00f6rld"
    UNICODE      = u"UNICODE"
    type_unicode_string = type(u"")
else:
    test_unicode = "h\udce9llo w\u00f6rld"
    UNICODE      = "UNICODE"
    type_unicode_string = type("")

# Test that byte string inputs and outputs work as expected
bdbl = python_strict_unicode.double_str(test_bytes)
if bdbl != test_bytes + test_bytes:
    raise RuntimeError("Failed to double string")
if type(bdbl) != type(BYTES):
    raise RuntimeError("Wrong type output for string")
bout = python_strict_unicode.same_str(test_bytes)
if bout != test_bytes:
    raise RuntimeError("Failed to copy char*")
if type(bout) != type(BYTES):
    raise RuntimeError("Wrong type output for char*")

# Test that unicode string inputs and outputs work as expected
udbl = python_strict_unicode.double_wstr(test_unicode)
if udbl != test_unicode + test_unicode:
    raise RuntimeError("Failed to double wide string")
if type(udbl) != type_unicode_string:
    raise RuntimeError("Wrong type output for wide string")
uout = python_strict_unicode.same_wstr(test_unicode)
if uout != test_unicode:
    raise RuntimeError("Failed to copy wchar_t*")
if type(uout) != type_unicode_string:
    raise RuntimeError("Wrong type output for wchar_t*")

# Test that overloading is handled properly
bovr = python_strict_unicode.overload(test_bytes)
if bovr != BYTES:
    raise RuntimeError("Failed to return bytes from overload")
if type(bovr) != type(BYTES):
    raise RuntimeError("Wrong type output from overload")
uovr = python_strict_unicode.overload(test_unicode)
if uovr != UNICODE:
    raise RuntimeError("Failed to return unicode from overload")
if type(uovr) != type_unicode_string:
    raise RuntimeERror("Wrong type output from overload")

# Test that bytes aren't accepted as wide strings and unicode isn't accepted as narrow strings
try:
    python_strict_unicode.double_str(test_unicode)
    error = 1
except TypeError:
    error = 0
if error:
    raise RuntimeError("Unicode accepted for string")
try:
    python_strict_unicode.same_str(test_unicode)
    error = 1
except TypeError:
    error = 0
if error:
    raise RuntimeError("Unicode accepted for char*")
try:
    python_strict_unicode.double_wstr(test_bytes)
    error = 1
except TypeError:
    error = 0
if error:
    raise RuntimeError("Bytes accepted for wstring")
try:
    python_strict_unicode.same_wstr(test_bytes)
    error = 1
except TypeError:
    error = 0
if error:
    raise RuntimeError("Bytes accepted for wchar_t*")