-
Notifications
You must be signed in to change notification settings - Fork 95
/
test_util.py
81 lines (64 loc) · 2.96 KB
/
test_util.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
73
74
75
76
77
78
79
80
81
#!/usr/bin/env python
from biom.unit_test import TestCase, main
from biom.util import natsort, _natsort_key, flatten, unzip
__author__ = "Daniel McDonald"
__copyright__ = "Copyright 2012, BIOM-Format Project"
__credits__ = ["Rob Knight", "Peter Maxwell", "Sandra Smit",
"Zongzhi Liu", "Micah Hamady", "Daniel McDonald"]
__license__ = "GPL"
__url__ = "http://biom-format.org"
__version__ = "0.9.3-dev"
__maintainer__ = "Daniel McDonald"
__email__ = "daniel.mcdonald@colorado.edu"
class UtilTests(TestCase):
def test_natsort(self):
"""natsort should perform numeric comparisons on strings
test pulled from QIIME (http://qiime.org)
"""
# string with alpha and numerics sort correctly
s = 'sample1 sample2 sample11 sample12'.split()
self.assertEqual(natsort(s),
'sample1 sample2 sample11 sample12'.split())
s.reverse()
self.assertEqual(natsort(s),
'sample1 sample2 sample11 sample12'.split())
self.assertEqual(natsort(list('cba321')),list('123abc'))
# strings with alpha only sort correctly
self.assertEqual(natsort(list('cdba')),list('abcd'))
# string of ints sort correctly
self.assertEqual(natsort(['11','2','1','0']),
['0','1','2','11'])
# strings of floats sort correctly
self.assertEqual(natsort(['1.11','1.12','1.00','0.009']),
['0.009','1.00','1.11','1.12'])
# string of ints sort correctly
self.assertEqual(natsort([('11','A'),('2','B'),('1','C'),('0','D')]),
[('0','D'),('1','C'),('2','B'),('11','A')])
def test_unzip(self):
"""unzip(items) should be the inverse of zip(*items)
method pulled from PyCogent (http://pycogent.sourceforge.net)
"""
chars = [list('abcde'), list('ghijk')]
numbers = [[1,2,3,4,5], [0,0,0,0,0]]
strings = [["abcde", "fghij", "klmno"], ['xxxxx'] * 3]
empty = [[]]
lists = [chars, numbers, strings]
zipped = [zip(*i) for i in lists]
unzipped = [unzip(i) for i in zipped]
for u, l in zip(unzipped, lists):
self.assertEqual(u, l)
def test_flatten_no_change(self):
"""flatten should not change non-nested sequences (except to list)
test pulled from PyCogent (http://pycogent.sourceforge.net)
"""
self.assertEqual(flatten('abcdef'), list('abcdef')) #test identities
self.assertEqual(flatten([]), []) #test empty sequence
self.assertEqual(flatten(''), []) #test empty string
def test_flatten(self):
"""flatten should remove one level of nesting from nested sequences
test pulled from PyCogent (http://pycogent.sourceforge.net)
"""
self.assertEqual(flatten(['aa', 'bb', 'cc']), list('aabbcc'))
self.assertEqual(flatten([1,[2,3], [[4, [5]]]]), [1, 2, 3, [4,[5]]])
if __name__ == '__main__':
main()