-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathtest.js
301 lines (261 loc) · 9.69 KB
/
test.js
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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
var tessel = require('tessel');
var test = require('tinytap');
var climatelib = require('../');
var port = process.argv[2] || 'A';
var climate = null;
var TIMEOUT = 10000;
test.count(31);
function completionChecker (required, test) {
this.required = required;
this.completed = 0;
this.test = test;
}
completionChecker.prototype.check = function () {
this.completed++;
if (this.completed === this.required) {
this.test.end();
}
}
test('Reasonable boot time and \'ready\' event', function (t) {
// Don't end the test until the last part is done. There is 1 required event.
var checker = new completionChecker(1, t);
// Connect to the module in a reasonable amount of time
var startTime = new Date();
climate = climatelib.use(tessel.port[port]);
var requireTime = new Date();
t.ok(requireTime - startTime < 100, 'Module took longer than 100ms to boot');
var rl;
climate.on('ready', rl = function () {
clearTimeout(rlf);
t.ok(true, 'Ready event fired');
checker.check();
climate.removeListener('ready', rl);
});
var rlf = setTimeout(function () {
climate.removeListener('ready', rl);
t.ok(false, '\'ready\' event never fired');
}, TIMEOUT);
});
///////////////////////////// Functions ///////////////////////////////
test('getData', function (t) {
// Temperature read
climate.getData(0x10, function (err, data) {
t.ok(!err, 'Error retrieving data from Si7005');
t.ok(data, 'Invalid data retrieved from Si7005');
t.end();
});
});
test('readTemperature - Valid temperature reading, Celsius', function (t) {
// Don't end the test until the last part is done. There is 1 required event.
var checker = new completionChecker(1, t);
// Celsius temperature event listener setup
var rtcl;
climate.on('temperature', rtcl = function (temp, type) {
if (type !== 'f') {
clearTimeout(rtcf);
climate.removeListener('temperature', rtcl);
t.ok(true, '\'temperature\' event for Celsius fired');
checker.check();
}
});
// Celsius temperature event failure to fire
var rtcf = setTimeout(function () {
climate.removeListener('temperature', rtcl);
t.ok(false, '\'temperature\' event for Celsius never fired');
checker.check();
}, TIMEOUT);
climate.readTemperature(function (err, temp) {
// Make sure there was no error reading the temperature
t.ok(!err, 'Error reading temperature in Celsius');
// Does the temperature reading make sense?
t.equal(typeof temp, 'number', 'Returned temperature in Celsius was not a number');
t.ok(temp > -100 && temp < 200, 'Returned temperature in Celsius does not make sense physically');
});
});
test('readTemperature - Valid temperature reading, Farenheit', function (t) {
// Don't end the test until the last part is done. There is 1 required event.
var checker = new completionChecker(1, t);
// Farenheit temperature event listener setup
var rtfl;
climate.on('temperature', rtfl = function (temp, type) {
if (type === 'f') {
clearTimeout(rtff);
climate.removeListener('temperature', rtfl);
t.ok(true, '\'temperature\' event for Farenheit fired');
checker.check();
}
});
// Farenheit temperature event failure to fire
var rtff = setTimeout(function () {
climate.removeListener('temperature', rtfl);
t.ok(false, '\'temperature\' event for Farenheit never fired');
checker.check();
}, TIMEOUT);
climate.readTemperature('f', function (err, temp) {
// Make sure there was no error reading the temperature
t.ok(!err, 'Error reading temperature in Farenheit');
// Does the temperature reading make sense?
t.equal(typeof temp, 'number', 'Returned temperature in Farenheit was not a number');
t.ok(temp > -100 && temp < 200, 'Returned temperature in Farenheit does not make sense physically');
});
});
test('readTemperature - consistent Farenheit and Celsius measurements and events', function (t) {
// Read the temperatures
climate.readTemperature(function (errC, tempC) {
climate.readTemperature('f', function (errF, tempF) {
// Sanity check
t.ok(!errC && !errF, 'Error reading temperatures, which is odd because they passed last time...');
t.ok(Math.abs((tempF - 32) * (5.0/9.0) - tempC) < 0.5, 'Temperature mismatch by more than 0.5 degrees C in consecutive reads with unit conversion');
t.end();
});
});
});
test('readHumidity', function (t) {
// Don't end the test until the last part is done. There is 1 required event.
var checker = new completionChecker(1, t);
// Event listener setup
var rhl;
climate.on('humidity', rhl = function () {
clearTimeout(rhf);
climate.removeListener('humidity', rhl);
t.ok(true, '\'humidity\' event fired');
checker.check();
});
// Humidity event failure to fire
var rhf = setTimeout(function () {
climate.removeListener('humidity', rhl);
t.ok(false, '\'humidity\' event never fired');
checker.check();
}, TIMEOUT);
// Actually read the humidity
climate.readHumidity(function (err, hum) {
// Make sure there was no error reading the humidity
t.ok(!err, 'Error reading humidity');
// Check the humidity value
t.equal(typeof hum, 'number', 'Returned humidity was not a number');
t.ok(hum >= -50 && hum <= 150, 'Returned humidity value does not make sense physically');
});
});
test('setHeater', function (t) {
// Get the current heater setting
var heaterStart = climate._configReg & 0x02;
// Set the heater bit to false
climate.setHeater(false);
var heaterOff = climate._configReg & 0x02;
// Set the heater bit to true
climate.setHeater(true);
var heaterOn = climate._configReg & 0x02;
// Make sure we're setting the internal variable correctly
t.ok(!heaterStart, 'Heater was on by default');
t.ok(!heaterOff, 'Unable to deactivate heater');
t.ok(heaterOn, 'Unable to activate heater');
// Make sure it can get set/reset randomly
var targets = [];
var values = [];
var tries = 100;
for (var i = 0; i < tries; i++) {
var target = Math.random() > 0.5 ? true : false;
targets.push(target);
climate.setHeater(target);
values.push(climate._configReg & 0x02 ? true : false);
}
for (i = 0; i < tries; i++) {
if (targets[i] != values[i]) {
console.log('fail\n', targets, values);
t.ok(false, 'Random heater set/reset failed');
break;
}
if (i == tries-1) {
t.ok(true, 'Random heater set/reset successful');
}
}
climate.setHeater(true);
// Actually turn the heater on by sending a read command
climate.readTemperature(function (err1, temp1) {
// Wait for the thing to heat up a little
setTimeout( function () {
// Take another measurement
climate.readTemperature(function (err2, temp2) {
t.ok(!err1 && !err2 && temp1 && temp2, 'Error or invalid measurements during reads');
t.ok(temp2 >= temp1, 'Heater nonfunctional: t1='+temp1+', t2='+temp2);
// Turn the heater back off
climate.setHeater(false);
climate.readTemperature(function () {
t.end();
});
});
}, 100);
});
});
test('setFastMeasure', function (t) {
// Get the current fastMeasure setting
var fastMeasureStart = climate._configReg & 0x20;
// Set the fastMeasure bit to false
climate.setFastMeasure(false);
var fastMeasureOff = climate._configReg & 0x20;
// Set the fastMeasure bit to true
climate.setFastMeasure(true);
var fastMeasureOn = climate._configReg & 0x20;
// Make sure we're setting the internal variable correctly
t.ok(!fastMeasureStart, 'fastMeasure was on by default');
t.ok(!fastMeasureOff, 'Unable to deactivate fastMeasure');
t.ok(fastMeasureOn, 'Unable to activate fastMeasure');
// Make sure it can get set/reset randomly
var targets = [];
var values = [];
var tries = 100;
for (var i = 0; i < tries; i++) {
var target = Math.random() > 0.5 ? true : false;
targets.push(target);
climate.setFastMeasure(target);
values.push(climate._configReg & 0x20 ? true : false);
}
for (i = 0; i < tries; i++) {
if (targets[i] != values[i]) {
console.log('fail\n', targets, values);
t.ok(false, 'Random fastMeasure set/reset failed');
break;
}
if (i == tries-1) {
t.ok(true, 'Random fastMeasure set/reset successful');
}
}
// Actually see if we can measure faster. Start by allocating memory.
var dtNormalT = new Date();
var dtFastT = new Date();
var dtNormalH = new Date();
var dtFastH = new Date();
var foo = new Date();
// Start false
climate.setFastMeasure(false);
climate.readTemperature(function (err, data) {
// Setting transferred, so gather data
foo = new Date();
climate.readTemperature(function (err1, data1) {
dtNormalT = new Date() - foo;
foo = new Date();
climate.readHumidity(function (err2, data2) {
dtNormalH = new Date() - foo;
// Switch to true, transfer setting
climate.setFastMeasure(true);
climate.readHumidity(function (err3, data3) {
foo = new Date();
climate.readHumidity(function (err4, data4) {
dtFastH = new Date() - foo;
foo = new Date();
climate.readTemperature(function (err5, data5) {
dtFastT = new Date() - foo;
// Sanity check
t.ok(!err && !err1 && !err2 && !err3 && !err4 && !err5 &&
data && data1 && data2 && data3 && data4 && data5,
'Measurements unsuccessful or invalid');
t.ok(dtFastT < dtNormalT, 'fastMeasure was slow for temperature');
t.ok(dtFastH < dtNormalH, 'fastMeasure was slow for humidity');
t.end();
});
});
});
});
});
});
});