Skip to content

Commit b83da92

Browse files
author
Vyacheslav
committed
feat: test ReducingCapacity head before tail Own
1 parent 1eb33a0 commit b83da92

File tree

2 files changed

+77
-18
lines changed

2 files changed

+77
-18
lines changed

Src/StackMemoryCollections/GeneratePrimitiveQueue.cs

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -361,19 +361,22 @@ public void ReducingCapacity(in nuint reducingCount)
361361
_head -= reducingCount;{incrementVersion}
362362
}}
363363
else
364-
if ((_head < _tail && Capacity - _tail + 1 < reducingCount) ||
365-
(_head == _tail && Capacity - _tail + 1 < reducingCount)
366-
)
364+
if (_head <= _tail)
367365
{{
368-
var freeCountToEnd = Capacity - (_head + 1);
369-
if(freeCountToEnd != 0 && freeCountToEnd <= reducingCount)
366+
if(Size == 1)
370367
{{
371-
//do nothing
368+
*(_start) = *(_start + _tail);
369+
_tail = 0;
370+
_head = 0;
372371
}}
373372
else
374373
{{
375-
_stackMemoryS->ShiftLeft((byte*)(_start + _head), (byte*)(_start + (_tail + 1)), (long)((Capacity - freeCountToEnd - ((_tail + 1) - _head)) * {sizeOf}));
376-
_tail = 0;{incrementVersion}
374+
var freeCountToEnd = Capacity - (_tail + 1);
375+
if(freeCountToEnd == 0 || freeCountToEnd < reducingCount)
376+
{{
377+
_stackMemoryS->ShiftLeft((byte*)(_start + _head), (byte*)(_start + (_tail + 1)), (long)((Capacity - freeCountToEnd - ((_tail + 1) - _head)) * {sizeOf}));
378+
_head = 0;{incrementVersion}
379+
}}
377380
}}
378381
}}
379382
@@ -398,19 +401,22 @@ public void ReducingCapacity(in nuint reducingCount)
398401
_head -= reducingCount;{incrementVersion}
399402
}}
400403
else
401-
if ((_head < _tail && Capacity - _tail + 1 < reducingCount) ||
402-
(_head == _tail && Capacity - _tail + 1 < reducingCount)
403-
)
404+
if (_head <= _tail)
404405
{{
405-
var freeCountToEnd = Capacity - (_head + 1);
406-
if(freeCountToEnd != 0 && freeCountToEnd <= reducingCount)
406+
if(Size == 1)
407407
{{
408-
//do nothing
408+
*(_start) = *(_start + _tail);
409+
_tail = 0;
410+
_head = 0;
409411
}}
410412
else
411413
{{
412-
_stackMemoryC.ShiftLeft((byte*)(_start + _head), (byte*)(_start + (_tail + 1)), (long)((Capacity - freeCountToEnd - ((_tail + 1) - _head)) * {sizeOf}));
413-
_tail = 0;{incrementVersion}
414+
var freeCountToEnd = Capacity - (_tail + 1);
415+
if(freeCountToEnd == 0 || freeCountToEnd < reducingCount)
416+
{{
417+
_stackMemoryC.ShiftLeft((byte*)(_start + _head), (byte*)(_start + (_tail + 1)), (long)((Capacity - freeCountToEnd - ((_tail + 1) - _head)) * {sizeOf}));
418+
_head = 0;{incrementVersion}
419+
}}
414420
}}
415421
}}
416422

Src/TestGenerator/GenerateQueuePrimitiveTest.cs

Lines changed: 55 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,7 @@ in Func<T, string> toStr
291291
QueuePrimitiveReducingCapacity(in values, in builder, in queueNamespace);
292292
QueuePrimitiveReducingCapacityHeadAfterTail(in values, in builder, in queueNamespace, in toStr);
293293
QueuePrimitiveReducingCapacityHeadAfterTailOwn(in values, in builder, in queueNamespace, in toStr);
294+
QueuePrimitiveReducingCapacityHeadBeforeTail(in values, in builder, in queueNamespace, in toStr);
294295

295296
QueuePrimitiveSize(in values, in builder, in queueNamespace, in toStr);
296297
QueuePrimitiveCapacity(in values, in builder, in queueNamespace, in toStr);
@@ -1089,7 +1090,7 @@ public void ExpandCapacityHeadAfterTailTest()
10891090
builder.Append($@"
10901091
Assert.That(queue.Front(), Is.EqualTo({toStr(values[values.Count - 1])}));
10911092
queue.Pop();
1092-
1093+
Assert.That(queue.Size, Is.EqualTo((nuint)0));
10931094
}}
10941095
}}
10951096
}}
@@ -1162,6 +1163,7 @@ public void ExpandCapacityHeadAfterTailOwnTest()
11621163
");
11631164
}
11641165
builder.Append($@"
1166+
Assert.That(queue.Size, Is.EqualTo((nuint)0));
11651167
}}
11661168
}}
11671169
");
@@ -1209,7 +1211,7 @@ public void ExpandCapacityHeadBeforeTailTest()
12091211
builder.Append($@"
12101212
Assert.That(queue.Front(), Is.EqualTo({toStr(values[values.Count - 1])}));
12111213
queue.Pop();
1212-
1214+
Assert.That(queue.Size, Is.EqualTo((nuint)0));
12131215
}}
12141216
}}
12151217
}}
@@ -1264,6 +1266,7 @@ public void ExpandCapacityHeadBeforeTailOwnTest()
12641266
");
12651267
}
12661268
builder.Append($@"
1269+
Assert.That(queue.Size, Is.EqualTo((nuint)0));
12671270
}}
12681271
}}
12691272
");
@@ -1364,6 +1367,7 @@ public void ReducingCapacityHeadAfterTailTest()
13641367
builder.Append($@"
13651368
Assert.That(queue.Front(), Is.EqualTo({toStr(values[values.Count - 1])}));
13661369
queue.Pop();
1370+
Assert.That(queue.Size, Is.EqualTo((nuint)0));
13671371
}}
13681372
}}
13691373
}}
@@ -1427,6 +1431,55 @@ public void ReducingCapacityHeadAfterTailOwnTest()
14271431
");
14281432
}
14291433
builder.Append($@"
1434+
Assert.That(queue.Size, Is.EqualTo((nuint)0));
1435+
}}
1436+
}}
1437+
");
1438+
}
1439+
1440+
private void QueuePrimitiveReducingCapacityHeadBeforeTail<T>(
1441+
in List<T> values,
1442+
in StringBuilder builder,
1443+
in string queueNamespace,
1444+
in Func<T, string> toStr
1445+
) where T : unmanaged
1446+
{
1447+
if (values.Count < 5)
1448+
{
1449+
throw new ArgumentException($"{nameof(values)} Must have minimum 5 values to generate tests");
1450+
}
1451+
1452+
builder.Append($@"
1453+
[Test]
1454+
public void ReducingCapacityHeadBeforeTailTest()
1455+
{{
1456+
unsafe
1457+
{{
1458+
using (var memory = new StackMemoryCollections.Struct.StackMemory(sizeof({typeof(T).Name}) * {values.Count}))
1459+
{{
1460+
var queue = new StackMemoryCollections.{queueNamespace}.QueueOf{typeof(T).Name}({values.Count}, &memory);
1461+
");
1462+
for (int i = 0; i < values.Count - 1; i++)
1463+
{
1464+
builder.Append($@"
1465+
queue.Push({toStr(values[i])});
1466+
");
1467+
}
1468+
1469+
builder.Append($@"
1470+
queue.Pop();
1471+
queue.ReducingCapacity(2);
1472+
");
1473+
for (int i = 1; i < values.Count - 1; i++)
1474+
{
1475+
builder.Append($@"
1476+
Assert.That(queue.Front(), Is.EqualTo({toStr(values[i])}));
1477+
queue.Pop();
1478+
");
1479+
}
1480+
builder.Append($@"
1481+
Assert.That(queue.Size, Is.EqualTo((nuint)0));
1482+
}}
14301483
}}
14311484
}}
14321485
");

0 commit comments

Comments
 (0)