From 815f2f78a48abc01b09a3cbdf6f8ae9d9d545b65 Mon Sep 17 00:00:00 2001 From: Charles Betros Date: Sun, 21 Feb 2016 10:13:29 -0600 Subject: [PATCH] Added some globalization and culture plugs. Also added string and struct tests. --- .../System/StringTest.cs | 16 +++++- Tests/SimpleStructsAndArraysTest/Kernel.cs | 54 ++++++++++++++----- source/Cosmos.Common/StringHelper.cs | 40 ++++++++++++++ .../Cosmos.Core.Plugs.csproj | 5 ++ .../System/Globalization/CompareInfoImpl.cs | 33 ++++++++++++ .../System/Globalization/CultureInfoImpl.cs | 46 ++++++++++++++++ .../Globalization/NumberFormatInfoImpl.cs | 22 ++++++++ .../System/Globalization/TextInfoImpl.cs | 34 ++++++++++++ source/Cosmos.Core.Plugs/System/StringImpl.cs | 31 +++++++---- source/Cosmos.IL2CPU/Cosmos.IL2CPU.csproj | 2 - .../System/Globalization/CultureInfoImpl.cs | 25 --------- .../Globalization/NumberFormatInfoImpl.cs | 19 ------- .../FileSystem/VFS/VFSManager.cs | 10 ++-- 13 files changed, 262 insertions(+), 75 deletions(-) create mode 100644 source/Cosmos.Core.Plugs/System/Globalization/CompareInfoImpl.cs create mode 100644 source/Cosmos.Core.Plugs/System/Globalization/CultureInfoImpl.cs create mode 100644 source/Cosmos.Core.Plugs/System/Globalization/NumberFormatInfoImpl.cs create mode 100644 source/Cosmos.Core.Plugs/System/Globalization/TextInfoImpl.cs delete mode 100644 source/Cosmos.IL2CPU/Plugs/System/Globalization/CultureInfoImpl.cs delete mode 100644 source/Cosmos.IL2CPU/Plugs/System/Globalization/NumberFormatInfoImpl.cs diff --git a/Tests/Cosmos.Compiler.Tests.Bcl/System/StringTest.cs b/Tests/Cosmos.Compiler.Tests.Bcl/System/StringTest.cs index e36319480..b0666f67a 100644 --- a/Tests/Cosmos.Compiler.Tests.Bcl/System/StringTest.cs +++ b/Tests/Cosmos.Compiler.Tests.Bcl/System/StringTest.cs @@ -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\""); } } } diff --git a/Tests/SimpleStructsAndArraysTest/Kernel.cs b/Tests/SimpleStructsAndArraysTest/Kernel.cs index 81572fcd6..fe40603aa 100644 --- a/Tests/SimpleStructsAndArraysTest/Kernel.cs +++ b/Tests/SimpleStructsAndArraysTest/Kernel.cs @@ -7,7 +7,7 @@ using Sys = Cosmos.System; namespace SimpleStructsAndArraysTest { - public class Kernel: Sys.Kernel + public class Kernel : Sys.Kernel { protected override void BeforeRun() { @@ -53,13 +53,13 @@ namespace SimpleStructsAndArraysTest //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; @@ -216,8 +216,8 @@ namespace SimpleStructsAndArraysTest var xListClasses = new OurList(); var xListStructs = new OurList(); - 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.ExpectedIndex = 0; var xListItem = xListClasses[0]; @@ -225,11 +225,11 @@ namespace SimpleStructsAndArraysTest Assert.AreEqual(2, xListItem.Value, "xListClasses[0].Value == 2"); OurList.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.ExpectedIndex = 0; var xStructItem = xListStructs[0]; @@ -268,6 +268,32 @@ namespace SimpleStructsAndArraysTest 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(ref T x) where T : ITestMutate + { + x.Mutate(); + } + + private void MutateStructTest() + { + TestMutateStruct a = new TestMutateStruct(); + DoMutate(ref a); + } + protected override void Run() { TestStep1(); @@ -275,6 +301,8 @@ namespace SimpleStructsAndArraysTest Assert.IsTrue(true, "After TestOurList"); TestStandardList(); Assert.IsTrue(true, "After TestStandardList"); + MutateStructTest(); + Assert.IsTrue(true, "After MutateTestStruct"); TestController.Completed(); } } diff --git a/source/Cosmos.Common/StringHelper.cs b/source/Cosmos.Common/StringHelper.cs index dd5eee374..0bb55d367 100644 --- a/source/Cosmos.Common/StringHelper.cs +++ b/source/Cosmos.Common/StringHelper.cs @@ -12,6 +12,15 @@ namespace Cosmos.Common { 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) @@ -131,5 +140,36 @@ namespace Cosmos.Common 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; + } } } diff --git a/source/Cosmos.Core.Plugs/Cosmos.Core.Plugs.csproj b/source/Cosmos.Core.Plugs/Cosmos.Core.Plugs.csproj index 8423972e4..89aba91a8 100644 --- a/source/Cosmos.Core.Plugs/Cosmos.Core.Plugs.csproj +++ b/source/Cosmos.Core.Plugs/Cosmos.Core.Plugs.csproj @@ -86,6 +86,10 @@ + + + + @@ -130,6 +134,7 @@ + diff --git a/source/Cosmos.Core.Plugs/System/Globalization/CompareInfoImpl.cs b/source/Cosmos.Core.Plugs/System/Globalization/CompareInfoImpl.cs new file mode 100644 index 000000000..69fa4b423 --- /dev/null +++ b/source/Cosmos.Core.Plugs/System/Globalization/CompareInfoImpl.cs @@ -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); + } + } +} diff --git a/source/Cosmos.Core.Plugs/System/Globalization/CultureInfoImpl.cs b/source/Cosmos.Core.Plugs/System/Globalization/CultureInfoImpl.cs new file mode 100644 index 000000000..00427e002 --- /dev/null +++ b/source/Cosmos.Core.Plugs/System/Globalization/CultureInfoImpl.cs @@ -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); + } + } +} diff --git a/source/Cosmos.Core.Plugs/System/Globalization/NumberFormatInfoImpl.cs b/source/Cosmos.Core.Plugs/System/Globalization/NumberFormatInfoImpl.cs new file mode 100644 index 000000000..2a5783383 --- /dev/null +++ b/source/Cosmos.Core.Plugs/System/Globalization/NumberFormatInfoImpl.cs @@ -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; + } + } +} diff --git a/source/Cosmos.Core.Plugs/System/Globalization/TextInfoImpl.cs b/source/Cosmos.Core.Plugs/System/Globalization/TextInfoImpl.cs new file mode 100644 index 000000000..e23dd0ee0 --- /dev/null +++ b/source/Cosmos.Core.Plugs/System/Globalization/TextInfoImpl.cs @@ -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); + } + } +} diff --git a/source/Cosmos.Core.Plugs/System/StringImpl.cs b/source/Cosmos.Core.Plugs/System/StringImpl.cs index 8714f0db6..cff1e3cab 100644 --- a/source/Cosmos.Core.Plugs/System/StringImpl.cs +++ b/source/Cosmos.Core.Plugs/System/StringImpl.cs @@ -1,6 +1,5 @@ using System; using System.Globalization; -using System.Text; using Cosmos.Common; using Cosmos.IL2CPU.Plugs; @@ -51,11 +50,11 @@ namespace Cosmos.Core.Plugs.System } 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; @@ -595,8 +594,6 @@ namespace Cosmos.Core.Plugs.System } } - // 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 @@ -609,9 +606,23 @@ namespace Cosmos.Core.Plugs.System 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) diff --git a/source/Cosmos.IL2CPU/Cosmos.IL2CPU.csproj b/source/Cosmos.IL2CPU/Cosmos.IL2CPU.csproj index 8b0175102..4c4190165 100644 --- a/source/Cosmos.IL2CPU/Cosmos.IL2CPU.csproj +++ b/source/Cosmos.IL2CPU/Cosmos.IL2CPU.csproj @@ -110,8 +110,6 @@ - - diff --git a/source/Cosmos.IL2CPU/Plugs/System/Globalization/CultureInfoImpl.cs b/source/Cosmos.IL2CPU/Plugs/System/Globalization/CultureInfoImpl.cs deleted file mode 100644 index 080742e11..000000000 --- a/source/Cosmos.IL2CPU/Plugs/System/Globalization/CultureInfoImpl.cs +++ /dev/null @@ -1,25 +0,0 @@ -using System.Globalization; - -namespace Cosmos.IL2CPU.Plugs.System.Globalization { - [Plug(Target = typeof(CultureInfo))] - public static class CultureInfoImpl { - public static CultureInfo get_CurrentCulture() { - return null; - } - - public static CultureInfo get_InvariantCulture() - { - return null; - } - - //[PlugMethod(Signature = "System_Void__System_Globalization_CultureInfo__cctor__")] - public static void CCtor() - { - } - - public static bool Equals(CultureInfo aThis, object aThat) - { - return false; - } - } -} \ No newline at end of file diff --git a/source/Cosmos.IL2CPU/Plugs/System/Globalization/NumberFormatInfoImpl.cs b/source/Cosmos.IL2CPU/Plugs/System/Globalization/NumberFormatInfoImpl.cs deleted file mode 100644 index 23bf36083..000000000 --- a/source/Cosmos.IL2CPU/Plugs/System/Globalization/NumberFormatInfoImpl.cs +++ /dev/null @@ -1,19 +0,0 @@ -using System; -using System.Globalization; - -namespace Cosmos.IL2CPU.Plugs.System.Globalization { - [Plug(Target=typeof(NumberFormatInfo))] - public static class NumberFormatInfoImpl { - public static NumberFormatInfo GetInstance(IFormatProvider aProvider) { - //Console.WriteLine("NumberFormatInfo.GetInstance(IFormatProvider) is not working!"); - return null; - } - - - public static NumberFormatInfo get_CurrentInfo() - { - //Console.WriteLine("NumberFormatInfo.get_CurrentInfo is not working!"); - return null; - } - } -} \ No newline at end of file diff --git a/source/Cosmos.System/FileSystem/VFS/VFSManager.cs b/source/Cosmos.System/FileSystem/VFS/VFSManager.cs index 7e5563a93..701b4e3de 100644 --- a/source/Cosmos.System/FileSystem/VFS/VFSManager.cs +++ b/source/Cosmos.System/FileSystem/VFS/VFSManager.cs @@ -294,13 +294,14 @@ namespace Cosmos.System.FileSystem.VFS public static char[] GetInvalidFileNameChars() { - return new[] - { '"', '<', '>', '|', '\0', '\a', '\b', '\t', '\n', '\v', '\f', '\r', ':', '*', '?', '\\', '/' }; + char[] xReturn = { '"', '<', '>', '|', '\0', '\a', '\b', '\t', '\n', '\v', '\f', '\r', ':', '*', '?', '\\', '/' }; + return xReturn; } public static char[] GetInvalidPathCharsWithAdditionalChecks() { - return new[] { '"', '<', '>', '|', '\0', '\a', '\b', '\t', '\n', '\v', '\f', '\r', '*', '?' }; + char[] xReturn = { '"', '<', '>', '|', '\0', '\a', '\b', '\t', '\n', '\v', '\f', '\r', '*', '?' }; + return xReturn; } public static char GetPathSeparator() @@ -310,7 +311,8 @@ namespace Cosmos.System.FileSystem.VFS public static char[] GetRealInvalidPathChars() { - return new[] { '"', '<', '>', '|', '\0', '\a', '\b', '\t', '\n', '\v', '\f', '\r' }; + char[] xReturn = { '"', '<', '>', '|', '\0', '\a', '\b', '\t', '\n', '\v', '\f', '\r' }; + return xReturn; } public static char[] GetTrimEndChars()