Hardware Reference
In-Depth Information
Listing 13-6. ATS_GetFreeMemory Tests Example, from Matthew Murdoch
#include <ArduinoTestSuite.h>
void setup() {
ATS_begin("Arduino", "ATS_GetFreeMemory() Tests");
testAllocatingThenDeallocatingPreservesFreeMemory();
testRepeatedlyAllocatingAndDeallocatingMemoryPreservesFreeMemory();
testAllocatingAndDeallocatingSmallerPreservesFreeMemory();
ATS_end();
}
// This test checks that the free list is taken into account when free memory is calculated
// when using versions of free() which *don't* reset __brkval (such as in avr-libc 1.6.4)
void testAllocatingThenDeallocatingPreservesFreeMemory() {
int startMemory = ATS_GetFreeMemory();
void* buffer = malloc(10);
free(buffer);
ATS_PrintTestStatus("Allocating then deallocating preserves free memory", startMemory ==
ATS_GetFreeMemory());
}
// This test checks that the free list is taken into account when free memory is calculated
// even when using versions of free() which *do* reset __brkval (such as in avr-libc 1.7.1)
void testAllocatingAndDeallocatingInterleavedPreservesFreeMemory() {
void* buffer1 = malloc(10);
int startMemory = ATS_GetFreeMemory();
void* buffer2 = malloc(10);
free(buffer1);
ATS_PrintTestStatus("Interleaved allocation and deallocation preserves free memory",
startMemory == ATS_GetFreeMemory());
free(buffer2);
}
void testRepeatedlyAllocatingAndDeallocatingMemoryPreservesFreeMemory() {
int startMemory = ATS_GetFreeMemory();
for (int i = 0; i < 10; i++) {
void* buffer1 = malloc(10);
void* buffer2 = malloc(10);
void* buffer3 = malloc(10);
free(buffer3);
 
Search WWH ::




Custom Search