diff options
author | Daniel Friesel <daniel.friesel@uos.de> | 2019-09-11 17:33:36 +0200 |
---|---|---|
committer | Daniel Friesel <daniel.friesel@uos.de> | 2019-09-11 17:33:52 +0200 |
commit | af15bd8bcdb70de5dbf49fb0e785cda6da7e0bfc (patch) | |
tree | 9e0470835a858fdf344ea06798854fd6e71d0a89 /test | |
parent | f3f0b1dc3c506672d4655c80948136460811dae4 (diff) |
add simulation classes for online energy accounting inaccuracies
Diffstat (limited to 'test')
-rwxr-xr-x | test/test_codegen.py | 159 |
1 files changed, 159 insertions, 0 deletions
diff --git a/test/test_codegen.py b/test/test_codegen.py new file mode 100755 index 0000000..2d54158 --- /dev/null +++ b/test/test_codegen.py @@ -0,0 +1,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() |