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

Fixing more unit tests #142

Merged
merged 8 commits into from
May 31, 2021
Merged
Show file tree
Hide file tree
Changes from 5 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
129 changes: 77 additions & 52 deletions Tests/NFUnitTestSystemLib/UnitTestParseTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,14 +68,13 @@ public void ParseByte_Test_2()
{
Debug.WriteLine("Byte MinValue = " + Byte.MinValue.ToString());
Debug.WriteLine("Byte MaxValue = " + Byte.MaxValue.ToString());
//Debug.WriteLine("This currently fails, see 21634 for details");

String[] strArr = GetRandomStringArray(Byte.MaxValue, false);
Byte[] _byte = new Byte[intArr.Length];
for (int i = 0; i < _byte.Length; i++)
{
_byte[i] = (Byte)intArr[i];
}
_byte[i] = (Byte)intArr[i];
}

Byte temp = 0;
for (int i = 0; i < strArr.Length; i++)
Expand All @@ -97,7 +96,6 @@ public void ParseInt16_Test_3()
{
_int16[i] = (Int16)intArr[i];
}
int counter = 0;
Int16 temp = 0;
for (int i = 0; i < strArr.Length; i++)
{
Expand Down Expand Up @@ -305,11 +303,8 @@ public void ParseUInt64_Test_8()
}

[TestMethod]
public void ParseDouble_Test_x()
public void ParseDouble_Test_Valid_Values()
{
Debug.WriteLine("double MinValue = " + double.MinValue.ToString());
Debug.WriteLine("double MaxValue = " + double.MaxValue.ToString());
//Debug.WriteLine("This currently fails, see 21634 for details");

Random random = new Random();
String[] strArr = new String[] { "0", "-0","+0",
Expand All @@ -318,11 +313,11 @@ public void ParseDouble_Test_x()
"+123", " +123 ", " +123", "+123 "};
double[] _double = new double[] { 0, 0, 0, 0, 0, 0, 0, 0, 123, 123, 123, 123 };

double temp = 0;
double temp;
for (int i = 0; i < strArr.Length; i++)
{
temp = double.Parse(strArr[i]);
Assert.Equal(temp, _double[i]);
Assert.Equal(temp, _double[i], $"Failed while parsing string: {strArr[i]} expecting: {_double[i].ToString()}");
}

double d = double.Parse("-0.1");
Expand All @@ -343,20 +338,49 @@ public void ParseDouble_Test_x()
d = double.Parse("-0.01e-10");
Assert.Equal(d, -0.01e-10);

d = double.Parse(" ");
Assert.Equal(d, 0.0);
// can't use Min/MaxValue.ToString() because the fast float-to-string routine only works in the range 2^64 to 2^-64 (there-about).
string t = "-1.7976931348623157E+308"; // double.MinValue
Assert.Equal(double.MinValue, double.Parse(t), "Testing double min value parse");

t = "1.7976931348623157E+308";
Assert.Equal(double.MaxValue, double.Parse(t), "Testing double max value parse");

t = "-3.40282347E+38";
Assert.Equal(float.MinValue, (float)double.Parse(t), "Testing float min value parse");

t = "3.40282347E+38";
Assert.Equal(float.MaxValue, (float)double.Parse(t), "Testing float max value parse");

}
[TestMethod]
public void ParseDouble_Test_Invalid_Values()
{

String[] strArr = new String[] { "", " ", " ", "-0e-a", "+123a4", " +123f.1", "123ea2", "1.111.1",
" -123-e3", " 123.456 777", "1234567ee73", " +1234e-77+", "++1", "--1", "+1+", " .1123abc", " .123+456",
"+123e++10", "+123e--10", "-123e++10"};

for (int i = 0; i < strArr.Length; i++)
{
Assert.Throws(typeof(FormatException), () => { double.Parse(strArr[i]); }, $"Should throw exception of type FormatExeception while parsing string: '{strArr[i]}'");
}
}

string t = double.MinValue.ToString();
Assert.Equal(double.MinValue, double.Parse(t));
[TestMethod]
public void ParseDouble_OverflowTests()
{
// Note we have to check hex values - again, the ToString() works over a subset of the range for double/float, and returns 'oor' or '-oor' for anything outside that range
string t = "-1.7976931348623180E+320";
Assert.Equal(double.NegativeInfinity, double.Parse(t), "High negative values should return double.NegativeInfinity value when parsed");

t = double.MaxValue.ToString();
Assert.Equal(double.MaxValue, double.Parse(t));
t = "1.7976931348623180E+308";
Assert.Equal(double.PositiveInfinity, double.Parse(t), "High positive values should return double.PositiveInfinity value when parsed");

t = float.MinValue.ToString();
Assert.Equal(float.MinValue, (float)double.Parse(t));
t = "-3.40282380E+38";
Assert.Equal(float.NegativeInfinity, float.Parse(t), "High negative values should return float.NegativeInfinity value when parsed");

t = float.MaxValue.ToString();
Assert.Equal(float.MaxValue, (float)double.Parse(t));
t = "3.40282380E+38";
Assert.Equal(float.PositiveInfinity, float.Parse(t), "High positive values should return float.PositiveInfinity value when parsed");

}

Expand Down Expand Up @@ -566,7 +590,6 @@ private static string GetRandomString()
chars[i] = (char)('0' + s_random.Next(10));
break;
}
break;
}

return new string(chars);
Expand All @@ -581,12 +604,12 @@ public void ParseSByte_FormatException_Test_25()
String[] strArr = new String[] { "", "1,234", "123e5", "a", "3.14159265358979" };
for (int i = 0; i < strArr.Length; i++)
{
Assert.Throws(typeof(Exception), () => { SByte.Parse(strArr[i]); });
Assert.Throws(typeof(FormatException), () => { SByte.Parse(strArr[i]); });
}
for (int i = 0; i < 5; i++)
{
String rdmString = GetRandomString();
Assert.Throws(typeof(Exception), () => { SByte.Parse(rdmString); });
Assert.Throws(typeof(FormatException), () => { SByte.Parse(rdmString); });
}
}

Expand All @@ -596,12 +619,12 @@ public void ParseByte_FormatException_Test_26()
String[] strArr = new String[] { "", "1,234", "123e5", "a", "3.14159265358979" };
for (int i = 0; i < strArr.Length; i++)
{
Assert.Throws(typeof(Exception), () => { Byte.Parse(strArr[i]); });
Assert.Throws(typeof(FormatException), () => { Byte.Parse(strArr[i]); }, $"Value '{strArr[i]}' did not throw exception of type FormatException");
}
for (int i = 0; i < 5; i++)
{
String rdmString = GetRandomString();
Assert.Throws(typeof(Exception), () => { Byte.Parse(rdmString); });
Assert.Throws(typeof(FormatException), () => { Byte.Parse(rdmString); }, $"Random string '{rdmString}' did not throw exception of FormatException");
}
}

Expand All @@ -611,12 +634,12 @@ public void ParseInt16_FormatException_Test_27()
String[] strArr = new String[] { "", "1,234", "123e5", "a", "3.14159265358979" };
for (int i = 0; i < strArr.Length; i++)
{
Assert.Throws(typeof(Exception), () => { Int16.Parse(strArr[i]); });
Assert.Throws(typeof(FormatException), () => { Int16.Parse(strArr[i]); });
}
for (int i = 0; i < 5; i++)
{
String rdmString = GetRandomString();
Assert.Throws(typeof(Exception), () => { Int16.Parse(rdmString); });
Assert.Throws(typeof(FormatException), () => { Int16.Parse(rdmString); });
}
}

Expand All @@ -626,12 +649,12 @@ public void ParseUInt16_FormatException_Test_28()
String[] strArr = new String[] { "", "1,234", "123e5", "a", "3.14159265358979" };
for (int i = 0; i < strArr.Length; i++)
{
Assert.Throws(typeof(Exception), () => { UInt16.Parse(strArr[i]); });
Assert.Throws(typeof(FormatException), () => { UInt16.Parse(strArr[i]); });
}
for (int i = 0; i < 5; i++)
{
String rdmString = GetRandomString();
Assert.Throws(typeof(Exception), () => { UInt16.Parse(rdmString); });
Assert.Throws(typeof(FormatException), () => { UInt16.Parse(rdmString); });
}
}

Expand All @@ -641,12 +664,12 @@ public void ParseInt32_FormatException_Test_29()
String[] strArr = new String[] { "", "1,234", "123e5", "a", "3.14159265358979" };
for (int i = 0; i < strArr.Length; i++)
{
Assert.Throws(typeof(Exception), () => { Int32.Parse(strArr[i]); });
Assert.Throws(typeof(FormatException), () => { Int32.Parse(strArr[i]); });
}
for (int i = 0; i < 5; i++)
{
String rdmString = GetRandomString();
Assert.Throws(typeof(Exception), () => { Int32.Parse(rdmString); });
Assert.Throws(typeof(FormatException), () => { Int32.Parse(rdmString); });
}
}

Expand All @@ -656,12 +679,12 @@ public void ParseUInt32_FormatException_Test_30()
String[] strArr = new String[] { "", "1,234", "123e5", "a", "3.14159265358979" };
for (int i = 0; i < strArr.Length; i++)
{
Assert.Throws(typeof(Exception), () => { UInt32.Parse(strArr[i]); });
Assert.Throws(typeof(FormatException), () => { UInt32.Parse(strArr[i]); });
}
for (int i = 0; i < 5; i++)
{
String rdmString = GetRandomString();
Assert.Throws(typeof(Exception), () => { UInt32.Parse(rdmString); });
Assert.Throws(typeof(FormatException), () => { UInt32.Parse(rdmString); });
}
}

Expand All @@ -671,12 +694,12 @@ public void ParseInt64_FormatException_Test_31()
String[] strArr = new String[] { "", "1,234", "123e5", "a", "3.14159265358979" };
for (int i = 0; i < strArr.Length; i++)
{
Assert.Throws(typeof(Exception), () => { Int64.Parse(strArr[i]); });
Assert.Throws(typeof(FormatException), () => { Int64.Parse(strArr[i]); });
}
for (int i = 0; i < 5; i++)
{
String rdmString = GetRandomString();
Assert.Throws(typeof(Exception), () => { Int64.Parse(rdmString); });
Assert.Throws(typeof(FormatException), () => { Int64.Parse(rdmString); });
}
}

Expand All @@ -686,12 +709,12 @@ public void ParseUInt64_FormatException_Test_32()
String[] strArr = new String[] { "", "1,234", "123e5", "a", "3.14159265358979" };
for (int i = 0; i < strArr.Length; i++)
{
Assert.Throws(typeof(Exception), () => { UInt64.Parse(strArr[i]); });
Assert.Throws(typeof(FormatException), () => { UInt64.Parse(strArr[i]); });
}
for (int i = 0; i < 5; i++)
{
String rdmString = GetRandomString();
Assert.Throws(typeof(Exception), () => { UInt64.Parse(rdmString); });
Assert.Throws(typeof(FormatException), () => { UInt64.Parse(rdmString); });
}
}

Expand All @@ -707,7 +730,7 @@ public void ParseSByte_OverflowException_Test_33()
((Int64)SByte.MaxValue + 1).ToString(), ((Int64)SByte.MaxValue + 100).ToString() };
for (int i = 0; i < strArr.Length; i++)
{
Assert.Throws(typeof(Exception), () => { SByte.Parse(strArr[i]); });
Assert.Throws(typeof(ArgumentOutOfRangeException), () => { SByte.Parse(strArr[i]); }, $"The value '{strArr[i]}' did not produce an exception type of ArgumentOutOfRange");
}
}

Expand All @@ -718,7 +741,7 @@ public void ParseByte_OverflowException_Test_34()
((Int64)Byte.MaxValue + 1).ToString(), ((Int64)Byte.MaxValue + 100).ToString() };
for (int i = 0; i < strArr.Length; i++)
{
Assert.Throws(typeof(Exception), () => { Byte.Parse(strArr[i]); });
Assert.Throws(typeof(ArgumentOutOfRangeException), () => { Byte.Parse(strArr[i]); });
}
}

Expand All @@ -729,7 +752,7 @@ public void ParseInt16_OverflowException_Test_35()
((Int64)Int16.MaxValue + 1).ToString(), ((Int64)Int16.MaxValue + 100).ToString() };
for (int i = 0; i < strArr.Length; i++)
{
Assert.Throws(typeof(Exception), () => { Int16.Parse(strArr[i]); });
Assert.Throws(typeof(ArgumentOutOfRangeException), () => { Int16.Parse(strArr[i]); });
}
}

Expand All @@ -740,7 +763,7 @@ public void ParseUInt16_OverflowException_Test_36()
((Int64)UInt16.MaxValue + 1).ToString(), ((Int64)UInt16.MaxValue + 100).ToString() };
for (int i = 0; i < strArr.Length; i++)
{
Assert.Throws(typeof(Exception), () => { UInt16.Parse(strArr[i]); });
Assert.Throws(typeof(ArgumentOutOfRangeException), () => { UInt16.Parse(strArr[i]); });
}
}

Expand All @@ -751,7 +774,7 @@ public void ParseInt32_OverflowException_Test_37()
((Int64)Int32.MaxValue + 1).ToString(), ((Int64)Int32.MaxValue + 100).ToString() };
for (int i = 0; i < strArr.Length; i++)
{
Assert.Throws(typeof(Exception), () => { Int32.Parse(strArr[i]); });
Assert.Throws(typeof(ArgumentOutOfRangeException), () => { Int32.Parse(strArr[i]); });
}
}

Expand All @@ -762,19 +785,20 @@ public void ParseUInt32_OverflowException_Test_38()
((Int64)UInt32.MaxValue + 1).ToString(), ((Int64)UInt32.MaxValue + 100).ToString() };
for (int i = 0; i < strArr.Length; i++)
{
Assert.Throws(typeof(Exception), () => { UInt32.Parse(strArr[i]); });
Assert.Throws(typeof(ArgumentOutOfRangeException), () => { UInt32.Parse(strArr[i]); });
}
}

[TestMethod]
public void ParseInt64_OverflowException_Test_39()
{
Debug.WriteLine("This currently fails, see 21641 for details");

string[] strArr = new string[] { "-9223372036854775809", "-9223372036854775900",
"9223372036854775808", "9223372036854775900" };
for (int i = 0; i < strArr.Length; i++)
{
Assert.Throws(typeof(Exception), () => { Int64.Parse(strArr[i]); });
Assert.Throws(typeof(ArgumentOutOfRangeException), () => { Int64.Parse(strArr[i]); }, $"An exception of type ArgumentOutOfRangeException was not thrown when values was {strArr[i]}");
}
}

Expand All @@ -784,7 +808,7 @@ public void ParseUInt64_OverflowException_Test_40()
string[] strArr = new string[] { "-1", "-100", "18446744073709551616", "18446744073709551700" };
for (int i = 0; i < strArr.Length; i++)
{
Assert.Throws(typeof(Exception), () => { UInt64.Parse(strArr[i]); });
Assert.Throws(typeof(ArgumentOutOfRangeException), () => { UInt64.Parse(strArr[i]); });
}
}

Expand Down Expand Up @@ -852,27 +876,28 @@ public void box_unbox_Test_1()
// Now casts that should throw exception. Any cast that does not throw - means error.
Assert.Throws(typeof(InvalidCastException), () => {
MyEnum1 e1 = (MyEnum1)o_enum;
});
}, "Trying to cast incompatible enums - should throw InvalidCastException");

// Now casts that should throw exception. Any cast that does not throw - means error.
Assert.Throws(typeof(InvalidCastException), () => {
int i = (int)o_long;
});
}, "Trying to cast long to int - should throw InvalidCastException");

// Now casts that should throw exception. Any cast that does not throw - means error.
Assert.Throws(typeof(InvalidCastException), () => {
int i = (int)o_class;
});
}, "Trying to cast object to int - should throw InvalidCastException");

// Now casts that should throw exception. Any cast that does not throw - means error.
Assert.Throws(typeof(InvalidCastException), () => {
int i = (int)o_enum;
});
}, "Trying to cast enum to int - should throw InvalidCastException");

// Now casts that should throw exception. Any cast that does not throw - means error.
Assert.Throws(typeof(InvalidCastException), () => {
int i = (int)o_guid;
});
//Assert.Throws(typeof(InvalidCastException), () => {
// int i = (int)o_guid;
//}, "Trying to cast Guid to int - should throw InvalidCastException");
Assert.SkipTest("test of casting guid disabled");
}

}
Expand Down
4 changes: 2 additions & 2 deletions Tests/NFUnitTestSystemLib/UnitTestTypeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -94,11 +94,11 @@ public void Number_ToString_Test()

Assert.Equal("NaN", ((float)0f / 0f).ToString());
Assert.Equal("Infinity", ((float)1f / 0f).ToString());
Assert.Equal("-Infinity", ((float)-1f / 0f).ToString());
Assert.Equal("-Infinity", ((float)(-1f / 0f)).ToString(),"float negative infinity test");

Assert.Equal("NaN", ((double)0f / 0f).ToString());
Assert.Equal("Infinity", ((double)1f / 0f).ToString());
Assert.Equal("-Infinity", ((double)-1f / 0f).ToString());
Assert.Equal("-Infinity", double.NegativeInfinity.ToString(), "double negative infinity"); // ((double)1f / -0f).ToString());


Assert.Equal("Da1x", (1).ToString("Da1x"));
Expand Down
1 change: 1 addition & 0 deletions nanoFramework.CoreLibrary/CoreLibrary.nfproj
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@
<Compile Include="System\EventHandler.cs" />
<Compile Include="System\Exception.cs" />
<Compile Include="System\FlagsAttribute.cs" />
<Compile Include="System\FormatException.cs" />
<Compile Include="System\GC.cs" />
<Compile Include="System\Globalization\CultureInfo.cs" />
<Compile Include="System\Globalization\DateTimeFormat.cs" />
Expand Down
Loading