@@ -168,120 +168,165 @@ def test(a: uint256, b: String[50] = "foo") -> Bytes[100]:
168
168
assert c .test (12345 , "bar" )[- 3 :] == b"bar"
169
169
170
170
171
- def test_string_equality (get_contract_with_gas_estimation ):
172
- code = """
173
- _compA: String[100]
174
- _compB: String[100]
171
+ string_equality_tests = [
172
+ (
173
+ 100 ,
174
+ "The quick brown fox jumps over the lazy dog" ,
175
+ "The quick brown fox jumps over the lazy hog" ,
176
+ ),
177
+ # check <= 32 codepath
178
+ (32 , "abc" , "abc\0 " ),
179
+ (32 , "abc" , "abc\1 " ), # use a_init dirty bytes
180
+ (32 , "abc\2 " , "abc" ), # use b_init dirty bytes
181
+ (32 , "" , "\0 " ),
182
+ (32 , "" , "\1 " ),
183
+ (33 , "" , "\1 " ),
184
+ (33 , "" , "\0 " ),
185
+ ]
186
+
187
+
188
+ @pytest .mark .parametrize ("len_,a,b" , string_equality_tests )
189
+ def test_string_equality (get_contract_with_gas_estimation , len_ , a , b ):
190
+ # fixtures to initialize strings with dirty bytes
191
+ a_init = "\\ 1" * len_
192
+ b_init = "\\ 2" * len_
193
+ string1 = a .encode ("unicode_escape" ).decode ("utf-8" )
194
+ string2 = b .encode ("unicode_escape" ).decode ("utf-8" )
195
+ code = f"""
196
+ a: String[{ len_ } ]
197
+ b: String[{ len_ } ]
175
198
176
199
@external
177
200
def equal_true() -> bool:
178
- compA: String[100] = "The quick brown fox jumps over the lazy dog"
179
- compB: String[100] = "The quick brown fox jumps over the lazy dog"
180
- return compA == compB
201
+ a: String[{ len_ } ] = "{ a_init } "
202
+ b: String[{ len_ } ] = "{ b_init } "
203
+ a = "{ string1 } "
204
+ b = "{ string1 } "
205
+ return a == b
181
206
182
207
@external
183
208
def equal_false() -> bool:
184
- compA: String[100] = "The quick brown fox jumps over the lazy dog"
185
- compB: String[100] = "The quick brown fox jumps over the lazy hog"
186
- return compA == compB
209
+ a: String[{ len_ } ] = "{ a_init } "
210
+ b: String[{ len_ } ] = "{ b_init } "
211
+ a = "{ string1 } "
212
+ b = "{ string2 } "
213
+ return a == b
187
214
188
215
@external
189
216
def not_equal_true() -> bool:
190
- compA: String[100] = "The quick brown fox jumps over the lazy dog"
191
- compB: String[100] = "The quick brown fox jumps over the lazy hog"
192
- return compA != compB
217
+ a: String[{ len_ } ] = "{ a_init } "
218
+ b: String[{ len_ } ] = "{ b_init } "
219
+ a = "{ string1 } "
220
+ b = "{ string2 } "
221
+ return a != b
193
222
194
223
@external
195
224
def not_equal_false() -> bool:
196
- compA: String[100] = "The quick brown fox jumps over the lazy dog"
197
- compB: String[100] = "The quick brown fox jumps over the lazy dog"
198
- return compA != compB
225
+ a: String[{ len_ } ] = "{ a_init } "
226
+ b: String[{ len_ } ] = "{ b_init } "
227
+ a = "{ string1 } "
228
+ b = "{ string1 } "
229
+ return a != b
199
230
200
231
@external
201
232
def literal_equal_true() -> bool:
202
- return "The quick brown fox jumps over the lazy dog" == \
203
- "The quick brown fox jumps over the lazy dog"
233
+ return "{ string1 } " == "{ string1 } "
204
234
205
235
@external
206
236
def literal_equal_false() -> bool:
207
- return "The quick brown fox jumps over the lazy dog" == \
208
- "The quick brown fox jumps over the lazy hog"
237
+ return "{ string1 } " == "{ string2 } "
209
238
210
239
@external
211
240
def literal_not_equal_true() -> bool:
212
- return "The quick brown fox jumps over the lazy dog" != \
213
- "The quick brown fox jumps over the lazy hog"
241
+ return "{ string1 } " != "{ string2 } "
214
242
215
243
@external
216
244
def literal_not_equal_false() -> bool:
217
- return "The quick brown fox jumps over the lazy dog" != \
218
- "The quick brown fox jumps over the lazy dog"
245
+ return "{ string1 } " != "{ string1 } "
219
246
220
247
@external
221
248
def storage_equal_true() -> bool:
222
- self._compA = "The quick brown fox jumps over the lazy dog"
223
- self._compB = "The quick brown fox jumps over the lazy dog"
224
- return self._compA == self._compB
249
+ self.a = "{ a_init } "
250
+ self.b = "{ b_init } "
251
+ self.a = "{ string1 } "
252
+ self.b = "{ string1 } "
253
+ return self.a == self.b
225
254
226
255
@external
227
256
def storage_equal_false() -> bool:
228
- self._compA = "The quick brown fox jumps over the lazy dog"
229
- self._compB = "The quick brown fox jumps over the lazy hog"
230
- return self._compA == self._compB
257
+ self.a = "{ a_init } "
258
+ self.b = "{ b_init } "
259
+ self.a = "{ string1 } "
260
+ self.b = "{ string2 } "
261
+ return self.a == self.b
231
262
232
263
@external
233
264
def storage_not_equal_true() -> bool:
234
- self._compA = "The quick brown fox jumps over the lazy dog"
235
- self._compB = "The quick brown fox jumps over the lazy hog"
236
- return self._compA != self._compB
265
+ self.a = "{ a_init } "
266
+ self.b = "{ b_init } "
267
+ self.a = "{ string1 } "
268
+ self.b = "{ string2 } "
269
+ return self.a != self.b
237
270
238
271
@external
239
272
def storage_not_equal_false() -> bool:
240
- self._compA = "The quick brown fox jumps over the lazy dog"
241
- self._compB = "The quick brown fox jumps over the lazy dog"
242
- return self._compA != self._compB
273
+ self.a = "{ a_init } "
274
+ self.b = "{ b_init } "
275
+ self.a = "{ string1 } "
276
+ self.b = "{ string1 } "
277
+ return self.a != self.b
243
278
244
279
@external
245
- def string_compare_equal(str1: String[100 ], str2: String[100 ]) -> bool:
280
+ def string_compare_equal(str1: String[{ len_ } ], str2: String[{ len_ } ]) -> bool:
246
281
return str1 == str2
247
282
248
283
@external
249
- def string_compare_not_equal(str1: String[100 ], str2: String[100 ]) -> bool:
284
+ def string_compare_not_equal(str1: String[{ len_ } ], str2: String[{ len_ } ]) -> bool:
250
285
return str1 != str2
251
286
252
287
@external
253
- def compare_passed_storage_equal(str: String[100]) -> bool:
254
- self._compA = "The quick brown fox jumps over the lazy dog"
255
- return self._compA == str
288
+ def compare_passed_storage_equal(str_: String[{ len_ } ]) -> bool:
289
+ self.a = "{ a_init } "
290
+ self.a = "{ string1 } "
291
+ return self.a == str_
256
292
257
293
@external
258
- def compare_passed_storage_not_equal(str: String[100]) -> bool:
259
- self._compA = "The quick brown fox jumps over the lazy dog"
260
- return self._compA != str
294
+ def compare_passed_storage_not_equal(str_: String[{ len_ } ]) -> bool:
295
+ self.a = "{ a_init } "
296
+ self.a = "{ string1 } "
297
+ return self.a != str_
261
298
262
299
@external
263
300
def compare_var_storage_equal_true() -> bool:
264
- self._compA = "The quick brown fox jumps over the lazy dog"
265
- compB: String[100] = "The quick brown fox jumps over the lazy dog"
266
- return self._compA == compB
301
+ self.a = "{ a_init } "
302
+ b: String[{ len_ } ] = "{ b_init } "
303
+ self.a = "{ string1 } "
304
+ b = "{ string1 } "
305
+ return self.a == b
267
306
268
307
@external
269
308
def compare_var_storage_equal_false() -> bool:
270
- self._compA = "The quick brown fox jumps over the lazy dog"
271
- compB: String[100] = "The quick brown fox jumps over the lazy hog"
272
- return self._compA == compB
309
+ self.a = "{ a_init } "
310
+ b: String[{ len_ } ] = "{ b_init } "
311
+ self.a = "{ string1 } "
312
+ b = "{ string2 } "
313
+ return self.a == b
273
314
274
315
@external
275
316
def compare_var_storage_not_equal_true() -> bool:
276
- self._compA = "The quick brown fox jumps over the lazy dog"
277
- compB: String[100] = "The quick brown fox jumps over the lazy hog"
278
- return self._compA != compB
317
+ self.a = "{ a_init } "
318
+ b: String[{ len_ } ] = "{ b_init } "
319
+ self.a = "{ string1 } "
320
+ b = "{ string2 } "
321
+ return self.a != b
279
322
280
323
@external
281
324
def compare_var_storage_not_equal_false() -> bool:
282
- self._compA = "The quick brown fox jumps over the lazy dog"
283
- compB: String[100] = "The quick brown fox jumps over the lazy dog"
284
- return self._compA != compB
325
+ self.a = "{ a_init } "
326
+ b: String[{ len_ } ] = "{ b_init } "
327
+ self.a = "{ string1 } "
328
+ b = "{ string1 } "
329
+ return self.a != b
285
330
"""
286
331
287
332
c = get_contract_with_gas_estimation (code )
@@ -298,8 +343,6 @@ def compare_var_storage_not_equal_false() -> bool:
298
343
assert c .storage_not_equal_true () is True
299
344
assert c .storage_not_equal_false () is False
300
345
301
- a = "The quick brown fox jumps over the lazy dog"
302
- b = "The quick brown fox jumps over the lazy hog"
303
346
assert c .string_compare_equal (a , a ) is True
304
347
assert c .string_compare_equal (a , b ) is False
305
348
assert c .string_compare_not_equal (b , a ) is True
0 commit comments