-
Notifications
You must be signed in to change notification settings - Fork 34
High-level wire implementation, complete with unit tests #189
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
Changes from 5 commits
bb61617
4989505
13862bc
226bc18
42aaa39
6ddd254
194af8b
d6c3bb8
2821146
01eeaf3
7593ab8
f2fd95a
bb25c28
e4fd2e4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
#include <ArduinoUnitTests.h> | ||
#include <Arduino.h> | ||
#include <Wire.h> | ||
using namespace std; | ||
|
||
unittest(begin_write_end) { | ||
deque<uint8_t>* mosi = Wire.getMosi(14); | ||
assertEqual(0, mosi->size()); | ||
Wire.begin(); | ||
Wire.beginTransmission(14); | ||
Wire.write(0x07); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There are a lot of random-looking numbers in this unit test, which might be better expressed as named variables. Otherwise, please add some comments that explain the different sections of the tests There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Rather than named variables, let's just add comments along the line of "try some random value (0x07) sent to some random slave (14)." There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. With the aid of your comments I can see what you're after. But I'm going to insist on using named constants. The reason for this is purely readability; as a maintainer I have to scan a lot of contributions in a short amount of time and solve problems in them. Consider the following chunk of this test as currently written: assertEqual(1, Wire.read());
assertEqual(2, Wire.available());
assertEqual(4, Wire.read());
assertEqual(1, Wire.available());
assertEqual(7, Wire.read()); This requires me (and any future contributors to this code) to puzzle out things like
This affects numeric tests more than strings or objects (which end up being a bit more self-descriptive). Here's an example of how I'd like this to be written: const uint8_t randomSlave = 14;
const uint8_t randomValues[] = { 0x07, 0x0E };
Wire.begin();
Wire.beginTransmission(randomSlave);
Wire.write(randomValues[0]);
Wire.write(randomValues[1]);
Wire.endTransmission();
// check master write buffer values
assertEqual(2, mosi->size());
assertEqual(randomValues[0], mosi->front());
mosi->pop_front();
assertEqual(randomValues[1], mosi->front());
mosi->pop_front();
assertEqual(0, mosi->size()); Similar for the values Wire.begin();
deque<uint8_t>* miso;
// place some values on random slaves' read buffers
const int aRandomSlave = 19;
const int anotherRandomSlave = 34;
const uint8_t moreRandomValues = { 1, 4, 7 };
miso = Wire.getMiso(aRandomSlave);
miso->push_back(randomValues[0]);
miso->push_back(randomValues[1]);
miso = Wire.getMiso(anotherRandomSlave);
miso->push_back(moreRandomValues[0]);
miso->push_back(moreRandomValues[1]);
miso->push_back(moreRandomValues[2]); I chose |
||
Wire.write(0x0E); | ||
Wire.endTransmission(); | ||
assertEqual(2, mosi->size()); | ||
assertEqual(0x07, mosi->front()); | ||
mosi->pop_front(); | ||
assertEqual(0x0E, mosi->front()); | ||
mosi->pop_front(); | ||
assertEqual(0, mosi->size()); | ||
} | ||
|
||
unittest(readTwo_writeOne) { | ||
Wire.begin(); | ||
deque<uint8_t>* miso; | ||
miso = Wire.getMiso(19); | ||
miso->push_back(0x07); | ||
miso->push_back(0x0E); | ||
miso = Wire.getMiso(34); | ||
miso->push_back(1); | ||
miso->push_back(4); | ||
miso->push_back(7); | ||
|
||
assertEqual(0, Wire.requestFrom(19, 3)); | ||
assertEqual(2, Wire.requestFrom(19, 2)); | ||
assertEqual(2, Wire.available()); | ||
assertEqual(0x07, Wire.read()); | ||
assertEqual(1, Wire.available()); | ||
assertEqual(0x0E, Wire.read()); | ||
assertEqual(0, Wire.available()); | ||
assertEqual(3, Wire.requestFrom(34, 3)); | ||
assertEqual(3, Wire.available()); | ||
assertEqual(1, Wire.read()); | ||
assertEqual(2, Wire.available()); | ||
assertEqual(4, Wire.read()); | ||
assertEqual(1, Wire.available()); | ||
assertEqual(7, Wire.read()); | ||
assertEqual(0, Wire.available()); | ||
|
||
Wire.beginTransmission(47); | ||
for (int i = 1; i < 4; i++) { | ||
Wire.write(i * 2); | ||
} | ||
Wire.endTransmission(); | ||
deque<uint8_t>* mosi = Wire.getMosi(47); | ||
|
||
assertEqual(3, mosi->size()); | ||
assertEqual(2, mosi->front()); | ||
mosi->pop_front(); | ||
assertEqual(2, mosi->size()); | ||
assertEqual(4, mosi->front()); | ||
mosi->pop_front(); | ||
assertEqual(1, mosi->size()); | ||
assertEqual(6, mosi->front()); | ||
mosi->pop_front(); | ||
assertEqual(0, mosi->size()); | ||
} | ||
|
||
unittest_main() | ||
ianfixes marked this conversation as resolved.
Show resolved
Hide resolved
|
Uh oh!
There was an error while loading. Please reload this page.