def test_calc_mem_used():
    # should be able to send at least 100 fps at 1024 bytes
    assert calc_mem_used(1, 1024) < 512000
    # should use less memory at 64 than 1024
    assert calc_mem_used(1, 64) < calc_mem_used(1, 1024)
    # should use less memory with 1 stream than 100 streams
    assert calc_mem_used(1, 1024) < calc_mem_used(1000, 1024)
def test_check_mem():
    # starting with 0 confidence should always return 0 confidence
    assert check_mem(100000, 1, 64, Confidence(0.0)).percent == 0.0

    # huge mem should have no effect on confidence
    assert check_mem(1e10, 1, 64, Confidence(99.0)).percent == 99.0
    assert check_mem(1e10, 1000, 8192, Confidence(62.0)).percent == 62.0

    # oversubscribed means no confidence
    assert check_mem(calc_mem_used(1, 1024) - 1, 1, 1024,
                     Confidence(100.0)).percent == 0.0

    # undersubscribed means no change to confidence
    assert check_mem(calc_mem_used(1, 1024)*2, 1, 1024,
                     Confidence(75.0)).percent == 75.0

    # in the middle means scaling confidence
    scaled_confidence = check_mem(calc_mem_used(1, 1024) /
                                  ((1 + SAFETY_FACTOR) / 2),
                                  1, 1024, Confidence(100.0)).percent
    assert round(scaled_confidence, 2) == 50.0
def test_check_mem_reasons():
    # starting with a reason should return that reason (maybe with additions)
    assert "foo" in check_mem(5000000, 1, 64, Confidence(reason="foo")).reason
    assert "foo" in check_mem(0, 1, 64, Confidence(reason="foo")).reason

    # huge mem should have no effect on reason
    assert check_mem(1e10, 1, 64, Confidence(99.0, "foo")).reason == "foo"
    assert check_mem(1e10, 1000, 8192, Confidence(62.0, "foo")).reason == "foo"

    # oversubscribed means no confidence and a reason
    reason = check_mem(calc_mem_used(1, 1024) - 1, 1, 1024,
                       Confidence()).reason
    assert "memory" in reason

    # undersubscribed means no effect on reason
    assert check_mem(calc_mem_used(1, 1024)*2, 1, 1024,
                     Confidence(75.0, "foo")).reason == "foo"

    # in the middle means a reason
    scaled_reason = check_mem(calc_mem_used(1, 1024) /
                              ((1 + SAFETY_FACTOR) / 2),
                              1, 1024, Confidence()).reason
    assert "memory" in scaled_reason