Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

List Bug Fix #1241 #1247

Merged
merged 16 commits into from
Oct 21, 2024
Merged
Show file tree
Hide file tree
Changes from 15 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ protected static void CopyCompound(Transform transform, Context context, Operand
var srcReg = transform.VirtualRegisters.AllocateNativeInteger();
var dstReg = transform.VirtualRegisters.AllocateNativeInteger();

context.AppendInstruction(transform.AddInstruction, srcReg, sourceBase, source);
context.SetInstruction(transform.AddInstruction, srcReg, sourceBase, source);
context.AppendInstruction(transform.AddInstruction, dstReg, destinationBase, destination);

var tmp = transform.VirtualRegisters.AllocateNativeInteger();
Expand Down
4 changes: 2 additions & 2 deletions Source/Mosa.Korlib/System.Collections.Generic/List.cs
Original file line number Diff line number Diff line change
Expand Up @@ -345,11 +345,11 @@ public void Insert(int index, T item)
{
EnsureCapacity(_size + 1);

_size++;
for (int i = index; i < _size; i++)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This loop should start from the end and work backwards, otherwise it is overwriting as it moved forwards.

e.g.
[5, 6, 7]
Insert 9 at index 1
After first loop iteration: [5, 6, 6, 0]
After second loop iteration: [5, 6, 6, 6]
After final assignment at desired index: [5, 9, 6, 6]

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

{
_items[i] = _items[i + 1];
_items[i + 1] = _items[i];
}
_size++;

_items[index] = item;
}
Expand Down
250 changes: 248 additions & 2 deletions Source/Mosa.UnitTests/Korlib/ListTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public static bool Add1()

list.Add(101);

return list.Count == 1;
return list[0] == 101 && list.Count == 1;
}

[MosaUnitTest]
Expand All @@ -40,7 +40,77 @@ public static bool Add2()
list.Add(101);
list.Add(202);

return list.Count == 2;
return list[0] == 101 && list[1] == 202 && list.Count == 2;
}

[MosaUnitTest]
public static bool Add4()
{
var list = new List<int>();

list.Add(0);
list.Add(1);
list.Add(2);
list.Add(3);
list.Add(4);

return list[0] == 0 && list[1] == 1 && list[2] == 2 && list[3] == 3 && list[4] == 4 && list[5] == 5;
}

[MosaUnitTest]
public static int Check0()
{
var list = new List<int>();

list.Add(0);
list.Add(1);
list.Add(2);
list.Add(3);
list.Add(4);

return list[0];
}

[MosaUnitTest]
public static int Check1()
{
var list = new List<int>();

list.Add(0);
list.Add(1);
list.Add(2);
list.Add(3);
list.Add(4);

return list[1];
}

[MosaUnitTest]
public static int Check2()
{
var list = new List<int>();

list.Add(0);
list.Add(1);
list.Add(2);
list.Add(3);
list.Add(4);

return list[2];
}

[MosaUnitTest]
public static int Check4()
{
var list = new List<int>();

list.Add(0);
list.Add(1);
list.Add(2);
list.Add(3);
list.Add(4);

return list[4];
}

[MosaUnitTest]
Expand Down Expand Up @@ -110,6 +180,182 @@ public static bool Remove2()
return list.IndexOf(101) == 0;
}

[MosaUnitTest]
public static bool RemoveAt0()
{
var list = new List<int>();

list.Add(0);
list.Add(1);
list.Add(2);
list.Add(3);
list.Add(4);

list.RemoveAt(0);

return list[0] == 1 && list[1] == 2 && list[2] == 3 && list[3] == 4;
}

[MosaUnitTest]
public static bool RemoveAt1()
{
var list = new List<int>();

list.Add(0);
list.Add(1);
list.Add(2);
list.Add(3);
list.Add(4);

list.RemoveAt(1);

return list[0] == 0 && list[1] == 2 && list[2] == 3 && list[3] == 3;
}

[MosaUnitTest]
public static bool RemoveAt3()
{
var list = new List<int>();

list.Add(0);
list.Add(1);
list.Add(2);
list.Add(3);
list.Add(4);

list.RemoveAt(4);

return list[0] == 0 && list[1] == 1 && list[2] == 2 && list[3] == 4;
}

[MosaUnitTest]
public static bool RemoveAt4()
{
var list = new List<int>();

list.Add(0);
list.Add(1);
list.Add(2);
list.Add(3);
list.Add(4);

list.RemoveAt(4);

return list[0] == 0 && list[1] == 1 && list[2] == 2 && list[3] == 3;
}

[MosaUnitTest]
public static bool Insert0()
{
var list = new List<int>();

list.Add(0);
list.Add(1);
list.Add(2);
list.Add(3);
list.Add(4);

list.Insert(0, 99);

return list[0] == 99 && list[1] == 0 && list[2] == 1 && list[3] == 2 && list[4] == 3 && list[5] == 4;
}

[MosaUnitTest]
public static bool Insert0b()
{
var list = new List<int>();

list.Add(0);
list.Add(1);
list.Add(2);
list.Add(3);
list.Add(4);

list.Insert(0, 99);

return list[0] == 99 && list[1] == 0;// && list[2] == 1;// && list[3] == 2 && list[4] == 3 && list[5] == 4;
}

[MosaUnitTest]
public static bool Insert0c()
{
var list = new List<int>();

list.Add(0);
list.Add(1);
list.Add(2);
list.Add(3);
list.Add(4);

list.Insert(0, 99);

return list[0] == 99 && list[1] == 0 && list[2] == 1;// && list[3] == 2 && list[4] == 3 && list[5] == 4;
}

[MosaUnitTest]
public static bool Insert1()
{
var list = new List<int>();

list.Add(0);
list.Add(1);
list.Add(2);
list.Add(3);
list.Add(4);

list.Insert(1, 99);

return list[0] == 0 && list[1] == 99 && list[2] == 1 && list[3] == 2 && list[4] == 3 && list[5] == 4;
}

[MosaUnitTest]
public static bool Insert2()
{
var list = new List<int>();

list.Add(0);
list.Add(1);
list.Add(2);
list.Add(3);
list.Add(4);

list.Insert(2, 99);

return list[0] == 0 && list[1] == 1 && list[2] == 99 && list[3] == 2 && list[4] == 3 && list[5] == 4;
}

[MosaUnitTest]
public static bool Insert3()
{
var list = new List<int>();

list.Add(0);
list.Add(1);
list.Add(2);
list.Add(3);
list.Add(4);

list.Insert(3, 99);

return list[0] == 0 && list[1] == 1 && list[2] == 2 && list[3] == 99 && list[4] == 3 && list[5] == 4;
}

[MosaUnitTest]
public static bool Insert4()
{
var list = new List<int>();

list.Add(0);
list.Add(1);
list.Add(2);
list.Add(3);
list.Add(4);

list.Insert(4, 99);

return list[0] == 0 && list[1] == 1 && list[2] == 2 && list[3] == 3 && list[4] == 99 && list[5] == 4;
}

private static List<int> Populate(int count)
{
var list = new List<int>();
Expand Down
Loading