Skip to content

Commit 488ecc0

Browse files
author
Vyacheslav
committed
fix: list of primitive types
1 parent c0530cb commit 488ecc0

File tree

2 files changed

+45
-38
lines changed

2 files changed

+45
-38
lines changed

Src/StackMemoryCollections/GeneratePrimitiveList.cs

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,7 @@ public void ReducingCapacity(in nuint reducingCount)
300300
}}
301301
else if (_stackMemoryS != null)
302302
{{
303-
if (new IntPtr(_stackMemoryS->Current) != new IntPtr((byte*)_start + (Capacity * 4)))
303+
if (new IntPtr(_stackMemoryS->Current) != new IntPtr((byte*)_start + (Capacity * {sizeOf})))
304304
{{
305305
throw new Exception(""Failed to reduce available memory, stack moved further"");
306306
}}
@@ -632,11 +632,11 @@ public void Remove(nuint index)
632632
}}
633633
else if (_stackMemoryS != null)
634634
{{
635-
_stackMemoryS->ShiftLeft((byte*)_start, (byte*)(_start + Size), (long){sizeOf});
635+
_stackMemoryS->ShiftLeft((byte*)(_start + (index + 1)), (byte*)(_start + Size), (long){sizeOf});
636636
}}
637637
else if (_stackMemoryC != null)
638638
{{
639-
_stackMemoryC.ShiftLeft((byte*)_start, (byte*)(_start + Size), (long){sizeOf});
639+
_stackMemoryC.ShiftLeft((byte*)(_start + (index + 1)), (byte*)(_start + Size), (long){sizeOf});
640640
}}
641641
642642
Size -= 1;{incrementVersion}
@@ -659,7 +659,12 @@ in string listNamespace
659659
builder.Append($@"
660660
public void Insert(in {typeof(T).Name} value, nuint index)
661661
{{
662-
if (index == Size - 1)
662+
if(index > Size || Size == Capacity)
663+
{{
664+
throw new IndexOutOfRangeException(""Element outside the list"");
665+
}}
666+
667+
if (index == Size)
663668
{{
664669
{incrementVersion}
665670
*(_start + Size++) = value;
@@ -709,7 +714,7 @@ public void Insert(in {typeof(T).Name} value, nuint index)
709714
_stackMemoryS->AllocateMemory({sizeOf});
710715
}}
711716
712-
_stackMemoryS->ShiftRight((byte*)(_start + index), (byte*)(_start + (Size - index)), (long){sizeOf});
717+
_stackMemoryS->ShiftRight((byte*)(_start + index), (byte*)(_start + Size), (long){sizeOf});
713718
*(_start + index) = value;
714719
}}
715720
else if (_stackMemoryC != null)
@@ -719,7 +724,7 @@ public void Insert(in {typeof(T).Name} value, nuint index)
719724
_stackMemoryC.AllocateMemory({sizeOf});
720725
}}
721726
722-
_stackMemoryC.ShiftRight((byte*)(_start + index), (byte*)(_start + (Size - index)), (long){sizeOf});
727+
_stackMemoryC.ShiftRight((byte*)(_start + index), (byte*)(_start + Size), (long){sizeOf});
723728
*(_start + index) = value;
724729
}}
725730
@@ -1064,7 +1069,7 @@ public void Copy(in Class.ListOf{typeof(T).Name} destList)
10641069
return;
10651070
}}
10661071
1067-
if (destList.Size < Size)
1072+
if (destList.Capacity < Size)
10681073
{{
10691074
throw new ArgumentException(""Destination list not enough capacity"");
10701075
}}

Src/TestGenerator/GenerateListPrimitiveTest.cs

Lines changed: 33 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -522,7 +522,7 @@ public void AddFutureTest()
522522
Assert.That(list.IsEmpty, Is.EqualTo(false));
523523
Assert.That(list.Capacity, Is.EqualTo((nuint){values.Count}));
524524
Assert.That(list.Size, Is.EqualTo((nuint){i + 1}));
525-
Assert.That(*list[{i}], Is.EqualTo({toStr(values[0])}));
525+
Assert.That(*list[{i}], Is.EqualTo({toStr(values[i])}));
526526
");
527527
}
528528

@@ -578,7 +578,7 @@ public void AddPtrTest()
578578
Assert.That(list.IsEmpty, Is.EqualTo(false));
579579
Assert.That(list.Capacity, Is.EqualTo((nuint){values.Count}));
580580
Assert.That(list.Size, Is.EqualTo((nuint){i + 1}));
581-
Assert.That(*list[{i}], Is.EqualTo({toStr(values[0])}));
581+
Assert.That(*list[{i}], Is.EqualTo({toStr(values[i])}));
582582
");
583583
}
584584

@@ -1267,7 +1267,7 @@ public void IndexTest()
12671267

12681268
builder.Append($@"
12691269
Assert.That(() => list[{values.Count}],
1270-
Throws.Exception.TypeOf(typeof(Exception))
1270+
Throws.Exception.TypeOf(typeof(IndexOutOfRangeException))
12711271
.And.Message.EqualTo(""Element outside the list"")
12721272
);
12731273
}}
@@ -1297,6 +1297,10 @@ public void RemoveTest()
12971297
using (var memory = new StackMemoryCollections.Struct.StackMemory(sizeof({typeof(T).Name}) * {values.Count}))
12981298
{{
12991299
var list = new StackMemoryCollections.{listNamespace}.ListOf{typeof(T).Name}({values.Count}, &memory);
1300+
Assert.That(() => list.Remove(0),
1301+
Throws.Exception.TypeOf(typeof(IndexOutOfRangeException))
1302+
.And.Message.EqualTo(""Element outside the list"")
1303+
);
13001304
");
13011305
for (int i = 0; i < values.Count; i++)
13021306
{
@@ -1305,32 +1309,25 @@ public void RemoveTest()
13051309
");
13061310
}
13071311
builder.Append($@"
1308-
list.Remove(2);
1309-
list.Remove({values.Count - 1});
1312+
list.Remove(1);
1313+
list.Remove({values.Count - 2});
13101314
list.Remove(0);
13111315
");
13121316
int j = 0;
1313-
for (int i = 0; i < values.Count - 3; i++)
1317+
for (int i = 0; i < values.Count; i++)
13141318
{
1315-
if (j == 0 || j == 2 || j == values.Count - 1)
1319+
if (i == 0 || i == 1 || i == values.Count - 1)
13161320
{
1317-
i -= 1;
1318-
j += 1;
13191321
continue;
13201322
}
13211323
builder.Append($@"
1322-
Assert.That(*list[{i}], Is.EqualTo({toStr(values[i])}));
1324+
Assert.That(*list[{j}], Is.EqualTo({toStr(values[i])}));
13231325
");
13241326
j += 1;
13251327
}
13261328

13271329
builder.Append($@"
13281330
Assert.That(list.Size, Is.EqualTo((nuint){values.Count - 3}));
1329-
1330-
Assert.That(() => list.Remove(0),
1331-
Throws.Exception.TypeOf(typeof(Exception))
1332-
.And.Message.EqualTo(""There are no elements on the list"")
1333-
);
13341331
}}
13351332
}}
13361333
}}
@@ -1367,44 +1364,49 @@ public void InsertTest()
13671364
");
13681365
}
13691366
builder.Append($@"
1370-
list.Insert({toStr(values[values.Count - 3])} , 2);
1371-
list.Insert({toStr(values[values.Count - 2])} , {values.Count - 3});
1367+
list.Insert({toStr(values[values.Count - 3])} , 1);
1368+
list.Insert({toStr(values[values.Count - 2])} , {values.Count - 2});
13721369
list.Insert({toStr(values[values.Count - 1])} , 0);
13731370
");
1371+
int j = 0;
13741372
for (int i = 0; i < values.Count; i++)
13751373
{
13761374
if (i == 0)
13771375
{
13781376
builder.Append($@"
13791377
Assert.That(*list[{i}], Is.EqualTo({toStr(values[values.Count - 1])}));
13801378
");
1379+
continue;
13811380
}
13821381
else if (i == 2)
13831382
{
13841383
builder.Append($@"
1385-
Assert.That(*list[{i}], Is.EqualTo({toStr(values[values.Count - 2])}));
1384+
Assert.That(*list[{i}], Is.EqualTo({toStr(values[values.Count - 3])}));
13861385
");
1386+
continue;
13871387
}
1388-
else if (i == 2)
1388+
else if (i == values.Count - 1)
13891389
{
13901390
builder.Append($@"
13911391
Assert.That(*list[{i}], Is.EqualTo({toStr(values[values.Count - 2])}));
13921392
");
1393+
continue;
13931394
}
13941395
else
13951396
{
13961397
builder.Append($@"
1397-
Assert.That(*list[{i}], Is.EqualTo({toStr(values[i])}));
1398+
Assert.That(*list[{i}], Is.EqualTo({toStr(values[j])}));
13981399
");
13991400
}
1401+
j += 1;
14001402
}
14011403

14021404
builder.Append($@"
1403-
Assert.That(list.Size, Is.EqualTo((nuint){values.Count - 3}));
1405+
Assert.That(list.Size, Is.EqualTo((nuint){values.Count}));
14041406
1405-
Assert.That(() => list.Remove(0),
1406-
Throws.Exception.TypeOf(typeof(Exception))
1407-
.And.Message.EqualTo(""There are no elements on the list"")
1407+
Assert.That(() => list.Insert({toStr(values[values.Count - 1])}, 0),
1408+
Throws.Exception.TypeOf(typeof(IndexOutOfRangeException))
1409+
.And.Message.EqualTo(""Element outside the list"")
14081410
);
14091411
}}
14101412
}}
@@ -1435,8 +1437,8 @@ public void GetByIndexTest()
14351437
{{
14361438
var list = new StackMemoryCollections.{listNamespace}.ListOf{typeof(T).Name}({values.Count}, &memory);
14371439
Assert.That(() => list.GetByIndex(0),
1438-
Throws.Exception.TypeOf(typeof(Exception))
1439-
.And.Message.EqualTo(""There are no elements on the list"")
1440+
Throws.Exception.TypeOf(typeof(IndexOutOfRangeException))
1441+
.And.Message.EqualTo(""Element outside the list"")
14401442
);
14411443
");
14421444
for (int i = 0; i < values.Count; i++)
@@ -1479,8 +1481,8 @@ public void GetOutByIndexTest()
14791481
{{
14801482
var list = new StackMemoryCollections.{listNamespace}.ListOf{typeof(T).Name}({values.Count}, &memory);
14811483
Assert.That(() => list.GetOutByIndex(0, out _),
1482-
Throws.Exception.TypeOf(typeof(Exception))
1483-
.And.Message.EqualTo(""There are no elements on the list"")
1484+
Throws.Exception.TypeOf(typeof(IndexOutOfRangeException))
1485+
.And.Message.EqualTo(""Element outside the list"")
14841486
);
14851487
");
14861488
for (int i = 0; i < values.Count; i++)
@@ -1516,7 +1518,7 @@ in Func<T, string> toStr
15161518
builder.Append($@"
15171519
[Test]
15181520
[SkipLocalsInit]
1519-
public void FrontBackRefValueTest()
1521+
public void GetByIndexRefTest()
15201522
{{
15211523
unsafe
15221524
{{
@@ -1529,8 +1531,8 @@ public void FrontBackRefValueTest()
15291531
{typeof(T).Name} temp = {toStr(values[0])};
15301532
list.GetByIndex(0, ref temp);
15311533
}},
1532-
Throws.Exception.TypeOf(typeof(Exception))
1533-
.And.Message.EqualTo(""There are no elements on the list"")
1534+
Throws.Exception.TypeOf(typeof(IndexOutOfRangeException))
1535+
.And.Message.EqualTo(""Element outside the list"")
15341536
);
15351537
{typeof(T).Name} item;
15361538
");

0 commit comments

Comments
 (0)