forked from livecode/livecode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbyte.lcb
More file actions
329 lines (258 loc) · 12.2 KB
/
byte.lcb
File metadata and controls
329 lines (258 loc) · 12.2 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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
/*
Copyright (C) 2015 LiveCode Ltd.
This file is part of LiveCode.
LiveCode is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License v3 as published by the Free
Software Foundation.
LiveCode is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.
You should have received a copy of the GNU General Public License
along with LiveCode. If not see <http://www.gnu.org/licenses/>. */
module com.livecode.byte.tests
use com.livecode.foreign
use com.livecode.__INTERNAL._testlib
public handler TestEmpty()
test "empty" when the number of bytes in the empty data is 0
end handler
----------------------------------------------------------------
-- Integer conversion
----------------------------------------------------------------
handler TestByteWithCode_Negative()
return the byte with code -1
end handler
handler TestByteWithCode_Overflow()
return the byte with code 256
end handler
public handler TestByteWithCode()
variable tString
put the byte with code 0 into tString
test "code->byte" when the number of bytes in tString is 1
MCUnitTestHandlerThrows(TestByteWithCode_Negative, "code->byte (negative)")
MCUnitTestHandlerThrows(TestByteWithCode_Overflow, "code->byte (overflow)")
end handler
handler TestCodeOfByte_Long()
variable tString
put the byte with code 0 into tString
put tString after tString
return the code of tString
end handler
handler TestCodeOfByte_Empty()
variable tString
put the empty data into tString
return the code of tString
end handler
public handler TestCodeOfByte()
test "byte->code" when the code of (the byte with code 1) is 1
MCUnitTestHandlerThrows(TestCodeOfByte_Long, "byte->code (multibyte)")
MCUnitTestHandlerThrows(TestCodeOfByte_Empty, "byte->code (empty)")
end handler
----------------------------------------------------------------
-- Finding subsequence offsets
----------------------------------------------------------------
public handler TestOffset()
variable N as Data
variable H as Data
-- Only test single-byte needles for now
put EncodeUTF8("x") into N
put EncodeUTF8(".xx.") into H
test "offset (single)" when the offset of N in H is 2
test "first offset (single)" when the first offset of N in H is 2
test "last offset (single)" when the last offset of N in H is 3
put EncodeUTF8("xx") into N
put EncodeUTF8(".x.x.") into H
test "offset (missing)" when the offset of N in H is 0
put EncodeUTF8("x") into N
put EncodeUTF8("x") into H
test "offset (single, same)" when the offset of N in H is 1
test "first offset (single, same)" when the first offset of N in H is 1
test "last offset (single, same)" when the last offset of N in H is 1
put the empty data into N
put EncodeUTF8("x") into H
test "offset (empty)" when the offset of N in H is 0
test "first offset (empty)" when the first offset of N in H is 0
test "last offset (empty)" when the last offset of N in H is 0
test "offset (empty, empty)" when the offset of N in the empty data is 0
-- For bytes, "index" is synonymous with "offset"
put EncodeUTF8("x") into N
put EncodeUTF8(".xx.") into H
test "index" when the index of N in H is 2
end handler
----------------------------------------------------------------
public handler TestOffsetAfter()
variable N as Data
variable H as Data
-- Only test single-byte needles for now
put EncodeUTF8("x") into N
put EncodeUTF8("x.xx.") into H
test "offset after (single, +ve)" when the offset of N after 1 in H is 3
test "first offset after (single, +ve)" when the first offset of N after 1 in H is 3
test "last offset after (single, +ve)" when the last offset of N after 1 in H is 4
test "offset after (single, -ve)" when the offset of N after -5 in H is 3
test "first offset after (single, -ve)" when the first offset of N after -5 in H is 3
test "last offset after (single, -ve)" when the last offset of N after -5 in H is 4
-- For bytes, "index" is synonymous with "offset"
test "index after" when the index of N after 1 in H is 3
put EncodeUTF8("x") into N
put EncodeUTF8("x..") into H
test "offset after (single, missing, +ve)" when the offset of N after 1 in H is 0
test "first offset after (single, missing, +ve)" when the first offset of N after 1 in H is 0
test "last offset after (single, missing, +ve)" when the last offset of N after 1 in H is 0
test "offset after (single, missing, -ve)" when the offset of N after -3 in H is 0
test "first offset after (single, missing, -ve)" when the first offset of N after -3 in H is 0
test "last offset after (single, missing, -ve)" when the last offset of N after -3 in H is 0
MCUnitTestHandlerThrows(TestOffsetAfter_SingleInvalidPositive, "offset after (single, invalid +ve)")
MCUnitTestHandlerThrows(TestOffsetAfter_SingleInvalidNegative, "offset after (single, invalid -ve)")
MCUnitTestHandlerThrows(TestFirstOffsetAfter_SingleInvalidPositive, "first offset after (single, invalid +ve)")
MCUnitTestHandlerThrows(TestFirstOffsetAfter_SingleInvalidNegative, "first offset after (single, invalid -ve)")
MCUnitTestHandlerThrows(TestLastOffsetAfter_SingleInvalidPositive, "last offset after (single, invalid +ve)")
MCUnitTestHandlerThrows(TestLastOffsetAfter_SingleInvalidNegative, "last offset after (single, invalid -ve)")
end handler
handler TestOffsetAfter_SingleInvalidPositive()
get the offset of the byte with code 0 after 2 in the byte with code 0
end handler
handler TestOffsetAfter_SingleInvalidNegative()
get the offset of the byte with code 0 after -3 in the byte with code 0
end handler
handler TestFirstOffsetAfter_SingleInvalidPositive()
get the first offset of the byte with code 0 after 2 in the byte with code 0
end handler
handler TestFirstOffsetAfter_SingleInvalidNegative()
get the first offset of the byte with code 0 after -3 in the byte with code 0
end handler
handler TestLastOffsetAfter_SingleInvalidPositive()
get the last offset of the byte with code 0 after 2 in the byte with code 0
end handler
handler TestLastOffsetAfter_SingleInvalidNegative()
get the last offset of the byte with code 0 after -3 in the byte with code 0
end handler
public handler TestOffsetAfterZero()
variable N as Data
variable H as Data
put EncodeUTF8("x") into N
put EncodeUTF8("x") into H
-- "offset of _ after 0 in _" should be equivalent to "offset of _ in _"
variable tNoAfter
put the offset of N in H into tNoAfter
test "offset after (single, 0)" when the offset of N after 0 in H is tNoAfter
put the first offset of N in H into tNoAfter
test "first offset after (single, 0)" when the first offset of N after 0 in H is tNoAfter
put the last offset of N in H into tNoAfter
test "the last offset after (single, 0, same)" when the last offset of N after 0 in H is tNoAfter
put EncodeUTF8("x") into N
put EncodeUTF8(".x.") into H
put the last offset of N in H into tNoAfter
test "last offset after (single, 0)" when the last offset of N after 0 in H is tNoAfter
end handler
----------------------------------------------------------------
public handler TestOffsetBefore()
variable N as Data
variable H as Data
-- Only test single-byte needles for now
put EncodeUTF8("x") into N
put EncodeUTF8("x.xx.") into H
test "offset before (single, +ve)" when the offset of N before 4 in H is 3
test "first offset before (single, +ve)" when the first offset of N before 4 in H is 1
test "last offset before (single, +ve)" when the last offset of N before 4 in H is 3
test "offset before (single, -ve)" when the offset of N before -2 in H is 3
test "first offset before (single, -ve)" when the first offset of N before -2 in H is 1
test "last offset before (single, -ve)" when the last offset of N before -2 in H is 3
-- For bytes, "index" is synonymous with "offset"
test "index before" when the index of N before 4 in H is 3
put EncodeUTF8("x") into N
put EncodeUTF8("x..") into H
test "offset before (single, missing, +ve)" when the offset of N before 1 in H is 0
test "first offset before (single, missing, +ve)" when the first offset of N before 1 in H is 0
test "last offset before (single, missing, +ve)" when the last offset of N before 1 in H is 0
test "offset before (single, missing, -ve)" when the offset of N before -3 in H is 0
test "first offset before (single, missing, -ve)" when the first offset of N before -3 in H is 0
test "last offset before (single, missing, -ve)" when the last offset of N before -3 in H is 0
MCUnitTestHandlerThrows(TestOffsetBefore_SingleInvalidPositive, "offset before (single, invalid +ve)")
MCUnitTestHandlerThrows(TestOffsetBefore_SingleInvalidNegative, "offset before (single, invalid -ve)")
MCUnitTestHandlerThrows(TestFirstOffsetBefore_SingleInvalidPositive, "first offset before (single, invalid +ve)")
MCUnitTestHandlerThrows(TestFirstOffsetBefore_SingleInvalidNegative, "first offset before (single, invalid -ve)")
MCUnitTestHandlerThrows(TestLastOffsetBefore_SingleInvalidPositive, "last offset before (single, invalid +ve)")
MCUnitTestHandlerThrows(TestLastOffsetBefore_SingleInvalidNegative, "last offset before (single, invalid -ve)")
end handler
handler TestOffsetBefore_SingleInvalidPositive()
get the offset of the byte with code 0 before 3 in the byte with code 0
end handler
handler TestOffsetBefore_SingleInvalidNegative()
get the offset of the byte with code 0 before -2 in the byte with code 0
end handler
handler TestFirstOffsetBefore_SingleInvalidPositive()
get the first offset of the byte with code 0 before 3 in the byte with code 0
end handler
handler TestFirstOffsetBefore_SingleInvalidNegative()
get the first offset of the byte with code 0 before -2 in the byte with code 0
end handler
handler TestLastOffsetBefore_SingleInvalidPositive()
get the last offset of the byte with code 0 before 3 in the byte with code 0
end handler
handler TestLastOffsetBefore_SingleInvalidNegative()
get the last offset of the byte with code 0 before -2 in the byte with code 0
end handler
public handler TestOffsetBeforeZero()
variable N as Data
variable H as Data
put EncodeUTF8("x") into N
put EncodeUTF8("x") into H
-- "offset of _ before 0 in _" should be equivalent to "last offset of _ in _"
variable tNoBefore
put the last offset of N in H into tNoBefore
test "offset before (single, 0)" when the offset of N before 0 in H is tNoBefore
return
put the first offset of N in H into tNoBefore
test "first offset before (single, 0)" when the first offset of N before 0 in H is tNoBefore
put the last offset of N in H into tNoBefore
test "the last offset before (single, 0, same)" when the last offset of N before 0 in H is tNoBefore
put EncodeUTF8("x") into N
put EncodeUTF8(".x.") into H
put the last offset of N in H into tNoBefore
test "last offset before (single, 0)" when the last offset of N before 0 in H is tNoBefore
end handler
----------------------------------------------------------------
-- Other manipulations
----------------------------------------------------------------
public handler TestReverse()
variable kFOdd
put EncodeUTF8("abc") into kFOdd
variable kROdd
put EncodeUTF8("cba") into kROdd
variable kFEven
put EncodeUTF8("0123") into kFEven
variable kREven
put EncodeUTF8("3210") into kREven
variable tReversed
put kFOdd into tReversed
reverse tReversed
test "reverse in-place (odd)" when tReversed is kROdd
reverse tReversed
test "rereverse in-place (odd)" when tReversed is kFOdd
put kFEven into tReversed
reverse tReversed
test "reverse in-place (even)" when tReversed is kREven
reverse tReversed
test "rereverse in-place (even)" when tReversed is kFEven
put the empty data into tReversed
reverse tReversed
test "reverse in-place (empty)" when tReversed is empty
end handler
----------------------------------------------------------------
-- Helper functions
----------------------------------------------------------------
constant kEncodingIndexUTF8 is 4
foreign handler MCStringEncode(in Unencoded as String, in Encoding as CInt, in IsExternalRep as CBool, out Encoded as Data) returns CBool binds to "<builtin>"
handler EncodeUTF8(in pString as String) returns Data
variable tEncoded as Data
variable tStatus as Boolean
unsafe
MCStringEncode(pString, kEncodingIndexUTF8, false, tEncoded)
end unsafe
if tEncoded is empty then
return the empty data
end if
return tEncoded
end handler
end module