File size: 1,247 Bytes
63f6453
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import pytest
from solution import merge, total_length, contains, gaps, validate


def test_merge_overlapping_and_adjacent():
    assert merge([(5, 7), (1, 3), (2, 4), (8, 9)]) == [(1, 9)]


def test_merge_disjoint_sorted():
    assert merge([(10, 12), (1, 2)]) == [(1, 2), (10, 12)]


def test_merge_empty():
    assert merge([]) == []


def test_validate_rejects_reversed_and_wrong_size():
    with pytest.raises(ValueError):
        validate((3, 1))
    with pytest.raises(ValueError):
        validate((1, 2, 3))


def test_total_length_counts_integers():
    assert total_length([(1, 3), (2, 5), (10, 10)]) == 6


@pytest.mark.parametrize("x,expected", [(1, True), (4, True), (6, False), (0, False), (11, True), (20, False)])
def test_contains(x, expected):
    assert contains([(1, 4), (8, 12)], x) is expected


def test_gaps_inside_bounds():
    assert gaps([(3, 4), (7, 8)], 1, 10) == [(1, 2), (5, 6), (9, 10)]


def test_gaps_ignores_outside_and_full_cover():
    assert gaps([(-5, -1), (20, 30)], 0, 2) == [(0, 2)]
    assert gaps([(0, 10)], 0, 10) == []


def test_gaps_single_point_at_upper():
    assert gaps([(0, 9)], 0, 10) == [(10, 10)]


def test_gaps_invalid_bounds():
    with pytest.raises(ValueError):
        gaps([], 5, 1)