Debian Codegraph @

tests/test_coverup_59.py

raw symbols SCIP
1 # file scalene/adaptive.py:4-43
2 # lines [4, 5, 7, 9, 10, 11, 13, 14, 15, 16, 17, 18, 20, 21, 22, 23, 24, 26, 27, 29, 30, 31, 32, 33, 34, 35, 36, 37, 39, 40, 42, 43]
3 # branches ['15->16', '15->17', '21->22', '21->23', '27->29', '27->36', '30->31', '30->34']
4
5 import pytest
6 from scalene.adaptive import Adaptive
7
8
9 def test_adaptive_add():
10 size = 8
11 adaptive1 = Adaptive(size)
12 adaptive2 = Adaptive(size)
13
14 # Fill adaptive1 and adaptive2 with values to trigger decimation
15 for i in range(size):
16 adaptive1.add(i)
17 adaptive2.add(size - i)
18
19 # Perform addition
20 adaptive_sum = adaptive1 + adaptive2
21
22 # Check postconditions
23 assert adaptive_sum.len() == size
24 for i in range(size):
25 assert adaptive_sum.get()[i] == adaptive1.get()[i] + adaptive2.get()[i]
26
27 # Check if decimation occurred
28 adaptive1.add(100)
29 assert adaptive1.len() == size // 3 + 1
30
31 # Check if the median was correctly calculated
32 for i in range(size // 3):
33 arr = [i * 3, i * 3 + 1, i * 3 + 2]
34 arr.sort()
35 assert adaptive1.get()[i] == arr[1] # Median
36
37
38 def test_adaptive_iadd():
39 size = 8
40 adaptive1 = Adaptive(size)
41 adaptive2 = Adaptive(size)
42
43 # Fill adaptive1 and adaptive2 with values to trigger decimation
44 for i in range(size):
45 adaptive1.add(i)
46 adaptive2.add(size - i)
47
48 # Perform in-place addition
49 adaptive1 += adaptive2
50
51 # Check postconditions
52 assert adaptive1.len() == size
53 for i in range(size):
54 assert adaptive1.get()[i] == i + (size - i)
55
56 # Check if decimation occurred
57 adaptive1.add(100)
58 assert adaptive1.len() == size // 3 + 1
59
60 # Check if the median was correctly calculated
61 for i in range(size // 3):
62 # Since we are adding the arrays before decimation, we need to calculate the median of the sums
63 arr = [
64 i * 3 + (size - i * 3),
65 i * 3 + 1 + (size - i * 3 - 1),
66 i * 3 + 2 + (size - i * 3 - 2),
67 ]
68 arr.sort()
69 assert adaptive1.get()[i] == arr[1] # Median of the sums