Skip to content

Commit 520d4e1

Browse files
committed
ArduinoCITable copy construction and clearing (bug chasing)
1 parent 055dde7 commit 520d4e1

File tree

3 files changed

+45
-2
lines changed

3 files changed

+45
-2
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
1111
- CPP library class now reaches into included Arduino libraries for source files
1212
- SPI mocks
1313
- `ensure_arduino_installation.rb` to allow custom libraries to be installed
14+
- Copy constructor for `ArduinoCITable`
1415

1516
### Changed
1617
- Refactored documentation
@@ -24,6 +25,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
2425

2526
### Fixed
2627
- OSX splash screen re-disabled
28+
- ArduinoCITable didn't initialize its size on `clear()`
2729

2830
### Security
2931

SampleProjects/TestSomething/test/table.cpp

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,7 @@ void setResult3(long l, int k, int v) {
1818
}
1919

2020

21-
unittest(basic_table)
22-
{
21+
unittest(basic_table) {
2322
ArduinoCITable<String, int> t;
2423
assertTrue(t.empty());
2524

@@ -46,6 +45,40 @@ unittest(basic_table)
4645

4746
}
4847

48+
unittest(clear) {
49+
ArduinoCITable<String, int> t1;
50+
int data[5] = {11, 22, 33, 44, 55};
51+
52+
for (int i = 0; i < 5; ++i) {
53+
t1.add(String(data[i]), data[i]);
54+
}
55+
56+
assertEqual(5, t1.size());
57+
t1.clear();
58+
assertEqual(0, t1.size());
59+
}
60+
61+
unittest(copy_construction) {
62+
ArduinoCITable<String, int> t1;
63+
int data[5] = {11, 22, 33, 44, 55};
64+
65+
for (int i = 0; i < 5; ++i) {
66+
t1.add(String(data[i]), data[i]);
67+
}
68+
69+
ArduinoCITable<String, int> t2 = t1;
70+
t1.clear();
71+
assertEqual(0, t1.size());
72+
assertEqual(5, t2.size());
73+
74+
for (int i = 0; i < 5; ++i) {
75+
assertTrue(t2.has(String(data[i])));
76+
assertTrue(t2.remove(String(data[i])));
77+
assertFalse(t2.has(String(data[i])));
78+
}
79+
assertEqual(0, t2.size());
80+
}
81+
4982
unittest(iteration_no_arg) {
5083
ArduinoCITable<int, int> t;
5184
for (int i = 0; i < 5; ++i) {

cpp/arduino/ci/Table.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,13 @@ class ArduinoCITable {
2727
public:
2828
ArduinoCITable() : mNilK(), mNilV() { init(); }
2929

30+
ArduinoCITable(const ArduinoCITable& obj) : mNilK(), mNilV() {
31+
init();
32+
for (Node* p = obj.mStart; p; p = p->next) {
33+
add(p->key, p->val);
34+
}
35+
}
36+
3037
// number of things in the table
3138
inline unsigned long size() const { return mSize; }
3239

@@ -119,6 +126,7 @@ class ArduinoCITable {
119126
mStart = mStart->next;
120127
delete p;
121128
}
129+
mSize = 0;
122130
}
123131

124132
~ArduinoCITable() { clear(); }

0 commit comments

Comments
 (0)