Skip to content

Commit f7555f2

Browse files
authored
feat: Notecard::setFn() (#144)
1 parent f7b7cc9 commit f7555f2

File tree

5 files changed

+208
-0
lines changed

5 files changed

+208
-0
lines changed

src/Notecard.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -483,6 +483,23 @@ void Notecard::setDebugOutputStream(NoteLog * noteLog_)
483483
}
484484
}
485485

486+
/**************************************************************************/
487+
/*!
488+
@brief Override default memory management and timing functions.
489+
@param mallocHook
490+
A memory allocation hook.
491+
@param freeHook
492+
A memory deallocation hook.
493+
@param delayMsHook
494+
A delay execution hook.
495+
@param getMsHook
496+
A get current time hook.
497+
*/
498+
/**************************************************************************/
499+
void Notecard::setFn(mallocFn mallocHook, freeFn freeHook, delayMsFn delayMsHook, getMsFn getMsHook) {
500+
NoteSetFn(mallocHook, freeHook, delayMsHook, getMsHook);
501+
}
502+
486503
/**************************************************************************/
487504
/*!
488505
@brief Set the lock/unlock functions the Notecard uses for I2C access.

src/Notecard.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ class Notecard
106106
bool sendRequest(J *req) const;
107107
bool sendRequestWithRetry(J *req, uint32_t timeoutSeconds) const;
108108
void setDebugOutputStream(NoteLog * noteLog);
109+
void setFn(mallocFn mallocHook, freeFn freeHook, delayMsFn delayMsHook, getMsFn getMsHook);
109110
void setFnI2cMutex(mutexFn lockI2cFn, mutexFn unlockI2cFn);
110111
void setFnNoteMutex(mutexFn lockNoteFn, mutexFn unlockNoteFn);
111112
void setTransactionPins(NoteTxn * noteTxn);

test/Notecard.test.cpp

Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1758,6 +1758,146 @@ int test_notecard_clearDebugOutputStream_clears_the_debug_log_function_pointer()
17581758
return result;
17591759
}
17601760

1761+
int test_notecard_setFn_shares_a_memory_allocation_function_pointer()
1762+
{
1763+
int result;
1764+
1765+
// Arrange
1766+
////////////
1767+
1768+
const mallocFn mockMallocFn = reinterpret_cast<mallocFn>(0x19790917);
1769+
1770+
Notecard notecard;
1771+
noteSetFn_Parameters.reset();
1772+
1773+
// Action
1774+
///////////
1775+
1776+
notecard.setFn(mockMallocFn, nullptr, nullptr, nullptr);
1777+
1778+
// Assert
1779+
///////////
1780+
1781+
if (noteSetFn_Parameters.mallocfn)
1782+
{
1783+
result = 0;
1784+
}
1785+
else
1786+
{
1787+
result = static_cast<int>('n' + 'o' + 't' + 'e' + 'c' + 'a' + 'r' + 'd');
1788+
std::cout << "\33[31mFAILED\33[0m] " << __FILE__ << ":" << __LINE__ << std::endl;
1789+
std::cout << "\tnoteSetFn_Parameters.mallocfn == " << !!noteSetFn_Parameters.mallocfn << ", EXPECTED: not 0 (`nullptr`)" << std::endl;
1790+
std::cout << "[";
1791+
}
1792+
1793+
return result;
1794+
}
1795+
1796+
int test_notecard_setFn_shares_a_memory_deallocation_function_pointer()
1797+
{
1798+
int result;
1799+
1800+
// Arrange
1801+
////////////
1802+
1803+
const freeFn mockFreeFn = reinterpret_cast<freeFn>(0x19790917);
1804+
1805+
Notecard notecard;
1806+
noteSetFn_Parameters.reset();
1807+
1808+
// Action
1809+
///////////
1810+
1811+
notecard.setFn(nullptr, mockFreeFn, nullptr, nullptr);
1812+
1813+
// Assert
1814+
///////////
1815+
1816+
if (noteSetFn_Parameters.freefn)
1817+
{
1818+
result = 0;
1819+
}
1820+
else
1821+
{
1822+
result = static_cast<int>('n' + 'o' + 't' + 'e' + 'c' + 'a' + 'r' + 'd');
1823+
std::cout << "\33[31mFAILED\33[0m] " << __FILE__ << ":" << __LINE__ << std::endl;
1824+
std::cout << "\tnoteSetFn_Parameters.freefn == " << !!noteSetFn_Parameters.freefn << ", EXPECTED: not 0 (`nullptr`)" << std::endl;
1825+
std::cout << "[";
1826+
}
1827+
1828+
return result;
1829+
}
1830+
1831+
int test_notecard_setFn_shares_a_delay_function_pointer()
1832+
{
1833+
int result;
1834+
1835+
// Arrange
1836+
////////////
1837+
1838+
const delayMsFn mockDelayFn = reinterpret_cast<delayMsFn>(0x19790917);
1839+
1840+
Notecard notecard;
1841+
noteSetFn_Parameters.reset();
1842+
1843+
// Action
1844+
///////////
1845+
1846+
notecard.setFn(nullptr, nullptr, mockDelayFn, nullptr);
1847+
1848+
// Assert
1849+
///////////
1850+
1851+
if (noteSetFn_Parameters.delayfn)
1852+
{
1853+
result = 0;
1854+
}
1855+
else
1856+
{
1857+
result = static_cast<int>('n' + 'o' + 't' + 'e' + 'c' + 'a' + 'r' + 'd');
1858+
std::cout << "\33[31mFAILED\33[0m] " << __FILE__ << ":" << __LINE__ << std::endl;
1859+
std::cout << "\tnoteSetFn_Parameters.delayfn == " << !!noteSetFn_Parameters.delayfn << ", EXPECTED: not 0 (`nullptr`)" << std::endl;
1860+
std::cout << "[";
1861+
}
1862+
1863+
return result;
1864+
}
1865+
1866+
int test_notecard_setFn_shares_a_millis_function_pointer()
1867+
{
1868+
int result;
1869+
1870+
// Arrange
1871+
////////////
1872+
1873+
const getMsFn mockMillisFn = reinterpret_cast<getMsFn>(0x19790917);
1874+
1875+
Notecard notecard;
1876+
noteSetFn_Parameters.reset();
1877+
1878+
// Action
1879+
///////////
1880+
1881+
notecard.setFn(nullptr, nullptr, nullptr, mockMillisFn);
1882+
1883+
// Assert
1884+
///////////
1885+
1886+
if (noteSetFn_Parameters.millisfn)
1887+
{
1888+
result = 0;
1889+
}
1890+
else
1891+
{
1892+
result = static_cast<int>('n' + 'o' + 't' + 'e' + 'c' + 'a' + 'r' + 'd');
1893+
std::cout << "\33[31mFAILED\33[0m] " << __FILE__ << ":" << __LINE__ << std::endl;
1894+
std::cout << "\tnoteSetFn_Parameters.millisfn == " << !!noteSetFn_Parameters.millisfn << ", EXPECTED: not 0 (`nullptr`)" << std::endl;
1895+
std::cout << "[";
1896+
}
1897+
1898+
return result;
1899+
}
1900+
17611901
int test_notecard_setFnI2cMutex_does_not_modify_locking_mutex_func_parameter_value_before_passing_to_note_c()
17621902
{
17631903
int result;
@@ -4928,6 +5068,10 @@ int main(void)
49285068
{test_notecard_setDebugOutputStream_shares_a_debug_log_function_pointer, "test_notecard_setDebugOutputStream_shares_a_debug_log_function_pointer"},
49295069
{test_notecard_setDebugOutputStream_clears_the_debug_log_function_pointer_when_nullptr_is_provided, "test_notecard_setDebugOutputStream_clears_the_debug_log_function_pointer_when_nullptr_is_provided"},
49305070
{test_notecard_clearDebugOutputStream_clears_the_debug_log_function_pointer, "test_notecard_clearDebugOutputStream_clears_the_debug_log_function_pointer"},
5071+
{test_notecard_setFn_shares_a_memory_allocation_function_pointer, "test_notecard_setFn_shares_a_memory_allocation_function_pointer"},
5072+
{test_notecard_setFn_shares_a_memory_deallocation_function_pointer, "test_notecard_setFn_shares_a_memory_deallocation_function_pointer"},
5073+
{test_notecard_setFn_shares_a_delay_function_pointer, "test_notecard_setFn_shares_a_delay_function_pointer"},
5074+
{test_notecard_setFn_shares_a_millis_function_pointer, "test_notecard_setFn_shares_a_millis_function_pointer"},
49315075
{test_notecard_setFnI2cMutex_does_not_modify_locking_mutex_func_parameter_value_before_passing_to_note_c, "test_notecard_setFnI2cMutex_does_not_modify_locking_mutex_func_parameter_value_before_passing_to_note_c"},
49325076
{test_notecard_setFnI2cMutex_does_not_modify_unlocking_mutex_func_parameter_value_before_passing_to_note_c, "test_notecard_setFnI2cMutex_does_not_modify_unlocking_mutex_func_parameter_value_before_passing_to_note_c"},
49335077
{test_notecard_setFnNoteMutex_does_not_modify_locking_mutex_func_parameter_value_before_passing_to_note_c, "test_notecard_setFnNoteMutex_does_not_modify_locking_mutex_func_parameter_value_before_passing_to_note_c"},

test/mock/mock-note-c-note.c

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ NoteRequestResponse_Parameters noteRequestResponse_Parameters;
1414
NoteRequestResponseWithRetry_Parameters noteRequestResponseWithRetry_Parameters;
1515
NoteResponseError_Parameters noteResponseError_Parameters;
1616
NoteSetFnDebugOutput_Parameters noteSetFnDebugOutput_Parameters;
17+
NoteSetFn_Parameters noteSetFn_Parameters;
1718
NoteSetFnDefault_Parameters noteSetFnDefault_Parameters;
1819
NoteSetFnI2C_Parameters noteSetFnI2C_Parameters;
1920
NoteSetFnI2CMutex_Parameters noteSetFnI2CMutex_Parameters;
@@ -263,6 +264,23 @@ NoteSetFnDebugOutput(
263264
noteSetFnDebugOutput_Parameters.fn = fn_;
264265
}
265266

267+
void
268+
NoteSetFn(
269+
mallocFn malloc_fn_,
270+
freeFn free_fn_,
271+
delayMsFn delay_fn_,
272+
getMsFn millis_fn_
273+
) {
274+
// Record invocation(s)
275+
++noteSetFn_Parameters.invoked;
276+
277+
// Stash parameter(s)
278+
noteSetFn_Parameters.mallocfn = malloc_fn_;
279+
noteSetFn_Parameters.freefn = free_fn_;
280+
noteSetFn_Parameters.delayfn = delay_fn_;
281+
noteSetFn_Parameters.millisfn = millis_fn_;
282+
}
283+
266284
void
267285
NoteSetFnDefault(
268286
mallocFn malloc_fn_,

test/mock/mock-parameters.hpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,33 @@ struct NoteSetFnDebugOutput_Parameters {
371371
debugOutputFn fn;
372372
};
373373

374+
struct NoteSetFn_Parameters {
375+
NoteSetFn_Parameters(
376+
void
377+
) :
378+
invoked(0),
379+
mallocfn(nullptr),
380+
freefn(nullptr),
381+
delayfn(nullptr),
382+
millisfn(nullptr)
383+
{ }
384+
void
385+
reset (
386+
void
387+
) {
388+
invoked = 0;
389+
mallocfn = nullptr;
390+
freefn = nullptr;
391+
delayfn = nullptr;
392+
millisfn = nullptr;
393+
}
394+
size_t invoked;
395+
mallocFn mallocfn;
396+
freeFn freefn;
397+
delayMsFn delayfn;
398+
getMsFn millisfn;
399+
};
400+
374401
struct NoteSetFnDefault_Parameters {
375402
NoteSetFnDefault_Parameters(
376403
void
@@ -552,6 +579,7 @@ extern NoteRequestResponse_Parameters noteRequestResponse_Parameters;
552579
extern NoteRequestResponseWithRetry_Parameters noteRequestResponseWithRetry_Parameters;
553580
extern NoteResponseError_Parameters noteResponseError_Parameters;
554581
extern NoteSetFnDebugOutput_Parameters noteSetFnDebugOutput_Parameters;
582+
extern NoteSetFn_Parameters noteSetFn_Parameters;
555583
extern NoteSetFnDefault_Parameters noteSetFnDefault_Parameters;
556584
extern NoteSetFnI2C_Parameters noteSetFnI2C_Parameters;
557585
extern NoteSetFnI2CMutex_Parameters noteSetFnI2CMutex_Parameters;

0 commit comments

Comments
 (0)