Debian Codegraph @

starlark/testdata/bytes.star

raw
1 # Tests of 'bytes' (immutable byte strings).
2
3 load("assert.star", "assert")
4
5 # bytes(string) -- UTF-k to UTF-8 transcoding with U+FFFD replacement
6 hello = bytes("hello, 世界")
7 goodbye = bytes("goodbye")
8 empty = bytes("")
9 nonprinting = bytes("\t\n\x7F\u200D") # TAB, NEWLINE, DEL, ZERO_WIDTH_JOINER
10 assert.eq(bytes("hello, 世界"[:-1]), b"hello, 世��")
11
12 # bytes(iterable of int) -- construct from numeric byte values
13 assert.eq(bytes([65, 66, 67]), b"ABC")
14 assert.eq(bytes((65, 66, 67)), b"ABC")
15 assert.eq(bytes([0xf0, 0x9f, 0x98, 0xbf]), b"😿")
16 assert.fails(lambda: bytes([300]),
17 "at index 0, 300 out of range .want value in unsigned 8-bit range")
18 assert.fails(lambda: bytes([b"a"]),
19 "at index 0, got bytes, want int")
20 assert.fails(lambda: bytes(1), "want string, bytes, or iterable of ints")
21
22 # literals
23 assert.eq(b"hello, 世界", hello)
24 assert.eq(b"goodbye", goodbye)
25 assert.eq(b"", empty)
26 assert.eq(b"\t\n\x7F\u200D", nonprinting)
27 assert.ne("abc", b"abc")
28 assert.eq(b"\012\xff\u0400\U0001F63F", b"\n\xffЀ😿") # see scanner tests for more
29 assert.eq(rb"\r\n\t", b"\\r\\n\\t") # raw
30
31 # type
32 assert.eq(type(hello), "bytes")
33
34 # len
35 assert.eq(len(hello), 13)
36 assert.eq(len(goodbye), 7)
37 assert.eq(len(empty), 0)
38 assert.eq(len(b"A"), 1)
39 assert.eq(len(b"Ѐ"), 2)
40 assert.eq(len(b"世"), 3)
41 assert.eq(len(b"😿"), 4)
42
43 # truth
44 assert.true(hello)
45 assert.true(goodbye)
46 assert.true(not empty)
47
48 # str(bytes) does UTF-8 to UTF-k transcoding.
49 # TODO(adonovan): specify.
50 assert.eq(str(hello), "hello, 世界")
51 assert.eq(str(hello[:-1]), "hello, 世��") # incomplete UTF-8 encoding => U+FFFD
52 assert.eq(str(goodbye), "goodbye")
53 assert.eq(str(empty), "")
54 assert.eq(str(nonprinting), "\t\n\x7f\u200d")
55 assert.eq(str(b"\xED\xB0\x80"), "���") # UTF-8 encoding of unpaired surrogate => U+FFFD x 3
56
57 # repr
58 assert.eq(repr(hello), r'b"hello, 世界"')
59 assert.eq(repr(hello[:-1]), r'b"hello, 世\xe7\x95"') # (incomplete UTF-8 encoding )
60 assert.eq(repr(goodbye), 'b"goodbye"')
61 assert.eq(repr(empty), 'b""')
62 assert.eq(repr(nonprinting), 'b"\\t\\n\\x7f\\u200d"')
63
64 # equality
65 assert.eq(hello, hello)
66 assert.ne(hello, goodbye)
67 assert.eq(b"goodbye", goodbye)
68
69 # ordered comparison
70 assert.lt(b"abc", b"abd")
71 assert.lt(b"abc", b"abcd")
72 assert.lt(b"\x7f", b"\x80") # bytes compare as uint8, not int8
73
74 # bytes are dict-hashable
75 dict = {hello: 1, goodbye: 2}
76 dict[b"goodbye"] = 3
77 assert.eq(len(dict), 2)
78 assert.eq(dict[goodbye], 3)
79
80 # hash(bytes) is 32-bit FNV-1a.
81 assert.eq(hash(b""), 0x811c9dc5)
82 assert.eq(hash(b"a"), 0xe40c292c)
83 assert.eq(hash(b"ab"), 0x4d2505ca)
84 assert.eq(hash(b"abc"), 0x1a47e90b)
85
86 # indexing
87 assert.eq(goodbye[0], b"g")
88 assert.eq(goodbye[-1], b"e")
89 assert.fails(lambda: goodbye[100], "out of range")
90
91 # slicing
92 assert.eq(goodbye[:4], b"good")
93 assert.eq(goodbye[4:], b"bye")
94 assert.eq(goodbye[::2], b"gobe")
95 assert.eq(goodbye[3:4], b"d") # special case: len=1
96 assert.eq(goodbye[4:4], b"") # special case: len=0
97
98 # bytes in bytes
99 assert.eq(b"bc" in b"abcd", True)
100 assert.eq(b"bc" in b"dcab", False)
101 assert.fails(lambda: "bc" in b"dcab", "requires bytes or int as left operand, not string")
102
103 # int in bytes
104 assert.eq(97 in b"abc", True) # 97='a'
105 assert.eq(100 in b"abc", False) # 100='d'
106 assert.fails(lambda: 256 in b"abc", "int in bytes: 256 out of range")
107 assert.fails(lambda: -1 in b"abc", "int in bytes: -1 out of range")
108
109 # ord TODO(adonovan): specify
110 assert.eq(ord(b"a"), 97)
111 assert.fails(lambda: ord(b"ab"), "ord: bytes has length 2, want 1")
112 assert.fails(lambda: ord(b""), "ord: bytes has length 0, want 1")
113
114 # concatenation (bytes + bytes)
115 # The compiler folds sums of adjacent bytes literals, so each case below keeps at
116 # least one operand dynamic to exercise the interpreter instead.
117 def concat(x, y):
118 return x + y
119
120 assert.eq(concat(b"abc", b"def"), b"abc" + b"def") # both paths must agree
121 assert.eq(goodbye + b"!", b"goodbye!")
122 assert.eq(b"good" + goodbye[4:], goodbye)
123 assert.eq(b"[" + goodbye + b"]", b"[goodbye]")
124 assert.eq(goodbye + empty, goodbye)
125 assert.eq(empty + goodbye, goodbye)
126
127 # Concatenation joins bytes, not text: no UTF-8 validation or U+FFFD replacement.
128 assert.eq(concat(b"\xed\xb0", b"\x80"), b"\xed\xb0\x80")
129 assert.eq(concat(hello[:-1], hello[-1:]), hello) # split mid-code-point
130
131 def inplace():
132 x = goodbye
133 x += b"!"
134 return x
135
136 assert.eq(inplace(), b"goodbye!")
137
138 # Text and binary strings do not mix.
139 assert.fails(lambda: goodbye + "!", "unknown binary op: bytes \\+ string")
140 assert.fails(lambda: "!" + goodbye, "unknown binary op: string \\+ bytes")
141
142 # repeat (bytes * int)
143 assert.eq(goodbye * 3, b"goodbyegoodbyegoodbye")
144 assert.eq(3 * goodbye, b"goodbyegoodbyegoodbye")
145
146 # elems() returns an iterable value over 1-byte substrings.
147 assert.eq(type(hello.elems()), "bytes.elems")
148 assert.eq(str(hello.elems()), "b\"hello, 世界\".elems()")
149 assert.eq(list(hello.elems()), [104, 101, 108, 108, 111, 44, 32, 228, 184, 150, 231, 149, 140])
150 assert.eq(bytes([104, 101, 108, 108, 111, 44, 32, 228, 184, 150, 231, 149, 140]), hello)
151 assert.eq(list(goodbye.elems()), [103, 111, 111, 100, 98, 121, 101])
152 assert.eq(list(empty.elems()), [])
153 assert.eq(bytes(hello.elems()), hello) # bytes(iterable) is dual to bytes.elems()
154
155 # x[i] = ...
156 def f():
157 b"abc"[1] = b"B"
158
159 assert.fails(f, "bytes.*does not support.*assignment")
160
161 # TODO(adonovan): the specification is not finalized in many areas:
162 # - chr, ord functions
163 # - encoding/decoding bytes to string.
164 # - methods: find, index, split, etc.
165 #
166 # Summary of string operations (put this in spec).
167 #
168 # string to number:
169 # - bytes[i] returns numeric value of ith byte.
170 # - ord(string) returns numeric value of sole code point in string.
171 # - ord(string[i]) is not a useful operation: fails on non-ASCII; see below.
172 # Q. Perhaps ord should return the first (not sole) code point? Then it becomes a UTF-8 decoder.
173 # Perhaps ord(string, index=int) should apply the index and relax the len=1 check.
174 # - string.codepoint() iterates over 1-codepoint substrings.
175 # - string.codepoint_ords() iterates over numeric values of code points in string.
176 # - string.elems() iterates over 1-element (UTF-k code) substrings.
177 # - string.elem_ords() iterates over numeric UTF-k code values.
178 # - string.elem_ords()[i] returns numeric value of ith element (UTF-k code).
179 # - string.elems()[i] returns substring of a single element (UTF-k code).
180 # - int(string) parses string as decimal (or other) numeric literal.
181 #
182 # number to string:
183 # - chr(int) returns string, UTF-k encoding of Unicode code point (like Python).
184 # Redundant with '%c' % int (which Python2 calls 'unichr'.)
185 # - bytes(chr(int)) returns byte string containing UTF-8 encoding of one code point.
186 # - bytes([int]) returns 1-byte string (with regrettable list allocation).
187 # - str(int) - format number as decimal.