1+ using NUnit . Framework ;
2+ using System ;
3+
4+ namespace Tests
5+ {
6+ [ TestFixture ]
7+ public class MemoryFixture
8+ {
9+ [ Test ]
10+ public void AllocateTest ( )
11+ {
12+ unsafe
13+ {
14+ using ( var memory = new StackMemoryCollections . Struct . StackMemory ( 6 ) )
15+ {
16+ var start = memory . Start ;
17+
18+ Assert . That ( memory . ByteCount , Is . EqualTo ( ( nuint ) 6 ) ) ;
19+ Assert . That ( memory . FreeByteCount , Is . EqualTo ( ( nuint ) 6 ) ) ;
20+ Assert . That ( new IntPtr ( memory . Current ) , Is . EqualTo ( new IntPtr ( ( byte * ) memory . Start ) ) ) ;
21+
22+ var current0 = memory . Current ;
23+ var mPtr0 = memory . AllocateMemory ( 1 ) ;
24+ Assert . That ( memory . ByteCount , Is . EqualTo ( ( nuint ) 6 ) ) ;
25+ Assert . That ( memory . FreeByteCount , Is . EqualTo ( ( nuint ) 5 ) ) ;
26+ Assert . That ( new IntPtr ( current0 ) , Is . EqualTo ( new IntPtr ( mPtr0 ) ) ) ;
27+ Assert . That ( new IntPtr ( start ) , Is . EqualTo ( new IntPtr ( memory . Start ) ) ) ;
28+ Assert . That ( new IntPtr ( memory . Current ) , Is . EqualTo ( new IntPtr ( ( byte * ) memory . Start + 1 ) ) ) ;
29+
30+ var current1 = memory . Current ;
31+ var mPtr1 = memory . AllocateMemory ( 5 ) ;
32+ Assert . That ( memory . ByteCount , Is . EqualTo ( ( nuint ) 6 ) ) ;
33+ Assert . That ( memory . FreeByteCount , Is . EqualTo ( ( nuint ) 0 ) ) ;
34+ Assert . That ( new IntPtr ( current1 ) , Is . EqualTo ( new IntPtr ( mPtr1 ) ) ) ;
35+ Assert . That ( new IntPtr ( start ) , Is . EqualTo ( new IntPtr ( memory . Start ) ) ) ;
36+ Assert . That ( new IntPtr ( memory . Current ) , Is . EqualTo ( new IntPtr ( ( byte * ) memory . Start + 6 ) ) ) ;
37+
38+ Assert . That ( ( ) => memory . AllocateMemory ( 1 ) ,
39+ Throws . Exception . TypeOf ( typeof ( ArgumentException ) )
40+ . And . Message . EqualTo ( "Can't allocate memory" )
41+ ) ;
42+ Assert . That ( memory . ByteCount , Is . EqualTo ( ( nuint ) 6 ) ) ;
43+ Assert . That ( memory . FreeByteCount , Is . EqualTo ( ( nuint ) 0 ) ) ;
44+ Assert . That ( new IntPtr ( current1 ) , Is . EqualTo ( new IntPtr ( mPtr1 ) ) ) ;
45+ Assert . That ( new IntPtr ( start ) , Is . EqualTo ( new IntPtr ( memory . Start ) ) ) ;
46+ Assert . That ( new IntPtr ( memory . Current ) , Is . EqualTo ( new IntPtr ( ( byte * ) memory . Start + 6 ) ) ) ;
47+ }
48+ }
49+ }
50+
51+ [ Test ]
52+ public void TryAllocateTest ( )
53+ {
54+ unsafe
55+ {
56+ using ( var memory = new StackMemoryCollections . Struct . StackMemory ( 6 ) )
57+ {
58+ var start = memory . Start ;
59+
60+ Assert . That ( memory . ByteCount , Is . EqualTo ( ( nuint ) 6 ) ) ;
61+ Assert . That ( memory . FreeByteCount , Is . EqualTo ( ( nuint ) 6 ) ) ;
62+ Assert . That ( new IntPtr ( memory . Current ) , Is . EqualTo ( new IntPtr ( ( byte * ) memory . Start ) ) ) ;
63+
64+ var current0 = memory . Current ;
65+ var result0 = memory . TryAllocateMemory ( 1 , out var mPtr0 ) ;
66+ Assert . That ( result0 , Is . EqualTo ( true ) ) ;
67+ Assert . That ( memory . ByteCount , Is . EqualTo ( ( nuint ) 6 ) ) ;
68+ Assert . That ( memory . FreeByteCount , Is . EqualTo ( ( nuint ) 5 ) ) ;
69+ Assert . That ( new IntPtr ( current0 ) , Is . EqualTo ( new IntPtr ( mPtr0 ) ) ) ;
70+ Assert . That ( new IntPtr ( start ) , Is . EqualTo ( new IntPtr ( memory . Start ) ) ) ;
71+ Assert . That ( new IntPtr ( memory . Current ) , Is . EqualTo ( new IntPtr ( ( byte * ) memory . Start + 1 ) ) ) ;
72+
73+ var current1 = memory . Current ;
74+ var result1 = memory . TryAllocateMemory ( 5 , out var mPtr1 ) ;
75+ Assert . That ( result1 , Is . EqualTo ( true ) ) ;
76+ Assert . That ( memory . ByteCount , Is . EqualTo ( ( nuint ) 6 ) ) ;
77+ Assert . That ( memory . FreeByteCount , Is . EqualTo ( ( nuint ) 0 ) ) ;
78+ Assert . That ( new IntPtr ( current1 ) , Is . EqualTo ( new IntPtr ( mPtr1 ) ) ) ;
79+ Assert . That ( new IntPtr ( start ) , Is . EqualTo ( new IntPtr ( memory . Start ) ) ) ;
80+ Assert . That ( new IntPtr ( memory . Current ) , Is . EqualTo ( new IntPtr ( ( byte * ) memory . Start + 6 ) ) ) ;
81+
82+ var current2 = memory . Current ;
83+ var result2 = memory . TryAllocateMemory ( 5 , out var mPtr2 ) ;
84+ Assert . That ( result2 , Is . EqualTo ( false ) ) ;
85+ Assert . That ( memory . ByteCount , Is . EqualTo ( ( nuint ) 6 ) ) ;
86+ Assert . That ( memory . FreeByteCount , Is . EqualTo ( ( nuint ) 0 ) ) ;
87+ Assert . That ( new IntPtr ( current2 ) , Is . EqualTo ( new IntPtr ( memory . Current ) ) ) ;
88+ Assert . That ( new IntPtr ( start ) , Is . EqualTo ( new IntPtr ( memory . Start ) ) ) ;
89+ Assert . That ( new IntPtr ( mPtr2 ) , Is . EqualTo ( IntPtr . Zero ) ) ;
90+ }
91+ }
92+ }
93+
94+ [ Test ]
95+ public void FreeTest ( )
96+ {
97+ unsafe
98+ {
99+ using ( var memory = new StackMemoryCollections . Struct . StackMemory ( 6 ) )
100+ {
101+ memory . AllocateMemory ( 6 ) ;
102+
103+ var end = memory . Current ;
104+ memory . FreeMemory ( 1 ) ;
105+ Assert . That ( memory . ByteCount , Is . EqualTo ( ( nuint ) 6 ) ) ;
106+ Assert . That ( memory . FreeByteCount , Is . EqualTo ( ( nuint ) 1 ) ) ;
107+ Assert . That ( new IntPtr ( memory . Current ) , Is . EqualTo ( new IntPtr ( ( byte * ) end - 1 ) ) ) ;
108+
109+ memory . FreeMemory ( 5 ) ;
110+ Assert . That ( memory . ByteCount , Is . EqualTo ( ( nuint ) 6 ) ) ;
111+ Assert . That ( memory . FreeByteCount , Is . EqualTo ( ( nuint ) 6 ) ) ;
112+ Assert . That ( new IntPtr ( memory . Current ) , Is . EqualTo ( new IntPtr ( ( byte * ) end - 6 ) ) ) ;
113+
114+ var current0 = memory . Current ;
115+ Assert . That ( ( ) => memory . FreeMemory ( 1 ) ,
116+ Throws . Exception . TypeOf ( typeof ( Exception ) )
117+ . And . Message . EqualTo ( "Unable to free memory, it is out of available memory" )
118+ ) ;
119+ Assert . That ( memory . ByteCount , Is . EqualTo ( ( nuint ) 6 ) ) ;
120+ Assert . That ( memory . FreeByteCount , Is . EqualTo ( ( nuint ) 6 ) ) ;
121+ Assert . That ( new IntPtr ( memory . Current ) , Is . EqualTo ( new IntPtr ( current0 ) ) ) ;
122+ }
123+ }
124+ }
125+
126+ [ Test ]
127+ public void TryFreeTest ( )
128+ {
129+ unsafe
130+ {
131+ using ( var memory = new StackMemoryCollections . Struct . StackMemory ( 6 ) )
132+ {
133+ memory . AllocateMemory ( 6 ) ;
134+
135+ var end = memory . Current ;
136+ var result0 = memory . TryFreeMemory ( 1 ) ;
137+ Assert . That ( result0 , Is . EqualTo ( true ) ) ;
138+ Assert . That ( memory . ByteCount , Is . EqualTo ( ( nuint ) 6 ) ) ;
139+ Assert . That ( memory . FreeByteCount , Is . EqualTo ( ( nuint ) 1 ) ) ;
140+ Assert . That ( new IntPtr ( memory . Current ) , Is . EqualTo ( new IntPtr ( ( byte * ) end - 1 ) ) ) ;
141+
142+ var result1 = memory . TryFreeMemory ( 5 ) ;
143+ Assert . That ( result1 , Is . EqualTo ( true ) ) ;
144+ Assert . That ( memory . ByteCount , Is . EqualTo ( ( nuint ) 6 ) ) ;
145+ Assert . That ( memory . FreeByteCount , Is . EqualTo ( ( nuint ) 6 ) ) ;
146+ Assert . That ( new IntPtr ( memory . Current ) , Is . EqualTo ( new IntPtr ( ( byte * ) end - 6 ) ) ) ;
147+
148+ var current0 = memory . Current ;
149+ var result2 = memory . TryFreeMemory ( 5 ) ;
150+ Assert . That ( result2 , Is . EqualTo ( false ) ) ;
151+ Assert . That ( memory . ByteCount , Is . EqualTo ( ( nuint ) 6 ) ) ;
152+ Assert . That ( memory . FreeByteCount , Is . EqualTo ( ( nuint ) 6 ) ) ;
153+ Assert . That ( new IntPtr ( memory . Current ) , Is . EqualTo ( new IntPtr ( current0 ) ) ) ;
154+ }
155+ }
156+ }
157+ }
158+ }
0 commit comments