Permalink
Browse files

BootstrapAllocator: unit tests.

Unit tests added for bootstrap allocator, including some utilities in
BuddyAllocator for testing purpose.
  • Loading branch information...
1 parent 41f169c commit 71ef2ecd9f036075abdf3d37a6a5d2c37eae76c1 @ddejean ddejean committed May 15, 2012
@@ -7,6 +7,7 @@
*/
#include "BootstrapAllocator.h"
+#include "string.h"
/* Allocator heap size: the size is computed like this: 2^BOOTSTRAP_POWER */
#ifndef BOOTSTRAP_POWER
@@ -53,3 +54,10 @@ BootstrapAllocator* BootstrapAllocator::getInstance(void)
return BootstrapAllocator::_instance;
}
+void BootstrapAllocator::reset(void)
+{
+ memset(memoryHeap, BOOTSTRAP_HEAP_SIZE, 0);
+ memset(freeAreas, BOOTSTRAP_POWER, sizeof(struct freeblock));
+ _instance = new BootstrapAllocator();
+}
+
@@ -24,8 +24,18 @@ class BootstrapAllocator: public BuddyAllocator {
void* operator new(size_t);
public:
- /* Partial singleton implementation */
+ /*
+ * Singleton implementation: retrieve the instance of the
+ * allocator inside the singleton.
+ */
static BootstrapAllocator *getInstance(void);
+
+ /*
+ * Reset the allocator state. Beware, this will destroy memory
+ * allocation mapping and data in the allocated chunks.
+ * For testing purpose essentially.
+ */
+ static void reset(void);
};
#endif /*_BOOTSTRAP_ALLOCATOR_H_ */
@@ -32,6 +32,11 @@ BuddyAllocator::BuddyAllocator(struct freeblock *freeAreas,
}
+uint32_t BuddyAllocator::heapSize(void)
+{
+ return _heapSize;
+}
+
/**
* Compute the upper power of two that contains an number.
* @param n the number from which we compute the power
@@ -38,6 +38,9 @@ class BuddyAllocator {
/* Allocator implementation */
void *alloc(size_t size);
void free(void *chunk, size_t size);
+
+ /* Convenience */
+ uint32_t heapSize(void);
};
#endif /*_BUDDY_ALLOCATOR_H_ */
@@ -0,0 +1,40 @@
+#include "TestBootstrapAllocator.h"
+#include <string.h>
+#include <stdint.h>
+
+void TestBootstrapAllocator::setUp(void)
+{
+ BootstrapAllocator::reset();
+}
+
+void TestBootstrapAllocator::testSingleton(void)
+{
+ BootstrapAllocator *a1, *a2;
+
+ a1 = BootstrapAllocator::getInstance();
+ TS_ASSERT_DIFFERS(a1, NULL);
+ a2 = BootstrapAllocator::getInstance();
+ TS_ASSERT_DIFFERS(a2, NULL);
+
+ TS_ASSERT_EQUALS(a1, a2);
+}
+
+void TestBootstrapAllocator::testReset(void)
+{
+ uint32_t *chunk;
+ BootstrapAllocator *ba;
+
+ ba = BootstrapAllocator::getInstance();
+ TS_ASSERT_DIFFERS(ba, NULL);
+
+ chunk = (uint32_t*) ba->alloc(32);
+ TS_ASSERT_DIFFERS(chunk, NULL);
+ *chunk = 0xDEADBEEFu;
+
+ BootstrapAllocator::reset();
+ TS_ASSERT_DIFFERS(*chunk, 0xDEADBEEFu);
+
+ chunk = (uint32_t*) ba->alloc((uint32_t) ba->heapSize());
+ TS_ASSERT_DIFFERS(chunk, NULL);
+}
+
@@ -0,0 +1,15 @@
+#ifndef TESTBOOTSTRAPALLOCATOR_H_
+#define TESTBOOTSTRAPALLOCATOR_H_
+
+#include "CxxTest/TestSuite.h"
+#include "Memory/BootstrapAllocator.h"
+
+class TestBootstrapAllocator: public CxxTest::TestSuite {
+ public:
+ void setUp(void);
+
+ void testSingleton(void);
+ void testReset(void);
+};
+
+#endif /* TESTBUDDYALLOCATOR_H_ */
@@ -15,6 +15,13 @@ void TestBuddyAllocator::tearDown(void)
delete _allocator;
}
+void TestBuddyAllocator::testHeapSize(void)
+{
+ uint32_t heapSize;
+
+ heapSize = _allocator->heapSize();
+ TS_ASSERT_EQUALS(heapSize, (uint32_t)HEAP_SIZE);
+}
void TestBuddyAllocator::testSimpleAllocations(void)
{
@@ -17,15 +17,16 @@ class TestBuddyAllocator: public CxxTest::TestSuite {
void setUp(void);
void tearDown(void);
- void testSimpleAllocations(void);
- void testFreeBoundaries(void);
- void testCheckAddresses(void);
- void testFullAllocationsBySizes(void);
- void testAllocateOneOfEachSize(void);
- void testSmallAllocations(void);
+ void testHeapSize(void);
+ void testSimpleAllocations(void);
+ void testFreeBoundaries(void);
+ void testCheckAddresses(void);
+ void testFullAllocationsBySizes(void);
+ void testAllocateOneOfEachSize(void);
+ void testSmallAllocations(void);
- void testBadAllocationRequests(void);
- void testBadFreeRequests(void);
+ void testBadAllocationRequests(void);
+ void testBadFreeRequests(void);
};
#endif /* TESTBUDDYALLOCATOR_H_ */

0 comments on commit 71ef2ec

Please sign in to comment.