-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_StrBuf.py
executable file
·519 lines (433 loc) · 18.1 KB
/
test_StrBuf.py
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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
#!/usr/bin/env python3
#
# test_strbuf: Basic testing for strbuf or other string classes.
# 2021-08-03: Written by Steven J. DeRose.
#
#pylint: disable=W0221 # To allow added 'inplace' args.
#
import unittest
import codecs
import math
import string
from types import SimpleNamespace
from typing import List # Callable, Iterable, IO, Dict, Union
import time
import gc
import random
import logging
from strbuf import StrBuf
import array
from basedomtypes import SIO
# from io import StringIO
# from mutablestring import mstring
# (str, bytes, list)
mutableClasses = (StrBuf, array.array, SIO, list)
lg = logging.getLogger()
__metadata__ = {
"title" : "test_strbuf",
"description" : "Test driver for strbuf or other string classes.",
"rightsHolder" : "Steven J. DeRose",
"creator" : "http://viaf.org/viaf/50334488",
"type" : "http://purl.org/dc/dcmitype/Software",
"language" : "Python 3.7",
"created" : "2023-08-05",
"modified" : "2025-01-07",
"publisher" : "http://github.com/sderose",
"license" : "https://creativecommons.org/licenses/by-sa/3.0/"
}
__version__ = __metadata__["modified"]
descr = """
=Description=
Test at least basic functionality for StrBuf or other str-like classes.
=To do=
See if there's a standard Python str test suite to apply.
"""
args = SimpleNamespace(**{
"atEnd": False, # "Do appending rather than prepending."
"deletePer": 0, # "If set, do a random delection every N adds."
"maxPower": 2, # "Last power of 2 for size to run timing for."
"missing": False, # "List methods that are on str but not StrBuf."
"partDft": 512, # "Size preference for separate blocks of a string."
"partMax": 1024, # "Size limit for separate blocks of a string."
"smokeLength": 0, # "Size for random smoketest string.")
"width": 40,
})
def loadDict(path:str="/usr/share/dict/words") -> List:
theWords = []
with codecs.open(path, "rb", encoding="utf-8") as ifh:
theWords.extend([ x.strip() for x in ifh.readlines() ])
print("Loaded %d words from %s." % (len(theWords), path))
return theWords
dictWords = loadDict()
nwords = len(dictWords)
w1 = (
"Adam Beth Cris Dave ever fond grow high isle jump " +
"knot lamp melt nope Owen Paul quit rope sees test " +
"Urdo vent wrap Xeno yurt zoom " )
src = """Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do
eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut
enim ad minim veniam, quis nostrud exercitation ullamco laboris
nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in
reprehenderit in voluptate velit esse cillum dolore eu fugiat
nulla pariatur. Excepteur sint occaecat cupidatat non proident,
sunt in culpa qui officia deserunt mollit anim id est laborum."""
def genText(n:int=20000) -> str:
s = ""
n = min(n, len(dictWords))
while len(s) < n:
s += dictWords[random.randrange(0, nwords)] + " "
return s
def warmup() -> str:
print("Warming up the CPU...")
buf = ""
for _i in range(10000):
cp = random.randrange(32, 126)
buf += chr(cp)
return buf
def timer(maxPower: int, partMax: int, words:List):
warmup()
print("\nTesting StrBuf")
s = StrBuf("")
s.setSizes(partMax=partMax)
for p in range(maxPower+1):
print("\n\n================= 2**%d" % (p))
gc.collect()
n = 2**p
t0 = time.time()
s.clear()
for i in range(n):
if (args.atEnd): s.append(words[i % nwords] + " ")
else: s.insert(0, words[i % nwords] + " ")
lg.warning("### '%s'", s)
if (args.deletePer and i % args.deletePer == 0):
delStart = random.randint(0, len(s)-10)
delEnd = delStart + random.randint(
0, math.floor(args.partMax*1.5))
if (delEnd >= len(s)): delEnd = delStart + 5
lg.warning("Deleting [%d:%d]", delStart, delEnd)
s.delete(delStart,delEnd)
t1 = time.time()
msec = 1000.0 * (t1-t0)
print("Added 2**%-2d (%8d) words: %8.3fms: %6.3f µs/word, final len %10d" %
(p, n, msec, msec*1000/n, len(s)))
m = s.min()
print("Min: '%s' (U+%04x)" % (m, ord(m)))
s.clear()
def makeOne(s:str):
#return str(s)
return SIO(s)
return StrBuf(s)
###############################################################################
#
class testStrBuf(unittest.TestCase):
def setUp(self):
self.s1 = makeOne(w1)
self.assertEqual(len(self.s1), len(w1))
self.s2 = makeOne(src)
self.short = makeOne("Laughably short string")
def testTime(self):
timer(maxPower=8, partMax=512, words=dictWords)
def testCast(self):
self.assertIsInstance(bool(self.s1), bool)
self.assertIsInstance(str(self.s1), str)
self.assertEqual(bool(self.s1), True)
self.assertEqual(str(self.s1), self.s1.getvalue())
def testCompare(self):
print("Testing compares.")
first = makeOne("this is a strinz.")
other = makeOne("this is a string.")
self.assertEqual(first[0], "t")
self.assertEqual(other[-2], "g")
self.assertEqual("is", other[5:7])
self.assertFalse(first == other)
self.assertTrue(first != other)
self.assertTrue(first >= other)
self.assertTrue(first > other)
self.assertFalse(first <= other)
self.assertFalse(first < other)
self.assertEqual(first.__cmp__(other), 1)
self.assertEqual(len(self.s1.splitlines(keepends=True)), 1)
self.assertEqual(len(self.s2.splitlines(keepends=True)), 7)
def testSearch(self):
tgt = w1[0:len(w1)>>1]
tgt2 = w1[len(w1)>>2:]
sub = "the"
self.assertEqual(self.s1.startswith(tgt), True)
self.assertEqual(self.s1.endswith(tgt), False)
self.assertEqual(self.s1.startswith(tgt2), False)
self.assertEqual(self.s1.endswith(tgt2), True)
with self.assertRaises(ValueError):
self.s1.index(sub, 100, 2000)
def testInAndContains(self):
# These wonky.... For str, there is:
# An "in" infix operator, but no "contains"
# A "__contains__" dunder, but no "__in__"
# And the one dunder, has to flips its args.
# And TypeError refers to "left" operand!
#
bigStr = "aardvark"
tgtStr = "v"
bigObj = makeOne(bigStr)
tgtObj = makeOne(tgtStr)
# First, bypassing the dispatching from infix to dunder:
self.assertTrue(bigStr.__contains__(tgtStr)) # This sure better work
self.assertTrue(bigObj.__contains__(tgtStr))
self.assertTrue(bigObj.__contains__(tgtObj))
# Then try the dispatching
self.assertTrue(tgtStr in bigStr)
self.assertTrue(tgtStr in bigObj)
self.assertTrue(tgtObj in bigObj)
# These fail on "requires string as left operand, not SIO" TODO
#self.assertTrue(bigStr.__contains__(tgtObj))
#self.assertTrue(tgtObj in bigStr)
aNum = SIO("93124")
self.assertEqual(int(str(aNum)), 93124)
self.assertEqual(aNum.__int__(), 93124)
self.assertEqual(int(aNum), 93124)
self.assertEqual(93124, int(aNum))
aNonNum = makeOne("93*124")
with self.assertRaises(ValueError):
int(aNonNum)
# TODO int() haas similar problem with non-str
#self.assertEqual(int(aNum, 10), 93124)
def testInfo(self):
self.assertEqual(len(self.s1), len(w1))
#self.assertEqual(self.s1.hash(), w1.hash())
for i in range(len(self.s1)):
self.assertEqual(self.s1[i], w1[i])
def testProperties(self):
#fillchar = "*"
inplace = True
other = "Some other text"
s2 = self.s1 * 3 # TODO inplace?
s3 = self.s1 + other
self.assertEqual(len(s2.getvalue()), len(self.s1.getvalue())*3)
self.assertEqual(len(s2), len(self.s1)*3)
# TODO
#w = args.width
#self.assertEqual(len(self.s1.zfill(w, inplace)), w)
#self.assertEqual(len(self.s1.ljust(w, fillchar, inplace)), w)
#self.assertEqual(len(self.s1.rjust(w, fillchar, inplace)), w)
#self.assertEqual(len(self.s1.center(w, fillchar, inplace)), w)
#s2 = self.s1.reverse(inplace=False)
# TODO More Unicode examples.
for wShort in [ "", "abc", "XYZ", "0354", "\u2420", "Python7", " \t\n\r\f" ]:
sShort = makeOne(wShort)
self.assertEqual(sShort.isalnum(), wShort.isalnum())
self.assertEqual(sShort.isalpha(), wShort.isalpha())
self.assertEqual(sShort.isascii(), wShort.isascii())
self.assertEqual(sShort.isdecimal(), wShort.isdecimal())
self.assertEqual(sShort.isdigit(), wShort.isdigit())
self.assertEqual(sShort.isidentifier(), wShort.isidentifier())
self.assertEqual(sShort.islower(), wShort.islower())
self.assertEqual(sShort.isnumeric(), wShort.isnumeric())
self.assertEqual(sShort.isprintable(), wShort.isprintable())
self.assertEqual(sShort.isspace(), wShort.isspace())
self.assertEqual(sShort.istitle(), wShort.istitle())
self.assertEqual(sShort.isupper(), wShort.isupper())
#self.s1.isa(isaWhat)
def testCaseFoldersImmutable(self):
if not isinstance(self.s1, mutableClasses):
print("Skipping testCaseFolders for class %s." % (type(self.s1)))
return
wShort = "All THE words (73) are here."
sShort = makeOne(wShort)
# TODO Gotta make eq work, too.
self.assertTrue(str(sShort.casefold()) == wShort.casefold())
sShort = makeOne(wShort)
self.assertTrue(str(sShort.lower()) == wShort.lower())
sShort = makeOne(wShort)
self.assertTrue(str(sShort.capitalize()) == wShort.capitalize())
sShort = makeOne(wShort)
self.assertTrue(str(sShort.title()) == wShort.title())
sShort = makeOne(wShort)
self.assertTrue(str(sShort.swapcase()) == wShort.swapcase())
sShort = makeOne(wShort)
table = str.maketrans(string.ascii_lowercase, string.ascii_uppercase)
self.assertTrue(str(sShort.translate(table)) == wShort.translate(table))
##############################################################################
### Mutators
### Since a StrBuf is mutable, anything that produces a modified version has
### to decide whether to matate in place, or create and return a new object.
### Those methods all take an optional 'inplace' argument.
###
def testMutators(self):
if not isinstance(self.s1, mutableClasses):
print("Skipping testMutators for class %s." % (type(self.s1)))
return
pre = w1[0:10]
suf = w1[-10:]
tmp = makeOne(w1)
self.assertEqual(str(tmp.removeprefix(suf)), w1)
self.assertTrue(tmp.startswith(pre))
self.assertEqual(str(tmp.removeprefix(pre)), w1[10:])
tmp = makeOne(w1)
self.assertEqual(str(tmp.removesuffix(pre)), w1)
self.assertEqual(w1[-10:], str(tmp[-10:]))
self.assertTrue(tmp.endswith(suf))
self.assertEqual(str(tmp.removesuffix(suf)), w1[0:-10])
tmp = makeOne(w1)
self.assertEqual(str(tmp.delete(100, 200)), w1[0:100] + w1[100:])
s2 = makeOne()
s3 = s2.reversed()
self.assertEqual(len(s2) == len(s3))
self.assertEqual(str(s2) == str(s3).reversed())
reps = 1000
s2 = makeOne("")
for i in range(reps): s2 = s2.append(w1smoke)
self.assertEqual(len(s2), reps * len(w1smoke))
self.assertEqual(str(s2), w1smoke * reps)
def testModify(self):
if not isinstance(self.s1, mutableClasses):
print("Skipping testModify for class %s." % (type(self.s1)))
return
str0 = "aardvark " * 20
obj0 = makeOne(str0)
str1 = "anteater"
obj1 = makeOne(str1)
obj0.append(obj1)
self.assertEqual(str0+str1, str(obj0))
#s0.appendShort(pnum, s) ######## __?
#s0.prependShort(pnum, s) ######## __?
obj3 = obj0.copy()
self.assertEqual(str(obj0), str(obj3))
# self.assertEqual(obj0, obj3) # TODO
obj3.insert(100, obj1)
self.assertEqual(str0[0:100]+str1+str0[100:], str(obj3))
obj4 = obj1.copy().pop(12)
obj5 = makeOne(w1)
insertAt = 100
obj5.insert(insertAt, str1)
self.assertEqual(obj5, w1[0:insertAt] + str1 + w1[insertAt:])
p1 = makeOne("docbook:para")
p2 = makeOne(":")
pre, colon, post = p1.partition(p2)
self.assertEqual(colon, ":")
self.assertEqual(pre+colon+post, p1)
sJoiner = "###"
tokens = [ dictWords[random.randint(0, nwords)] for i in range(1000) ]
sbTokens = [ makeOne(t) for t in tokens ]
tjoined = sJoiner.join(tokens)
sbtjoined = sJoiner.join(sbTokens)
self.assertEqual(tjoined, sbtjoined)
def testNormalizers(self):
if not isinstance(self.s1, mutableClasses):
print("Skipping testNormalizers for class %s." % (type(self.s1)))
return
self.assertIsInstance(self.s1.tostring(), str)
self.assertEqual(self.s1.tostring(), str(self.s1))
w2 = "Hello\tto\t\tall\tthish...."
s2 = makeOne(w2)
s3 = self.s1.expandtabs(4)
self.assertFalse("\t" in s3)
s2.strip("Hh.")
print(s2)
s2.lstrip("Hel")
self.assertEqual(str(s2), w2[4:])
s2.rstrip("")
self.assertEqual(stR(s2), w2[4:])
def testCaseFoldersInplace(self):
if not isinstance(self.s1, StrBuf):
print("Skipping testCaseFolders for class %s." % (type(self.s1)))
return
inplace = True
wShort = "All THE words (73) are here."
sShort = makeOne(wShort)
self.assertEqual(sShort.casefold(inplace=True), wShort.casefold())
self.assertEqual(sShort.lower(inplace=True), wShort.lower())
self.assertEqual(sShort.capitalize(inplace=True), wShort.capitalize())
self.assertEqual(sShort.title(inplace=True), wShort.title())
self.assertEqual(sShort.swapcase(inplace=True), wShort.swapcase())
self.assertEqual(sShort.casefold(inplace=False), wShort.casefold())
self.assertEqual(sShort.lower(inplace=False), wShort.lower())
self.assertEqual(sShort.capitalize(inplace=False), wShort.capitalize())
self.assertEqual(sShort.title(inplace=False), wShort.title())
self.assertEqual(sShort.swapcase(inplace=False), wShort.swapcase())
table = str.maketrans(string.ascii_lowercase, string.ascii_uppercase)
self.assertEqual(sShort.translate(table, inplace), wShort.translate(table))
self.assertEqual(sShort.upper(inplace), wShort.upper())
#assert sShort.applyToAllParts(what, inplace) == wShort.applyToAllParts(what)
def testStrBufSpecials(self):
if (not isinstance(self.s1, StrBuf)):
print("Skipping testStrBufSpecials for class %s." % (type(self.s1)))
return
self.assertEqual(self.s1.copy(), self.s1)
# This should assert if something's wrong.
self.s1.check()
# TODO ??? self.s1.iter()
#tmp.__delByPairs__(pnum0, offset0, pnum1, offset1)
tmp = makeOne(w1)
# Ensure there *are* at least 3 parts...
while len(tmp.parts) < 3:
tmp += w1
lenPre = len(tmp)
len3 = len(tmp.parts[3])
tmp.deletePart(3)
self.assertEqual(len(tmp), lenPre - len3)
s2 = self.s1.copy()
nParts = len(s2.parts)
for i in range(nParts, 0, -1):
s2.splitPart(i, len(s2.parts[i])>>2)
self.assertEqual(s2, self.s1)
#self.s1.insertPart(pnum, s)
s2.clear()
self.assertEqual(s2, "")
#self.s1.availInPart(pnum, forDft)
self.s1.getPackingFactor()
self.s1.__coalesce__(3)
tgt = self.s1[200:20]
#self.s1.local2global(pnum, localOffset)
if isinstance(self.s1, StrBuf):
StrBuf.longestPrefixAtEnd(self.s1, tgt)
#self.s1.getChars(st, fin)
#self.s1.getChar(tgt)
#self.s1.findCharN(tgt)
s = makeOne(w1smoke)
x1 = " Some additional text"
p, o = s.findCharN(3)
print("findCharN(3) gives part %d, offset %d (total len %d)."
% (p, o, len(s)))
p, o = s.findCharN(-3)
print("findCharN(-3) gives part %d, offset %d (total len %d)."
% (p, o, len(s)))
print("s[0:10] is '%s'." % (s[0:10]))
print("s[-10:] is '%s'." % (s[-10:]))
self.s1.__repack__()
return
def testStrBufInplace(self):
if (not isinstance(self.s1, StrBuf)):
print("Skipping testStrBufInplace for class %s." % (type(self.s1)))
return
tmp = makeOne(w1)
tmp.removeprefix(suf, inplace=True)
self.assertEqual(str(tmp), w1)
self.assertTrue(tmp.startswith(pre))
tmp.removeprefix(pre, inplace=True)
self.assertEqual(str(tmp), w1[10:])
tmp = makeOne(w1)
tmp.removesuffix(pre, inplace=True)
self.assertEqual(str(tmp), w1)
self.assertEqual(w1[-10:], str(tmp[-10:]))
self.assertTrue(tmp.endswith(suf))
tmp.removesuffix(suf, inplace=True)
self.assertEqual(str(tmp), w1[0:-10])
###############################################################################
#
class testSomeMore(unittest.TestCase):
def setUp(self):
self.s1 = makeOne(w1)
def testsmoke(self):
w1smoke = w1
if (args.smokeLength):
w1smoke += genText(n=args.smokeLength)
s = makeOne(w1smoke)
tgt = "knot"
tgtPos = 50
if (w1smoke.index(tgt) != tgtPos):
print("counted wrong for 'knot', it's at %d." % (w1smoke.index(tgt)))
if (s.index(tgt) != tgtPos):
print("wrong index for 'knot', string class thinks it's at %d."
% (s.index(tgt)))
print(" string class has:\n%s" % (str(s)))
if __name__ == '__main__':
unittest.main()