Skip to content

Commit

Permalink
Add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
xoofx committed Jul 5, 2024
1 parent 529aa94 commit 3e6a2ff
Show file tree
Hide file tree
Showing 15 changed files with 499 additions and 61 deletions.
117 changes: 117 additions & 0 deletions src/XenoAtom.Allocators.Tests/BasicTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ public async Task TestAllocate1()
tlsf.Reset();
Assert.AreEqual(0, tlsf.Chunks.Length);
Assert.AreEqual(0, allocator.RequestedChunkAllocations.Count);

await VerifyStep(tlsf, "03-Reset");
}

[TestMethod]
Expand Down Expand Up @@ -115,6 +117,121 @@ public async Task TestAllocate3()
Assert.AreEqual(0, tlsf.Chunks.Length);
Assert.AreEqual(0, allocator.RequestedChunkAllocations.Count);
}

[TestMethod]
public async Task TestAllocateInterleavedFree()
{
var allocator = new TlsfChunkAllocatorTestInstance(BaseAddress, BaseChunkSize);

var tlsf = new TlsfAllocator(allocator, 64);

Assert.AreEqual(0, tlsf.Chunks.Length);

var allocation1 = tlsf.Allocate(64);
var allocation2 = tlsf.Allocate(64);
var allocation3 = tlsf.Allocate(64);
var allocation4 = tlsf.Allocate(64);

// Dump intermediate result
await VerifyStep(tlsf, "01-Allocations");

// Free allocation 2
// Free allocation 4
tlsf.Free(allocation2);
tlsf.Free(allocation4);

await VerifyStep(tlsf, "02-Free24");

// Free allocation 2
// Free allocation 4
tlsf.Free(allocation1);
tlsf.Free(allocation3);

await VerifyStep(tlsf, "03-Free13");

// Resets the allocator (free chunks)
tlsf.Reset();
Assert.AreEqual(0, tlsf.Chunks.Length);
Assert.AreEqual(0, allocator.RequestedChunkAllocations.Count);
}

[TestMethod]
public async Task TestAllocateInterleavedFree2()
{
var allocator = new TlsfChunkAllocatorTestInstance(BaseAddress, BaseChunkSize);

var tlsf = new TlsfAllocator(allocator, 64);

Assert.AreEqual(0, tlsf.Chunks.Length);

var allocation1 = tlsf.Allocate(64);
var allocation2 = tlsf.Allocate(64);
var allocation3 = tlsf.Allocate(64);

// Dump intermediate result
await VerifyStep(tlsf, "01-Allocations");

// Free allocation 2
tlsf.Free(allocation2);

await VerifyStep(tlsf, "02-Free2");

// Free allocation 3
tlsf.Free(allocation3);

await VerifyStep(tlsf, "03-Free3");

// Free allocation 1
tlsf.Free(allocation1);

await VerifyStep(tlsf, "04-Free1");


// Resets the allocator (free chunks)
tlsf.Reset();
Assert.AreEqual(0, tlsf.Chunks.Length);
Assert.AreEqual(0, allocator.RequestedChunkAllocations.Count);
}


[TestMethod]
public async Task TestAllocateInterleavedFreeAndRealloc()
{
var allocator = new TlsfChunkAllocatorTestInstance(BaseAddress, BaseChunkSize);

var tlsf = new TlsfAllocator(allocator, 64);

Assert.AreEqual(0, tlsf.Chunks.Length);

var allocation1 = tlsf.Allocate(64);
var allocation2 = tlsf.Allocate(64);
var allocation3 = tlsf.Allocate(64);

// Dump intermediate result
await VerifyStep(tlsf, "01-Allocations");

// Free allocation 2
tlsf.Free(allocation2);

await VerifyStep(tlsf, "02-Free2");

// Allocation 2
allocation2 = tlsf.Allocate(64);

await VerifyStep(tlsf, "03-Allocate2");

// Free allocation 1
tlsf.Free(allocation3);
tlsf.Free(allocation2);
tlsf.Free(allocation1);

await VerifyStep(tlsf, "04-Free321");

// Resets the allocator (free chunks)
tlsf.Reset();
Assert.AreEqual(0, tlsf.Chunks.Length);
Assert.AreEqual(0, allocator.RequestedChunkAllocations.Count);
}

[TestMethod]
public async Task TestAllocateBiggerThanChunk()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
TLSF Allocator
==============

Alignment: 64

No chunks allocated

No bins allocated

No blocks allocated

Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ Chunk MemoryChunkId(0x0):
FreeBlockCount: 0
FirstBlockInPhysicalOrder: 0

Bins: 0b0000_0000_0000_0000_0000_0000_0000_0000
----------------------------------------
No bins allocated

Blocks: 1
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
TLSF Allocator
==============

Alignment: 64

Chunks: 1
----------
Chunk MemoryChunkId(0x0):
BaseAddress: MemoryAddress(0xFE00120000000000)
Size: MemorySize(65536)
TotalAllocated: MemorySize(256)
UsedBlockCount: 4
FreeBlockCount: 1
FirstBlockInPhysicalOrder: 1

Bins: 0b0000_0000_0000_0000_0000_0000_0010_0000
----------------------------------------
Bin L1 (5): [0x4000, 0x7fff[ -> Mask: 0b1000_0000_0000_0000
Bin L2 (15): [0x7c00, 0x7fff[ -> FirstFreeBlock: 0

Blocks: 5
-----------
Block Chunk Offset Size Status Free Links Phys Links
[0] 0 256 65280 Free -1 <-> -1 4 <-> -1
[1] 0 0 64 Used -1 <-> -1 -1 <-> 2
[2] 0 64 64 Used -1 <-> -1 1 <-> 3
[3] 0 128 64 Used -1 <-> -1 2 <-> 4
[4] 0 192 64 Used -1 <-> -1 3 <-> 0
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
TLSF Allocator
==============

Alignment: 64

Chunks: 1
----------
Chunk MemoryChunkId(0x0):
BaseAddress: MemoryAddress(0xFE00120000000000)
Size: MemorySize(65536)
TotalAllocated: MemorySize(128)
UsedBlockCount: 2
FreeBlockCount: 2
FirstBlockInPhysicalOrder: 1

Bins: 0b0000_0000_0000_0000_0000_0000_0010_0001
----------------------------------------
Bin L1 (0): [0x0, 0x1ff[ -> Mask: 0b0000_0000_0000_0001
Bin L2 (0): [0x0, 0x1f[ -> FirstFreeBlock: 2
Bin L1 (5): [0x4000, 0x7fff[ -> Mask: 0b1000_0000_0000_0000
Bin L2 (15): [0x7c00, 0x7fff[ -> FirstFreeBlock: 4

Blocks: 5
-----------
Block Chunk Offset Size Status Free Links Phys Links
[0] Avail
[1] 0 0 64 Used -1 <-> -1 -1 <-> 2
[2] 0 64 64 Free -1 <-> -1 1 <-> 3
[3] 0 128 64 Used -1 <-> -1 2 <-> 4
[4] 0 192 65344 Free -1 <-> -1 3 <-> -1
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
TLSF Allocator
==============

Alignment: 64

Chunks: 1
----------
Chunk MemoryChunkId(0x0):
BaseAddress: MemoryAddress(0xFE00120000000000)
Size: MemorySize(65536)
TotalAllocated: MemorySize(0)
UsedBlockCount: 0
FreeBlockCount: 1
FirstBlockInPhysicalOrder: 3

Bins: 0b0000_0000_0000_0000_0000_0000_0100_0000
----------------------------------------
Bin L1 (6): [0x8000, 0xffff[ -> Mask: 0b0000_0000_0000_0001
Bin L2 (0): [0x8000, 0x87ff[ -> FirstFreeBlock: 3

Blocks: 5
-----------
Block Chunk Offset Size Status Free Links Phys Links
[0-2] Avail
[3] 0 0 65536 Free -1 <-> -1 -1 <-> -1
[4] Avail
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
TLSF Allocator
==============

Alignment: 64

Chunks: 1
----------
Chunk MemoryChunkId(0x0):
BaseAddress: MemoryAddress(0xFE00120000000000)
Size: MemorySize(65536)
TotalAllocated: MemorySize(192)
UsedBlockCount: 3
FreeBlockCount: 1
FirstBlockInPhysicalOrder: 1

Bins: 0b0000_0000_0000_0000_0000_0000_0010_0000
----------------------------------------
Bin L1 (5): [0x4000, 0x7fff[ -> Mask: 0b1000_0000_0000_0000
Bin L2 (15): [0x7c00, 0x7fff[ -> FirstFreeBlock: 0

Blocks: 4
-----------
Block Chunk Offset Size Status Free Links Phys Links
[0] 0 192 65344 Free -1 <-> -1 3 <-> -1
[1] 0 0 64 Used -1 <-> -1 -1 <-> 2
[2] 0 64 64 Used -1 <-> -1 1 <-> 3
[3] 0 128 64 Used -1 <-> -1 2 <-> 0
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
TLSF Allocator
==============

Alignment: 64

Chunks: 1
----------
Chunk MemoryChunkId(0x0):
BaseAddress: MemoryAddress(0xFE00120000000000)
Size: MemorySize(65536)
TotalAllocated: MemorySize(128)
UsedBlockCount: 2
FreeBlockCount: 2
FirstBlockInPhysicalOrder: 1

Bins: 0b0000_0000_0000_0000_0000_0000_0010_0001
----------------------------------------
Bin L1 (0): [0x0, 0x1ff[ -> Mask: 0b0000_0000_0000_0001
Bin L2 (0): [0x0, 0x1f[ -> FirstFreeBlock: 2
Bin L1 (5): [0x4000, 0x7fff[ -> Mask: 0b1000_0000_0000_0000
Bin L2 (15): [0x7c00, 0x7fff[ -> FirstFreeBlock: 0

Blocks: 4
-----------
Block Chunk Offset Size Status Free Links Phys Links
[0] 0 192 65344 Free -1 <-> -1 3 <-> -1
[1] 0 0 64 Used -1 <-> -1 -1 <-> 2
[2] 0 64 64 Free -1 <-> -1 1 <-> 3
[3] 0 128 64 Used -1 <-> -1 2 <-> 0
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
TLSF Allocator
==============

Alignment: 64

Chunks: 1
----------
Chunk MemoryChunkId(0x0):
BaseAddress: MemoryAddress(0xFE00120000000000)
Size: MemorySize(65536)
TotalAllocated: MemorySize(64)
UsedBlockCount: 1
FreeBlockCount: 1
FirstBlockInPhysicalOrder: 1

Bins: 0b0000_0000_0000_0000_0000_0000_0010_0000
----------------------------------------
Bin L1 (5): [0x4000, 0x7fff[ -> Mask: 0b1000_0000_0000_0000
Bin L2 (15): [0x7c00, 0x7fff[ -> FirstFreeBlock: 3

Blocks: 4
-----------
Block Chunk Offset Size Status Free Links Phys Links
[0] Avail
[1] 0 0 64 Used -1 <-> -1 -1 <-> 3
[2] Avail
[3] 0 64 65472 Free -1 <-> -1 1 <-> -1
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
TLSF Allocator
==============

Alignment: 64

Chunks: 1
----------
Chunk MemoryChunkId(0x0):
BaseAddress: MemoryAddress(0xFE00120000000000)
Size: MemorySize(65536)
TotalAllocated: MemorySize(0)
UsedBlockCount: 0
FreeBlockCount: 1
FirstBlockInPhysicalOrder: 1

Bins: 0b0000_0000_0000_0000_0000_0000_0100_0000
----------------------------------------
Bin L1 (6): [0x8000, 0xffff[ -> Mask: 0b0000_0000_0000_0001
Bin L2 (0): [0x8000, 0x87ff[ -> FirstFreeBlock: 1

Blocks: 4
-----------
Block Chunk Offset Size Status Free Links Phys Links
[0] Avail
[1] 0 0 65536 Free -1 <-> -1 -1 <-> -1
[2-3] Avail
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
TLSF Allocator
==============

Alignment: 64

Chunks: 1
----------
Chunk MemoryChunkId(0x0):
BaseAddress: MemoryAddress(0xFE00120000000000)
Size: MemorySize(65536)
TotalAllocated: MemorySize(192)
UsedBlockCount: 3
FreeBlockCount: 1
FirstBlockInPhysicalOrder: 1

Bins: 0b0000_0000_0000_0000_0000_0000_0010_0000
----------------------------------------
Bin L1 (5): [0x4000, 0x7fff[ -> Mask: 0b1000_0000_0000_0000
Bin L2 (15): [0x7c00, 0x7fff[ -> FirstFreeBlock: 0

Blocks: 4
-----------
Block Chunk Offset Size Status Free Links Phys Links
[0] 0 192 65344 Free -1 <-> -1 3 <-> -1
[1] 0 0 64 Used -1 <-> -1 -1 <-> 2
[2] 0 64 64 Used -1 <-> -1 1 <-> 3
[3] 0 128 64 Used -1 <-> -1 2 <-> 0
Loading

0 comments on commit 3e6a2ff

Please sign in to comment.