Skip to content

Commit affe163

Browse files
authored
chore: increase code coverage (#55)
1 parent 5e85578 commit affe163

9 files changed

+305
-16
lines changed

src/NoteI2c_Arduino.cpp

+9-4
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,17 @@ static const char *i2cerr = "i2c {io}";
77
NoteI2c *
88
make_note_i2c (
99
NoteI2c::bus_t i2c_bus_
10-
) {
10+
)
11+
{
1112
static NoteI2c * note_i2c = nullptr;
12-
if (note_i2c) {
13-
delete note_i2c;
13+
if (!i2c_bus_) {
14+
if (note_i2c) {
15+
delete note_i2c;
16+
note_i2c = nullptr;
17+
}
18+
} else if (!note_i2c) {
19+
note_i2c = new NoteI2c_Arduino(*reinterpret_cast<TwoWire *>(i2c_bus_));
1420
}
15-
note_i2c = new NoteI2c_Arduino(*reinterpret_cast<TwoWire *>(i2c_bus_));
1621
return note_i2c;
1722
}
1823

src/NoteLog_Arduino.cpp

+7-3
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,14 @@ make_note_log (
66
)
77
{
88
static NoteLog * note_log = nullptr;
9-
if (note_log) {
10-
delete note_log;
9+
if (!log_channel_) {
10+
if (note_log) {
11+
delete note_log;
12+
note_log = nullptr;
13+
}
14+
} else if (!note_log) {
15+
note_log = new NoteLog_Arduino(reinterpret_cast<Stream *>(log_channel_));
1116
}
12-
note_log = new NoteLog_Arduino(reinterpret_cast<Stream *>(log_channel_));
1317
return note_log;
1418
}
1519

src/NoteSerial_Arduino.cpp

+7-3
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,14 @@ make_note_serial (
77
)
88
{
99
static NoteSerial * note_serial = nullptr;
10-
if (note_serial) {
11-
delete note_serial;
10+
if (!serial_channel_) {
11+
if (note_serial) {
12+
delete note_serial;
13+
note_serial = nullptr;
14+
}
15+
} else if (!note_serial) {
16+
note_serial = new NoteSerial_Arduino(*reinterpret_cast<HardwareSerial *>(serial_channel_), baud_rate_);
1217
}
13-
note_serial = new NoteSerial_Arduino(*reinterpret_cast<HardwareSerial *>(serial_channel_), baud_rate_);
1418
return note_serial;
1519
}
1620

src/Notecard.cpp

+8
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,14 @@ namespace
165165
PUBLIC FUNCTIONS
166166
***************************************************************************/
167167

168+
Notecard::~Notecard (void)
169+
{
170+
// Delete Singleton(s)
171+
noteI2c = make_note_i2c(nullptr);
172+
noteLog = make_note_log(nullptr);
173+
noteSerial = make_note_serial(nullptr, 0);
174+
}
175+
168176
/**************************************************************************/
169177
/*!
170178
@brief Initialize the Notecard for I2C.

src/Notecard.h

+1
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@
135135
class Notecard
136136
{
137137
public:
138+
~Notecard(void);
138139
void begin(uint32_t i2cAddress = NOTE_I2C_ADDR_DEFAULT,
139140
uint32_t i2cMax = NOTE_I2C_MAX_DEFAULT,
140141
TwoWire &wirePort = Wire);

test/NoteI2c_Arduino.test.cpp

+91-2
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,95 @@
33
#include "mock/mock-arduino.hpp"
44
#include "mock/mock-parameters.hpp"
55

6-
#include <string.h>
6+
#include <cassert>
7+
#include <cstring>
78

8-
// Compile command: g++ -Wall -Wextra -Wpedantic mock/mock-arduino.cpp mock/mock-note-c-note.c ../src/NoteI2c_Arduino.cpp NoteI2c_Arduino.test.cpp -std=c++14 -I. -I../src -DNOTE_MOCK && ./a.out || echo "Tests Result: $?"
9+
// Compile command: g++ -Wall -Wextra -Wpedantic mock/mock-arduino.cpp mock/mock-note-c-note.c ../src/NoteI2c_Arduino.cpp NoteI2c_Arduino.test.cpp -std=c++11 -I. -I../src -DNOTE_MOCK && ./a.out || echo "Tests Result: $?"
10+
11+
int test_make_note_i2c_instantiates_notei2c_object()
12+
{
13+
int result;
14+
15+
// Arrange
16+
NoteI2c * notei2c = nullptr;
17+
18+
// Action
19+
notei2c = make_note_i2c(reinterpret_cast<NoteI2c::bus_t>(&Wire));
20+
21+
// Assert
22+
if (nullptr != notei2c)
23+
{
24+
result = 0;
25+
}
26+
else
27+
{
28+
result = static_cast<int>('i' + '2' + 'c');
29+
std::cout << "FAILED] " << __FILE__ << ":" << __LINE__ << std::endl;
30+
std::cout << "\tnotei2c == " << !!notei2c << ", EXPECTED: not nullptr" << std::endl;
31+
std::cout << "[";
32+
}
33+
34+
// Clean-up
35+
make_note_i2c(nullptr);
36+
37+
return result;
38+
}
39+
40+
int test_make_note_i2c_enforces_singleton_by_returning_same_notei2c_object_for_all_calls()
41+
{
42+
int result;
43+
44+
// Arrange
45+
NoteI2c * const notei2c_1 = make_note_i2c(reinterpret_cast<NoteI2c::bus_t>(&Wire));
46+
47+
// Action
48+
NoteI2c * const notei2c_2 = make_note_i2c(reinterpret_cast<NoteI2c::bus_t>(&Wire));
49+
50+
// Assert
51+
if (notei2c_1 == notei2c_2)
52+
{
53+
result = 0;
54+
}
55+
else
56+
{
57+
result = static_cast<int>('i' + '2' + 'c');
58+
std::cout << "FAILED] " << __FILE__ << ":" << __LINE__ << std::endl;
59+
std::cout << "\tnotei2c_2 == " << std::hex << notei2c_2 << ", EXPECTED: " << notei2c_1 << std::endl;
60+
std::cout << "[";
61+
}
62+
63+
// Clean-up
64+
make_note_i2c(nullptr);
65+
66+
return result;
67+
}
68+
69+
int test_make_note_i2c_deletes_singleton_when_nullptr_is_passed_as_parameter()
70+
{
71+
int result;
72+
73+
// Arrange
74+
NoteI2c * notei2c = make_note_i2c(reinterpret_cast<NoteI2c::bus_t>(&Wire));
75+
assert(notei2c);
76+
77+
// Action
78+
notei2c = make_note_i2c(nullptr);
79+
80+
// Assert
81+
if (nullptr == notei2c)
82+
{
83+
result = 0;
84+
}
85+
else
86+
{
87+
result = static_cast<int>('i' + '2' + 'c');
88+
std::cout << "FAILED] " << __FILE__ << ":" << __LINE__ << std::endl;
89+
std::cout << "\tnotei2c == " << std::hex << notei2c << ", EXPECTED: 0 (nullptr)" << std::endl;
90+
std::cout << "[";
91+
}
92+
93+
return result;
94+
}
995

1096
int test_notei2c_arduino_constructor_invokes_twowire_parameter_begin_method()
1197
{
@@ -1448,6 +1534,9 @@ int test_notei2c_arduino_transmit_returns_error_message_on_unexpected_i2c_transm
14481534
int main(void)
14491535
{
14501536
TestFunction tests[] = {
1537+
{test_make_note_i2c_instantiates_notei2c_object, "test_make_note_i2c_instantiates_notei2c_object"},
1538+
{test_make_note_i2c_enforces_singleton_by_returning_same_notei2c_object_for_all_calls, "test_make_note_i2c_enforces_singleton_by_returning_same_notei2c_object_for_all_calls"},
1539+
{test_make_note_i2c_deletes_singleton_when_nullptr_is_passed_as_parameter, "test_make_note_i2c_deletes_singleton_when_nullptr_is_passed_as_parameter"},
14511540
{test_notei2c_arduino_constructor_invokes_twowire_parameter_begin_method, "test_notei2c_arduino_constructor_invokes_twowire_parameter_begin_method"},
14521541
{test_notei2c_arduino_receive_requests_response_data_from_notecard, "test_notei2c_arduino_receive_requests_response_data_from_notecard"},
14531542
{test_notei2c_arduino_receive_will_retry_transmission_on_i2c_failure, "test_notei2c_arduino_receive_will_retry_transmission_on_i2c_failure"},

test/NoteLog_Arduino.test.cpp

+90-1
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,96 @@
33
#include "mock/mock-arduino.hpp"
44
#include "mock/mock-parameters.hpp"
55

6-
#include <string.h>
6+
#include <cassert>
7+
#include <cstring>
78

89
// Compile command: g++ -Wall -Wextra -Wpedantic mock/mock-arduino.cpp ../src/NoteLog_Arduino.cpp NoteLog_Arduino.test.cpp -std=c++11 -I. -I../src -DNOTE_MOCK && ./a.out || echo "Tests Result: $?"
910

11+
int test_make_note_log_instantiates_notelog_object()
12+
{
13+
int result;
14+
15+
// Arrange
16+
NoteLog * notelog = nullptr;
17+
18+
// Action
19+
notelog = make_note_log(reinterpret_cast<NoteLog::channel_t>(&Serial));
20+
21+
// Assert
22+
if (nullptr != notelog)
23+
{
24+
result = 0;
25+
}
26+
else
27+
{
28+
result = static_cast<int>('d' + 'e' + 'b' + 'u' + 'g');
29+
std::cout << "FAILED] " << __FILE__ << ":" << __LINE__ << std::endl;
30+
std::cout << "\tnotelog == " << !!notelog << ", EXPECTED: not nullptr" << std::endl;
31+
std::cout << "[";
32+
}
33+
34+
// Clean-up
35+
make_note_log(nullptr);
36+
37+
return result;
38+
}
39+
40+
int test_make_note_log_enforces_singleton_by_returning_same_notelog_object_for_all_calls()
41+
{
42+
int result;
43+
44+
// Arrange
45+
NoteLog * const notelog_1 = make_note_log(reinterpret_cast<NoteLog::channel_t>(&Serial));
46+
47+
// Action
48+
NoteLog * const notelog_2 = make_note_log(reinterpret_cast<NoteLog::channel_t>(&Serial));
49+
50+
// Assert
51+
if (notelog_1 == notelog_2)
52+
{
53+
result = 0;
54+
}
55+
else
56+
{
57+
result = static_cast<int>('d' + 'e' + 'b' + 'u' + 'g');
58+
std::cout << "FAILED] " << __FILE__ << ":" << __LINE__ << std::endl;
59+
std::cout << "\tnotelog_2 == " << std::hex << notelog_2 << ", EXPECTED: " << notelog_1 << std::endl;
60+
std::cout << "[";
61+
}
62+
63+
// Clean-up
64+
make_note_log(nullptr);
65+
66+
return result;
67+
}
68+
69+
int test_make_note_log_deletes_singleton_when_nullptr_is_passed_as_parameter()
70+
{
71+
int result;
72+
73+
// Arrange
74+
NoteLog * notelog = make_note_log(reinterpret_cast<NoteLog::channel_t>(&Serial));
75+
assert(notelog);
76+
77+
// Action
78+
notelog = make_note_log(nullptr);
79+
80+
// Assert
81+
if (nullptr == notelog)
82+
{
83+
result = 0;
84+
}
85+
else
86+
{
87+
result = static_cast<int>('d' + 'e' + 'b' + 'u' + 'g');
88+
std::cout << "FAILED] " << __FILE__ << ":" << __LINE__ << std::endl;
89+
std::cout << "\tnotelog == " << std::hex << notelog << ", EXPECTED: 0 (nullptr)" << std::endl;
90+
std::cout << "[";
91+
}
92+
93+
return result;
94+
}
95+
1096
int test_notelog_arduino_print_does_not_modify_str_parameter_value_before_passing_to_stream_print()
1197
{
1298
int result;
@@ -69,6 +155,9 @@ int test_notelog_arduino_print_does_not_modify_stream_print_result_value_before_
69155
int main(void)
70156
{
71157
TestFunction tests[] = {
158+
{test_make_note_log_instantiates_notelog_object, "test_make_note_log_instantiates_notelog_object"},
159+
{test_make_note_log_enforces_singleton_by_returning_same_notelog_object_for_all_calls, "test_make_note_log_enforces_singleton_by_returning_same_notelog_object_for_all_calls"},
160+
{test_make_note_log_deletes_singleton_when_nullptr_is_passed_as_parameter, "test_make_note_log_deletes_singleton_when_nullptr_is_passed_as_parameter"},
72161
{test_notelog_arduino_print_does_not_modify_str_parameter_value_before_passing_to_stream_print, "test_notelog_arduino_print_does_not_modify_buffer_parameter_value_before_passing_to_stream_print"},
73162
{test_notelog_arduino_print_does_not_modify_stream_print_result_value_before_returning_to_caller, "test_notelog_arduino_print_does_not_modify_stream_print_result_value_before_returning_to_caller"},
74163
};

0 commit comments

Comments
 (0)