diff options
author | Li, Hao <hao.x.li@intel.com> | 2016-11-18 16:19:52 +0800 |
---|---|---|
committer | Geoff Gustafson <geoff@linux.intel.com> | 2016-11-22 11:59:57 -0800 |
commit | dcb06fbda93cae5a6f59ff1acd7014ad9c21faa8 (patch) | |
tree | 69ec96525f597db3a6b4860e22a9d968898e895f | |
parent | 046206f4d87634a57b04bbdfa8e51455b86b7b1e (diff) |
[tests] Sync tests from master
These tests should be tested in zjs-0.1
Signed-off-by: Li, Hao <hao.x.li@intel.com>
-rw-r--r-- | tests/test-a101-pins.js | 125 | ||||
-rw-r--r-- | tests/test-gpio.js | 134 | ||||
-rw-r--r-- | tests/test-k64f-pins.js | 126 | ||||
-rw-r--r-- | tests/test-pwm.js | 143 |
4 files changed, 528 insertions, 0 deletions
diff --git a/tests/test-a101-pins.js b/tests/test-a101-pins.js new file mode 100644 index 0000000..969f264 --- /dev/null +++ b/tests/test-a101-pins.js @@ -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"); diff --git a/tests/test-gpio.js b/tests/test-gpio.js new file mode 100644 index 0000000..8d67c19 --- /dev/null +++ b/tests/test-gpio.js @@ -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); +}); diff --git a/tests/test-k64f-pins.js b/tests/test-k64f-pins.js new file mode 100644 index 0000000..2bbf708 --- /dev/null +++ b/tests/test-k64f-pins.js @@ -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"); diff --git a/tests/test-pwm.js b/tests/test-pwm.js new file mode 100644 index 0000000..273f015 --- /dev/null +++ b/tests/test-pwm.js @@ -0,0 +1,143 @@ +// Copyright (c) 2016, Intel Corporation. + +console.log("Wire IO3 to IO2"); + +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 pwm = require("pwm"); +var gpio = require("gpio"); +var pins = require("arduino101_pins"); +var pinA, pinB, msTimer, cycleTimer; + +pinB = gpio.open({pin: pins.IO2, direction: 'in'}); + +// PWMPins open +pinA = pwm.open({channel: pins.IO3}); +assert(pinA != null && typeof pinA == "object", + "open: defined pin and default argument"); + +expectThrow("open: undefined pin", function () { + pinA = pwm.open({channel: 1024}); +}); + +// set Period and PulseWidth with ms +// duty cycle: 30% +var msTrue = 0; +var msFalse = 0; +var msCount = 0; + +expectThrow("pwmpin: set pulseWidth without period", function () { + pinA.setPulseWidth(300); +}); + +pinA = pwm.open({channel: pins.IO3, period: 3, pulseWidth: 1}); +assert(pinA != null && typeof pinA == "object", + "open: with period and pulseWidth"); + +pinA.setPeriod(1000); + +expectThrow("pwmpin: set pulseWidth greater than period", function () { + pinA.setPulseWidth(3000); +}); + +pinA.setPulseWidth(300); + +msTimer = setInterval(function () { + if (pinB.read()) { + msTrue = msTrue + 1; + } else { + msFalse = msFalse + 1; + } + + msCount = msCount + 1; +}, 50); + +setTimeout(function() { + assert(msTrue == 6 && msFalse == 14 && msCount == 20, + "pwmpin: set period and pulseWidth"); + clearInterval(msTimer); + + expectThrow("pwmpin: set period with invalid value", function () { + pinA.setPeriod("Value"); + }); + + expectThrow("pwmpin: set pulseWidth with invalid value", function () { + pinA.setPulseWidth("Value"); + }); + + // set Period and PulseWidth with cycle + // duty cycle: 70% + var cyclesTrue = 0; + var cyclesFlase = 0; + var cyclesCount = 0; + var periodCount = 0; + var Flag = false; + var oldFlag = false; + pinA = pwm.open({channel: pins.IO3, polarity: "reverse"}); + assert(pinA != null && typeof pinA == "object", "open: reverse polarity"); + + pinA.setPeriodCycles(10000000); + pinA.setPulseWidthCycles(3000000); + + cycleTimer = setInterval(function () { + Flag = pinB.read(); + + if(Flag == oldFlag){ + cyclesCount = cyclesCount + 1; + } else { + if (oldFlag) { + cyclesTrue = cyclesTrue + cyclesCount; + } else { + cyclesFlase = cyclesFlase + cyclesCount; + } + + oldFlag = Flag; + cyclesCount = 0; + + if (Flag == false) { + periodCount = periodCount + 1; + } + + if (periodCount == 3) { + assert((25 < cyclesFlase) && (cyclesFlase < 29) && + (60 < cyclesTrue) && (cyclesTrue < 64), + "pwmpin: set periodCycles and pulseWidthCycles"); + + console.log("TOTAL: " + passed + " of " + total + " passed"); + clearInterval(cycleTimer); + } + } + }, 10); +}, 1000); + +expectThrow("pwmpin: set periodCycles with invalid value", function () { + pinA.setPeriodCycles("Value"); +}); + +expectThrow("pwmpin: set pulseWidthCycles with invalid value", function () { + pinA.setPulseWidthCycles("Value"); +}); |