forked from ilastik/lazyflow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtestMemory.py
106 lines (84 loc) · 3.09 KB
/
testMemory.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
###############################################################################
# lazyflow: data flow based lazy parallel computation framework
#
# Copyright (C) 2011-2014, the ilastik developers
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the Lesser GNU General Public License
# as published by the Free Software Foundation; either version 2.1
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# See the files LICENSE.lgpl2 and LICENSE.lgpl3 for full text of the
# GNU Lesser General Public License version 2.1 and 3 respectively.
# This information is also available on the ilastik web site at:
# http://ilastik.org/license/
###############################################################################
import unittest
from lazyflow.utility import Memory
from lazyflow.utility.memory import FormatError
from functools import partial
from numpy.testing import assert_equal
class TestMemory(unittest.TestCase):
def setUp(self):
Memory.setAvailableRam(-1)
Memory.setAvailableRamCaches(-1)
def tearDown(self):
Memory.setAvailableRam(-1)
Memory.setAvailableRamCaches(-1)
def testFormatting(self):
fmt = partial(Memory.format, trailing_digits=1)
r = 1
assert fmt(r) == "1.0B"
r *= 1024
assert fmt(r) == "1.0KiB"
r *= 1024
assert fmt(r) == "1.0MiB"
r *= 1024
assert fmt(r) == "1.0GiB"
r *= 1024
assert fmt(r) == "1.0TiB"
def testSettings(self):
assert Memory.getAvailableRam() > 0
assert Memory.getAvailableRamCaches() > 0
ram = 47*1111
Memory.setAvailableRam(ram)
assert Memory.getAvailableRam() == ram
cache_ram = ram//3
Memory.setAvailableRamCaches(cache_ram)
assert Memory.getAvailableRamCaches() == cache_ram
def testParsing(self):
parse = Memory.parse
d = parse("15B")
assert_equal(d, 15)
d = parse("1.0KiB")
assert_equal(d, 1024)
d = parse("2.25MiB")
assert_equal(d, int(2.25*1024**2))
with self.assertRaises(FormatError):
d = parse("bla")
with self.assertRaises(FormatError):
d = parse("15mB")
def testScientific(self):
sci = Memory.toScientific
x = 2.2 * 1024**3
(mant, exp) = sci(x)
assert_equal(mant, 2.2)
assert_equal(exp, 3)
x = 5.34 * 1e16
(mant, exp) = sci(x, base=10, explimit=100)
assert_equal(mant, 5.34)
assert_equal(exp, 16)
x = 17.2
(mant, exp) = sci(x, base=100)
assert_equal(mant, x)
assert_equal(exp, 0)
x = 223 * 1e3
(mant, exp) = sci(x, base=10, expstep=3)
assert_equal(mant, 223)
assert_equal(exp, 3)