Debian Codegraph @

Lib/test/test_free_threading/test_bisect.py

raw
1 import unittest
2 from test.support import import_helper, threading_helper
3 import random
4
5 py_bisect = import_helper.import_fresh_module('bisect', blocked=['_bisect'])
6 c_bisect = import_helper.import_fresh_module('bisect', fresh=['_bisect'])
7
8
9 NTHREADS = 4
10 OBJECT_COUNT = 500
11
12
13 class TestBase:
14 def do_racing_insort(self, insert_method):
15 def insert(data):
16 for _ in range(OBJECT_COUNT):
17 x = random.randint(-OBJECT_COUNT, OBJECT_COUNT)
18 insert_method(data, x)
19
20 data = list(range(OBJECT_COUNT))
21 threading_helper.run_concurrently(
22 worker_func=insert, args=(data,), nthreads=NTHREADS
23 )
24 if False:
25 # These functions are not thread-safe and so the list can become
26 # unsorted. However, we don't want Python to crash if these
27 # functions are used concurrently on the same sequence. This
28 # should also not produce any TSAN warnings.
29 self.assertTrue(self.is_sorted_ascending(data))
30
31 def test_racing_insert_right(self):
32 self.do_racing_insort(self.mod.insort_right)
33
34 def test_racing_insert_left(self):
35 self.do_racing_insort(self.mod.insort_left)
36
37 @staticmethod
38 def is_sorted_ascending(lst):
39 """
40 Check if the list is sorted in ascending order (non-decreasing).
41 """
42 return all(lst[i - 1] <= lst[i] for i in range(1, len(lst)))
43
44
45 @threading_helper.requires_working_threading()
46 class TestPyBisect(unittest.TestCase, TestBase):
47 mod = py_bisect
48
49
50 @threading_helper.requires_working_threading()
51 class TestCBisect(unittest.TestCase, TestBase):
52 mod = c_bisect
53
54
55 if __name__ == "__main__":
56 unittest.main()