-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathtest.py
executable file
·246 lines (216 loc) · 5.88 KB
/
test.py
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
#!/usr/bin/python
# Copyright 2017 loblab
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# Self test scripts
from __future__ import print_function
import time
import sys
import random
import os.path
from bitstring import *
from ask_signal import *
from ask_config import *
DITHER = 0.0
def setDither20():
global DITHER
DITHER = 0.2
return True
def setDither40():
global DITHER
DITHER = 0.4
return True
def setDither60():
global DITHER
DITHER = 0.6
return True
def testBitWave():
wave = BitWave()
wave.random()
wave.show()
return True
def testCodec(sig1, sig2, dither=0.0):
global DITHER
size = random.randint(13, 32)
val = random.randint(257, 2**size)
sig1.bits = BitArray('0x%x' % val)
sig1.show()
t1 = time.time()
wave = sig1.encode()
t2 = time.time()
print("Encode time: %.2fms" % ((t2 - t1) * 1000))
wave.dither(sig1.period * DITHER)
wave.show()
t1 = time.time()
if sig2.decode(wave):
t2 = time.time()
print("Decode time: %.2fms" % ((t2 - t1) * 1000))
sig2.show()
return sig2 == sig1
return False
def testCodecRaw():
sig1 = SignalRaw()
sig2 = SignalRaw()
return testCodec(sig1, sig2)
def testCodecPWM75():
sig1 = SignalPWM(0.75)
sig2 = SignalPWM()
return testCodec(sig1, sig2)
def testCodecPWM75Low():
sig1 = SignalPWM(0.75, start1=0, start0=5e-3)
sig2 = SignalPWM()
return testCodec(sig1, sig2)
def testCodecBP():
sig1 = SignalBP(39)
sig2 = SignalBP()
return testCodec(sig1, sig2)
def testAutoDecode():
sig1 = SignalPWM(0.75)
sig2 = Signal()
b1 = testCodec(sig1, sig2)
sig1 = SignalBP(39)
b2 = testCodec(sig1, sig2)
return b1 and b2
def testAutoInit():
for key in ASK_CMD:
cfg = ASK_CMD[key]
sig = Signal(cfg)
print(key, end=' ')
sig.show()
wave = sig.encode()
sig2 = Signal()
if not sig2.decode(wave):
print("testAutoInit: failed to decode")
return False
sig2.show()
if sig2 != sig:
print("testAutoInit: error in compare")
return False
return True
def testDump(filename, obj):
obj.show()
t1 = time.time()
with open (filename, 'wb') as fp:
obj.dump(fp)
t2 = time.time()
print("Dump time: %.2fms" % ((t2 - t1) * 1000))
def testLoad(filename, obj):
t1 = time.time()
with open (filename, 'rb') as fp:
obj.load(fp)
t2 = time.time()
print("Load time: %.2fms" % ((t2 - t1) * 1000))
obj.show()
def testStoreWave():
tmpfile='/tmp/wave.dat'
wave1 = BitWave()
wave1.random()
wave2 = BitWave()
testDump(tmpfile, wave1)
testLoad(tmpfile, wave2)
return wave1 == wave2
def testStoreSignal(sig1, sig2):
tmpfile='/tmp/signal.dat'
size = random.randint(13, 32)
val = random.randint(257, 2**size)
sig1.bits = BitArray('0x%x' % val)
testDump(tmpfile, sig1)
testLoad(tmpfile, sig2)
return sig1 == sig2
def testStoreRaw():
sig1 = SignalRaw()
sig2 = SignalRaw()
return testStoreSignal(sig1, sig2)
def testStorePWM():
sig1 = SignalPWM(0.75)
sig2 = SignalPWM()
return testStoreSignal(sig1, sig2)
def testStoreBP():
sig1 = SignalBP(39)
sig2 = SignalBP()
return testStoreSignal(sig1, sig2)
def testLoadFiles():
i = 0
while True:
i += 1
filename = DATA_FILE % i
if not os.path.isfile(filename):
break
#print(i)
#print(filename)
sig = Signal()
with open (filename, 'rb') as fp:
sig.load(fp)
print("%3d:" % i, end=' ')
sig.show()
return True
TEST_CASES = [
[testBitWave, "testBitWave"],
[testCodecRaw, "testCodecRaw"],
[testCodecPWM75, "testCodecPWM 75%"],
[testCodecPWM75Low, "testCodecPWM 75%, Low level start"],
[testCodecBP, "testCodecBP(Biphase)"],
[testAutoDecode, "testAutoDecode"],
[testAutoInit, "testAutoInit"],
[testStoreWave, "testStoreWave"],
[testStoreRaw, "testStoreRaw"],
[testStorePWM, "testStorePWM"],
[testStoreBP, "testStoreBP"],
[testLoadFiles, "testLoadFiles"],
[setDither20, "setDither 20%"],
[setDither40, "setDither 40%"],
[setDither40, "setDither 60%"],
]
def list_cases():
for i in range(len(TEST_CASES)):
print("%2d: %s" % (i + 1, TEST_CASES[i][1]))
def run_case(index):
case = TEST_CASES[index - 1]
print
print("Case %d: %s" % (index, case[1]))
print('=========================================')
t1 = time.time()
rc = case[0]()
t2 = time.time()
print('----------------------')
print("%s. %.2f ms" % ('PASS' if rc else 'FAIL', (t2 - t1) * 1000))
return rc
def run_cases(cases):
passed = 0
failed = 0
t1 = time.time()
for case in cases:
rc = run_case(int(case))
if rc:
passed += 1
else:
failed += 1
t2 = time.time()
total = passed + failed
if total > 1:
print
print('#########################################')
print("%d/%d FAIL. %.2f ms" % (failed, total, (t2 - t1) * 1000))
def main(argv=None):
if argv is None:
argv = sys.argv
if len(argv) < 2:
list_cases()
return 1
if argv[1] == 'all':
cases = range(1, len(TEST_CASES) + 1)
else:
cases = argv[1:]
run_cases(cases)
if __name__ == "__main__":
sys.exit(main())