Debian Codegraph @

Aircraft/Generic/Systems/Tests/FailureMgr/test_cycle_counter.nas

raw
1 # CycleCounter unit tests
2 #
3 # Copyright (C) 2014 Anton Gomez Alvedro
4 #
5 # This program is free software; you can redistribute it and/or
6 # modify it under the terms of the GNU General Public License as
7 # published by the Free Software Foundation; either version 2 of the
8 # License, or (at your option) any later version.
9 #
10 # This program is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 # General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18
19 io.include("Aircraft/Generic/Systems/Tests/test.nas");
20 io.include("Aircraft/Generic/Systems/failures.nas");
21
22 var TestCycleCounter = {
23
24 parents: [TestSuite],
25
26 setup: func {
27 props.globals.initNode("/test");
28 },
29
30 cleanup: func {
31 props.globals.getNode("/test").remove();
32 me.counter = nil;
33 },
34
35 _shake_that_prop: func (pattern=nil) {
36
37 if (pattern == nil)
38 pattern = [0, -10, 10, -10, 10, -10, 10, 0];
39
40 setprop("/test/property", pattern[0]);
41 me.counter.reset();
42
43 var i = 0;
44 var value = pattern[0];
45 var target = pattern[1];
46 var delta = 0;
47
48 while(i < size(pattern) - 1) {
49
50 target = pattern[i+1];
51 delta = pattern[i+1] > pattern[i] ? 1 : -1;
52
53 while(value != target) {
54 value += delta;
55 setprop("/test/property", value);
56 }
57
58 i += 1;
59 }
60 },
61
62 test_cycles_dont_grow_while_disabled: func {
63 me.counter = CycleCounter.new("/test/property");
64 me._shake_that_prop();
65 assert(me.counter.cycles == 0);
66 },
67
68 test_cycles_grow_while_enabled: func {
69 me.counter = CycleCounter.new("/test/property");
70
71 me._shake_that_prop();
72 assert(me.counter.cycles == 0);
73
74 me.counter.enable();
75
76 me._shake_that_prop();
77 assert(me.counter.cycles == 3);
78 },
79
80 test_reset: func {
81 me.counter = CycleCounter.new("/test/property");
82 me.counter.enable();
83
84 me._shake_that_prop();
85 assert(me.counter.cycles > 0);
86
87 me.counter.reset();
88 assert(me.counter.cycles == 0);
89 },
90
91 test_callback_every_half_cycle: func {
92 var count = 0;
93
94 me.counter = CycleCounter.new(
95 property: "/test/property",
96 on_update: func (cycles) { count += 1 });
97
98 me.counter.enable();
99 me._shake_that_prop();
100
101 assert(count == 6);
102 },
103
104 test_callback_reports_cycle_count: func {
105 var count = 0;
106 var cb = func (cycles) {
107 count += 1;
108 assert(cycles == count * 0.5);
109 };
110
111 me.counter = CycleCounter.new(
112 property: "/test/property", on_update: cb);
113
114 me.counter.enable();
115 me._shake_that_prop();
116 },
117
118 test_counter_works_for_binary_props: func {
119 me.counter = CycleCounter.new("/test/property");
120 me.counter.enable();
121 me._shake_that_prop([0, 1, 0, 1, 0, 1]);
122 assert(me.counter.cycles == 2);
123 }
124 };