File: test_coverup_79.py

package info (click to toggle)
scalene 1.5.51-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 15,528 kB
  • sloc: cpp: 22,930; python: 13,403; javascript: 11,769; ansic: 817; makefile: 196; sh: 45
file content (49 lines) | stat: -rw-r--r-- 1,672 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# file scalene/scalene_statistics.py:386-394
# lines [392, 393, 394]
# branches ['392->exit', '392->393', '393->392', '393->394']

import pytest
from scalene.scalene_statistics import ScaleneStatistics
from collections import defaultdict

class RunningStats:
    def __init__(self, cpu_samples=0, malloc_samples=0, free_samples=0, python_fraction=0):
        self.cpu_samples = cpu_samples
        self.malloc_samples = malloc_samples
        self.free_samples = free_samples
        self.python_fraction = python_fraction

    def __iadd__(self, other):
        self.cpu_samples += other.cpu_samples
        self.malloc_samples += other.malloc_samples
        self.free_samples += other.free_samples
        self.python_fraction += other.python_fraction
        return self

Filename = str
LineNumber = int

@pytest.fixture
def cleanup():
    # Setup code if necessary
    yield
    # Cleanup code if necessary

def test_increment_core_utilization(cleanup):
    dest = {
        Filename("file1"): {LineNumber(1): RunningStats(), LineNumber(2): RunningStats()},
        Filename("file2"): {LineNumber(1): RunningStats()}
    }
    src = {
        Filename("file1"): {LineNumber(1): RunningStats(1, 1, 1, 1), LineNumber(2): RunningStats(2, 2, 2, 2)},
        Filename("file2"): {LineNumber(1): RunningStats(3, 3, 3, 3)}
    }

    ScaleneStatistics.increment_core_utilization(dest, src)

    # Assertions to verify postconditions
    assert dest[Filename("file1")][LineNumber(1)].cpu_samples == 1
    assert dest[Filename("file1")][LineNumber(2)].cpu_samples == 2
    assert dest[Filename("file2")][LineNumber(1)].cpu_samples == 3

    # Cleanup is handled by the fixture