예제 #1
0
def test_nr_groups():
    with pytest.raises(ValueError):
        rpack.group([1], -1)
    with pytest.raises(ValueError):
        rpack.group([1], 0)
    data = list(range(1, 100))
    for nr_groups in range(1, 100):
        assert len(rpack.group(data, nr_groups)) == nr_groups
예제 #2
0
def test_swap_complete():
    """Test that no further "swapping" will improve the result"""
    # Get random data
    data = [random.random() for _ in range(500)]
    nr_groups = random.randint(2, 15)
    # Figure out which group is the maximal-sum group
    groups = rpack.group(data, nr_groups)
    group_sums = [sum(g) for g in groups]
    max_duration = max(group_sums)
    group_id_max = group_sums.index(max_duration)
    max_group = groups[group_id_max]

    # For each element in max_group, try to swap element with other
    # elements in the other groups and check if the "max-group" can
    # be improved.
    for i in range(len(max_group)):
        for group_id, group in enumerate(groups):
            if group_id == group_id_max:
                # We don't want to swap within the same group
                continue
            for j in range(len(group)):
                # Swap
                group[j], max_group[i] = max_group[i], group[j]
                # Swap should not make any improvements
                group_sums = [sum(g) for g in groups]
                assert max(group_sums) >= max_duration
                # Undo swap
                group[j], max_group[i] = max_group[i], group[j]
예제 #3
0
def test_input_error():
    with pytest.raises(TypeError):
        rpack.group(None, 3)
    with pytest.raises(TypeError):
        rpack.group([1, 2, 3], None)
    with pytest.raises(TypeError):
        rpack.group(None, None)
예제 #4
0
def test_empty_list():
    assert rpack.group([], 3) == [[], [], []]
예제 #5
0
def test_flat():
    data = [3, 2, 1]
    # Todo: the order should not matter
    assert rpack.group(data, 3) == [[3], [2], [1]]
예제 #6
0
def lol():
    t0 = time.time()
    groups = rpack.group(data, 5)
    t1 = time.time()
    print(max(sum(g) for g in groups), t1 - t0)