1
+ """
2
+ mbed SDK
3
+ Copyright (c) 2011-2013 ARM Limited
4
+
5
+ Licensed under the Apache License, Version 2.0 (the "License");
6
+ you may not use this file except in compliance with the License.
7
+ You may obtain a copy of the License at
8
+
9
+ http://www.apache.org/licenses/LICENSE-2.0
10
+
11
+ Unless required by applicable law or agreed to in writing, software
12
+ distributed under the License is distributed on an "AS IS" BASIS,
13
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ See the License for the specific language governing permissions and
15
+ limitations under the License.
16
+ """
17
+
18
+ from mbed_host_tests import BaseHostTest
19
+
20
+
21
+ class TimingDriftTest (BaseHostTest ):
22
+ """ This test is reading single characters from stdio
23
+ and measures time between their occurrences.
24
+ """
25
+ __result = None
26
+
27
+ # This is calculated later: average_drift_max * number of tick events
28
+ total_drift_max = None
29
+
30
+ average_drift_max = 0.05
31
+ ticks = []
32
+ start_time = None
33
+ finish_time = None
34
+ dut_seconds_passed = None
35
+ total_time = None
36
+ total_drift = None
37
+ average_drift = None
38
+
39
+ def _callback_result (self , key , value , timestamp ):
40
+ # We should not see result data in this test
41
+ self .__result = False
42
+
43
+ def _callback_end (self , key , value , timestamp ):
44
+ """ {{end;%s}}} """
45
+ self .log ("Received end event, timestamp: %f" % timestamp )
46
+ self .notify_complete (result = self .result (print_stats = True ))
47
+
48
+
49
+ def _callback_tick (self , key , value , timestamp ):
50
+ """ {{tick;%d}}} """
51
+ self .log ("tick! %f" % timestamp )
52
+ self .ticks .append ((key , value , timestamp ))
53
+
54
+
55
+ def setup (self ):
56
+ self .register_callback ("end" , self ._callback_end )
57
+ self .register_callback ('tick' , self ._callback_tick )
58
+
59
+
60
+ def result (self , print_stats = True ):
61
+ self .dut_seconds_passed = len (self .ticks ) - 1
62
+
63
+ if self .dut_seconds_passed < 1 :
64
+ if print_stats :
65
+ self .log ("FAIL: failed to receive at least two tick events" )
66
+ self .__result = False
67
+ return self .__result
68
+
69
+ self .total_drift_max = self .dut_seconds_passed * self .average_drift_max
70
+
71
+ self .start_time = self .ticks [0 ][2 ]
72
+ self .finish_time = self .ticks [- 1 ][2 ]
73
+ self .total_time = self .finish_time - self .start_time
74
+ self .total_drift = self .total_time - self .dut_seconds_passed
75
+ self .average_drift = self .total_drift / self .dut_seconds_passed
76
+
77
+ if print_stats :
78
+ self .log ("Start: %f" % self .start_time )
79
+ self .log ("Finish: %f" % self .finish_time )
80
+ self .log ("Total time taken: %f" % self .total_time )
81
+
82
+ total_drift_ratio_string = "Total drift/Max total drift: %f/%f"
83
+ self .log (total_drift_ratio_string % (self .total_drift ,
84
+ self .total_drift_max ))
85
+
86
+ average_drift_ratio_string = "Average drift/Max average drift: %f/%f"
87
+ self .log (average_drift_ratio_string % (self .average_drift ,
88
+ self .average_drift_max ))
89
+
90
+
91
+ if self .total_drift > self .total_drift_max :
92
+ if print_stats :
93
+ self .log ("FAIL: Total drift exceeded max total drift" )
94
+ self .__result = False
95
+ elif self .average_drift > self .average_drift_max :
96
+ if print_stats :
97
+ self .log ("FAIL: Average drift exceeded max average drift" )
98
+ self .__result = False
99
+ else :
100
+ self .__result = True
101
+
102
+ return self .__result
103
+
104
+
105
+ def teardown (self ):
106
+ pass
0 commit comments