test_who.py 2.22 KB
Newer Older
root's avatar
root committed
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
import cupy


class TestWho:
    def test_who_empty(self, capsys):
        cupy.who()
        out, err = capsys.readouterr()
        lines = out.split('\n')
        assert len(lines) == 3
        assert lines[1] == 'Upper bound on total bytes  =       0'

    def test_who_local_var(self, capsys):
        # Variables declared inside an object function are not visible
        # this is true also for numpy
        x = cupy.ones(10)  # NOQA
        cupy.who()
        out, err = capsys.readouterr()
        lines = out.split('\n')
        assert len(lines) == 3
        assert lines[1] == 'Upper bound on total bytes  =       0'

    def test_who_global(self, capsys):
        global x
        x = cupy.ones(10)  # NOQA
        cupy.who()
        out, err = capsys.readouterr()
        lines = out.split('\n')
        assert lines[-4].split() == ['x', '10', '80', 'float64']
        assert lines[-2] == 'Upper bound on total bytes  =       80'

    def test_who_global_multi(self, capsys):
        global x
        global y
        x = cupy.ones(10)  # NOQA
        y = cupy.ones(20, dtype=cupy.int32)  # NOQA
        cupy.who()
        out, err = capsys.readouterr()
        lines = out.split('\n')
        # depending on the env, the order in which vars are print
        # might be different
        var_1 = lines[-5].split()
        var_2 = lines[-4].split()
        if var_1[0] == 'x':
            assert var_1 == ['x', '10', '80', 'float64']
            assert var_2 == ['y', '20', '80', 'int32']
        else:
            assert var_2 == ['x', '10', '80', 'float64']
            assert var_1 == ['y', '20', '80', 'int32']
        assert lines[-2] == 'Upper bound on total bytes  =       160'

    def test_who_dict_arrays(self, capsys):
        var_dict = {'x': cupy.ones(10)}
        cupy.who(var_dict)
        out, err = capsys.readouterr()
        lines = out.split('\n')
        assert lines[-4].split() == ['x', '10', '80', 'float64']
        assert lines[-2] == 'Upper bound on total bytes  =       80'

    def test_who_dict_empty(self, capsys):
        global x
        x = cupy.ones(10)  # NOQA
        cupy.who({})
        out, err = capsys.readouterr()
        lines = out.split('\n')
        assert lines[-2] == 'Upper bound on total bytes  =       0'