Skip to content

Commit 998a937

Browse files
committed
added test CreateProxy/DestroyProxy in b2BroadPhase
1 parent 8f49218 commit 998a937

File tree

4 files changed

+102
-4
lines changed

4 files changed

+102
-4
lines changed

test/Box2D.NET.Test/B2BoardPhasesTests.cs

Lines changed: 79 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ public void Test_B2BoardPhases_b2UnBufferMove()
9191
{
9292
B2BroadPhase bp = null;
9393
b2CreateBroadPhase(ref bp);
94-
94+
9595
int proxyKeyA = 42;
9696
int proxyKeyB = 99;
9797
int proxyKeyC = 88;
@@ -135,7 +135,84 @@ public void Test_B2BoardPhases_b2UnBufferMove()
135135
Assert.That(b2ContainsKey(ref bp.moveSet, (ulong)proxyKeyA + 1), Is.False);
136136
Assert.That(b2ContainsKey(ref bp.moveSet, (ulong)proxyKeyB + 1), Is.False);
137137
Assert.That(b2ContainsKey(ref bp.moveSet, (ulong)proxyKeyC + 1), Is.False);
138-
138+
139139
Assert.That(bp.moveArray.count, Is.EqualTo(0));
140140
}
141+
142+
[Test]
143+
public void Test_B2BoardPhases_b2BroadPhase_CreateProxy()
144+
{
145+
B2BroadPhase bp = null;
146+
b2CreateBroadPhase(ref bp);
147+
148+
var aabb = new B2AABB
149+
{
150+
lowerBound = new B2Vec2(0, 0),
151+
upperBound = new B2Vec2(1, 1)
152+
};
153+
ulong categoryBits = 0x0001;
154+
int shapeIndex = 123;
155+
156+
// case 1: static body, forcePairCreation = false
157+
int proxyKeyA = b2BroadPhase_CreateProxy(bp, B2BodyType.b2_staticBody, aabb, categoryBits, shapeIndex, false);
158+
159+
Assert.That(bp.moveSet.count, Is.EqualTo(0), "Should not add to moveSet if static body and forcePairCreation == false");
160+
Assert.That(bp.moveArray.count, Is.EqualTo(0), "Should not add to moveArray if static body and forcePairCreation == false");
161+
162+
// case 2: static body, forcePairCreation = true
163+
int proxyKeyB = b2BroadPhase_CreateProxy(bp, B2BodyType.b2_staticBody, aabb, categoryBits, shapeIndex + 1, true);
164+
165+
Assert.That(bp.moveSet.count, Is.EqualTo(1), "Should add to moveSet if static body but forcePairCreation == true");
166+
Assert.That(bp.moveArray.count, Is.EqualTo(1), "Should add to moveArray if static body but forcePairCreation == true");
167+
Assert.That(bp.moveArray.data, Does.Contain(proxyKeyB), "moveArray should contain proxyKeyB");
168+
169+
// case 3: dynamic body, forcePairCreation irrelevant
170+
int proxyKeyC = b2BroadPhase_CreateProxy(bp, B2BodyType.b2_dynamicBody, aabb, categoryBits, shapeIndex + 2, false);
171+
172+
Assert.That(bp.moveSet.count, Is.EqualTo(2), "Should add to moveSet if body is dynamic");
173+
Assert.That(bp.moveArray.count, Is.EqualTo(2), "Should add to moveArray if body is dynamic");
174+
Assert.That(bp.moveArray.data, Does.Contain(proxyKeyC), "moveArray should contain proxyKeyC");
175+
}
176+
177+
[Test]
178+
public void Test_B2BoardPhases_b2BroadPhase_DestroyProxy()
179+
{
180+
B2BroadPhase bp = null;
181+
b2CreateBroadPhase(ref bp);
182+
183+
var aabb = new B2AABB
184+
{
185+
lowerBound = new B2Vec2(0, 0),
186+
upperBound = new B2Vec2(1, 1)
187+
};
188+
ulong categoryBits = 0x0001;
189+
int shapeIndex = 123;
190+
191+
// Create and buffer proxies
192+
int proxyKeyA = b2BroadPhase_CreateProxy(bp, B2BodyType.b2_dynamicBody, aabb, categoryBits, shapeIndex, false);
193+
int proxyKeyB = b2BroadPhase_CreateProxy(bp, B2BodyType.b2_dynamicBody, aabb, categoryBits, shapeIndex + 1, false);
194+
195+
Assert.That(bp.moveSet.count, Is.EqualTo(2), "moveSet should have 2 items before destroying proxies");
196+
Assert.That(bp.moveArray.count, Is.EqualTo(2), "moveArray should have 2 items before destroying proxies");
197+
Assert.That(bp.proxyCount, Is.EqualTo(0));
198+
199+
// Destroy proxy A
200+
b2BroadPhase_DestroyProxy(bp, proxyKeyA);
201+
202+
Assert.That(bp.moveSet.count, Is.EqualTo(1), "moveSet should have 1 item after destroying proxy A");
203+
Assert.That(bp.moveArray.count, Is.EqualTo(1), "moveArray should have 1 item after destroying proxy A");
204+
Assert.That(bp.proxyCount, Is.EqualTo(-1), "proxyCount should decrease after destroying a proxy");
205+
206+
// Destroy proxy B
207+
b2BroadPhase_DestroyProxy(bp, proxyKeyB);
208+
209+
Assert.That(bp.moveSet.count, Is.EqualTo(0), "moveSet should have 0 items after destroying proxy B");
210+
Assert.That(bp.moveArray.count, Is.EqualTo(0), "moveArray should have 0 items after destroying proxy B");
211+
Assert.That(bp.proxyCount, Is.EqualTo(-2), "proxyCount should decrease to 0 after destroying all proxies");
212+
213+
#if DEBUG
214+
// Case 3: Destroy a non-existent proxy
215+
Assert.Throws<InvalidOperationException>(() => b2BroadPhase_DestroyProxy(bp, proxyKeyB), "Destroying a non-existent proxy should throw an exception");
216+
#endif
217+
}
141218
}

test/Box2D.NET.Test/B2DeterminismTest.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public void Dispose()
3838
_semaphore.Dispose();
3939
_semaphore = null;
4040

41-
Debug.Assert(0 >= _runningTasks.Count);
41+
B2_ASSERT(0 >= _runningTasks.Count);
4242
}
4343

4444
private IEnumerable<int> Next(int itemCount, int minRange)
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
using System.Runtime.InteropServices;
2+
using NUnit.Framework;
3+
using static Box2D.NET.B2Tables;
4+
5+
namespace Box2D.NET.Test;
6+
7+
public class B2TableTests
8+
{
9+
[Test]
10+
public void Test_B2Tables_b2GetHashSetBytes()
11+
{
12+
int size = Marshal.SizeOf<B2SetItem>();
13+
const int capacity = 1024;
14+
15+
B2HashSet set = b2CreateSet(capacity);
16+
set.capacity = capacity;
17+
18+
int bytes = b2GetHashSetBytes(ref set);
19+
Assert.That(bytes, Is.EqualTo(size * capacity));
20+
}
21+
}

test/Box2D.NET.Test/B2WorldTest.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ public void TestIsValid()
211211
[Test]
212212
public void TestWorldRecycle()
213213
{
214-
Debug.Assert(WORLD_COUNT > 0, "world count");
214+
B2_ASSERT(WORLD_COUNT > 0, "world count");
215215

216216
int count = 100;
217217

0 commit comments

Comments
 (0)