OILS / mycpp / examples / test_strings.py View on Github | oilshell.org

253 lines, 136 significant
1#!/usr/bin/env python2
2"""
3strings.py
4"""
5from __future__ import print_function
6
7import os
8from mycpp import mylib
9from mycpp.mylib import log
10
11from typing import List
12
13
14def banner(s):
15 # type: (str) -> None
16 print('')
17 print('=== %s ===' % s)
18 print('')
19
20
21class Foo(object):
22
23 def __init__(self):
24 # type: () -> None
25 self.s = 'mystr'
26
27
28def TestMethods():
29 # type: () -> None
30
31 s = 'a1bc'
32
33 if s.startswith(''):
34 print('empty yes')
35
36 if s.startswith('a1'):
37 print('a1 yes')
38
39 if not s.startswith('zz'):
40 print('zz no')
41
42 if s.endswith(''):
43 print('empty yes')
44
45 if s.endswith('bc'):
46 print('bc yes')
47
48 if s.endswith('c'):
49 print('bc yes')
50
51 if not s.endswith('zzzzzz'):
52 print('zzzzzz no')
53
54 # This file is out of date! It thinks it happens in Python 3, but we have
55 # it in Python 2.7
56
57 # /home/andy/wedge/oils-for-unix.org/pkg/mypy/0.780/mypy/typeshed/stdlib/2/__builtin__.pyi:509: note: "startswith" of "str" defined here
58
59 # Fixed here - https://github.com/python/typeshed/blob/main/stdlib/builtins.pyi
60 #
61 # It can be fixed by patching, gah
62
63 # start pos
64 #if s.startswith('bc', start=2):
65 # print('bc YES')
66
67 # find(s, start, end) can be used to implement TokenStartsWith() and
68 # TokenEndsWith(), TokenEquals(), IsPlusEquals(), TokenContains(), etc.
69 i1 = s.find('b', 1)
70 i2 = s.find('b', 2)
71 i3 = s.find('b', 3) # not found
72 print('i1 = %d, i2 = %d, i3 = %d' % (i1, i2, i3))
73
74 # TODO: Implement end index here
75 #j1 = s.find('b', 1, 3)
76 #j2 = s.find('b', 1, 2)
77 #print('j1 = %d, j2 = %d' % (j1, j2))
78
79
80def TestFormat():
81 # type: () -> None
82
83 banner('TestFormat')
84
85 print('foo' + 'bar')
86 print('foo' * 3)
87 obj = Foo()
88 print('foo' + obj.s)
89
90 s = 'mystr'
91 print('[%s]' % s)
92
93 s = 'mystr'
94 print('[%s, %s]' % (s, 'abc'))
95
96 print('%s: 5%%-100%%' % 'abc')
97
98 print('<a href="foo.html">%s</a>' % 'anchor')
99
100 print("foo? %d" % ('f' in s))
101 print("str? %d" % ('s' in s))
102
103 print("int 5d %5d" % 35)
104
105 print("'single'")
106 print('"double"')
107
108 # test escape codes
109 print("a\tb\nc\td\n")
110
111 x = 'x'
112 print("%s\tb\n%s\td\n" % (x, x))
113
114 fmt = "%dfoo"
115 print(fmt % 10)
116
117 fmts = ["foo%d"]
118 print(fmts[0] % 10)
119
120 print(("foo " + "%s") % "bar")
121
122 # NUL bytes
123 s = "spam\0%s" % "eggs"
124
125 # TODO: There's a bug here -- we get len == 4 in C++, but it should be 9.
126 # It's either StrFormat() or the bad JSON literals \u0000
127 if 0:
128 print("len(s) = %d" % len(s))
129 print(s)
130
131 s = "foo%s" % "\0bar"
132 print("len(s) = %d" % len(s))
133
134 print("%o" % 12345)
135 print("%17o" % 12345)
136 print("%017o" % 12345)
137
138 print("%%%d%%%%" % 12345)
139
140 print("%r" % "tab\tline\nline\r\n")
141
142 s = 'a1b2c3d4e5'
143 # Disable step support
144 # print(s[0:10:2])
145 # print(s[1:10:2])
146 print(s.upper())
147
148
149def TestByteOperations():
150 # type: () -> None
151 banner('TestByteOperations')
152
153 s = 'foo' * 10
154
155 i = 0
156 n = len(s)
157 total = 0
158 total2 = 0
159 while i < n:
160 byte = ord(s[i])
161 byte2 = mylib.ByteAt(s, i)
162
163 total += byte
164 total2 += byte2
165
166 i += 1
167
168 if total != total2:
169 raise AssertionError()
170
171 print('total = %d' % total)
172 print('total2 = %d' % total2)
173
174
175def TestBytes2():
176 # type: () -> None
177
178 banner('TestBytes2')
179
180 b = [] # type: List[int]
181 ch = [] # type: List[str]
182 for i in xrange(256):
183 # Shuffle it a bit, make it a better test
184 j = 255 - i
185 if j == 2:
186 j = 0
187
188 b.append(j)
189 ch.append(chr(j))
190
191 print('len(b) = %d' % len(b))
192 print('len(ch) = %d' % len(ch))
193
194 all_bytes = ''.join(ch)
195
196 b2 = mylib.JoinBytes(b)
197 if all_bytes == b2:
198 print('EQUAL ==')
199 else:
200 raise AssertionError('should be equal')
201
202 n = len(all_bytes)
203 print('len(all_bytes) = %d' % n)
204 print('')
205 #print('[%s]' % all_bytes)
206
207 i = 0
208 while i < n:
209 byte = mylib.ByteAt(all_bytes, i)
210 #log('byte = %d', byte)
211
212 if mylib.ByteEquals(byte, '['):
213 print('LEFT')
214 if mylib.ByteEquals(byte, ']'):
215 print('RIGHT')
216 if mylib.ByteEquals(byte, '\\'):
217 print('BACKSLASH')
218
219 # TODO: get rid of JSON crap
220 #if mylib.ByteEqualsStr(byte, '\xff'):
221 # print('0xff')
222
223 if mylib.ByteEquals(byte, chr(255)):
224 print('0xff')
225
226 if mylib.ByteInSet(byte, 'abcXYZ'):
227 print('abcXYZ')
228
229 i += 1
230
231 print('')
232
233
234def run_tests():
235 # type: () -> None
236
237 TestFormat()
238 TestMethods()
239 TestByteOperations()
240 TestBytes2()
241
242
243def run_benchmarks():
244 # type: () -> None
245 pass
246
247
248if __name__ == '__main__':
249 if os.getenv('BENCHMARK'):
250 log('Benchmarking...')
251 run_benchmarks()
252 else:
253 run_tests()