Skip to content

Commit 87e943a

Browse files
committed
Add pinhistory tests, fix pinhistory, and make rspec better with C++ unit tests
1 parent da9b857 commit 87e943a

File tree

5 files changed

+123
-32
lines changed

5 files changed

+123
-32
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#include <ArduinoUnitTests.h>
2+
#include <Arduino.h>
3+
4+
5+
unittest(pin_read_history)
6+
{
7+
PinHistory<int> phi;
8+
9+
const int future[6] = {33, 22, 55, 11, 44, 66};
10+
assertEqual(0, phi.queueSize());
11+
phi.fromArray(future, 6);
12+
for (int i = 0; i < 6; ++i)
13+
{
14+
assertEqual(6 - i, phi.queueSize());
15+
assertEqual(future[i], phi.retrieve());
16+
assertEqual(6 - (i + 1), phi.queueSize());
17+
}
18+
19+
// assert end of history works
20+
assertEqual(future[5], phi.retrieve());
21+
22+
PinHistory<bool> phb;
23+
assertEqual(0, phb.queueSize());
24+
phb.fromAscii("Yo", true);
25+
assertEqual(16, phb.queueSize());
26+
// digitial history as serial data, big-endian
27+
bool binaryAscii[16] = {
28+
0, 1, 0, 1, 1, 0, 0, 1,
29+
0, 1, 1, 0, 1, 1, 1, 1
30+
};
31+
32+
for (int i = 0; i < 16; ++i) {
33+
assertEqual(binaryAscii[i], phb.retrieve());
34+
}
35+
}
36+
37+
38+
unittest_main()
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
#include <ArduinoUnitTests.h>
2+
#include <Queue.h>
3+
4+
unittest(basic_queue_dequeue_and_size)
5+
{
6+
Queue<int> q;
7+
int data[5] = {11, 22, 33, 44, 55};
8+
9+
assertTrue(q.empty());
10+
11+
for (int i = 0; i < 5; ++i) {
12+
assertEqual(i, q.size());
13+
q.push(data[i]);
14+
assertEqual(data[i], q.back());
15+
assertEqual(i + 1, q.size());
16+
}
17+
18+
for (int i = 0; i < 5; ++i) {
19+
assertEqual(5 - i, q.size());
20+
assertEqual(data[i], q.front());
21+
q.pop();
22+
assertEqual(5 - i - 1, q.size());
23+
}
24+
25+
assertTrue(q.empty());
26+
}
27+
28+
unittest(copy_constructor)
29+
{
30+
Queue<int> q;
31+
int data[5] = {11, 22, 33, 44, 55};
32+
for (int i = 0; i < 5; ++i) q.push(data[i]);
33+
34+
Queue<int> q2(q);
35+
36+
for (int i = 0; i < 5; ++i) {
37+
assertEqual(5 - i, q2.size());
38+
assertEqual(data[i], q2.front());
39+
q2.pop();
40+
assertEqual(5 - i - 1, q2.size());
41+
}
42+
43+
for (int i = 0; i < 5; ++i) {
44+
assertEqual(5 - i, q.size());
45+
assertEqual(data[i], q.front());
46+
q.pop();
47+
assertEqual(5 - i - 1, q.size());
48+
}
49+
}
50+
51+
unittest_main()

cpp/arduino/PinHistory.h

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
11
#pragma once
2-
#include "queue.h"
2+
#include "Queue.h"
33
#include "WString.h"
44

55
// pins with history.
66
template <typename T>
77
class PinHistory {
88
private:
9-
queue<T> qIn;
10-
queue<T> qOut;
11-
12-
unsigned long mLastRead = 0;
9+
Queue<T> qIn;
10+
Queue<T> qOut;
1311

1412
void clear() {
1513
qOut.clear();
@@ -24,36 +22,39 @@ class PinHistory {
2422
qOut.push(val);
2523
}
2624

27-
unsigned int historySize() { return qOut.size(); }
25+
unsigned int historySize() const { return qOut.size(); }
26+
27+
unsigned int queueSize() const { return qIn.size(); }
2828

2929
// This returns the "value" of the pin in a raw sense
3030
operator T() const {
31-
if (mLastRead == 0 && qIn.size()) return qIn.front();
31+
if (!qIn.empty()) return qIn.front();
3232
return qOut.back();
3333
}
3434

3535
// this sets the value of the pin authoritatively
3636
// so if there was a queue, dump it.
3737
// the actual "set" operation doesn't happen until the next read
3838
const T &operator=(const T& i) {
39+
qIn.clear();
3940
qOut.push(i);
4041
return qOut.back();
4142
}
4243

4344
// This returns the "value" of the pin according to the queued values
44-
// if now is the same as the last read time, return the same val and exit
4545
// if there is input, advance it to the output.
4646
// then take the latest output.
4747
T retrieve() {
48-
if (qIn.size()) {
49-
qOut.push(qIn.front());
48+
if (!qIn.empty()) {
49+
T hack_required_by_travis_ci = qIn.front();
5050
qIn.pop();
51+
qOut.push(hack_required_by_travis_ci);
5152
}
5253
return qOut.back();
5354
}
5455

5556
// enqueue a set of elements
56-
void fromArray(T* arr, unsigned int length) {
57+
void fromArray(T const * const arr, unsigned int length) {
5758
for (int i = 0; i < length; ++i) qIn.push(arr[i]);
5859
}
5960

@@ -73,8 +74,8 @@ class PinHistory {
7374

7475
// copy elements to an array, up to a given length
7576
// return the number of elements moved
76-
int toArray(T* arr, unsigned int length) {
77-
queue<T> q2(qOut);
77+
int toArray (T* arr, unsigned int length) const {
78+
Queue<T> q2(qOut);
7879

7980
int ret = 0;
8081
for (int i = 0; i < length && q2.size(); ++i) {
@@ -86,9 +87,9 @@ class PinHistory {
8687
}
8788

8889
// see if the array matches the elements in the queue
89-
bool hasElements(T* arr, unsigned int length) {
90+
bool hasElements (T const * const arr, unsigned int length) const {
9091
int i;
91-
queue<T> q2(qOut);
92+
Queue<T> q2(qOut);
9293
for (i = 0; i < length && q2.size(); ++i) {
9394
if (q2.front() != arr[i]) return false;
9495
q2.pop();
@@ -98,10 +99,10 @@ class PinHistory {
9899

99100
// convert the pin history to a string as if it was Serial comms
100101
// start from offset, consider endianness
101-
String toAscii(unsigned int offset, bool bigEndian) {
102+
String toAscii (unsigned int offset, bool bigEndian) const {
102103
String ret = "";
103104

104-
queue<T> q2(qOut);
105+
Queue<T> q2(qOut);
105106

106107
while (offset) {
107108
q2.pop();

cpp/arduino/queue.h renamed to cpp/arduino/Queue.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#pragma once
22

33
template <typename T>
4-
class queue {
4+
class Queue {
55
private:
66
struct Node {
77
T data;
@@ -19,9 +19,9 @@ class queue {
1919
}
2020

2121
public:
22-
queue() { init(); }
22+
Queue(): mNil() { init(); }
2323

24-
queue(const queue<T>& q) {
24+
Queue(const Queue<T>& q) {
2525
init();
2626
for (Node* n = q.mFront; n; n = n->next) push(n->data);
2727
}
@@ -69,5 +69,5 @@ class queue {
6969

7070
void clear() { while (!empty()) pop(); }
7171

72-
~queue() { clear(); }
72+
~Queue() { clear(); }
7373
};

spec/testsomething_unittests_spec.rb

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
sampleproj_path = File.join(File.dirname(File.dirname(__FILE__)), "SampleProjects")
44

5-
RSpec.describe "TestSomething C++ unit tests" do
5+
RSpec.describe "TestSomething C++" do
66
cpp_lib_path = File.join(sampleproj_path, "TestSomething")
77
cpp_library = ArduinoCI::CppLibrary.new(cpp_lib_path)
88
context "cpp_files" do
@@ -14,7 +14,7 @@
1414
end
1515
config = ArduinoCI::CIConfig.default.from_example(cpp_lib_path)
1616

17-
context "test" do
17+
context "unit tests" do
1818

1919
it "is going to test more than one library" do
2020
test_files = cpp_library.test_files
@@ -29,7 +29,11 @@
2929
test_files = config.allowable_unittest_files(cpp_library.test_files)
3030
test_files.each do |path|
3131
tfn = File.basename(path)
32-
context "unit test #{tfn}" do
32+
context "file #{tfn}" do
33+
34+
before(:all) do
35+
@exe = cpp_library.build_for_test_with_configuration(path, [], config.gcc_config("uno"))
36+
end
3337

3438
# extra debug for c++ failures
3539
after(:each) do |example|
@@ -42,15 +46,12 @@
4246
end
4347
end
4448

45-
exe = nil
46-
it "builds #{tfn} successfully" do
47-
exe = cpp_library.build_for_test_with_configuration(path, [], config.gcc_config("uno"))
48-
expect(exe).not_to be nil
49+
it "#{tfn} builds successfully" do
50+
expect(@exe).not_to be nil
4951
end
50-
unless exe.nil?
51-
it "tests #{tfn} successfully" do
52-
expect(cpp_library.run_test_file(exe)).to_not be_falsey
53-
end
52+
it "#{tfn} passes tests" do
53+
skip "Can't run the test program because it failed to build" if @exe.nil?
54+
expect(cpp_library.run_test_file(@exe)).to_not be_falsey
5455
end
5556
end
5657
end

0 commit comments

Comments
 (0)