@@ -135,7 +135,7 @@ def test_arithmetic_operators(uint_class: Type[BaseUint]) -> None:
135135 assert uint_class (b_val ) ** 4 == uint_class (b_val ** 4 )
136136 if uint_class .BITS <= 16 : # Pow gets too big quickly
137137 with pytest .raises (OverflowError ):
138- _ = a ** b . as_int ( )
138+ _ = a ** int ( b )
139139
140140
141141@pytest .mark .parametrize ("uint_class" , ALL_UINT_TYPES )
@@ -241,6 +241,63 @@ def test_hash(uint_class: Type[BaseUint]) -> None:
241241 assert hash (uint_class (1 )) != hash (uint_class (2 ))
242242
243243
244+ @pytest .mark .parametrize ("uint_class" , ALL_UINT_TYPES )
245+ def test_index_list_access (uint_class : Type [BaseUint ]) -> None :
246+ """Tests that Uint types can be used directly for list indexing."""
247+ data = ["a" , "b" , "c" , "d" , "e" ]
248+ idx = uint_class (2 )
249+ assert data [idx ] == "c"
250+ assert data [uint_class (0 )] == "a"
251+ assert data [uint_class (4 )] == "e"
252+
253+
254+ @pytest .mark .parametrize ("uint_class" , ALL_UINT_TYPES )
255+ def test_index_slicing (uint_class : Type [BaseUint ]) -> None :
256+ """Tests that Uint types can be used in slice operations."""
257+ data = [0 , 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 ]
258+ start = uint_class (2 )
259+ stop = uint_class (7 )
260+ step = uint_class (2 )
261+
262+ assert data [start :stop ] == [2 , 3 , 4 , 5 , 6 ]
263+ assert data [:stop ] == [0 , 1 , 2 , 3 , 4 , 5 , 6 ]
264+ assert data [start :] == [2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 ]
265+ assert data [start :stop :step ] == [2 , 4 , 6 ]
266+
267+
268+ @pytest .mark .parametrize ("uint_class" , ALL_UINT_TYPES )
269+ def test_index_range (uint_class : Type [BaseUint ]) -> None :
270+ """Tests that Uint types can be used in range()."""
271+ n = uint_class (5 )
272+ result = list (range (n ))
273+ assert result == [0 , 1 , 2 , 3 , 4 ]
274+
275+ start = uint_class (2 )
276+ stop = uint_class (8 )
277+ step = uint_class (2 )
278+ result = list (range (start , stop , step ))
279+ assert result == [2 , 4 , 6 ]
280+
281+
282+ @pytest .mark .parametrize ("uint_class" , ALL_UINT_TYPES )
283+ def test_index_hex_bin_oct (uint_class : Type [BaseUint ]) -> None :
284+ """Tests that Uint types work with hex(), bin(), oct()."""
285+ val = uint_class (42 )
286+ assert hex (val ) == "0x2a"
287+ assert bin (val ) == "0b101010"
288+ assert oct (val ) == "0o52"
289+
290+
291+ @pytest .mark .parametrize ("uint_class" , ALL_UINT_TYPES )
292+ def test_index_operator_index (uint_class : Type [BaseUint ]) -> None :
293+ """Tests that operator.index() works with Uint types."""
294+ import operator
295+
296+ val = uint_class (42 )
297+ assert operator .index (val ) == 42
298+ assert isinstance (operator .index (val ), int )
299+
300+
244301@pytest .mark .parametrize ("uint_class" , ALL_UINT_TYPES )
245302def test_to_bytes_default (uint_class : Type [BaseUint ]) -> None :
246303 """Tests the default behavior of the to_bytes method."""
0 commit comments