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

Commit

Permalink
[tests] Sync tests from master
Browse files Browse the repository at this point in the history
These tests should be tested in zjs-0.1

Signed-off-by: Li, Hao <[email protected]>
  • Loading branch information
haoxli authored and grgustaf committed Nov 22, 2016
1 parent 046206f commit dcb06fb
Show file tree
Hide file tree
Showing 4 changed files with 528 additions and 0 deletions.
125 changes: 125 additions & 0 deletions tests/test-a101-pins.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
// Copyright (c) 2016, Intel Corporation.

// Test Arduino101Pins API

var pins = require('arduino101_pins');
var gpio = require('gpio');
var pwm = require('pwm');
var aio = require('aio');

var total = 0;
var passed = 0;

function assert(actual, description) {
total += 1;

var label = "\033[1m\033[31mFAIL\033[0m";
if (actual === true) {
passed += 1;
label = "\033[1m\033[32mPASS\033[0m";
}

console.log(label + " - " + description);
}

function expectThrow(description, func) {
var threw = false;
try {
func();
}
catch (err) {
threw = true;
}
assert(threw, description);
}

// Check pins defined and typeof Number
function checkDefined(name) {
assert(name in pins && typeof pins[name] == "number",
"Arduino101Pins: " + name + " defined");
}

// GPIO Pins
var GPIOPins = ["IO2", "IO3", "IO4", "IO5",
"IO6", "IO7", "IO8", "IO9",
"IO10", "IO11", "IO12", "IO13"];
for(var i = 0; i < GPIOPins.length; i++) {
var pinName = GPIOPins[i];

checkDefined(pinName);

// IO6 and IO9 are defined but unusable as GPIOs currently
if (pinName == "IO6" || pinName == "IO9") continue;

var pin = gpio.open({ pin: pins[pinName] });
var pinValue = pin.read();
assert(typeof pinValue == "boolean",
"Arduino101Pins: " + pinName + " input");

pin.write(!pinValue);
if (pinName == "IO3" || pinName == "IO5") {
// IO3 and IO5 can be used as GPIO inputs but not outputs currently
assert(pin.read() == pinValue,
"Arduino101Pins: " + pinName + " output");
} else {
assert(pin.read() != pinValue,
"Arduino101Pins: " + pinName + " output");
}
}

// LEDs
var LEDs = [["LED0", false],
["LED1", true ],
["LED2", true ]];
for(var i = 0; i < LEDs.length; i++) {
var pinName = LEDs[i][0];

checkDefined(pinName);

// activeLow
var lowFlag = LEDs[i][1];
var pin = gpio.open({ pin: pins[pinName], activeLow: lowFlag });
var pinValue = pin.read();
var lowStr = lowFlag ? "high" : "low";
assert(pinValue,
"Arduino101Pins: " + pinName + " active " + lowStr);

pin.write(!pinValue)
assert(pin.read() != pinValue,
"Arduino101Pins: " + pinName + " output");

if (pinName == "LED0") {
pinValue = pin.read();
var io13 = gpio.open({ pin: pins.IO13, activeLow: lowFlag });
io13.write(!pinValue);
assert(pin.read() != pinValue,
"Arduino101Pins: " + pinName + " displays current state of IO13");
}
}

// PWM Pins
var PWMPins = ["PWM0", "PWM1", "PWM2", "PWM3"];
for(var i = 0; i < PWMPins.length; i++) {
var pinName = PWMPins[i];

checkDefined(pinName);

var pin = pwm.open({channel: pins[pinName]});
assert(pin != null && typeof pin == "object",
"Arduino101Pins: " + pinName + " open");
}

// AIO Pins
var AIOPins = ["A0", "A1", "A2", "A3", "A4", "A5"];
for(var i = 0; i < AIOPins.length; i++) {
var pinName = AIOPins[i];

checkDefined(pinName);

var pin = aio.open({ device: 0, pin: pins[pinName] });
var pinValue = pin.read();
assert(pinValue >= 0 && pinValue <= 4095,
"Arduino101Pins: " + pinName + " digital value");
}

console.log("TOTAL: " + passed + " of " + total + " passed");
134 changes: 134 additions & 0 deletions tests/test-gpio.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
// Copyright (c) 2016, Intel Corporation.

// Testing GPIO APIs

// Pre-conditions
console.log("Wire IO2 to IO4");

var gpio = require("gpio");
var pins = require("arduino101_pins");

var total = 0;
var passed = 0;

function assert(actual, description) {
total += 1;

var label = "\033[1m\033[31mFAIL\033[0m";
if (actual === true) {
passed += 1;
label = "\033[1m\033[32mPASS\033[0m";
}

console.log(label + " - " + description);
}

function expectThrow(description, func) {
var threw = false;
try {
func();
}
catch (err) {
threw = true;
}
assert(threw, description);
}

var pinA, pinB, aValue, bValue;

// test GPIOPin onchange
var changes = [
["falling", 1, true],
["rising", 1, false],
["any", 1, false]
];
var mark = 0;

var edgeInterval = setInterval(function () {
var count = 0;
pinA = gpio.open({ pin: pins.IO2 });
pinA.write(changes[mark][2]);
pinB = gpio.open({
pin: pins.IO4,
direction: "in",
edge: changes[mark][0]
});

pinB.onchange = function () {
count++;
pinB.close();
};

pinA.write(!changes[mark][2]);

setTimeout(function () {
assert(count == changes[mark][1],
"gpiopin: onchange with edge '" + changes[mark][0] + "'");

if (changes[mark][0] == "any") {
// test GPIOPin close
pinA.write(!changes[mark][2]);
assert(count == changes[mark][1], "gpiopin: close onchange");
}

mark = mark + 1;

if (mark == 3) {
console.log("TOTAL: " + passed + " of " + total + " passed");
clearInterval(edgeInterval);
}
}, 1000);
}, 2000);

// test GPIO open
pinA = gpio.open({ pin: pins.IO2 });
pinB = gpio.open({ pin: pins.IO4, direction: "in" });

assert(pinA != null && typeof pinA == "object",
"open: defined pin and default as 'out' direction");

assert(pinB != null && typeof pinB == "object",
"open: defined pin with direction 'in'");

expectThrow("open: invalid pin", function () {
gpio.open({ pin: 1024 });
});

// test GPIOPin read and write
pinA.write(true);
bValue = pinB.read();
assert(bValue, "gpiopin: write and read");

aValue = pinA.read();
assert(aValue, "gpiopin: read output pin");

pinB.write(false);
bValue = pinB.read();
assert(bValue, "gpiopin: write input pin");

expectThrow("gpiopin: write invalid argument", function () {
pinA.write(1);
});

// test activeLow
pinB = gpio.open({ pin: pins.IO4, activeLow:true, direction: "in" });
pinA.write(false);
bValue = pinB.read();
assert(bValue, "activeLow: true");

// test GPIO openAsync
gpio.openAsync({ pin: pins.IO2 }).then(function (pin2) {
assert(pin2 != null && typeof pin2 == "object",
"openAsync: defined pin and default as 'out' direction");
gpio.openAsync({ pin: pins.IO4, direction: "in", edge: "any" })
.then(function (pin4) {
pin4.onchange = function (event) {
assert(true, "gpiopin: onchange in openAsync");
pin4.close();
};
pin2.write(true);
var pin4v = pin4.read();
assert(pin4v, "gpiopin: read and write in openAsync");
});
pin2.write(false);
});
126 changes: 126 additions & 0 deletions tests/test-k64f-pins.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
// Copyright (c) 2016, Intel Corporation.

console.log("Test K64f_pins APIs");

var pins = require('k64f_pins');
var gpio = require('gpio');

var total = 0;
var passed = 0;

function assert(actual, description) {
total += 1;

var label = "\033[1m\033[31mFAIL\033[0m";
if (actual === true) {
passed += 1;
label = "\033[1m\033[32mPASS\033[0m";
}

console.log(label + " - " + description);
}

function expectThrow(description, func) {
var threw = false;
try {
func();
}
catch (err) {
threw = true;
}
assert(threw, description);
}

// Check pins defined and typeof Number
function checkDefined(name) {
assert(name in pins && typeof pins[name] == "number",
"K64f_pins: " + name + " defined");
}

// GPIO Pins
var GPIOPins = ["D0", "D1", "D2", "D3", "D4", "D5",
"D6", "D7", "D8", "D9", "D10", "D11",
"D12", "D13", "D14", "D15"];

for (var i = 0; i < GPIOPins.length; i++) {
var pinName = GPIOPins[i];

checkDefined(pinName);

// D3, D5, D8 are defined but unusable as GPIOs currently
if (pinName == "D3" || pinName == "D5" || pinName == "D8") continue;

// GPIOPins as input pins
var pin = gpio.open({ pin: pins[pinName], direction: "in" });
var pinValue = pin.read();
assert(typeof pinValue == "boolean", "K64f_pins: " + pinName + " input");

// GPIOPins as output pins
pin = gpio.open({ pin: pins[pinName], direction: "out" });
pinValue = pin.read();
pin.write(!pinValue);

// D14, D15 can be used as GPIO inputs but not outputs currently
if (pinName == "D14" || pinName == "D15") {
assert(pin.read() == pinValue, "K64f_pins: " + pinName + " not output");
} else {
assert(pin.read() != pinValue, "K64f_pins: " + pinName + " output");
}
}

// LEDs
var LEDs = [
["LEDR", false],
["LEDG", true],
["LEDB", false]
];

for (var i = 0; i < LEDs.length; i++) {
var pinName = LEDs[i][0];

checkDefined(pinName);

// activeLow
var lowFlag = LEDs[i][1];
var pin = gpio.open({ pin: pins[pinName], activeLow: lowFlag });

var pinValue = pin.read();
assert(pinValue == lowFlag, "K64f_pins: " + pinName + " active high");

pin.write(!pinValue);
assert(pin.read() != pinValue, "K64f_pins: " + pinName + " output");
}

// Switches
checkDefined("SW2");
checkDefined("SW3");

var SW2 = gpio.open({ pin: pins.SW2, direction: "in" });

var SWValue = SW2.read();
assert(typeof SWValue == "boolean", "K64f_pins: SW2 input");

SW2.write(!SWValue);
assert(SW2.read() == SWValue, "K64f_pins: SW2 not output");

// PWM Pins
var PWMPins = ["PWM0", "PWM1", "PWM2", "PWM3",
"PWM4", "PWM5", "PWM6", "PWM7",
"PWM8", "PWM9"];

for (var i = 0; i < PWMPins.length; i++) {
var pinName = PWMPins[i];

checkDefined(pinName);
}

// AIO Pins
var AIOPins = ["A0", "A1", "A2", "A3", "A4", "A5"];

for (var i = 0; i < AIOPins.length; i++) {
var pinName = AIOPins[i];

checkDefined(pinName);
}

console.log("TOTAL: " + passed + " of " + total + " passed");
Loading

0 comments on commit dcb06fb

Please sign in to comment.