forked from IronLanguages/ironpython3
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_buffer.py
More file actions
176 lines (138 loc) · 4.77 KB
/
test_buffer.py
File metadata and controls
176 lines (138 loc) · 4.77 KB
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#####################################################################################
#
# Copyright (c) Microsoft Corporation. All rights reserved.
#
# This source code is subject to terms and conditions of the Apache License, Version 2.0. A
# copy of the license can be found in the License.html file at the root of this distribution. If
# you cannot locate the Apache License, Version 2.0, please send an email to
# ironpy@microsoft.com. By using this source code in any fashion, you are agreeing to be bound
# by the terms of the Apache License, Version 2.0.
#
# You must not remove this notice, or any other, from this software.
#
#
#####################################################################################
##
## Test builtin-type buffer
##
from iptest.assert_util import *
import array
def test_negative():
AssertError(TypeError, buffer, None)
AssertError(TypeError, buffer, None, 0)
AssertError(TypeError, buffer, None, 0, 0)
AssertError(ValueError, buffer, "abc", -1) #offset < 0
AssertError(ValueError, buffer, "abc", -1, 0) #offset < 0
#size < -1; -1 is allowed since that is the way to ask for the default value
AssertError(ValueError, buffer, "abc", 0, -2)
def test_len():
testData = ('hello world', array.array('b', 'hello world'), buffer('hello world'), buffer('abchello world', 3))
if is_cli:
import System
testData += (System.Array[System.Char]('hello world'), )
for x in testData:
b = buffer(x, 6)
AreEqual(len(b), 5)
b = buffer(x, 6, 2)
AreEqual(len(b), 2)
AreEqual(len(buffer("abc", 5)), 0)
AreEqual(len(buffer("abc", 5, 50)), 0)
def test_pass_in_string():
b = buffer("abc", 0, -1)
AreEqual(str(b), "abc")
AreEqual(len(b), 3)
b1 = buffer("abc")
AreEqual(str(b1), "abc")
b2 = buffer("def", 0)
AreEqual(str(b2), "def")
b3 = b1 + b2
AreEqual(str(b3), "abcdef")
b4 = 2 * (b2 * 2)
AreEqual(str(b4), "defdefdefdef")
b5 = 2 * b2
AreEqual(str(b5), 'defdef')
def test_pass_in_buffer():
a = buffer("abc")
b = buffer(a, 0, 2)
AreEqual("ab", str(b))
c = buffer(b, 0, 1)
AreEqual("a", str(c))
d = buffer(b, 0, 100)
AreEqual("ab", str(d))
e = buffer(a, 1, 2)
AreEqual(str(e), "bc")
e = buffer(a, 1, 5)
AreEqual(str(e), "bc")
e = buffer(a, 1, -1)
AreEqual(str(e), "bc")
e = buffer(a, 1, 0)
AreEqual(str(e), "")
e = buffer(a, 1, 1)
AreEqual(str(e), "b")
@skip('win32')
def test_pass_in_clrarray():
import System
a1 = System.Array[int]([1,2])
arrbuff1 = buffer(a1, 0, 5)
AreEqual(1, arrbuff1[0])
AreEqual(2, arrbuff1[1])
a2 = System.Array[System.String](["a","b"])
arrbuff2 = buffer(a2, 0, 2)
AreEqual("a", arrbuff2[0])
AreEqual("b", arrbuff2[1])
AreEqual(len(arrbuff1), len(arrbuff2))
arrbuff1 = buffer(a1, 1, 1)
AreEqual(2, arrbuff1[0])
AreEqual(len(arrbuff1), 1)
arrbuff1 = buffer(a1, 0, -1)
AreEqual(1, arrbuff1[0])
AreEqual(2, arrbuff1[1])
AreEqual(len(arrbuff1), 2)
a3 = System.Array[System.Guid]([])
AssertError(TypeError, buffer, a3)
def test_equality():
x = buffer('abc')
AreEqual(x == None, False)
AreEqual(None == x, False)
AreEqual(x == x, True)
def test_buffer_add():
AreEqual(buffer('abc') + 'def', 'abcdef')
import array
arr = array.array('b', [1,2,3,4,5])
AreEqual(buffer(arr) + 'abc', '\x01\x02\x03\x04\x05abc')
def test_buffer_tostr():
import array
AreEqual(str(buffer('abc')), 'abc')
AreEqual(str(buffer(array.array('b', [1,2,3,4,5]))), '\x01\x02\x03\x04\x05')
def test_buffer_bytes():
for x in (b'abc', bytearray(b'abc')):
AreEqual(str(buffer(x)), 'abc')
AreEqual(buffer(x)[0:1], b'a')
@skip("silverlight")
def test_write_file():
inputs = [buffer('abcdef'), buffer(b'abcdef'), buffer(bytearray(b'abcdef')), buffer(array.array('b', 'abcdef'))]
text_inputs = [array.array('b', 'abcdef'), array.array('c', 'abcdef')]
#if is_cli:
# inputs.append(System.Array[System.Char]('abcdef'))
for inp in inputs + text_inputs:
f = file('foo', 'wb')
f.write(inp)
f.close()
f = file('foo')
AreEqual(f.readlines(), ['abcdef'])
f.close()
# TODO: Arrays not allowed in non-binary mode
# buffer(array(...)) is currently allowed because disallowing it
# would require some hacks.
for inp in inputs:
f = file('foo', 'w')
f.write(inp)
f.close()
f = file('foo')
AreEqual(f.readlines(), ['abcdef'])
f.close()
for inp in text_inputs:
f = file('foo', 'w')
AssertError(TypeError, f.write, inp)
f.close()
run_test(__name__)