Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Basic unit testing of OneWire using TravisCI + arduino_ci #67

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .arduino-ci.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
compile:
platforms:
- due

unittest:
platforms:
- due
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# arduino_ci unit test files
*.bin
*.bin.dSYM

# arduino_ci ruby bundle lockfile
Gemfile.lock
6 changes: 6 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
sudo: false
language: ruby

script:
- bundle install
- bundle exec arduino_ci_remote.rb
3 changes: 3 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
source 'https://rubygems.org'

gem 'arduino_ci', '~> 0.2'
46 changes: 46 additions & 0 deletions test/test_crc.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#include <ArduinoUnitTests.h>
#include "../OneWire.h"

unittest(crc8)
{
byte seq1[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
byte seq2[8] = { 0, 1, 2, 3, 4, 5, 6, 7 };
byte seq3[8] = { 0, 10, 20, 30, 40, 50, 60, 70 };

assertEqual(0, (int)OneWire::crc8(seq1, 7));

assertEqual(216, (int)OneWire::crc8(seq2, 4));
assertEqual(244, (int)OneWire::crc8(seq2, 5));
assertEqual( 42, (int)OneWire::crc8(seq2, 6));
assertEqual(128, (int)OneWire::crc8(seq2, 7));

assertEqual( 63, (int)OneWire::crc8(seq3, 4));
assertEqual( 30, (int)OneWire::crc8(seq3, 5));
assertEqual(128, (int)OneWire::crc8(seq3, 6));
assertEqual(145, (int)OneWire::crc8(seq3, 7));
}

unittest(crc16)
{
byte seq1[16] = {
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0
};

byte seq2[16] = {
0xF0, 0xF1, 0xF2, 0xF3,
0xF4, 0xF5, 0xF6, 0xF7,
0xF8, 0xF9, 0xFA, 0xFB,
0xFC, 0xFD, 0xFE, 0xFF
};

assertEqual(0, (int)OneWire::crc16(seq1, 11));

assertEqual(55581, (int)OneWire::crc16(seq2, 11));
assertEqual(35416, (int)OneWire::crc16(seq2, 12));
assertEqual(48011, (int)OneWire::crc16(seq2, 13));
assertEqual(58938, (int)OneWire::crc16(seq2, 14));
}


unittest_main()
145 changes: 145 additions & 0 deletions test/test_onewire.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
#include <ArduinoUnitTests.h>
#include "../OneWire.h"

#define PIN 10

int delayToBit(unsigned long* timestamps, int lowIndex) {
int duration = timestamps[lowIndex + 1] - timestamps[lowIndex];
if (duration <= 15) return 1;
if (duration >= 60) return 0;
return -1; // indicating error
}

// use GODMODE to test pins
GodmodeState* state = GODMODE();

unittest_setup()
{
// reset pin state before each test
state->reset();
}

unittest(no_pin_noise_on_init)
{
int initalHistorySize = state->digitalPin[PIN].historySize();
OneWire ow(PIN);
assertEqual(initalHistorySize, state->digitalPin[PIN].historySize());
}

unittest(failed_search)
{
OneWire ow(PIN);
byte addr[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
assertFalse(ow.search(addr)); // this had better fail; we mocked nothing
assertEqual(0, (int)OneWire::crc8(addr, 7)); // should take no data
}

unittest(write_a_0)
{
bool actual[5]; // to store actual values written to pin
unsigned long timing[5]; // to store event timestamps
int numEntries;

// write a zero and verify
OneWire ow(PIN);
ow.write_bit(0);
assertEqual(3, state->digitalPin[PIN].historySize());
numEntries = state->digitalPin[PIN].toArray(actual, 5);
assertEqual(3, numEntries); // initial state plus low-high
assertEqual(LOW, actual[1]);
assertEqual(HIGH, actual[2]);

// assert that the timing indicates a 0
state->digitalPin[PIN].toTimestampArray(timing, 5);
assertEqual(0, delayToBit(timing, 1));


// same thing but for writing a 1
state->reset();
ow.write_bit(1);
assertEqual(3, state->digitalPin[PIN].historySize());
state->digitalPin[PIN].toArray(actual, 5);
assertEqual(LOW, actual[1]);
assertEqual(HIGH, actual[2]);

// assert that the timing indicates a 1
state->digitalPin[PIN].toTimestampArray(timing, 5);
assertEqual(1, delayToBit(timing, 1));
}

unittest(write_a_byte_no_power)
{
OneWire ow(PIN);

// without power
ow.write(0xAC, 0);
assertEqual(18, state->digitalPin[PIN].historySize());
bool expected[18] = {
LOW,
LOW, HIGH,
LOW, HIGH,
LOW, HIGH,
LOW, HIGH,
LOW, HIGH,
LOW, HIGH,
LOW, HIGH,
LOW, HIGH, // 8 of these
LOW
};
bool actual[18];
unsigned long timing[18]; // to store event timestamps

// convert history queue into an array so we can verify it.
state->digitalPin[PIN].toArray(actual, 18);

// verify each element
for (int i = 0; i < 18; ++i) {
assertEqual(expected[i], actual[i]);
}

// verify transmitted data, apparently it goes across little-endian
// 0xAC -> 10101100 becomes 00110101
state->digitalPin[PIN].toTimestampArray(timing, 18);
assertEqual(0, delayToBit(timing, 1));
assertEqual(0, delayToBit(timing, 3));
assertEqual(1, delayToBit(timing, 5));
assertEqual(1, delayToBit(timing, 7));
assertEqual(0, delayToBit(timing, 9));
assertEqual(1, delayToBit(timing, 11));
assertEqual(0, delayToBit(timing, 13));
assertEqual(1, delayToBit(timing, 15));
}

// and now a blatant copy-paste
unittest(write_a_byte_with_power)
{
OneWire ow(PIN);

// without power
ow.write(0xAC, 1);
assertEqual(17, state->digitalPin[PIN].historySize());
bool expected[17] = {
LOW,
LOW, HIGH,
LOW, HIGH,
LOW, HIGH,
LOW, HIGH,
LOW, HIGH,
LOW, HIGH,
LOW, HIGH,
LOW, HIGH, // 8 of these. power stays on -- high
};
bool actual[17];

// convert history queue into an array so we can verify it.
int copied = state->digitalPin[PIN].toArray(actual, 17);
assertEqual(17, copied);

// verify each element
for (int i = 0; i < copied; ++i) {
assertEqual(expected[i], actual[i]);
}
}


unittest_main()