Debian Codegraph @

tests/test_coverup_115.py

raw symbols SCIP
1 # file scalene/scalene_leak_analysis.py:6-45
2 # lines [21, 22, 23, 24, 25, 27, 28, 31, 34, 35, 37, 38, 39, 40, 41, 42, 45]
3 # branches ['21->22', '21->23', '25->27', '25->45', '33->25', '33->37', '37->25', '37->38']
4
5 import pytest
6 from collections import OrderedDict
7 from typing import Any, List
8 from scalene.scalene_statistics import ScaleneStatistics
9 from scalene.scalene_leak_analysis import ScaleneLeakAnalysis
10
11
12 class Filename(str):
13 pass
14
15
16 class LineNumber(int):
17 pass
18
19
20 @pytest.fixture
21 def scalene_statistics():
22 stats = ScaleneStatistics()
23 fname = Filename("test_file.py")
24 line_number = LineNumber(1)
25 stats.memory_stats.leak_score[fname][line_number] = (100, 1) # 100 allocs, 1 free
26 return stats
27
28
29 def test_compute_leaks(scalene_statistics):
30 stats = scalene_statistics
31 fname = Filename("test_file.py")
32 avg_mallocs = OrderedDict({LineNumber(1): 50.0})
33 growth_rate = 2.0 # 2% growth rate to exceed the threshold
34
35 leaks = ScaleneLeakAnalysis.compute_leaks(growth_rate, stats, avg_mallocs, fname)
36
37 assert len(leaks) == 1
38 assert leaks[0][0] == LineNumber(1) # Line number
39 assert leaks[0][1] == 1.0 - (1 + 1) / (100 - 1 + 2) # Expected leak
40 assert leaks[0][2] == 50.0 # Average mallocs
41
42 # Cleanup is not necessary as the test does not modify any global state