add some basic tests for clamp

these are expected to fail

* for Python 3, because the argument defaults (None) are incomparable
* for Python 2, because None<x for all ints and floats x, thus clamp(x)
  returns None
This commit is contained in:
tiwo 2017-06-18 14:22:33 +02:00
parent 8ec56cd1bf
commit 230b856dc3
1 changed files with 21 additions and 1 deletions

View File

@ -1,7 +1,9 @@
from pytest import raises from pytest import raises
from boltons.mathutils import ceil, floor from boltons.mathutils import clamp, ceil, floor
import math
INF, NAN = float('inf'), float('nan')
OPTIONS = [1618, 1378, 166, 1521, 2347, 2016, 879, 2123, OPTIONS = [1618, 1378, 166, 1521, 2347, 2016, 879, 2123,
269.3, 1230, 66, 425.2, 250, 2399, 2314, 439, 269.3, 1230, 66, 425.2, 250, 2399, 2314, 439,
@ -13,6 +15,24 @@ VALID_LOWER = 247
VALID_UPPER = 2314 VALID_UPPER = 2314
VALID_BETWEEN = 248.5 VALID_BETWEEN = 248.5
def test_clamp_examples():
"""some examples for clamp()"""
assert 0 == clamp(0, 0, 1) == clamp(-1, 0, 1)
assert 0 == clamp(-1, lower=0)
assert 1 == clamp(1, 0, 1) == clamp(5, 0, 1)
assert 1 == clamp(5, upper=1)
assert 0.5 == clamp(7, upper=0.5)
assert 1 == clamp(7.7, upper=1)
def test_clamp_transparent():
"""clamp(x) should equal x beacause both limits are omitted"""
assert clamp(0) == 0
assert clamp(1) == 1
assert clamp(10**100) == 10**100
assert clamp(INF) == INF
assert clamp(-INF) == -INF
assert math.isnan(clamp(NAN))
def test_ceil_basic(): def test_ceil_basic():
assert ceil(VALID_LOWER, OPTIONS) == VALID_LOWER assert ceil(VALID_LOWER, OPTIONS) == VALID_LOWER