Skip to content
This repository was archived by the owner on Aug 5, 2022. It is now read-only.

Commit dcb06fb

Browse files
haoxligrgustaf
authored andcommitted
[tests] Sync tests from master
These tests should be tested in zjs-0.1 Signed-off-by: Li, Hao <[email protected]>
1 parent 046206f commit dcb06fb

File tree

4 files changed

+528
-0
lines changed

4 files changed

+528
-0
lines changed

tests/test-a101-pins.js

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
// Copyright (c) 2016, Intel Corporation.
2+
3+
// Test Arduino101Pins API
4+
5+
var pins = require('arduino101_pins');
6+
var gpio = require('gpio');
7+
var pwm = require('pwm');
8+
var aio = require('aio');
9+
10+
var total = 0;
11+
var passed = 0;
12+
13+
function assert(actual, description) {
14+
total += 1;
15+
16+
var label = "\033[1m\033[31mFAIL\033[0m";
17+
if (actual === true) {
18+
passed += 1;
19+
label = "\033[1m\033[32mPASS\033[0m";
20+
}
21+
22+
console.log(label + " - " + description);
23+
}
24+
25+
function expectThrow(description, func) {
26+
var threw = false;
27+
try {
28+
func();
29+
}
30+
catch (err) {
31+
threw = true;
32+
}
33+
assert(threw, description);
34+
}
35+
36+
// Check pins defined and typeof Number
37+
function checkDefined(name) {
38+
assert(name in pins && typeof pins[name] == "number",
39+
"Arduino101Pins: " + name + " defined");
40+
}
41+
42+
// GPIO Pins
43+
var GPIOPins = ["IO2", "IO3", "IO4", "IO5",
44+
"IO6", "IO7", "IO8", "IO9",
45+
"IO10", "IO11", "IO12", "IO13"];
46+
for(var i = 0; i < GPIOPins.length; i++) {
47+
var pinName = GPIOPins[i];
48+
49+
checkDefined(pinName);
50+
51+
// IO6 and IO9 are defined but unusable as GPIOs currently
52+
if (pinName == "IO6" || pinName == "IO9") continue;
53+
54+
var pin = gpio.open({ pin: pins[pinName] });
55+
var pinValue = pin.read();
56+
assert(typeof pinValue == "boolean",
57+
"Arduino101Pins: " + pinName + " input");
58+
59+
pin.write(!pinValue);
60+
if (pinName == "IO3" || pinName == "IO5") {
61+
// IO3 and IO5 can be used as GPIO inputs but not outputs currently
62+
assert(pin.read() == pinValue,
63+
"Arduino101Pins: " + pinName + " output");
64+
} else {
65+
assert(pin.read() != pinValue,
66+
"Arduino101Pins: " + pinName + " output");
67+
}
68+
}
69+
70+
// LEDs
71+
var LEDs = [["LED0", false],
72+
["LED1", true ],
73+
["LED2", true ]];
74+
for(var i = 0; i < LEDs.length; i++) {
75+
var pinName = LEDs[i][0];
76+
77+
checkDefined(pinName);
78+
79+
// activeLow
80+
var lowFlag = LEDs[i][1];
81+
var pin = gpio.open({ pin: pins[pinName], activeLow: lowFlag });
82+
var pinValue = pin.read();
83+
var lowStr = lowFlag ? "high" : "low";
84+
assert(pinValue,
85+
"Arduino101Pins: " + pinName + " active " + lowStr);
86+
87+
pin.write(!pinValue)
88+
assert(pin.read() != pinValue,
89+
"Arduino101Pins: " + pinName + " output");
90+
91+
if (pinName == "LED0") {
92+
pinValue = pin.read();
93+
var io13 = gpio.open({ pin: pins.IO13, activeLow: lowFlag });
94+
io13.write(!pinValue);
95+
assert(pin.read() != pinValue,
96+
"Arduino101Pins: " + pinName + " displays current state of IO13");
97+
}
98+
}
99+
100+
// PWM Pins
101+
var PWMPins = ["PWM0", "PWM1", "PWM2", "PWM3"];
102+
for(var i = 0; i < PWMPins.length; i++) {
103+
var pinName = PWMPins[i];
104+
105+
checkDefined(pinName);
106+
107+
var pin = pwm.open({channel: pins[pinName]});
108+
assert(pin != null && typeof pin == "object",
109+
"Arduino101Pins: " + pinName + " open");
110+
}
111+
112+
// AIO Pins
113+
var AIOPins = ["A0", "A1", "A2", "A3", "A4", "A5"];
114+
for(var i = 0; i < AIOPins.length; i++) {
115+
var pinName = AIOPins[i];
116+
117+
checkDefined(pinName);
118+
119+
var pin = aio.open({ device: 0, pin: pins[pinName] });
120+
var pinValue = pin.read();
121+
assert(pinValue >= 0 && pinValue <= 4095,
122+
"Arduino101Pins: " + pinName + " digital value");
123+
}
124+
125+
console.log("TOTAL: " + passed + " of " + total + " passed");

tests/test-gpio.js

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
// Copyright (c) 2016, Intel Corporation.
2+
3+
// Testing GPIO APIs
4+
5+
// Pre-conditions
6+
console.log("Wire IO2 to IO4");
7+
8+
var gpio = require("gpio");
9+
var pins = require("arduino101_pins");
10+
11+
var total = 0;
12+
var passed = 0;
13+
14+
function assert(actual, description) {
15+
total += 1;
16+
17+
var label = "\033[1m\033[31mFAIL\033[0m";
18+
if (actual === true) {
19+
passed += 1;
20+
label = "\033[1m\033[32mPASS\033[0m";
21+
}
22+
23+
console.log(label + " - " + description);
24+
}
25+
26+
function expectThrow(description, func) {
27+
var threw = false;
28+
try {
29+
func();
30+
}
31+
catch (err) {
32+
threw = true;
33+
}
34+
assert(threw, description);
35+
}
36+
37+
var pinA, pinB, aValue, bValue;
38+
39+
// test GPIOPin onchange
40+
var changes = [
41+
["falling", 1, true],
42+
["rising", 1, false],
43+
["any", 1, false]
44+
];
45+
var mark = 0;
46+
47+
var edgeInterval = setInterval(function () {
48+
var count = 0;
49+
pinA = gpio.open({ pin: pins.IO2 });
50+
pinA.write(changes[mark][2]);
51+
pinB = gpio.open({
52+
pin: pins.IO4,
53+
direction: "in",
54+
edge: changes[mark][0]
55+
});
56+
57+
pinB.onchange = function () {
58+
count++;
59+
pinB.close();
60+
};
61+
62+
pinA.write(!changes[mark][2]);
63+
64+
setTimeout(function () {
65+
assert(count == changes[mark][1],
66+
"gpiopin: onchange with edge '" + changes[mark][0] + "'");
67+
68+
if (changes[mark][0] == "any") {
69+
// test GPIOPin close
70+
pinA.write(!changes[mark][2]);
71+
assert(count == changes[mark][1], "gpiopin: close onchange");
72+
}
73+
74+
mark = mark + 1;
75+
76+
if (mark == 3) {
77+
console.log("TOTAL: " + passed + " of " + total + " passed");
78+
clearInterval(edgeInterval);
79+
}
80+
}, 1000);
81+
}, 2000);
82+
83+
// test GPIO open
84+
pinA = gpio.open({ pin: pins.IO2 });
85+
pinB = gpio.open({ pin: pins.IO4, direction: "in" });
86+
87+
assert(pinA != null && typeof pinA == "object",
88+
"open: defined pin and default as 'out' direction");
89+
90+
assert(pinB != null && typeof pinB == "object",
91+
"open: defined pin with direction 'in'");
92+
93+
expectThrow("open: invalid pin", function () {
94+
gpio.open({ pin: 1024 });
95+
});
96+
97+
// test GPIOPin read and write
98+
pinA.write(true);
99+
bValue = pinB.read();
100+
assert(bValue, "gpiopin: write and read");
101+
102+
aValue = pinA.read();
103+
assert(aValue, "gpiopin: read output pin");
104+
105+
pinB.write(false);
106+
bValue = pinB.read();
107+
assert(bValue, "gpiopin: write input pin");
108+
109+
expectThrow("gpiopin: write invalid argument", function () {
110+
pinA.write(1);
111+
});
112+
113+
// test activeLow
114+
pinB = gpio.open({ pin: pins.IO4, activeLow:true, direction: "in" });
115+
pinA.write(false);
116+
bValue = pinB.read();
117+
assert(bValue, "activeLow: true");
118+
119+
// test GPIO openAsync
120+
gpio.openAsync({ pin: pins.IO2 }).then(function (pin2) {
121+
assert(pin2 != null && typeof pin2 == "object",
122+
"openAsync: defined pin and default as 'out' direction");
123+
gpio.openAsync({ pin: pins.IO4, direction: "in", edge: "any" })
124+
.then(function (pin4) {
125+
pin4.onchange = function (event) {
126+
assert(true, "gpiopin: onchange in openAsync");
127+
pin4.close();
128+
};
129+
pin2.write(true);
130+
var pin4v = pin4.read();
131+
assert(pin4v, "gpiopin: read and write in openAsync");
132+
});
133+
pin2.write(false);
134+
});

tests/test-k64f-pins.js

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
// Copyright (c) 2016, Intel Corporation.
2+
3+
console.log("Test K64f_pins APIs");
4+
5+
var pins = require('k64f_pins');
6+
var gpio = require('gpio');
7+
8+
var total = 0;
9+
var passed = 0;
10+
11+
function assert(actual, description) {
12+
total += 1;
13+
14+
var label = "\033[1m\033[31mFAIL\033[0m";
15+
if (actual === true) {
16+
passed += 1;
17+
label = "\033[1m\033[32mPASS\033[0m";
18+
}
19+
20+
console.log(label + " - " + description);
21+
}
22+
23+
function expectThrow(description, func) {
24+
var threw = false;
25+
try {
26+
func();
27+
}
28+
catch (err) {
29+
threw = true;
30+
}
31+
assert(threw, description);
32+
}
33+
34+
// Check pins defined and typeof Number
35+
function checkDefined(name) {
36+
assert(name in pins && typeof pins[name] == "number",
37+
"K64f_pins: " + name + " defined");
38+
}
39+
40+
// GPIO Pins
41+
var GPIOPins = ["D0", "D1", "D2", "D3", "D4", "D5",
42+
"D6", "D7", "D8", "D9", "D10", "D11",
43+
"D12", "D13", "D14", "D15"];
44+
45+
for (var i = 0; i < GPIOPins.length; i++) {
46+
var pinName = GPIOPins[i];
47+
48+
checkDefined(pinName);
49+
50+
// D3, D5, D8 are defined but unusable as GPIOs currently
51+
if (pinName == "D3" || pinName == "D5" || pinName == "D8") continue;
52+
53+
// GPIOPins as input pins
54+
var pin = gpio.open({ pin: pins[pinName], direction: "in" });
55+
var pinValue = pin.read();
56+
assert(typeof pinValue == "boolean", "K64f_pins: " + pinName + " input");
57+
58+
// GPIOPins as output pins
59+
pin = gpio.open({ pin: pins[pinName], direction: "out" });
60+
pinValue = pin.read();
61+
pin.write(!pinValue);
62+
63+
// D14, D15 can be used as GPIO inputs but not outputs currently
64+
if (pinName == "D14" || pinName == "D15") {
65+
assert(pin.read() == pinValue, "K64f_pins: " + pinName + " not output");
66+
} else {
67+
assert(pin.read() != pinValue, "K64f_pins: " + pinName + " output");
68+
}
69+
}
70+
71+
// LEDs
72+
var LEDs = [
73+
["LEDR", false],
74+
["LEDG", true],
75+
["LEDB", false]
76+
];
77+
78+
for (var i = 0; i < LEDs.length; i++) {
79+
var pinName = LEDs[i][0];
80+
81+
checkDefined(pinName);
82+
83+
// activeLow
84+
var lowFlag = LEDs[i][1];
85+
var pin = gpio.open({ pin: pins[pinName], activeLow: lowFlag });
86+
87+
var pinValue = pin.read();
88+
assert(pinValue == lowFlag, "K64f_pins: " + pinName + " active high");
89+
90+
pin.write(!pinValue);
91+
assert(pin.read() != pinValue, "K64f_pins: " + pinName + " output");
92+
}
93+
94+
// Switches
95+
checkDefined("SW2");
96+
checkDefined("SW3");
97+
98+
var SW2 = gpio.open({ pin: pins.SW2, direction: "in" });
99+
100+
var SWValue = SW2.read();
101+
assert(typeof SWValue == "boolean", "K64f_pins: SW2 input");
102+
103+
SW2.write(!SWValue);
104+
assert(SW2.read() == SWValue, "K64f_pins: SW2 not output");
105+
106+
// PWM Pins
107+
var PWMPins = ["PWM0", "PWM1", "PWM2", "PWM3",
108+
"PWM4", "PWM5", "PWM6", "PWM7",
109+
"PWM8", "PWM9"];
110+
111+
for (var i = 0; i < PWMPins.length; i++) {
112+
var pinName = PWMPins[i];
113+
114+
checkDefined(pinName);
115+
}
116+
117+
// AIO Pins
118+
var AIOPins = ["A0", "A1", "A2", "A3", "A4", "A5"];
119+
120+
for (var i = 0; i < AIOPins.length; i++) {
121+
var pinName = AIOPins[i];
122+
123+
checkDefined(pinName);
124+
}
125+
126+
console.log("TOTAL: " + passed + " of " + total + " passed");

0 commit comments

Comments
 (0)