Completed
Push — master ( 7b504c...b6f606 )
by Ionel Cristian
01:16
created

test_iqr()   F

Complexity

Conditions 16

Size

Total Lines 46

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 16
c 0
b 0
f 0
dl 0
loc 46
rs 2.6716

How to fix   Complexity   

Complexity

Complex classes like test_iqr() often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

1
from pytest import mark
2
3
from pytest_benchmark.stats import Stats
4
5
6 View Code Duplication
def test_1():
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
7
    stats = Stats()
8
    for i in 4., 36., 45., 50., 75.:
9
        stats.update(i)
10
    assert stats.mean == 42.
11
    assert stats.min == 4.
12
    assert stats.max == 75.
13
    assert stats.stddev == 25.700194551792794
14
    assert stats.rounds == 5
15
    assert stats.total == 210.
16
17
18 View Code Duplication
def test_2():
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
19
    stats = Stats()
20
    stats.update(17.)
21
    stats.update(19.)
22
    stats.update(24.)
23
    assert stats.mean == 20.
24
    assert stats.min == 17.
25
    assert stats.max == 24.
26
    assert stats.stddev == 3.605551275463989
27
    assert stats.rounds == 3
28
    assert stats.total == 60.
29
30
31
def test_single_item():
32
    stats = Stats()
33
    stats.update(1)
34
    assert stats.mean == 1
35
    assert stats.median == 1
36
    assert stats.iqr_outliers == 0
37
    assert stats.stddev_outliers == 0
38
    assert stats.min == 1
39
    assert stats.max == 1
40
    assert stats.stddev == 0
41
    assert stats.iqr == 0
42
    assert stats.rounds == 1
43
    assert stats.total == 1
44
    assert stats.ld15iqr == 1
45
    assert stats.hd15iqr == 1
46
47
48
@mark.parametrize('length', range(1, 10))
49
def test_length(length):
50
    stats = Stats()
51
    for i in range(length):
52
        stats.update(1)
53
54
    assert stats.as_dict()
55
56
57
def test_iqr():
58
    stats = Stats()
59
    for i in 6, 7, 15, 36, 39, 40, 41, 42, 43, 47, 49:
60
        stats.update(i)
61
    assert stats.iqr == 22.5  # https://en.wikipedia.org/wiki/Quartile#Example_1
62
63
    stats = Stats()
64
    for i in 7, 15, 36, 39, 40, 41:
65
        stats.update(i)
66
    assert stats.iqr == 25.0  # https://en.wikipedia.org/wiki/Quartile#Example_2
67
68
    stats = Stats()
69
    for i in 1, 2, 3, 4, 5, 6, 7, 8, 9:
70
        stats.update(i)
71
    assert stats.iqr == 4.5  # http://www.phusewiki.org/docs/2012/PRESENTATIONS/SP/SP06%20.pdf - method 1
72
73
    stats = Stats()
74
    for i in 1, 2, 3, 4, 5, 6, 7, 8:
75
        stats.update(i)
76
    assert stats.iqr == 4.0  # http://www.lexjansen.com/nesug/nesug07/po/po08.pdf - method 1
77
78
    stats = Stats()
79
    for i in 1, 2, 1, 123, 4, 1234, 1, 234, 12, 34, 12, 3, 2, 34, 23:
80
        stats.update(i)
81
    assert stats.iqr == 32.0
82
83
    stats = Stats()
84
    for i in 1, 2, 3, 10, 10.1234, 11, 12, 13., 10.1115, 11.1115, 12.1115, 13.5, 10.75, 11.75, 13.12175, 13.1175, 20, \
85
             50, 52:
86
        stats.update(i)
87
    assert stats.stddev == 13.518730097622106
88
    assert stats.iqr == 3.006212500000002  # close enough: http://www.wessa.net/rwasp_variability.wasp
89
90
    stats = Stats()
91
    for i in [
92
        11.2, 11.8, 13.2, 12.9, 12.1, 13.5, 14.8, 14.8, 13.6, 11.9, 10.4, 11.8, 11.5, 12.6, 14.1, 13.5, 12.5, 14.9,
93
        17.0, 17.0, 15.8, 13.3, 11.4, 14.0, 14.5, 15.0, 17.8, 16.3, 17.2, 17.8, 19.9, 19.9, 18.4, 16.2, 14.6, 16.6,
94
        17.1, 18.0, 19.3, 18.1, 18.3, 21.8, 23.0, 24.2, 20.9, 19.1, 17.2, 19.4, 19.6, 19.6, 23.6, 23.5, 22.9, 24.3,
95
        26.4, 27.2, 23.7, 21.1, 18.0, 20.1, 20.4, 18.8, 23.5, 22.7, 23.4, 26.4, 30.2, 29.3, 25.9, 22.9, 20.3, 22.9,
96
        24.2, 23.3, 26.7, 26.9, 27.0, 31.5, 36.4, 34.7, 31.2, 27.4, 23.7, 27.8, 28.4, 27.7, 31.7, 31.3, 31.8, 37.4,
97
        41.3, 40.5, 35.5, 30.6, 27.1, 30.6, 31.5, 30.1, 35.6, 34.8, 35.5, 42.2, 46.5, 46.7, 40.4, 34.7, 30.5, 33.6,
98
        34.0, 31.8, 36.2, 34.8, 36.3, 43.5, 49.1, 50.5, 40.4, 35.9, 31.0, 33.7, 36.0, 34.2, 40.6, 39.6, 42.0, 47.2,
99
        54.8, 55.9, 46.3, 40.7, 36.2, 40.5, 41.7, 39.1, 41.9, 46.1, 47.2, 53.5, 62.2, 60.6, 50.8, 46.1, 39.0, 43.2,
100
    ]:
101
        stats.update(i)
102
    assert stats.iqr == 18.1  # close enough: http://www.wessa.net/rwasp_variability.wasp
103