Debian Codegraph @

tests/test_coverup_62.py

raw symbols SCIP
1 # file scalene/scalene_output.py:84-300
2 # lines [84, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 101, 102, 103, 104, 105, 106, 107, 108, 110, 111, 112, 114, 117, 118, 119, 120, 123, 124, 127, 128, 129, 130, 132, 133, 134, 135, 136, 137, 140, 141, 142, 143, 147, 148, 150, 151, 153, 154, 155, 156, 158, 159, 161, 163, 164, 165, 166, 170, 171, 172, 173, 176, 177, 178, 182, 183, 184, 185, 187, 188, 189, 190, 193, 194, 195, 196, 197, 199, 201, 202, 204, 205, 207, 208, 210, 211, 214, 215, 216, 217, 219, 220, 222, 223, 224, 225, 227, 228, 229, 230, 233, 234, 235, 236, 237, 238, 239, 240, 241, 242, 243, 244, 247, 248, 249, 250, 251, 252, 253, 254, 255, 256, 261, 262, 263, 264, 265, 266, 267, 269, 270, 272, 273, 276, 277, 278, 280, 281, 282, 283, 284, 285, 286, 287, 288, 289, 292, 293, 294, 295, 296, 297, 300]
3 # branches ['110->111', '110->112', '112->114', '112->117', '133->134', '133->140', '150->151', '150->153', '158->159', '158->261', '163->164', '163->170', '176->177', '176->182', '192->201', '192->214', '219->220', '219->222', '233->234', '233->247', '261->266', '261->276', '280->281', '280->282', '282->283', '282->292']
4
5 import pytest
6 from scalene.scalene_output import ScaleneOutput
7 from scalene.scalene_json import ScaleneJSON
8 from scalene.scalene_statistics import ScaleneStatistics
9 from rich.console import Console
10 from rich.table import Table
11 from typing import Callable
12 from collections import defaultdict
13 import random
14
15
16 @pytest.fixture
17 def scalene_output():
18 return ScaleneOutput()
19
20
21 @pytest.fixture
22 def scalene_json():
23 return ScaleneJSON()
24
25
26 @pytest.fixture
27 def scalene_stats():
28 stats = ScaleneStatistics()
29 stats.cpu_samples = defaultdict(lambda: defaultdict(float))
30 stats.memory_free_samples = defaultdict(lambda: defaultdict(list))
31 stats.memory_malloc_samples = defaultdict(lambda: defaultdict(list))
32 return stats
33
34
35 @pytest.fixture
36 def console():
37 return Console()
38
39
40 @pytest.fixture
41 def table():
42 return Table()
43
44
45 def test_output_profile_line(
46 scalene_output, scalene_json, scalene_stats, console, table
47 ):
48 fname = "test.py"
49 line_no = 1
50 line = "print('Hello, world!')"
51 profile_this_code: Callable[[str, int], bool] = lambda fname, line_no: True
52
53 # Set up statistics to trigger different branches
54 scalene_stats.total_cpu_samples = 100
55 scalene_stats.cpu_samples[fname][line_no] = 2
56 scalene_stats.cpu_samples["<other>"][1] = 98
57 scalene_stats.memory_free_samples[fname][line_no] = [(0, 0.5)]
58 scalene_stats.memory_malloc_samples[fname][line_no] = [(0, 0.5)]
59 scalene_stats.max_footprint = 1024
60
61 # Set up JSON output to trigger different branches
62 json_output = {
63 "n_peak_mb": 0.5,
64 "n_cpu_percent_c": 0.5,
65 "n_gpu_percent": 0.5,
66 "n_cpu_percent_python": 0.5,
67 "n_usage_fraction": 0.5,
68 "n_sys_percent": 0.5,
69 "n_python_fraction": 0.5,
70 "n_copy_mb_s": 0.5,
71 "memory_samples": [(0, 0.5)],
72 }
73 scalene_json.output_profile_line = lambda **kwargs: json_output
74
75 # Set up output to trigger different branches
76 scalene_output.highlight_percentage = 0.1
77 scalene_output.highlight_color = "red"
78 scalene_output.gpu = True
79 scalene_output.max_sparkline_len_line = 1
80
81 # Mock random.sample to return a predictable result
82 random.sample = lambda a, _: a
83
84 # Call the method under test
85 result = scalene_output.output_profile_line(
86 json=scalene_json,
87 fname=fname,
88 line_no=line_no,
89 line=line,
90 console=console,
91 tbl=table,
92 stats=scalene_stats,
93 profile_this_code=profile_this_code,
94 force_print=False,
95 suppress_lineno_print=False,
96 is_function_summary=False,
97 profile_memory=True,
98 reduced_profile=False,
99 )
100
101 # Check postconditions
102 assert result == True
103 assert len(table.rows) == 1
104 # Clean up
105 del scalene_output
106 del scalene_json
107 del scalene_stats
108 del console
109 del table
110 random.sample = random.Random().sample # Restore the original random.sample