/
flabel_test.py
72 lines (62 loc) · 2.52 KB
/
flabel_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
"flabel (list of lists) unit tests."
import numpy as np
from numpy.testing import assert_equal
from la.flabel import listmap, listmap_fill
# ---------------------------------------------------------------------------
# listmap
#
# test to make sure listmap returns the same output as
#
# idx = map(list1.index, list2)
def listmap_test():
"listmap test"
list1 = list(range(6))
list2 = list(range(5))
msg = "listmap failed on list1=%s and list2=%s and ignore_unmappable=%s"
for i in range(100):
np.random.shuffle(list2)
idx1 = list(map(list1.index, list2))
idx2 = listmap(list1, list2)
ignore_unmappable = False
yield assert_equal, idx1, idx2, msg % (list1, list2, ignore_unmappable)
ignore_unmappable = True
yield assert_equal, idx1, idx2, msg % (list1, list2, ignore_unmappable)
def listmap_unmappable_test():
"listmap unmappable test"
msg = "listmap failed on list1=%s and list2=%s and ignore_unmappable=%s"
for i in range(100):
list1 = list(range(6))
list2 = list(range(5))
np.random.shuffle(list2)
idx1 = list(map(list1.index, list2))
list2 = ['unmappable #1'] + list2 + ['unmappable #2']
ignore_unmappable = True
idx2 = listmap(list1, list2, ignore_unmappable=ignore_unmappable)
yield assert_equal, idx1, idx2, msg % (list1, list2, ignore_unmappable)
# ---------------------------------------------------------------------------
# listmap_fill unit tests
def listmap_fill_test():
"listmap_fill test"
# test to make sure listmap_nofill returns the same output as
#
# idx = map(list1.index, list2)
#
# when there are no items in list2 that are not in list1
list1 = list(range(6))
list2 = list(range(5))
msg = "listmap_fill failed on list1=%s and list2=%s"
for i in range(100):
np.random.shuffle(list2)
idx1 = list(map(list1.index, list2))
idx2, ignore = listmap_fill(list1, list2)
yield assert_equal, idx1, idx2, msg % (list1, list2)
def listmap_fill_unmappable_test():
"listmap_fill unmappable test"
list1 = ['a', 2, 3]
list2 = ['a', 2, 3, 4]
idx, idx_unmappable = listmap_fill(list1, list2)
idx2 = [0, 1, 2, 0]
idx2_unmappable = [3]
msg = "listmap_fill failed on list1=%s and list2=%s"
yield assert_equal, idx, idx2, msg % (list1, list2)
yield assert_equal, idx_unmappable, idx2_unmappable, msg % (list1, list2)