|
3 | 3 | #include "mock/mock-arduino.hpp"
|
4 | 4 | #include "mock/mock-parameters.hpp"
|
5 | 5 |
|
6 |
| -#include <string.h> |
| 6 | +#include <cassert> |
| 7 | +#include <cstring> |
7 | 8 |
|
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 | +} |
9 | 95 |
|
10 | 96 | int test_notei2c_arduino_constructor_invokes_twowire_parameter_begin_method()
|
11 | 97 | {
|
@@ -1448,6 +1534,9 @@ int test_notei2c_arduino_transmit_returns_error_message_on_unexpected_i2c_transm
|
1448 | 1534 | int main(void)
|
1449 | 1535 | {
|
1450 | 1536 | 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"}, |
1451 | 1540 | {test_notei2c_arduino_constructor_invokes_twowire_parameter_begin_method, "test_notei2c_arduino_constructor_invokes_twowire_parameter_begin_method"},
|
1452 | 1541 | {test_notei2c_arduino_receive_requests_response_data_from_notecard, "test_notei2c_arduino_receive_requests_response_data_from_notecard"},
|
1453 | 1542 | {test_notei2c_arduino_receive_will_retry_transmission_on_i2c_failure, "test_notei2c_arduino_receive_will_retry_transmission_on_i2c_failure"},
|
|
0 commit comments