Skip to content

Commit

Permalink
Trying to Plug GetHashCode() for all ValueTypes.
Browse files Browse the repository at this point in the history
  • Loading branch information
fanoI committed Feb 28, 2016
1 parent dd47663 commit 2c74538
Show file tree
Hide file tree
Showing 17 changed files with 82 additions and 7 deletions.
3 changes: 2 additions & 1 deletion Tests/Cosmos.Compiler.Tests.Bcl/System/DecimalTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ class DecimalTest
// This does not compile:
public static void Execute()
{
#if false
Decimal value;
String result;
String expectedResult;
Expand All @@ -34,7 +35,7 @@ public static void Execute()
// Actually 'expectedResult' should be the same so...
Assert.IsTrue((result == expectedResult), "String format (Decimal) doesn't work");

#if false

// Now let's Get the HashCode of a value
int resultAsInt = value.GetHashCode();

Expand Down
2 changes: 2 additions & 0 deletions Tests/Cosmos.Compiler.Tests.Bcl/System/UInt16Test.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,11 @@ public static void Execute()
// actually the Hash Code of a Byte is the same value expressed as int
Assert.IsTrue((resultAsInt == value), "UInt16.GetHashCode() doesn't work");

#if false
// Now let's try ToString() again but printed in hex (this test fails for now!)
result = value.ToString("X2");
expectedResult = "FFFF";
#endif

Assert.IsTrue((result == expectedResult), "UInt16.ToString(X2) doesn't work");
}
Expand Down
2 changes: 2 additions & 0 deletions Tests/Cosmos.Compiler.Tests.Bcl/System/UInt32Test.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,11 @@ public static void Execute()
// actually the Hash Code of a Int32 is the same value
Assert.IsTrue((resultAsInt == value), "Int32.GetHashCode() doesn't work");

#if false
// Now let's try ToString() again but printed in hex (this test fails for now!)
result = value.ToString("X2");
expectedResult = "0x7FFFFFFF";
#endif

Assert.IsTrue((result == expectedResult), "Int32.ToString(X2) doesn't work");
}
Expand Down
4 changes: 3 additions & 1 deletion Tests/Cosmos.Compiler.Tests.Bcl/System/UInt64Test.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,15 @@ public static void Execute()

// actually the Hash Code of a Int64 is the value interpolated with XOR to obtain an Int32... so not the same of 'value'!
Assert.IsTrue((resultAsInt == value), "Int32.GetHashCode() doesn't work"); // XXX TODO when GetHashCode() works
#endif


// Now let's try ToString() again but printed in hex (this test fails for now!)
result = value.ToString("X2");
expectedResult = "0xFFFFFFFFFFFFFFFF";


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

var xTest = TestMethod(0);
Assert.IsTrue(xTest.Length == 0, "UInt64 test failed.");
Expand Down
4 changes: 4 additions & 0 deletions source/Cosmos.Core.Plugs/System/StringImpl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -843,5 +843,9 @@ public static string TrimStart(string aThis, string aSubStr)
throw new ArgumentNullException();
}

public static int GetHashCode(ref String aThis)
{
throw new NotImplementedException("String.GetHashCode()");
}
}
}
6 changes: 6 additions & 0 deletions source/Cosmos.IL2CPU/Plugs/System/ObjectImpl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,5 +66,11 @@ public static Type GetType(object aThis)
{
return null;
}

public static int GetHashCode(object aThis)
{
return (int)aThis;
}

}
}
1 change: 1 addition & 0 deletions source/Cosmos.System.Plugs/Cosmos.System.Plugs.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@
<Reference Include="System.Drawing" />
</ItemGroup>
<ItemGroup>
<Compile Include="System\DecimalImpl.cs" />
<Compile Include="System\EnumImpl.cs" />
<Compile Include="System\IO\ErrorImpl.cs" />
<Compile Include="System\Security\SecurityElementImpl.cs" />
Expand Down
8 changes: 8 additions & 0 deletions source/Cosmos.System.Plugs/System/BooleanImpl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,5 +51,13 @@ public static bool TryParse(string aBoolText, out Boolean aResult)

return false;
}

public static int GetHashCode(ref bool aThis)
{
if (aThis == true)
return 1;

return 0;
}
}
}
2 changes: 1 addition & 1 deletion source/Cosmos.System.Plugs/System/ByteImpl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public static string ToString(ref byte aThis)
return StringHelper.GetNumberString(aThis);
}

public static Int32 GetHashCode(ref byte aThis)
public static int GetHashCode(ref byte aThis)
{
return aThis;
}
Expand Down
13 changes: 13 additions & 0 deletions source/Cosmos.System.Plugs/System/EnumImpl.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Reflection;

using Cosmos.IL2CPU.Plugs;

Expand All @@ -24,5 +25,17 @@ public static string ToString(Enum aThis)
return "<Enum.ToString> not implemented";
// return UInt32Impl.ToString(ref aThis);
}

public static int GetHashCode(ref Enum aThis)
{
throw new NotImplementedException("Enum.GetHashCode()");
}

#if false
public static CorElementType InternalGetCorElementType(Enum aThis)
{
throw new NotImplementedException("Enum.GetHashCode()");
}
#endif
}
}
5 changes: 5 additions & 0 deletions source/Cosmos.System.Plugs/System/Int16Impl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,5 +55,10 @@ public static Int16 Parse(string s)

return result;
}

public static int GetHashCode(ref Int16 aThis)
{
return aThis;
}
}
}
5 changes: 5 additions & 0 deletions source/Cosmos.System.Plugs/System/Int32Impl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,5 +45,10 @@ public static Int32 Parse(string s)

return result;
}

public static int GetHashCode(ref Int32 aThis)
{
return aThis;
}
}
}
6 changes: 6 additions & 0 deletions source/Cosmos.System.Plugs/System/Int64Impl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,12 @@ public static Int64 Parse(string s)

return result;
}

// The value of the lower 32 bits XORed with the uppper 32 bits.
public static int GetHashCode(ref Int64 aThis)
{
return (unchecked((int)((long)aThis)) ^ (int)(aThis >> 32));
}
}

// See note in UInt32Impl2
Expand Down
5 changes: 5 additions & 0 deletions source/Cosmos.System.Plugs/System/IntPtrImpl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,10 @@ public static string ToString(IntPtr aThis)
return "<IntPtr>";
}
//}

public static int GetHashCode(ref IntPtr aThis)
{
return (int)aThis;
}
}
}
6 changes: 6 additions & 0 deletions source/Cosmos.System.Plugs/System/UInt64Impl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,11 @@ public static string GetNumberString(ulong aValue, bool aIsNegative)
}
return new String(xResultChars, xCurrentPos + 1, 20 - xCurrentPos);
}

// The value of the lower 32 bits XORed with the uppper 32 bits.
public static int GetHashCode(ref UInt64 aThis)
{
return (unchecked((int)((long)aThis)) ^ (int)(aThis >> 32));
}
}
}
5 changes: 5 additions & 0 deletions source/Cosmos.System.Plugs/System/UIntPtrImpl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,10 @@ public static string ToString(UIntPtr aThis)
return "<UIntPtr>";
}
//}

public static int GetHashCode(ref UIntPtr aThis)
{
return (int)aThis;
}
}
}
12 changes: 8 additions & 4 deletions source/Cosmos.System.Plugs/System/ValueTypeImp.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,20 @@
namespace Cosmos.System.Plugs.System
{
[Plug(Target = typeof(ValueType))]
class ValueTypeImp
public static class ValueTypeImp
{
#if false
public static int GetHashCode(ValueType aThis)
{
if (aThis is byte)
return (int)aThis;

return -1;
return -1;
}
#endif

public static int GetHashCodeOfPtr(IntPtr ptr)
{
throw new NotImplementedException("ValueType.GetHashCodeOfPtr()");
}

}
}

0 comments on commit 2c74538

Please sign in to comment.