From 5cf7c727f1e3859565000872bee8f52d2802628d Mon Sep 17 00:00:00 2001 From: Guillermo Santos Date: Thu, 28 Dec 2023 18:12:22 -0400 Subject: [PATCH 1/6] Update Int32Impl.cs Improved Hexadecimal format code as it was changing the value of the original variable (teste only on SharpLab), also added support for the decimal format --- .../Cosmos.System2_Plugs/System/Int32Impl.cs | 58 ++++++++++--------- 1 file changed, 30 insertions(+), 28 deletions(-) diff --git a/source/Cosmos.System2_Plugs/System/Int32Impl.cs b/source/Cosmos.System2_Plugs/System/Int32Impl.cs index 312521355b..7268e9d1d9 100644 --- a/source/Cosmos.System2_Plugs/System/Int32Impl.cs +++ b/source/Cosmos.System2_Plugs/System/Int32Impl.cs @@ -13,46 +13,48 @@ public static string ToString(ref int aThis) public static string ToString(ref int aThis, string format) { - if (format.Equals("X")) + if (string.IsNullOrEmpty(format)) { - string result = ""; + return "0"; + } - if(aThis == 0) - { - result = "0"; - } + string result = aThis > 0 ? string.Empty : "0"; - while (aThis != 0) - { - if (aThis % 16 < 10) - { - result = aThis % 16 + result; - } - else - { - string temp = ""; + switch (format[0]) + { + case 'X': + int value = aThis; - switch (aThis % 16) + while (value != 0) + { + int remainder = value % 16; + if (remainder < 10) { - case 10: temp = "A"; break; - case 11: temp = "B"; break; - case 12: temp = "C"; break; - case 13: temp = "D"; break; - case 14: temp = "E"; break; - case 15: temp = "F"; break; + result = remainder + result; + } + else + { + char temp = (char)('A' + (remainder - 10)); + result = temp + result; } - result = temp + result; + value /= 16; } + break; + case 'D': + result = aThis.ToString(); + break; + default: + return aThis.ToString(); + } - aThis /= 16; - } - - return result; + if (format.Length > 1) + { + return int.TryParse(format.AsSpan(1), out int number) ? result.PadLeft(number, '0') : aThis.ToString(); } else { - return aThis.ToString(); + return result; } } From aaea6da45b73ac409a726774ba86c7f9eb89ee4a Mon Sep 17 00:00:00 2001 From: Guillermo Santos Date: Thu, 28 Dec 2023 18:39:15 -0400 Subject: [PATCH 2/6] Guess this check is better --- source/Cosmos.System2_Plugs/System/Int32Impl.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/source/Cosmos.System2_Plugs/System/Int32Impl.cs b/source/Cosmos.System2_Plugs/System/Int32Impl.cs index 7268e9d1d9..1141d6964e 100644 --- a/source/Cosmos.System2_Plugs/System/Int32Impl.cs +++ b/source/Cosmos.System2_Plugs/System/Int32Impl.cs @@ -18,7 +18,7 @@ public static string ToString(ref int aThis, string format) return "0"; } - string result = aThis > 0 ? string.Empty : "0"; + string result = aThis == 0 ? "0" : string.Empty; switch (format[0]) { From 55708e171720cfb5ad9434be066389a041897221 Mon Sep 17 00:00:00 2001 From: Guillermo Santos Date: Fri, 29 Dec 2023 11:47:08 -0400 Subject: [PATCH 3/6] Changed to use StringBuilder --- source/Cosmos.System2_Plugs/System/Int32Impl.cs | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/source/Cosmos.System2_Plugs/System/Int32Impl.cs b/source/Cosmos.System2_Plugs/System/Int32Impl.cs index 1141d6964e..fc483ea864 100644 --- a/source/Cosmos.System2_Plugs/System/Int32Impl.cs +++ b/source/Cosmos.System2_Plugs/System/Int32Impl.cs @@ -1,3 +1,4 @@ +using System.Text; using Cosmos.Common; using IL2CPU.API.Attribs; @@ -18,11 +19,17 @@ public static string ToString(ref int aThis, string format) return "0"; } - string result = aThis == 0 ? "0" : string.Empty; + StringBuilder sb = new(); switch (format[0]) { case 'X': + if (aThis == 0) + { + sb.Append('0'); + break; + } + int value = aThis; while (value != 0) @@ -30,24 +37,26 @@ public static string ToString(ref int aThis, string format) int remainder = value % 16; if (remainder < 10) { - result = remainder + result; + sb.Insert(0, remainder); } else { char temp = (char)('A' + (remainder - 10)); - result = temp + result; + sb.Insert(0, temp); } value /= 16; } break; case 'D': - result = aThis.ToString(); + sb.Append(aThis); break; default: return aThis.ToString(); } + var result = sb.ToString(); + if (format.Length > 1) { return int.TryParse(format.AsSpan(1), out int number) ? result.PadLeft(number, '0') : aThis.ToString(); From 357ea04b8b0f3563992521af84217628cda721ee Mon Sep 17 00:00:00 2001 From: Guillermo Santos Date: Sun, 31 Dec 2023 20:53:53 -0400 Subject: [PATCH 4/6] Added test cases Some may need to be removed --- .../System/Int32Test.cs | 87 +++++++++++++++++-- 1 file changed, 79 insertions(+), 8 deletions(-) diff --git a/Tests/Kernels/Cosmos.Compiler.Tests.Bcl.System/System/Int32Test.cs b/Tests/Kernels/Cosmos.Compiler.Tests.Bcl.System/System/Int32Test.cs index e985711f13..7b5f509e7b 100644 --- a/Tests/Kernels/Cosmos.Compiler.Tests.Bcl.System/System/Int32Test.cs +++ b/Tests/Kernels/Cosmos.Compiler.Tests.Bcl.System/System/Int32Test.cs @@ -1,5 +1,6 @@ using System; - +using System.Linq; +using Cosmos.Common.Extensions; using Cosmos.TestRunner; namespace Cosmos.Compiler.Tests.Bcl.System @@ -9,7 +10,7 @@ internal static class Int32Test public static void Execute() { bool efuse; - int value; + int value, tmp; string result; string expectedResult; @@ -37,13 +38,37 @@ public static void Execute() // actually the Hash Code of an 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"; + // Now let's try ToString() again but printed in hex + result = value.ToString("X"); + expectedResult = "7FFFFFFF"; + + Assert.IsTrue((result == expectedResult), "Int32.ToString(X) brings incorrect result."); + + // Ensure value is not overrided + Assert.IsTrue((value != 0), "Int32.ToString(X) overrides the value of the variable."); + + tmp = value; + result = ToStringOld(ref tmp, "X"); + + // This is just to demostrates that the changed version bring correct results + Assert.IsTrue((result == expectedResult), "Int32.ToString(X) brings incorrect result"); + + // Demostrate how old version overrided the the variable value. + Assert.IsTrue((tmp == 0), "ToStringOld(X), did not override"); - Assert.IsTrue((result == expectedResult), "Int32.ToString(X2) doesn't work"); -#endif + // Test results with another implementation of hex format + foreach (int i in Enumerable.Range(0,256)) + { + result = i.ToString("X4"); + expectedResult = i.ToHex(4); + Assert.IsTrue((result == expectedResult), "Int32.ToString(X) brings incorrect result."); + } + + // Test Decimal format + value = 10; + expectedResult = "0010"; + result = value.ToString("D4"); + Assert.IsTrue((result == expectedResult), "Int32.ToString(D4) brings incorrect result."); // basic bit operations @@ -235,5 +260,51 @@ public static void ByRefTestMethod(ref int aParam) { aParam++; } + + // Original int32.ToString(format) + private static string ToStringOld(ref int aThis, string format) + { + if (format.Equals("X")) + { + string result = ""; + + if (aThis == 0) + { + result = "0"; + } + + while (aThis != 0) + { + if (aThis % 16 < 10) + { + result = aThis % 16 + result; + } + else + { + string temp = ""; + + switch (aThis % 16) + { + case 10: temp = "A"; break; + case 11: temp = "B"; break; + case 12: temp = "C"; break; + case 13: temp = "D"; break; + case 14: temp = "E"; break; + case 15: temp = "F"; break; + } + + result = temp + result; + } + + aThis /= 16; + } + + return result; + } + else + { + return aThis.ToString(); + } + } } } From 502ce21734694077a8c7324180eb98c25d817e17 Mon Sep 17 00:00:00 2001 From: Guillermo Santos Date: Mon, 1 Jan 2024 14:09:55 -0400 Subject: [PATCH 5/6] Removed test that are not needed Also added a test for hex with padding --- .../System/Int32Test.cs | 25 ++++++------------- 1 file changed, 7 insertions(+), 18 deletions(-) diff --git a/Tests/Kernels/Cosmos.Compiler.Tests.Bcl.System/System/Int32Test.cs b/Tests/Kernels/Cosmos.Compiler.Tests.Bcl.System/System/Int32Test.cs index 7b5f509e7b..b88a48dfd9 100644 --- a/Tests/Kernels/Cosmos.Compiler.Tests.Bcl.System/System/Int32Test.cs +++ b/Tests/Kernels/Cosmos.Compiler.Tests.Bcl.System/System/Int32Test.cs @@ -10,7 +10,7 @@ internal static class Int32Test public static void Execute() { bool efuse; - int value, tmp; + int value; string result; string expectedResult; @@ -47,24 +47,13 @@ public static void Execute() // Ensure value is not overrided Assert.IsTrue((value != 0), "Int32.ToString(X) overrides the value of the variable."); - tmp = value; - result = ToStringOld(ref tmp, "X"); + // Hex with padding + value = 255; + result = value.ToString("X4"); + expectedResult = "00FF"; + Assert.IsTrue((result == expectedResult), "Int32.ToString(X4) brings incorrect result."); - // This is just to demostrates that the changed version bring correct results - Assert.IsTrue((result == expectedResult), "Int32.ToString(X) brings incorrect result"); - - // Demostrate how old version overrided the the variable value. - Assert.IsTrue((tmp == 0), "ToStringOld(X), did not override"); - - // Test results with another implementation of hex format - foreach (int i in Enumerable.Range(0,256)) - { - result = i.ToString("X4"); - expectedResult = i.ToHex(4); - Assert.IsTrue((result == expectedResult), "Int32.ToString(X) brings incorrect result."); - } - - // Test Decimal format + // Test Decimal format wit padding value = 10; expectedResult = "0010"; result = value.ToString("D4"); From ac4dd55e49e9f10f371e5cf464e46c34859f531f Mon Sep 17 00:00:00 2001 From: Guillermo Santos Date: Mon, 1 Jan 2024 14:20:36 -0400 Subject: [PATCH 6/6] Removed unneeded method A method that was added to test the old impl, now that it will not be tested, it's reason to exists is no more. --- .../System/Int32Test.cs | 46 ------------------- 1 file changed, 46 deletions(-) diff --git a/Tests/Kernels/Cosmos.Compiler.Tests.Bcl.System/System/Int32Test.cs b/Tests/Kernels/Cosmos.Compiler.Tests.Bcl.System/System/Int32Test.cs index b88a48dfd9..1ab1d4a7a8 100644 --- a/Tests/Kernels/Cosmos.Compiler.Tests.Bcl.System/System/Int32Test.cs +++ b/Tests/Kernels/Cosmos.Compiler.Tests.Bcl.System/System/Int32Test.cs @@ -249,51 +249,5 @@ public static void ByRefTestMethod(ref int aParam) { aParam++; } - - // Original int32.ToString(format) - private static string ToStringOld(ref int aThis, string format) - { - if (format.Equals("X")) - { - string result = ""; - - if (aThis == 0) - { - result = "0"; - } - - while (aThis != 0) - { - if (aThis % 16 < 10) - { - result = aThis % 16 + result; - } - else - { - string temp = ""; - - switch (aThis % 16) - { - case 10: temp = "A"; break; - case 11: temp = "B"; break; - case 12: temp = "C"; break; - case 13: temp = "D"; break; - case 14: temp = "E"; break; - case 15: temp = "F"; break; - } - - result = temp + result; - } - - aThis /= 16; - } - - return result; - } - else - { - return aThis.ToString(); - } - } } }