Skip to content

Commit

Permalink
Added some globalization and culture plugs. Also added string and str…
Browse files Browse the repository at this point in the history
…uct tests.
  • Loading branch information
charlesbetros committed Feb 21, 2016
1 parent d790a6a commit 815f2f7
Show file tree
Hide file tree
Showing 13 changed files with 262 additions and 75 deletions.
16 changes: 14 additions & 2 deletions Tests/Cosmos.Compiler.Tests.Bcl/System/StringTest.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,28 @@
using System;
using System.Linq;
using System.Threading.Tasks;

using Cosmos.Debug.Kernel;
using Cosmos.TestRunner;

namespace Cosmos.Compiler.Tests.Bcl.System
{
public static class StringTest
{
static Debugger mDebugger = new Debugger("Tests", "String Tests");

public static void Execute()
{
Assert.IsTrue(("a" + "b") == "ab", "concatting 2 string using + doesn't work");
Assert.IsTrue(("a" + 'b') == "ab", "concatting 1 string and 1 character doesn't work");
Assert.IsTrue(string.Empty == "", "string.Empty == \"\"");
int xResult = string.Compare("a", "a");
mDebugger.Send(xResult.ToString());
Assert.IsTrue(xResult == 0, "string.Compare(\"a\", \"a\") == 0");

Assert.IsTrue(
string.Compare("abc", "abc") == 0, "string.Compare(\"abc\", \"abc\") == 0");
Assert.IsTrue(("a" + "b") == "ab", "(\"a\" + \"b\") == \"ab\"");
Assert.IsTrue(("a" + 'b') == "ab", "concatting 1 string and 1 character doesn\"t work");
Assert.IsTrue(string.Concat("a", "b") == "ab", "string.Concat(\"a\", \"b\") == \"ab\"");
}
}
}
54 changes: 41 additions & 13 deletions Tests/SimpleStructsAndArraysTest/Kernel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

namespace SimpleStructsAndArraysTest
{
public class Kernel: Sys.Kernel
public class Kernel : Sys.Kernel
{
protected override void BeforeRun()
{
Expand Down Expand Up @@ -53,13 +53,13 @@ private static void TestStep1()
//Console.Write("Char: ");
//Console.WriteLine(xResult.KeyChar);
var xItem = new MyStruct
{
A = 1,
B = 2,
C = 3,
D = 4,
E = 5
};
{
A = 1,
B = 2,
C = 3,
D = 4,
E = 5
};

var xArray = new MyStruct[1];
xArray[0] = xItem;
Expand Down Expand Up @@ -216,20 +216,20 @@ protected static void TestOurList()
var xListClasses = new OurList<KVPClass>();
var xListStructs = new OurList<KVPStruct>();

xListClasses.Add(new KVPClass {Key = 1, Value = 2});
xListClasses.Add(new KVPClass {Key = 2, Value = 5});
xListClasses.Add(new KVPClass { Key = 1, Value = 2 });
xListClasses.Add(new KVPClass { Key = 2, Value = 5 });

OurList<KVPClass>.ExpectedIndex = 0;
var xListItem = xListClasses[0];
Assert.AreEqual(1, xListItem.Key, "xListClasses[0].Key == 1");
Assert.AreEqual(2, xListItem.Value, "xListClasses[0].Value == 2");
OurList<KVPClass>.ExpectedIndex = 1;
xListItem = xListClasses[1];
Assert.AreEqual(2, xListItem.Key, "xListClasses[1].Key == 2");
Assert.AreEqual(2, xListItem.Key, "xListClasses[1].Key == 2");
Assert.AreEqual(5, xListItem.Value, "xListClasses[1].Value == 5");

xListStructs.Add(new KVPStruct {Key = 1, Value = 2});
xListStructs.Add(new KVPStruct {Key = 2, Value = 5});
xListStructs.Add(new KVPStruct { Key = 1, Value = 2 });
xListStructs.Add(new KVPStruct { Key = 2, Value = 5 });

OurList<KVPStruct>.ExpectedIndex = 0;
var xStructItem = xListStructs[0];
Expand Down Expand Up @@ -268,13 +268,41 @@ protected static void TestStandardList()
Assert.AreEqual(5, xStructItem.Value, "xListStructs[1].Value == 5");
}

private interface ITestMutate
{
void Mutate();
}

private struct TestMutateStruct : ITestMutate
{
int a;

public void Mutate()
{
a++;
}
}

private void DoMutate<T>(ref T x) where T : ITestMutate
{
x.Mutate();
}

private void MutateStructTest()
{
TestMutateStruct a = new TestMutateStruct();
DoMutate(ref a);
}

protected override void Run()
{
TestStep1();
TestOurList();
Assert.IsTrue(true, "After TestOurList");
TestStandardList();
Assert.IsTrue(true, "After TestStandardList");
MutateStructTest();
Assert.IsTrue(true, "After MutateTestStruct");
TestController.Completed();
}
}
Expand Down
40 changes: 40 additions & 0 deletions source/Cosmos.Common/StringHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,15 @@ public static class StringHelper
{
internal static Debugger mDebugger = new Debugger("Common", "String Helpers");

internal enum StringComparisonResultEnum
{
Less = -1,

Equal = 0,

Greater = 1
}

public static string GetCharArrayString(char[] aArray)
{
if (aArray == null)
Expand Down Expand Up @@ -131,5 +140,36 @@ public static int GetStringToNumber(string aString)

return xNumber;
}

public static int Compare(
string aString1,
int aIndex1,
string aString2,
int aIndex2,
int aLength1,
int aLength2)
{
if (aString1.Length < aString2.Length)
{
return (int)StringComparisonResultEnum.CSTR_LESS_THAN;
}
if (aString1.Length > aString2.Length)
{
return (int)StringComparisonResultEnum.CSTR_GREATER_THAN;
}

for (int i = aString1.Length; i < aString1.Length; i++)
{
if (aString1[i] < aString2[i])
{
return (int)StringComparisonResultEnum.CSTR_LESS_THAN;
}
if (aString1[i] > aString2[i])
{
return (int)StringComparisonResultEnum.CSTR_GREATER_THAN;
}
}
return (int)StringComparisonResultEnum.CSTR_EQUAL;
}
}
}
5 changes: 5 additions & 0 deletions source/Cosmos.Core.Plugs/Cosmos.Core.Plugs.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,10 @@
<Compile Include="System\Assemblers\InvokeImplAssembler.cs" />
<Compile Include="System\Buffer.cs" />
<Compile Include="System\DelegateImpl.cs" />
<Compile Include="System\Globalization\CompareInfoImpl.cs" />
<Compile Include="System\Globalization\CultureInfoImpl.cs" />
<Compile Include="System\Globalization\NumberFormatInfoImpl.cs" />
<Compile Include="System\Globalization\TextInfoImpl.cs" />
<Compile Include="System\IO\PathHelperImpl.cs" />
<Compile Include="System\MulticastDelegateImpl.cs" />
<Compile Include="System\NormalDelegateImpl.cs" />
Expand Down Expand Up @@ -130,6 +134,7 @@
</ItemGroup>
<ItemGroup>
<None Include="Cosmos.snk" />
<None Include="packages.config" />
</ItemGroup>
<ItemGroup />
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
Expand Down
33 changes: 33 additions & 0 deletions source/Cosmos.Core.Plugs/System/Globalization/CompareInfoImpl.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#define COSMOSDEBUG

using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using Cosmos.Common;
using Cosmos.Debug.Kernel;
using Cosmos.IL2CPU.Plugs;

namespace Cosmos.Core.Plugs.System.Globalization
{
[Plug(Target = typeof(CompareInfo))]
public static class CompareInfoImpl
{
static Debugger mDebugger = new Debugger("Core", "Compare Info Plug");

public static void Ctor(CompareInfo aThis, CultureInfo culture)
{
mDebugger.SendInternal("CompareInfo::Ctor");
}

public static int Compare(CompareInfo aThis, string aString1, string aString2, CompareOptions aOptions)
{
#warning TODO: Implement CompareOptions
mDebugger.SendInternal("CompareInfo.Compare");
return StringHelper.Compare(aString1, 0, aString2, 0, aString1.Length, aString2.Length);
}
}
}
46 changes: 46 additions & 0 deletions source/Cosmos.Core.Plugs/System/Globalization/CultureInfoImpl.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
using System.Globalization;

using Cosmos.Debug.Kernel;
using Cosmos.IL2CPU.Plugs;

namespace Cosmos.Core.Plugs.System.Globalization
{
[Plug(Target = typeof(CultureInfo))]
public static class CultureInfoImpl
{
static Debugger mDebugger = new Debugger("Core", "Compare Info Plug");

public static void Ctor(CultureInfo aThis, string name, bool useUserOverride)
{
}

public static bool get_UseUserOverride(CultureInfo aThis)
{
return false;
}

public static CultureInfo get_CurrentCulture()
{
return new CultureInfo("en-us");
}

public static CultureInfo get_InvariantCulture()
{
return null;
}

public static void CCtor()
{
}

public static CultureInfo GetCultureInfo(string aName)
{
return null;
}

public static bool Equals(CultureInfo aThis, object aThat)
{
return ReferenceEquals(aThis, aThat);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using System;
using System.Globalization;

using Cosmos.IL2CPU.Plugs;

namespace Cosmos.Core.Plugs.System.Globalization
{
[Plug(Target = typeof(NumberFormatInfo))]
public static class NumberFormatInfoImpl
{
public static NumberFormatInfo GetInstance(IFormatProvider aProvider)
{
return null;
}


public static NumberFormatInfo get_CurrentInfo()
{
return null;
}
}
}
34 changes: 34 additions & 0 deletions source/Cosmos.Core.Plugs/System/Globalization/TextInfoImpl.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using Cosmos.Common;
using Cosmos.Debug.Kernel;
using Cosmos.IL2CPU.Plugs;

namespace Cosmos.Core.Plugs.System.Globalization
{
[Plug(Target = typeof(TextInfo))]
public static class TextInfoImpl
{
static Debugger mDebugger = new Debugger("Core", "Compare Info Plug");

public static void Ctor(TextInfo aThis, object cultureData)
{
}

public static int InternalCompareStringOrdinalIgnoreCase(string aString1, int aIndex1, string aString2, int aIndex2, int aLength1, int aLength2)
{
mDebugger.SendInternal("InternalCompareStringOrdinalIgnoreCase");
mDebugger.SendInternal(aString1);
mDebugger.SendInternal(aString2);

string xString1 = aString1.ToLower();
string xString2 = aString2.ToLower();
return StringHelper.Compare(xString1, aIndex1, xString2, aIndex2, aLength1, aLength2);
}
}
}
31 changes: 21 additions & 10 deletions source/Cosmos.Core.Plugs/System/StringImpl.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System;
using System.Globalization;
using System.Text;

using Cosmos.Common;
using Cosmos.IL2CPU.Plugs;
Expand Down Expand Up @@ -51,11 +50,11 @@ public static unsafe void Ctor(
}

public static unsafe void Ctor(
string aThis,
char[] aChars,
[FieldAccess(Name = "System.String System.String.Empty")] ref string aStringEmpty,
[FieldAccess(Name = "System.Int32 System.String.m_stringLength")] ref int aStringLength,
[FieldAccess(Name = "System.Char System.String.m_firstChar")] char* aFirstChar)
string aThis,
char[] aChars,
[FieldAccess(Name = "System.String System.String.Empty")] ref string aStringEmpty,
[FieldAccess(Name = "System.Int32 System.String.m_stringLength")] ref int aStringLength,
[FieldAccess(Name = "System.Char System.String.m_firstChar")] char* aFirstChar)
{
aStringEmpty = "";
aStringLength = aChars.Length;
Expand Down Expand Up @@ -595,8 +594,6 @@ public static bool EndsWith(string aThis, string aSubStr, StringComparison aComp
}
}

// System.Int32 System.String.IndexOf(System.String, System.Int32, System.Int32, System.StringComparison)

public static bool Equals(string aThis, string aThat, StringComparison aComparison)
{
#warning TODO: implement
Expand All @@ -609,9 +606,23 @@ public static bool Equals(string aThis, string aThat, StringComparison aComparis
return EqualsHelper(aThis, aThat);
}

public static bool EqualsHelper(string aStrA, string aStrB)
public static bool EqualsHelper(string strA, string strB)
{
return aStrA.CompareTo(aStrB) == 0;
int xLength1 = strA.Length;
int xLength2 = strB.Length;

if (xLength1 != xLength2)
{
return false;
}
for (int i = 0; i < xLength1; i++)
{
if (strA[i] != strB[i])
{
return false;
}
}
return true;
}

private static bool CharArrayContainsChar(char[] aArray, char aChar)
Expand Down
Loading

0 comments on commit 815f2f7

Please sign in to comment.