1+ using System . Threading ;
2+ using System . Threading . Tasks ;
3+ using NUnit . Framework ;
4+
5+ namespace Box2D . NET . Test ;
6+
7+ public class B2ArenaAllocatorTests
8+ {
9+ [ Test ]
10+ public void GetOrCreateFor_CreatesAllocatorForType ( )
11+ {
12+ // Arrange
13+ var arena = new B2ArenaAllocator ( 10 ) ;
14+
15+ // Act
16+ var typedAllocator = arena . GetOrCreateFor < int > ( ) ;
17+
18+ // Assert
19+ Assert . That ( typedAllocator , Is . Not . Null , "Allocator should be created for the specified type." ) ;
20+ Assert . That ( typedAllocator , Is . AssignableFrom < B2ArenaAllocatorTyped < int > > ( ) , "Returned allocator should be of the correct type." ) ;
21+ }
22+
23+ [ Test ]
24+ public void GetOrCreateFor_ReturnsSameAllocatorForSameType ( )
25+ {
26+ // Arrange
27+ var arena = new B2ArenaAllocator ( 10 ) ;
28+
29+ // Act
30+ var typedAllocator1 = arena . GetOrCreateFor < int > ( ) ;
31+ var typedAllocator2 = arena . GetOrCreateFor < int > ( ) ;
32+
33+ // Assert
34+ Assert . That ( typedAllocator1 , Is . SameAs ( typedAllocator2 ) , "Allocator should be reused for the same type." ) ;
35+ }
36+
37+ [ Test ]
38+ public void GetOrCreateFor_AllocatesForDifferentTypes ( )
39+ {
40+ // Arrange
41+ var arena = new B2ArenaAllocator ( 10 ) ;
42+
43+ // Act
44+ IB2ArenaAllocatable typedAllocator1 = arena . GetOrCreateFor < int > ( ) ;
45+ Assert . That ( arena . Count , Is . EqualTo ( 1 ) ) ;
46+
47+ IB2ArenaAllocatable typedAllocator2 = arena . GetOrCreateFor < uint > ( ) ;
48+ Assert . That ( arena . Count , Is . EqualTo ( 2 ) ) ;
49+
50+ // Assert
51+ Assert . That ( typedAllocator1 , Is . Not . SameAs ( typedAllocator2 ) , "Different types should result in different allocators." ) ;
52+ }
53+
54+ [ Test ]
55+ public void AsSpan_ReturnsAllocatorsSpan ( )
56+ {
57+ // Arrange
58+ var arena = new B2ArenaAllocator ( 10 ) ;
59+ IB2ArenaAllocatable first = arena . GetOrCreateFor < int > ( ) ;
60+ IB2ArenaAllocatable second = arena . GetOrCreateFor < float > ( ) ;
61+ IB2ArenaAllocatable third = arena . GetOrCreateFor < double > ( ) ;
62+
63+ // Act
64+ var span = arena . AsSpan ( ) ;
65+
66+ // Assert
67+ Assert . That ( span . Length , Is . EqualTo ( 3 ) , "Span should contain at least one allocator." ) ;
68+ Assert . That ( span [ 0 ] , Is . EqualTo ( first ) ) ;
69+ Assert . That ( span [ 1 ] , Is . EqualTo ( second ) ) ;
70+ Assert . That ( span [ 2 ] , Is . EqualTo ( third ) ) ;
71+ }
72+
73+ [ Test ]
74+ public void GetOrCreateFor_ShouldBeThreadSafe_WhenCalledConcurrently ( )
75+ {
76+ // Arrange
77+ var arena = new B2ArenaAllocator ( 10 ) ;
78+ var tasks = new Task [ 100 ] ;
79+ var typedAllocators = new IB2ArenaAllocatable [ 100 ] ;
80+ var ce = new CountdownEvent ( 1 ) ;
81+
82+ // Act: Run 100 tasks concurrently
83+ for ( int i = 0 ; i < 100 ; i ++ )
84+ {
85+ int idx = i ;
86+ tasks [ i ] = Task . Run ( ( ) =>
87+ {
88+ ce . Wait ( 1000 ) ;
89+ // Multiple threads trying to get or create an allocator for MyType
90+ var allocator1 = arena . GetOrCreateFor < byte > ( ) ;
91+ var allocator2 = arena . GetOrCreateFor < char > ( ) ;
92+ var allocator3 = arena . GetOrCreateFor < short > ( ) ;
93+ var allocator4 = arena . GetOrCreateFor < int > ( ) ;
94+ var allocator5 = arena . GetOrCreateFor < long > ( ) ;
95+ var allocator6 = arena . GetOrCreateFor < ushort > ( ) ;
96+ var allocator7 = arena . GetOrCreateFor < uint > ( ) ;
97+ var allocator8 = arena . GetOrCreateFor < ulong > ( ) ;
98+ var allocator9 = arena . GetOrCreateFor < float > ( ) ;
99+ var allocator10 = arena . GetOrCreateFor < double > ( ) ;
100+ typedAllocators [ idx ] = allocator1 ;
101+ } ) ;
102+ }
103+
104+ ce . Signal ( ) ;
105+
106+
107+ // Wait for all tasks to complete
108+ Task . WhenAll ( tasks ) . Wait ( ) ;
109+
110+ // Assert: Ensure all threads got the same allocator instance for MyType
111+ for ( int i = 1 ; i < typedAllocators . Length ; i ++ )
112+ {
113+ Assert . That ( typedAllocators [ i ] , Is . SameAs ( typedAllocators [ 0 ] ) , "Allocator instance should be shared among threads." ) ;
114+ }
115+
116+ Assert . That ( arena . Count , Is . EqualTo ( 10 ) ) ;
117+ }
118+ }
0 commit comments