Skip to content

Commit

Permalink
Reworked MemoryBlock and ManagedMemoryBlock
Browse files Browse the repository at this point in the history
  • Loading branch information
quajak committed May 30, 2021
1 parent a3f15a0 commit 6bfdb1e
Show file tree
Hide file tree
Showing 8 changed files with 399 additions and 306 deletions.
111 changes: 87 additions & 24 deletions Tests/Cosmos.TestRunner.TestController/Assert.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,31 @@ namespace Cosmos.TestRunner
{
public static class Assert
{
public static void Suceed(string message, [CallerFilePath] string file = null, [CallerLineNumber] int line = 0)
{
TestController.Debugger.Send("Assertion succeeded:");
TestController.Debugger.Send(message);
TestController.AssertionSucceeded();
}
public static void Fail(string message, [CallerFilePath] string file = null, [CallerLineNumber] int line = 0)
{
TestController.Debugger.Send("Assertion failed:");
TestController.Debugger.Send("File: " + file);
TestController.Debugger.Send("Line number: " + line);
TestController.Debugger.Send(message);
TestController.Failed();
throw new Exception("Assertion failed!");
}

public static void IsTrue(bool condition, string message, [CallerFilePath] string file = null, [CallerLineNumber] int line = 0)
{
if (condition)
{
TestController.Debugger.Send("Assertion succeeded:");
TestController.Debugger.Send(message);
TestController.AssertionSucceeded();
Suceed(message, file, line);
}
else
{
TestController.Debugger.Send("Assertion failed:");
TestController.Debugger.Send("File: " + file);
TestController.Debugger.Send("Line number: " + line);
TestController.Debugger.Send(message);
TestController.Failed();
throw new Exception("Assertion failed!");
Fail(message, file, line);
}
}

Expand Down Expand Up @@ -74,34 +83,88 @@ public static void AreEqual(string[] expected, string[] actual, string message,
if(expected.Length != actual.Length)
{
TestController.Debugger.Send($"Array lengths differ: Expected: {expected.Length} Actual: {actual.Length}");
if (actual.Length < 32 && expected.Length < 32)
Fail(message, file, line);
return;
}
for (int i = 0; i < expected.Length; i++)
{
if(expected[i] != actual[i])
{
TestController.Debugger.Send("Values in Expected:");
for (int i = 0; i < expected.Length; i++)
{
TestController.Debugger.Send(expected[i]);
}
TestController.Debugger.Send("Values in Actual:");
for (int i = 0; i < actual.Length; i++)
{
TestController.Debugger.Send(actual[i]);
}
TestController.Debugger.Send($"Values differ in row {i}");
TestController.Debugger.Send($"Expected value: '{expected[i]}'");
TestController.Debugger.Send($"Actual value: '{actual[i]}'");
Fail(message, file, line);
return;
}
IsTrue(false, message, file, line);
}
Suceed(message, file, line);
}

public static void AreEqual(byte[] expected, byte[] actual, string message, [CallerFilePath] string file = null, [CallerLineNumber] int line = 0)
{
if (expected.Length != actual.Length)
{
TestController.Debugger.Send($"Array lengths differ: Expected: {expected.Length} Actual: {actual.Length}");
Fail(message, file, line);
return;
}
for (int i = 0; i < expected.Length; i++)
{
if(expected[i] != actual[i])
if (expected[i] != actual[i])
{
TestController.Debugger.Send($"Values differ in row {i}");
TestController.Debugger.Send($"Expected value: '{expected[i]}'");
TestController.Debugger.Send($"Actual value: '{actual[i]}'");
TestController.Debugger.Send(BitConverter.ToString(actual));
Fail(message, file, line);
return;
}
}
Suceed(message, file, line);
}

public static void AreEqual(uint[] expected, uint[] actual, string message, [CallerFilePath] string file = null, [CallerLineNumber] int line = 0)
{
if (expected.Length != actual.Length)
{
TestController.Debugger.Send($"Array lengths differ: Expected: {expected.Length} Actual: {actual.Length}");
Fail(message, file, line);
return;
}
for (int i = 0; i < expected.Length; i++)
{
if (expected[i] != actual[i])
{
TestController.Debugger.Send($"Values differ in row {i}");
TestController.Debugger.Send($"Expected value: '{expected[i]}'");
TestController.Debugger.Send($"Actual value: '{actual[i]}'");
Fail(message, file, line);
return;
}
}
Suceed(message, file, line);
}

public static void AreEqual(int[] expected, int[] actual, string message, [CallerFilePath] string file = null, [CallerLineNumber] int line = 0)
{
if (expected.Length != actual.Length)
{
TestController.Debugger.Send($"Array lengths differ: Expected: {expected.Length} Actual: {actual.Length}");
Fail(message, file, line);
return;
}
for (int i = 0; i < expected.Length; i++)
{
if (expected[i] != actual[i])
{
TestController.Debugger.Send($"Values differ in row {i}");
TestController.Debugger.Send($"Expected value: '{expected[i]}'");
TestController.Debugger.Send($"Actual value: '{actual[i]}'");
IsTrue(false, message, file, line);
Fail(message, file, line);
return;
}
}
IsTrue(true, message, file, line);
Suceed(message, file, line);
}
}
}
4 changes: 0 additions & 4 deletions Tests/Kernels/GraphicTest/Kernel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -126,10 +126,6 @@ private void DoTest(Canvas aCanvas)

aCanvas.DrawImageAlpha(bitmap3, new Point(0, 300));

/* Drawing BitmapHeaderV5 image */
Bitmap v5header = new Bitmap(Convert.FromBase64String(parrot), ColorOrder.RGB);
aCanvas.DrawImage(v5header, 0, 0);

/* Drawing ellipses */
aCanvas.DrawEllipse(pen, 100, 69, 10, 50);
aCanvas.DrawEllipse(pen, 100, 69, 10, 50);
Expand Down
108 changes: 58 additions & 50 deletions Tests/Kernels/MemoryOperationsTest/Kernel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,53 +27,6 @@ protected override void BeforeRun()
Console.WriteLine("Cosmos booted successfully. Let's Test MemoryOperations!");
}

/*
* It checks 32 byte any time to make it more faster, for now we need unsafe to do this
*/
public static unsafe bool AreArrayEquals(byte* b0, byte* b1, int length)
{
byte* lastAddr = b0 + length;
byte* lastAddrMinus32 = lastAddr - 32;
while (b0 < lastAddrMinus32) // unroll the loop so that we are comparing 32 bytes at a time.
{
if (*(ulong*)b0 != *(ulong*)b1)
return false;
if (*(ulong*)(b0 + 8) != *(ulong*)(b1 + 8))
return false;
if (*(ulong*)(b0 + 16) != *(ulong*)(b1 + 16))
return false;
if (*(ulong*)(b0 + 24) != *(ulong*)(b1 + 24))
return false;
b0 += 32;
b1 += 32;
}
while (b0 < lastAddr)
{
if (*b0 != *b1)
return false;
b0++;
b1++;
}
return true;
}

public static unsafe bool AreArrayEquals(byte[] arr0, byte[] arr1)
{
fixed (byte* b0 = arr0, b1 = arr1)
{
return b0 == b1 || AreArrayEquals(b0, b1, arr0.Length);
}
}

public static unsafe bool AreArrayEquals(int[] arr0, int[] arr1)
{
int lenght = arr0.Length * 4;
fixed (int* b0 = arr0, b1 = arr1)
{
return b0 == b1 || AreArrayEquals((byte *)b0, (byte*)b1, lenght);
}
}

private void TestIntArrayCopy(int size)
{
int[] src = new int[size];
Expand All @@ -87,7 +40,7 @@ private void TestIntArrayCopy(int size)
MemoryOperations.Copy(dst, src);
mDebugger.Send("Copy End");

Assert.IsTrue(AreArrayEquals(src, dst), $"Copy failed Array src and dst with size {size} are not equals");
Assert.AreEqual(src, dst, $"Copy failed Array src and dst with size {size} are not equals");

mDebugger.Send("End");
}
Expand All @@ -105,7 +58,7 @@ private void TestByteArrayCopy(int size)
MemoryOperations.Copy(dst, src);
mDebugger.Send("Copy End");

Assert.IsTrue(AreArrayEquals(src, dst), $"Copy failed Array src and dst with size {size} are not equals");
Assert.AreEqual(src, dst, $"Copy failed Array src and dst with size {size} are not equals");

mDebugger.Send("End");
}
Expand Down Expand Up @@ -138,13 +91,68 @@ private void TestCopy()
TestIntArrayCopy(1024 * 768); // XVGA resolution
TestIntArrayCopy(1920 * 1080); // HDTV resolution
}
static unsafe void TestManagedMemoryBlock(ManagedMemoryBlock memoryBlock)
{
memoryBlock.Write32(0, 1);
memoryBlock.Write32(1, 101);
memoryBlock.Write32(2, 2^16+2);
memoryBlock.Write32(3, int.MaxValue);
Assert.AreEqual(1, memoryBlock.Read32(0), "ManagedMemoryBlock read/write at index 0 works");
Assert.AreEqual(101, memoryBlock.Read32(1), "ManagedMemoryBlock read/write at index 1 works");
Assert.AreEqual(2^16+2, memoryBlock.Read32(2), "ManagedMemoryBlock read/write at index 2 works");
Assert.AreEqual(int.MaxValue, memoryBlock.Read32(3), "ManagedMemoryBlock read/write at index 3 works");

memoryBlock.Fill(101);
Assert.AreEqual(101, memoryBlock.Read32(0), "ManagedMemoryBlock fill works at index 0");
Assert.AreEqual(101, memoryBlock.Read32(10), "ManagedMemoryBlock fill works at index 10");

memoryBlock.Fill(1, 1, 987893745);
Assert.AreEqual(101, memoryBlock.Read32(0), "ManagedMemoryBlock Fill(int, int, int) works at index 0");
Assert.AreEqual(987893745, memoryBlock.Read32(1), "ManagedMemoryBlock Fill(int, int, int) works at index 1");
}

static unsafe void TestMemoryBlock(MemoryBlock memoryBlock)
{
uint[] values = new uint[] { 1, 101, 2 ^ 16 + 2, int.MaxValue };
memoryBlock.Write32(values);
uint[] read = new uint[4];
memoryBlock.Read32(read);
for (int i = 0; i < 4; i++)
{
if(values[i] != read[i])
{
Assert.Fail($"Values read differ at {i}. Expected: {values[i]} Actual: {read[i]}");
}
}
Assert.Suceed("Writing and reading uints works");
byte* ptr = (byte*)memoryBlock.Base;
Assert.AreEqual(1, *ptr, "Expected 1 in first byte of memory block when checking using pointer");
Assert.AreEqual(0, *(ptr + 3), "Expected 0 in fourth byte of memory block when checking using pointer");
byte[] valueBytes = new byte[] { 1, 0, 0, 0 };
byte[] readByte = new byte[4];
memoryBlock.Read8(readByte);
Assert.AreEqual(valueBytes, readByte, "Reading bytes works");
valueBytes[0] = 65;
valueBytes[1] = 127;
memoryBlock.Write8(valueBytes);
memoryBlock.Read8(readByte);
Assert.AreEqual(valueBytes, readByte, "Writing bytes works");
memoryBlock.Fill(101);
memoryBlock.Read8(readByte);
Assert.AreEqual(new byte[] { 101, 101, 101, 101 }, readByte, "Filling works");
values = new uint[] { 0x65656565, 987893745, 0x65656565, 0x65656565 };
memoryBlock.Fill(1, 1, 987893745);
memoryBlock.Read32(read);
Assert.AreEqual(values, read, "Using Fill(int, int, int) works");
}

protected override void Run()
{
try
{
TestCopy();

TestMemoryBlock(new MemoryBlock(0x60000, 128)); //we are testing in SVGA video memory which should not be in use
TestManagedMemoryBlock(new ManagedMemoryBlock(128));
TestController.Completed();
}
catch (Exception e)
Expand Down
2 changes: 1 addition & 1 deletion Tests/Kernels/NetworkTest/NetworkTest.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netcoreapp2.0</TargetFramework>
<TargetFramework>net5.0</TargetFramework>
<AllowUnsafeBlocks>True</AllowUnsafeBlocks>
</PropertyGroup>

Expand Down
Loading

0 comments on commit 6bfdb1e

Please sign in to comment.