def make_timespans():
    return [
        Timespan(0, 3),
        Timespan(1, 3),
        Timespan(1, 2),
        Timespan(2, 5),
        Timespan(6, 9),
    ]
def test_remove(accelerated):
    timespans = make_timespans()
    timespan_collection = make_timespan_collection(
        accelerated=accelerated, populated=True
    )
    timespan_collection.remove(timespans[1:-1])
    assert timespan_collection[:] == [
        Timespan(start_offset=0, stop_offset=3),
        Timespan(start_offset=6, stop_offset=9),
    ]
def test___getitem__(accelerated):
    timespan_collection = make_timespan_collection(
        accelerated=accelerated, populated=True
    )
    assert timespan_collection[-1] == Timespan(6, 9)
    assert [timespan for timespan in timespan_collection[:3]] == [
        Timespan(start_offset=0, stop_offset=3),
        Timespan(start_offset=1, stop_offset=2),
        Timespan(start_offset=1, stop_offset=3),
    ]
def make_target_timespans(range_=10):
    indices = list(range(range_))
    timespans = []
    for pair in itertools.permutations(indices, 2):
        start_offset, stop_offset = sorted(pair)
        target_timespan = Timespan(start_offset=start_offset, stop_offset=stop_offset)
        timespans.append(target_timespan)
    return timespans
def test___setitem__(accelerated):
    timespan_collection = make_timespan_collection(
        accelerated=accelerated, populated=True
    )
    timespan_collection[-1] = Timespan(-1, 4)
    assert [timespan for timespan in timespan_collection] == [
        Timespan(start_offset=-1, stop_offset=4),
        Timespan(start_offset=0, stop_offset=3),
        Timespan(start_offset=1, stop_offset=2),
        Timespan(start_offset=1, stop_offset=3),
        Timespan(start_offset=2, stop_offset=5),
    ]
    timespan_collection[:3] = Timespan(100, 200)
    assert [timespan for timespan in timespan_collection] == [
        Timespan(start_offset=1, stop_offset=3),
        Timespan(start_offset=2, stop_offset=5),
        Timespan(start_offset=100, stop_offset=200),
    ]
def test___contains__(accelerated):
    timespans = make_timespans()
    timespan_collection = make_timespan_collection(
        accelerated=accelerated, populated=True
    )
    assert timespans[0] in timespan_collection
    assert Timespan(-1, 100) not in timespan_collection
    timespan_collection.remove(timespans[-1])
    assert timespans[-1] not in timespan_collection
def make_random_timespans(count=10, range_=10):
    indices = list(range(range_))
    timespans = []
    for _ in range(count):
        random.shuffle(indices)
        start_offset, stop_offset = sorted(indices[:2])
        timespan = Timespan(start_offset=start_offset, stop_offset=stop_offset)
        timespans.append(timespan)
    return timespans
def test___sub__(accelerated):
    timespan_collection = make_timespan_collection(
        accelerated=accelerated,
        timespans=[Timespan(0, 16), Timespan(5, 12), Timespan(-2, 8)],
    )
    timespan = Timespan(5, 10)
    result = timespan_collection - timespan
    assert result[:] == [
        Timespan(-2, 5),
        Timespan(0, 5),
        Timespan(10, 12),
        Timespan(10, 16),
    ]
def test_index(accelerated):
    timespans = make_timespans()
    timespan_collection = make_timespan_collection(
        accelerated=accelerated, populated=True
    )
    assert timespan_collection.index(timespans[0]) == 0
    assert timespan_collection.index(timespans[1]) == 2
    assert timespan_collection.index(timespans[2]) == 1
    assert timespan_collection.index(timespans[3]) == 3
    assert timespan_collection.index(timespans[4]) == 4
    with pytest.raises(ValueError):
        timespan = Timespan(-100, 100)
        timespan_collection.index(timespan)
def test_insert(accelerated):
    timespan_collection = make_timespan_collection(
        accelerated=accelerated, populated=False
    )
    timespan_collection.insert(Timespan(1, 3))
    timespan_collection.insert((Timespan(0, 4), Timespan(2, 6)))
    assert timespan_collection[:] == [
        Timespan(start_offset=0, stop_offset=4),
        Timespan(start_offset=1, stop_offset=3),
        Timespan(start_offset=2, stop_offset=6),
    ]
def test___iter__(accelerated):
    timespan_collection = make_timespan_collection(
        accelerated=accelerated, populated=True
    )
    assert [timespan for timespan in timespan_collection] == [
        Timespan(start_offset=0, stop_offset=3),
        Timespan(start_offset=1, stop_offset=2),
        Timespan(start_offset=1, stop_offset=3),
        Timespan(start_offset=2, stop_offset=5),
        Timespan(start_offset=6, stop_offset=9),
    ]
    iterator = iter(timespan_collection)
    assert next(iterator) == Timespan(start_offset=0, stop_offset=3)