Skip to content

Commit 89213bf

Browse files
author
Vyacheslav
committed
feat: test ReducingCapacity head after tail
1 parent 483d93a commit 89213bf

File tree

3 files changed

+109
-25
lines changed

3 files changed

+109
-25
lines changed

Src/StackMemoryCollections/GenerateMemory.cs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,7 @@ in StringBuilder builder
292292
builder.Append($@"
293293
public void ShiftLeft(
294294
in byte* start,
295+
in byte* end,
295296
long bytesShift
296297
)
297298
{{
@@ -300,10 +301,16 @@ long bytesShift
300301
throw new ObjectDisposedException(nameof(StackMemory));
301302
}}
302303
303-
var newStart = start - (bytesShift - 1);
304-
for(int i = 0; i > bytesShift; i++)
304+
var current = start - bytesShift;
305+
var sd = *start;
306+
var newEnd = end - bytesShift;
307+
while(current != newEnd)
305308
{{
306-
newStart[i] = start[i];
309+
var ss = *current;
310+
var ss2 = *(current + bytesShift);
311+
312+
*current = *(current + bytesShift);
313+
current += 1;
307314
}}
308315
}}
309316
");

Src/StackMemoryCollections/GeneratePrimitiveQueue.cs

Lines changed: 46 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using Microsoft.CodeAnalysis;
22
using System;
3+
using System.IO;
34
using System.Text;
45

56
namespace StackMemoryCollections
@@ -348,23 +349,35 @@ public void ReducingCapacity(in nuint reducingCount)
348349
throw new Exception(""Failed to reduce available memory, stack moved further"");
349350
}}
350351
351-
if(_head > _tail && Capacity - _head + 1 < reducingCount)
352+
if(Size == 0)
352353
{{
353-
_stackMemoryS->ShiftLeft((byte*)_start + _head, (long)(reducingCount * {sizeOf}));
354+
_head = 0;
355+
_tail = 0;
356+
}}
357+
else
358+
if(_head > _tail)
359+
{{
360+
_stackMemoryS->ShiftLeft((byte*)(_start + _head), (byte*)(_start + (Capacity)), (long)(reducingCount * {sizeOf}));
354361
_head -= reducingCount;{incrementVersion}
355362
}}
356363
else
357364
if ((_head < _tail && Capacity - _tail + 1 < reducingCount) ||
358365
(_head == _tail && Capacity - _tail + 1 < reducingCount)
359366
)
360367
{{
361-
_stackMemoryS->ShiftLeft((byte*)_start + _tail, (long)(reducingCount * {sizeOf}));
362-
_tail -= reducingCount;{incrementVersion}
363-
}}
364-
else
365-
{{
366-
_stackMemoryS->FreeMemory(reducingCount * {sizeOf});
368+
var freeCountToEnd = Capacity - (_head + 1);
369+
if(freeCountToEnd != 0 && freeCountToEnd <= reducingCount)
370+
{{
371+
//do nothing
372+
}}
373+
else
374+
{{
375+
_stackMemoryS->ShiftLeft((byte*)(_start + _head), (byte*)(_start + (_tail + 1)), (long)((Capacity - freeCountToEnd - ((_tail + 1) - _head)) * {sizeOf}));
376+
_tail = 0;{incrementVersion}
377+
}}
367378
}}
379+
380+
_stackMemoryS->FreeMemory(reducingCount * {sizeOf});
368381
}}
369382
else if (_stackMemoryC != null)
370383
{{
@@ -373,23 +386,35 @@ public void ReducingCapacity(in nuint reducingCount)
373386
throw new Exception(""Failed to reduce available memory, stack moved further"");
374387
}}
375388
376-
if (_head > _tail && Capacity - _head + 1 < reducingCount)
389+
if(Size == 0)
377390
{{
378-
_stackMemoryC.ShiftLeft((byte*)_start + _head, (long)(reducingCount * {sizeOf}));
391+
_head = 0;
392+
_tail = 0;
393+
}}
394+
else
395+
if(_head > _tail)
396+
{{
397+
_stackMemoryC.ShiftLeft((byte*)(_start + _head), (byte*)(_start + (Capacity * {sizeOf})), (long)(reducingCount * {sizeOf}));
379398
_head -= reducingCount;{incrementVersion}
380399
}}
381400
else
382401
if ((_head < _tail && Capacity - _tail + 1 < reducingCount) ||
383402
(_head == _tail && Capacity - _tail + 1 < reducingCount)
384403
)
385404
{{
386-
_stackMemoryC.ShiftLeft((byte*)_start + _tail, (long)(reducingCount * {sizeOf}));
387-
_tail -= reducingCount;{incrementVersion}
388-
}}
389-
else
390-
{{
391-
_stackMemoryC.FreeMemory(reducingCount * {sizeOf});
392-
}}
405+
var freeCountToEnd = Capacity - (_head + 1);
406+
if(freeCountToEnd != 0 && freeCountToEnd <= reducingCount)
407+
{{
408+
//do nothing
409+
}}
410+
else
411+
{{
412+
_stackMemoryC.ShiftLeft((byte*)(_start + _head), (byte*)(_start + (_tail + 1)), (long)((Capacity - freeCountToEnd - ((_tail + 1) - _head)) * {sizeOf}));
413+
_tail = 0;{incrementVersion}
414+
}}
415+
}}
416+
417+
_stackMemoryC.FreeMemory(reducingCount * {sizeOf});
393418
}}
394419
}}
395420
@@ -468,7 +493,7 @@ public void ExpandCapacity(in nuint expandCount)
468493
_stackMemoryS->AllocateMemory(expandCount * {sizeOf});
469494
if(Size != 0 && _head != 0 && _head > _tail)
470495
{{
471-
_stackMemoryS->ShiftRight((byte*)_start + _head, (byte*)_start + ((_head + (Capacity - _head)) * {sizeOf}), (long)(expandCount * {sizeOf}));{incrementVersion}
496+
_stackMemoryS->ShiftRight((byte*)(_start + _head), (byte*)(_start + (_head + (Capacity - _head))), (long)(expandCount * {sizeOf}));{incrementVersion}
472497
_head += expandCount;
473498
}}
474499
}}
@@ -482,7 +507,7 @@ public void ExpandCapacity(in nuint expandCount)
482507
_stackMemoryC.AllocateMemory(expandCount * {sizeOf});
483508
if (Size != 0 && _head != 0 && _head > _tail)
484509
{{
485-
_stackMemoryC.ShiftRight((byte*)_start + _head, (byte*)_start + ((_head + (Capacity - _head)) * {sizeOf}), (long)(expandCount * {sizeOf}));{incrementVersion}
510+
_stackMemoryC.ShiftRight((byte*)(_start + _head), (byte*)(_start + (_head + (Capacity - _head))), (long)(expandCount * {sizeOf}));{incrementVersion}
486511
_head += expandCount;
487512
}}
488513
}}
@@ -567,7 +592,7 @@ public bool TryExpandCapacity(in nuint expandCount)
567592
568593
if(Size != 0 && _head != 0 && _head > _tail)
569594
{{
570-
_stackMemoryS->ShiftRight((byte*)_start + _head, (byte*)_start + ((_head + (Capacity - _head)) * {sizeOf}), (long)(expandCount * {sizeOf}));{incrementVersion}
595+
_stackMemoryS->ShiftRight((byte*)(_start + _head), (byte*)(_start + (_head + (Capacity - _head))), (long)(expandCount * {sizeOf}));{incrementVersion}
571596
_head += expandCount;
572597
}}
573598
}}
@@ -585,7 +610,7 @@ public bool TryExpandCapacity(in nuint expandCount)
585610
586611
if (Size != 0 && _head != 0 && _head > _tail)
587612
{{
588-
_stackMemoryC.ShiftRight((byte*)_start + _head, (byte*)_start + ((_head + (Capacity - _head)) * {sizeOf}), (long)(expandCount * {sizeOf}));{incrementVersion}
613+
_stackMemoryC.ShiftRight((byte*)(_start + _head), (byte*)(_start + (_head + (Capacity - _head))), (long)(expandCount * {sizeOf}));{incrementVersion}
589614
_head += expandCount;
590615
}}
591616
}}

Src/TestGenerator/GenerateQueuePrimitiveTest.cs

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,7 @@ in Func<T, string> toStr
289289
QueuePrimitiveExpandCapacityHeadBeforeTailOwn(in values, in builder, in queueNamespace, in toStr);
290290

291291
QueuePrimitiveReducingCapacity(in values, in builder, in queueNamespace);
292+
QueuePrimitiveReducingCapacityHeadAfterTail(in values, in builder, in queueNamespace, in toStr);
292293

293294
QueuePrimitiveSize(in values, in builder, in queueNamespace, in toStr);
294295
QueuePrimitiveCapacity(in values, in builder, in queueNamespace, in toStr);
@@ -306,7 +307,7 @@ in Func<T, string> toStr
306307
QueuePrimitiveEnd(in builder);
307308

308309
context.AddSource($"QueueOf{typeof(T).Name}{queueNamespace}Fixture.g.cs", builder.ToString());
309-
File.WriteAllText($"E:\\Work\\OutTrash\\QueueOf{typeof(T).Name}{queueNamespace}Fixture.g.cs", builder.ToString());
310+
//File.WriteAllText($"E:\\Work\\OutTrash\\QueueOf{typeof(T).Name}{queueNamespace}Fixture.g.cs", builder.ToString());
310311
}
311312

312313
private void QueuePrimitiveStart<T>(
@@ -1317,6 +1318,57 @@ public void ReducingCapacityTest()
13171318
");
13181319
}
13191320

1321+
private void QueuePrimitiveReducingCapacityHeadAfterTail<T>(
1322+
in List<T> values,
1323+
in StringBuilder builder,
1324+
in string queueNamespace,
1325+
in Func<T, string> toStr
1326+
) where T : unmanaged
1327+
{
1328+
if (values.Count < 5)
1329+
{
1330+
throw new ArgumentException($"{nameof(values)} Must have minimum 5 values to generate tests");
1331+
}
1332+
1333+
builder.Append($@"
1334+
[Test]
1335+
public void ReducingCapacityHeadAfterTailTest()
1336+
{{
1337+
unsafe
1338+
{{
1339+
using (var memory = new StackMemoryCollections.Struct.StackMemory(sizeof({typeof(T).Name}) * {values.Count}))
1340+
{{
1341+
var queue = new StackMemoryCollections.{queueNamespace}.QueueOf{typeof(T).Name}({values.Count}, &memory);
1342+
");
1343+
for (int i = 0; i < values.Count; i++)
1344+
{
1345+
builder.Append($@"
1346+
queue.Push({toStr(values[i])});
1347+
");
1348+
}
1349+
1350+
builder.Append($@"
1351+
queue.Pop();
1352+
queue.Pop();
1353+
queue.Push({toStr(values[values.Count - 1])});
1354+
queue.ReducingCapacity(1);
1355+
");
1356+
for (int i = 2; i < values.Count; i++)
1357+
{
1358+
builder.Append($@"
1359+
Assert.That(queue.Front(), Is.EqualTo({toStr(values[i])}));
1360+
queue.Pop();
1361+
");
1362+
}
1363+
builder.Append($@"
1364+
Assert.That(queue.Front(), Is.EqualTo({toStr(values[values.Count - 1])}));
1365+
queue.Pop();
1366+
}}
1367+
}}
1368+
}}
1369+
");
1370+
}
1371+
13201372
private void QueuePrimitiveSize<T>(
13211373
in List<T> values,
13221374
in StringBuilder builder,

0 commit comments

Comments
 (0)