summaryrefslogtreecommitdiff
path: root/test/test_codegen.py
blob: 2d54158bdabd90be6cad3091e0ec639fb31529e7 (plain)
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#!/usr/bin/env python3

from automata import PTA
from codegen import get_simulated_accountingmethod
import unittest

example_json_1 = {
    'parameters' : ['datarate', 'txbytes', 'txpower'],
    'initial_param_values' : [None, None, None],
    'state' : {
        'IDLE' : {
            'power' : {
                'static' : 5,
            }
        },
        'TX' : {
            'power' : {
                'static' : 100,
                'function' : {
                    'raw' : 'regression_arg(0) + regression_arg(1)'
                        ' * parameter(txpower)',
                    'regression_args' : [ 100, 2 ]
                },
            }
        },
    },
    'transitions' : [
        {
            'name' : 'init',
            'origin' : ['UNINITIALIZED', 'IDLE'],
            'destination' : 'IDLE',
            'duration' : {
                'static' : 50000,
            },
            'set_param' : {
                'txpower' : 10
            },
        },
        {
            'name' : 'setTxPower',
            'origin' : 'IDLE',
            'destination' : 'IDLE',
            'duration' : { 'static' : 120 },
            'energy ' : { 'static' : 10000 },
            'arg_to_param_map' : { 'txpower' : 0 },
            'argument_values' : [ [10, 20, 30] ],
        },
        {
            'name' : 'send',
            'origin' : 'IDLE',
            'destination' : 'TX',
            'duration' : {
                'static' : 10,
                'function' : {
                    'raw' : 'regression_arg(0) + regression_arg(1)'
                        ' * function_arg(1)',
                    'regression_args' : [48, 8],
                },
            },
            'energy' : {
                'static' : 3,
                'function' : {
                    'raw' : 'regression_arg(0) + regression_arg(1)'
                        ' * function_arg(1)',
                    'regression_args' : [3, 5],
                },
            },
            'arg_to_param_map' : { 'txbytes' : 1 },
            'argument_values' : [ ['"foo"', '"hodor"'], [3, 5] ],
            'argument_combination' : 'zip',
        },
        {
            'name' : 'txComplete',
            'origin' : 'TX',
            'destination' : 'IDLE',
            'is_interrupt' : 1,
            'timeout' : {
                'static' : 2000,
                'function' : {
                    'raw' : 'regression_arg(0) + regression_arg(1)'
                        ' * parameter(txbytes)',
                    'regression_args' : [ 500, 16 ],
                },
            },
        }
    ],
}

class TestCG(unittest.TestCase):
    def test_statetransition_immediate(self):
        pta = PTA.from_json(example_json_1)
        pta.set_random_energy_model()
        pta.state['IDLE'].power = 9
        cg = get_simulated_accountingmethod('static_statetransition_immediate')(pta, 1000000, 'uint8_t', 'uint8_t', 'uint8_t', 'uint8_t')
        cg.current_state = pta.state['IDLE']
        cg.sleep(7)
        self.assertEqual(cg.get_energy(), 9 * 7)
        pta.transitions[1].energy = 123
        cg.pass_transition(pta.transitions[1])
        self.assertEqual(cg.get_energy(), 9 * 7 + 123)
        cg.pass_transition(pta.transitions[1])
        self.assertEqual(cg.get_energy(), (9 * 7 + 123 + 123) % 256)

        cg = get_simulated_accountingmethod('static_statetransition_immediate')(pta, 100000, 'uint8_t', 'uint8_t', 'uint8_t', 'uint8_t')
        cg.current_state = pta.state['IDLE']
        cg.sleep(7)
        self.assertEqual(cg.get_energy(), 0)
        cg.sleep(15)
        self.assertEqual(cg.get_energy(), 90)
        cg.sleep(90)
        self.assertEqual(cg.get_energy(), 900 % 256)

    def test_statetransition(self):
        pta = PTA.from_json(example_json_1)
        pta.set_random_energy_model()
        pta.state['IDLE'].power = 9
        cg = get_simulated_accountingmethod('static_statetransition')(pta, 1000000, 'uint8_t', 'uint8_t', 'uint8_t', 'uint8_t')
        cg.current_state = pta.state['IDLE']
        cg.sleep(7)
        self.assertEqual(cg.get_energy(), 9 * 7)
        pta.transitions[1].energy = 123
        cg.pass_transition(pta.transitions[1])
        self.assertEqual(cg.get_energy(), 9 * 7 + 123)
        cg.pass_transition(pta.transitions[1])
        self.assertEqual(cg.get_energy(), (9 * 7 + 123 + 123) % 256)

    def test_state_immediate(self):
        pta = PTA.from_json(example_json_1)
        pta.set_random_energy_model()
        pta.state['IDLE'].power = 9
        cg = get_simulated_accountingmethod('static_state_immediate')(pta, 1000000, 'uint8_t', 'uint8_t', 'uint8_t', 'uint8_t')
        cg.current_state = pta.state['IDLE']
        cg.sleep(7)
        self.assertEqual(cg.get_energy(), 9 * 7)
        pta.transitions[1].energy = 123
        cg.pass_transition(pta.transitions[1])
        self.assertEqual(cg.get_energy(), 9 * 7)
        cg.pass_transition(pta.transitions[1])
        self.assertEqual(cg.get_energy(), 9 * 7)

    def test_state(self):
        pta = PTA.from_json(example_json_1)
        pta.set_random_energy_model()
        pta.state['IDLE'].power = 9
        cg = get_simulated_accountingmethod('static_state')(pta, 1000000, 'uint8_t', 'uint8_t', 'uint8_t', 'uint8_t')
        cg.current_state = pta.state['IDLE']
        cg.sleep(7)
        self.assertEqual(cg.get_energy(), 9 * 7)
        pta.transitions[1].energy = 123
        cg.pass_transition(pta.transitions[1])
        self.assertEqual(cg.get_energy(), 9 * 7)
        cg.pass_transition(pta.transitions[1])
        self.assertEqual(cg.get_energy(), 9 * 7)

        cg = get_simulated_accountingmethod('static_state')(pta, 1000000, 'uint8_t', 'uint16_t', 'uint16_t', 'uint16_t')


if __name__ == '__main__':
    unittest.main()