-
Notifications
You must be signed in to change notification settings - Fork 2
/
misc_test.py
74 lines (60 loc) · 2.69 KB
/
misc_test.py
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
from __future__ import absolute_import
from __future__ import print_function
from __future__ import division
from .misc import *
def test_nan():
assert True == isnan( float( 'nan' ))
assert False == isnan( 1.0 )
l = [1.0, nan, 0.0]
l.sort( key=nan_first )
assert isnan( l[0] )
l.sort( key=nan_last )
assert isnan( l[-1] )
def test_scale():
assert near( scale( 0., ( 0., 100. ), ( 32., 212. )), 32. )
assert near( scale( -40., ( 0., 100. ), ( 32., 212. )), -40. )
assert near( scale( 20., ( 0., 100. ), ( 32., 212. )), 68. )
# Try an inverted mapping (a reverse-ordered range)
assert near( scale( 0., ( 0., 100. ), ( 1., -1. )), 1. )
assert near( scale( -40., ( 0., 100. ), ( 1., -1. )), 1.80 )
assert near( scale( 20., ( 0., 100. ), ( 1., -1. )), 0.60 )
assert near( scale( 120., ( 0., 100. ), ( 1., -1. )), -1.40 )
# Try a reverse-ordered domain
assert near( scale( 0., ( 100., 0. ), ( 32., 212. )), 212. )
assert near( scale( -40., ( 100., 0. ), ( 32., 212. )), 284. )
assert near( scale( 20., ( 100., 0. ), ( 32., 212. )), 176. )
# An exponential mapping
assert near( scale( 40, ( 25 , 40 ), ( 0, 1 )), 1 )
assert near( scale( 40, ( 25 , 40 ), ( 0, 1 ), exponent=2), 1 )
assert near( scale( 25, ( 25 , 40 ), ( 0, 1 )), 0 )
assert near( scale( 25, ( 25 , 40 ), ( 0, 1 ), exponent=2), 0 )
assert near( scale( 25+15/2 , ( 25 , 40 ), ( 0, 1 )), .5 )
assert near( scale( 25+15/2 , ( 25 , 40 ), ( 0, 1 ), exponent=2), .25 )
assert near( scale( 39 , ( 25 , 40 ), ( 0, 1 )), .9333 )
assert near( scale( 39 , ( 25 , 40 ), ( 0, 1 ), exponent=2), .8711 )
assert near( scale( 26 , ( 25 , 40 ), ( 0, 1 )), .066667 )
assert near( scale( 26 , ( 25 , 40 ), ( 0, 1 ), exponent=2), .004444 )
# Ensure non-linear scaling ensures negatives may be handled by clamping domain
assert near( scale( 24 , ( 25 , 40 ), ( 0, 1 ), exponent=2, clamped=True ), 0 )
def test_magnitude():
# base 10 (the default)
assert near( magnitude( 23. ), 1. )
assert near( magnitude( .23 ), .01 )
assert near( magnitude( 75. ), 10. )
assert near( magnitude( .03 ), .001 )
# base 2
assert near( magnitude( 33., 2 ), 16. )
assert near( magnitude( 50., 2 ), 32. )
def test_value():
v = value( 1 )
v *= 5 # __imul__
assert 5 == v
i = 5
i //= v # __rfloordiv__
assert i == 1
i
assert type( i ) == int
x = v + 5
assert type( x ) == int
x = 5 + v
assert type( x ) == int