Skip to content

Commit

Permalink
BCL tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
jp2masa committed Jan 14, 2018
1 parent 06c814d commit 71e754f
Show file tree
Hide file tree
Showing 16 changed files with 986 additions and 270 deletions.
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace Cosmos.Compiler.Tests.Bcl.Helper
namespace Cosmos.Compiler.Tests.Bcl
{
class EqualityHelper
internal static class EqualityHelper
{
public static bool SinglesAreEqual(float left, float right)
{
// Define the tolerance for variation in their values
float difference = Math.Abs(left * .00001F);
return Math.Abs(left - right) <= difference;
}

public static bool DoublesAreEqual(double left, double right)
{
// Define the tolerance for variation in their values
double difference = Math.Abs(left * .00001);

if (Math.Abs(left - right) <= difference)
return true;
else
return false;
return Math.Abs(left - right) <= difference;
}

/// <summary>
Expand Down
21 changes: 15 additions & 6 deletions Tests/Cosmos.Compiler.Tests.Bcl/Kernel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@

using Cosmos.TestRunner;
using Sys = Cosmos.System;

using Cosmos.Compiler.Tests.Bcl.CSharp;
using Cosmos.Compiler.Tests.Bcl.System;
using Cosmos.Compiler.Tests.Bcl.System.Collections.Generic;
using Cosmos.Compiler.Tests.Bcl.System.Text;

namespace Cosmos.Compiler.Tests.Bcl
{
Expand All @@ -19,9 +23,11 @@ protected override void Run()
{
mDebugger.Send("Run");

CSharp.WhileLoopTests.Execute();
CSharp.ForeachLoopTests.Execute();
// C#
WhileLoopTests.Execute();
ForeachLoopTests.Execute();

// System
ObjectTests.Execute();
ArrayTests.Execute();
StringTest.Execute();
Expand All @@ -42,12 +48,15 @@ protected override void Run()
BitConverterTest.Execute();
UnsafeCodeTest.Execute();
DelegatesTest.Execute();
EncodingTest.Execute();
RandomTests.Execute();

System.Collections.Generic.ListTest.Execute();
System.Collections.Generic.QueueTest.Execute();
//System.Collections.Generic.DictionaryTest.Execute();
// System.Collections.Generic
ListTest.Execute();
QueueTest.Execute();
//DictionaryTest.Execute();

// System.Text
EncodingTest.Execute();

TestController.Completed();
}
Expand Down
22 changes: 9 additions & 13 deletions Tests/Cosmos.Compiler.Tests.Bcl/System/BitConverterTest.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
using System;
using System.Linq;
using System.Threading.Tasks;

using Cosmos.TestRunner;

namespace Cosmos.Compiler.Tests.Bcl.System
{
class BitConverterTest
internal static class BitConverterTest
{
public static void Execute()
{
String result;
String expectedResult;
string result;
string expectedResult;

int anInt = 1;

Expand Down Expand Up @@ -50,17 +49,14 @@ public static void Execute()

Assert.IsTrue((result == expectedResult), "BitConverter.ToString(floatBytes) doesn't work: result " + result + " != " + expectedResult);

//// This tests fails bytes are screwed!
//double aDouble = 1.0;

//byte[] doubleBytes = BitConverter.GetBytes(aDouble);

//result = BitConverter.ToString(doubleBytes, 0);
//expectedResult = "00-00-00-00-00-00-F0-3F";
double aDouble = 1.0;

//Assert.IsTrue((result == expectedResult), "BitConverter.ToString(doubleBytes) doesn't work: result " + result + " != " + expectedResult);
byte[] doubleBytes = BitConverter.GetBytes(aDouble);

result = BitConverter.ToString(doubleBytes, 0);
expectedResult = "00-00-00-00-00-00-F0-3F";

Assert.IsTrue((result == expectedResult), "BitConverter.ToString(doubleBytes) doesn't work: result " + result + " != " + expectedResult);
}
}
}
118 changes: 113 additions & 5 deletions Tests/Cosmos.Compiler.Tests.Bcl/System/ByteTest.cs
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
using System;
using System.Linq;
using System.Threading.Tasks;

using Cosmos.TestRunner;

namespace Cosmos.Compiler.Tests.Bcl.System
{
class ByteTest
internal static class ByteTest
{
public static void Execute()
{
byte value;
String result;
String expectedResult;
string result;
string expectedResult;

value = Byte.MaxValue;

Expand Down Expand Up @@ -44,6 +43,115 @@ public static void Execute()

Assert.IsTrue((result == expectedResult), "Byte.ToString(X2) doesn't work");
#endif

// basic bit operations

int val2;

value = 0x0C; // low-order bits: 0b0000_1100

val2 = ~value; // low-order bits: val2 = ~value = 0b1111_0011
Assert.IsTrue(val2 == -0x0D, "Byte bitwise not doesn't work got: " + val2);

val2 = value & 0x06; // low-order bits: val2 = value & 0b0000_0110 = 0b0000_0100
Assert.IsTrue(val2 == 0x04, "Byte bitwise and doesn't work got: " + val2);

val2 = value | 0x06; // low-order bits: val2 = value | 0b0000_0110 = 0b0000_1110
Assert.IsTrue(val2 == 0x0E, "Byte bitwise or doesn't work got: " + val2);

val2 = value ^ 0x06; // low-order bits: val2 = value ^ 0b0000_0110 = 0b0000_1010
Assert.IsTrue(val2 == 0x0A, "Byte bitwise xor doesn't work got: " + val2);

val2 = value >> 0x02; // low-order bits: val2 = value >> 0b0000_0010 = 0b0000_0011
Assert.IsTrue(val2 == 0x03, "Byte left shift doesn't work got: " + val2);

val2 = value << 0x02; // low-order bits: val2 = value << 0b0000_0010 = 0b0011_0000
Assert.IsTrue(val2 == 0x30, "Byte right shift doesn't work got: " + val2);

// basic arithmetic operations

value = 60;

val2 = value + 5;
Assert.IsTrue(val2 == 65, "Byte addition doesn't work got: " + val2);

val2 = value - 5;
Assert.IsTrue(val2 == 55, "Byte subtraction doesn't work got: " + val2);

val2 = value * 5;
Assert.IsTrue(val2 == 300, "Byte multiplication doesn't work got: " + val2);

val2 = value / 5;
Assert.IsTrue(val2 == 12, "Byte division doesn't work got: " + val2);

val2 = value % 7;
Assert.IsTrue(val2 == 4, "Byte remainder doesn't work got: " + val2);

// Now test conversions

byte maxValue = Byte.MaxValue;
byte minValue = Byte.MinValue;

// TODO: some convert instructions aren't being emitted, we should find other ways of getting them emitted

// Test Conv_I1
Assert.IsTrue((sbyte)maxValue == -0x01, "Conv_I1 for Byte doesn't work");
Assert.IsTrue((sbyte)minValue == 0x00, "Conv_I1 for Byte doesn't work");

// Test Conv_U1
Assert.IsTrue((byte)maxValue == 0xFF, "Conv_U1 for Byte doesn't work");
Assert.IsTrue((byte)minValue == 0x00, "Conv_U1 for Byte doesn't work");

// Test Conv_I2
Assert.IsTrue((short)maxValue == 0x00FF, "Conv_I2 for Byte doesn't work");
Assert.IsTrue((short)minValue == 0x0000, "Conv_I2 for Byte doesn't work");

// Test Conv_U2
Assert.IsTrue((ushort)maxValue == 0x00FF, "Conv_U2 for Byte doesn't work");
Assert.IsTrue((ushort)minValue == 0x0000, "Conv_U2 for Byte doesn't work");

// Test Conv_I4
Assert.IsTrue((int)maxValue == 0x000000FF, "Conv_I4 for Byte doesn't work");
Assert.IsTrue((int)minValue == 0x00000000, "Conv_I4 for Byte doesn't work");

// Test Conv_U4
Assert.IsTrue((uint)maxValue == 0x000000FF, "Conv_U4 for Byte doesn't work");
Assert.IsTrue((uint)minValue == 0x00000000, "Conv_U4 for Byte doesn't work");

// Test Conv_I8
Assert.IsTrue((long)maxValue == 0x00000000000000FF, "Conv_I8 for Byte doesn't work");
Assert.IsTrue((long)minValue == 0x0000000000000000, "Conv_I8 for Byte doesn't work");

// Test Conv_U8
Assert.IsTrue((ulong)maxValue == 0x00000000000000FF, "Conv_U8 for Byte doesn't work");
Assert.IsTrue((ulong)minValue == 0x0000000000000000, "Conv_U8 for Byte doesn't work");

// Test Conv_R4
Assert.IsTrue((float)maxValue == Byte.MaxValue, "Conv_R4 for Byte doesn't work" + (float)maxValue);
Assert.IsTrue((float)minValue == Byte.MinValue, "Conv_R4 for Byte doesn't work");

// Test Conv_R8
Assert.IsTrue((double)maxValue == Byte.MaxValue, "Conv_R8 for Byte doesn't work");
Assert.IsTrue((double)minValue == Byte.MinValue, "Conv_R8 for Byte doesn't work");

// Test Methods
val2 = TestMethod(value);
Assert.IsTrue(value == 60, "Passing a Byte as a method parameter doesn't work");
Assert.IsTrue(val2 == 61, "Returning a Byte value from a method doesn't work");

ByRefTestMethod(ref value);
Assert.IsTrue(value == 61, "Passing a Byte by ref to a method doesn't work");
}

public static byte TestMethod(byte aParam)
{
aParam++;
return aParam;
}

public static void ByRefTestMethod(ref byte aParam)
{
aParam++;
}
}
}
46 changes: 15 additions & 31 deletions Tests/Cosmos.Compiler.Tests.Bcl/System/DoubleTest.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
using System;

using Cosmos.TestRunner;
using Cosmos.Debug.Kernel;
using Cosmos.Compiler.Tests.Bcl.Helper;

namespace Cosmos.Compiler.Tests.Bcl.System
{
class DoubleTest
internal static class DoubleTest
{
public readonly Debugger mDebugger = new Debugger("User", "Double");

public static void Execute()
{
Double value;
Expand Down Expand Up @@ -157,18 +154,30 @@ public static void Execute()
#endif

// Now test some castings operations
sbyte valueAsSByte = (sbyte)value;
Assert.IsTrue((valueAsSByte == (sbyte)42), "double (sbyte) operator doesn't work");

byte valueAsByte = (byte)value;
Assert.IsTrue((valueAsByte == (byte)42), "double (byte) operator doesn't work");

short valueAsShort = (short)value;
Assert.IsTrue((valueAsByte == (short)42), "double (short) operator doesn't work");
Assert.IsTrue((valueAsShort == (short)42), "double (short) operator doesn't work");

ushort valueAsUShort = (ushort)value;
Assert.IsTrue((valueAsUShort == (ushort)42), "double (ushort) operator doesn't work");

int valueAsInt = (int)value;
Assert.IsTrue((valueAsInt == (int)42), "double (int) operator doesn't work");

uint valueAsUInt = (uint)value;
Assert.IsTrue((valueAsUInt == (uint)42), "double (uint) operator doesn't work");

long valueAsLong = (long)value;
Assert.IsTrue((valueAsLong == (long)42), "double (long) operator doesn't work");

ulong valueAsULong = (ulong)value;
Assert.IsTrue((valueAsULong == (ulong)42), "double (ulong) operator doesn't work");

// We put on anUInt a very big value Int32.MaxValue + 42. Why all this 42 :-) ?
uint anUInt = 2147483689;
value = (double)anUInt;
Expand All @@ -187,31 +196,6 @@ public static void Execute()
value = 42.0;
valueNegated = -value;
Assert.IsTrue((EqualityHelper.DoublesAreEqual(valueNegated, -42.0f)), "(double) negation of positive double doesn't work");

#region Parsing
value = double.Parse("0.4");
Assert.IsTrue(EqualityHelper.DoublesAreEqual(value, 0.4), "simple parsing of double works");

value = double.Parse("+0.3");
Assert.IsTrue(EqualityHelper.DoublesAreEqual(value, 0.3), "parsing of double with positive sign works!");

value = double.Parse("-0.4");
Assert.IsTrue(EqualityHelper.DoublesAreEqual(value, -0.4), "parsing of negative double works!");

value = double.Parse(" 0.7 ");
Assert.IsTrue(EqualityHelper.DoublesAreEqual(value, 0.7), "double parsing ignores leading and trailing whitespaces");

value = double.Parse("0.4E1");
Assert.IsTrue(EqualityHelper.DoublesAreEqual(value, 4), "double parsing takes in account E");

value = double.Parse("0.4E-1");
Assert.IsTrue(EqualityHelper.DoublesAreEqual(value, 0.04), "double parsing works with negative E");

Assert.IsFalse(double.TryParse("asd4", out value), "double TryParse returns false when it fails");

Assert.IsTrue(double.TryParse("2.3", out value), "double TryParse returns true when it works");
Assert.IsTrue(EqualityHelper.DoublesAreEqual(value, 2.3), "double TryParse returns correct result when it works");
#endregion
}
}
}
Loading

0 comments on commit 71e754f

Please sign in to comment.