Skip to content

Commit 2073033

Browse files
committed
added B2ArenaAllocatorTypedTests
1 parent e0a3fcb commit 2073033

3 files changed

Lines changed: 70 additions & 2 deletions

File tree

src/Box2D.NET/B2ArenaAllocators.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public static void b2DestroyArenaAllocator(B2ArenaAllocator allocator)
3535
allocatorImpl.allocation = 0;
3636
allocatorImpl.maxAllocation = 0;
3737
allocatorImpl.index = 0;
38-
allocatorImpl.entries = b2Array_Create<B2ArenaEntry<T>>(32);
38+
allocatorImpl.entries = b2Array_Create<B2ArenaEntry<T>>(capacity);
3939
return allocatorImpl;
4040
}
4141

src/Box2D.NET/B2Worlds.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ public static B2WorldId b2CreateWorld(ref B2WorldDef def)
140140
world.generation = generation;
141141
world.inUse = true;
142142

143-
world.arena = b2CreateArenaAllocator(2048);
143+
world.arena = b2CreateArenaAllocator(256);
144144
b2CreateBroadPhase(ref world.broadPhase);
145145
b2CreateGraph(ref world.constraintGraph, 16);
146146

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
using NUnit.Framework;
2+
using static Box2D.NET.B2ArenaAllocators;
3+
4+
namespace Box2D.NET.Test;
5+
6+
public class B2ArenaAllocatorTypedTests
7+
{
8+
[Test]
9+
public void Constructor_InitializesCorrectly()
10+
{
11+
B2ArenaAllocatorTyped<int> alloc = b2CreateArenaAllocator<int>(10);
12+
Assert.That(alloc.capacity, Is.EqualTo(10));
13+
Assert.That(alloc.data.Array, Is.Not.Null);
14+
Assert.That(alloc.data.Count, Is.EqualTo(10));
15+
Assert.That(alloc.index, Is.EqualTo(0));
16+
Assert.That(alloc.allocation, Is.EqualTo(0));
17+
Assert.That(alloc.maxAllocation, Is.EqualTo(0));
18+
Assert.That(alloc.entries.data, Is.Not.Null);
19+
Assert.That(alloc.entries.count, Is.EqualTo(0));
20+
Assert.That(alloc.entries.capacity, Is.EqualTo(10));
21+
}
22+
23+
[Test]
24+
public void Grow_IncreasesCapacityWhenMaxAllocationExceedsCurrent()
25+
{
26+
B2ArenaAllocatorTyped<int> alloc = b2CreateArenaAllocator<int>(10);
27+
28+
alloc.maxAllocation = 15;
29+
int oldCapacity = alloc.capacity;
30+
int newCapacity = alloc.Grow();
31+
32+
Assert.That(newCapacity, Is.GreaterThan(oldCapacity));
33+
Assert.That(newCapacity, Is.EqualTo(15 + 15 / 2)); // Check the growth factor
34+
Assert.That(alloc.data.Count, Is.EqualTo(newCapacity));
35+
Assert.That(alloc.allocation, Is.EqualTo(0)); // Allocation should be 0 before grow
36+
}
37+
38+
[Test]
39+
public void Grow_DoesNotIncreaseCapacityWhenMaxAllocationIsWithinCurrent()
40+
{
41+
B2ArenaAllocatorTyped<int> alloc = b2CreateArenaAllocator<int>(10);
42+
43+
alloc.maxAllocation = 5;
44+
int oldCapacity = alloc.capacity;
45+
int newCapacity = alloc.Grow();
46+
47+
Assert.That(newCapacity, Is.EqualTo(oldCapacity));
48+
Assert.That(alloc.data.Count, Is.EqualTo(newCapacity));
49+
Assert.That(alloc.allocation, Is.EqualTo(0)); // Allocation should be 0 before grow
50+
}
51+
52+
[Test]
53+
public void Destroy_ReleasesResourcesAndResetsProperties()
54+
{
55+
B2ArenaAllocatorTyped<int> alloc = b2CreateArenaAllocator<int>(10);
56+
57+
alloc.Destroy();
58+
59+
Assert.That(alloc.data.Array, Is.Null);
60+
Assert.That(alloc.capacity, Is.EqualTo(0));
61+
Assert.That(alloc.index, Is.EqualTo(0));
62+
Assert.That(alloc.allocation, Is.EqualTo(0));
63+
Assert.That(alloc.maxAllocation, Is.EqualTo(0));
64+
Assert.That(alloc.entries.data, Is.Null);
65+
Assert.That(alloc.entries.count, Is.EqualTo(0));
66+
Assert.That(alloc.entries.capacity, Is.EqualTo(0));
67+
}
68+
}

0 commit comments

Comments
 (0)