File size: 3,452 Bytes
079c32c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
import time
from unittest.mock import Mock

import pytest

from ding.utils.autolog import TickTime, NaturalTime, TimeProxy


class TestNaturalTime:

    @pytest.mark.unittest
    def test_natural_time(self):
        _time = NaturalTime()
        assert abs(_time.time() - time.time()) < 0.2

    @pytest.mark.benchmark
    def test_natural_time_for_100k_times(self):
        for i in range(0, 100000):
            _time = NaturalTime()
            assert abs(_time.time() - time.time()) < 0.2

    @pytest.mark.unittest
    def test_natural_time_with_mad_system(self):
        _time_func, time.time = time.time, Mock(side_effect=[1.5, 1.8, 2.0, 2.0, 1.75, 1.9, 2.2])

        try:
            _time = NaturalTime()
            assert _time.time() == 1.5
            assert _time.time() == 1.8
            assert _time.time() == 2.0
            assert _time.time() == 2.0
            assert _time.time() == 2.0
            assert _time.time() == 2.0
            assert _time.time() == 2.2
        finally:
            time.time = _time_func


class TestTickTime:

    @pytest.mark.unittest
    def test_tick_bare(self):
        _time = TickTime()
        assert _time.time() == 0
        assert _time.step() == 1
        assert _time.time() == 1
        assert _time.step(2) == 3
        assert _time.time() == 3

        with pytest.raises(TypeError):
            _time.step(0.9)

        with pytest.raises(ValueError):
            _time.step(0)

    @pytest.mark.unittest
    def test_tick_init(self):
        _time = TickTime(3)
        assert _time.time() == 3
        assert _time.step() == 4
        assert _time.time() == 4
        assert _time.step(2) == 6
        assert _time.time() == 6

        with pytest.raises(TypeError):
            _time.step(0.9)

        with pytest.raises(ValueError):
            _time.step(0)


class TestTimeProxy:

    @pytest.mark.unittest
    def test_time_proxy_for_tick_time(self):
        _time = TickTime()
        _proxy = TimeProxy(_time)

        assert _proxy.time() == 0
        assert _proxy.current_time() == 0
        assert not _proxy.is_frozen

        _time.step()
        assert _proxy.time() == 1
        assert _proxy.current_time() == 1
        assert not _proxy.is_frozen

        _proxy.freeze()
        _time.step(2)
        assert _proxy.time() == 1
        assert _proxy.current_time() == 3
        assert _proxy.is_frozen

        _time.step()
        assert _proxy.time() == 1
        assert _proxy.current_time() == 4
        assert _proxy.is_frozen

        _proxy.unfreeze()
        assert _proxy.time() == 4
        assert _proxy.current_time() == 4
        assert not _proxy.is_frozen

    @pytest.mark.unittest
    def test_time_proxy_frozen_for_tick_time(self):
        _time = TickTime()
        _proxy = TimeProxy(_time, frozen=True)

        assert _proxy.time() == 0
        assert _proxy.current_time() == 0
        assert _proxy.is_frozen

        _time.step()
        assert _proxy.time() == 0
        assert _proxy.current_time() == 1
        assert _proxy.is_frozen

        _time.step(2)
        assert _proxy.time() == 0
        assert _proxy.current_time() == 3
        assert _proxy.is_frozen

        _time.step()
        assert _proxy.time() == 0
        assert _proxy.current_time() == 4
        assert _proxy.is_frozen

        _proxy.unfreeze()
        assert _proxy.time() == 4
        assert _proxy.current_time() == 4
        assert not _proxy.is_frozen