From 62789616d2249dc1202db2e91b0e955c28235a5f Mon Sep 17 00:00:00 2001 From: Jimmy Date: Thu, 1 Aug 2024 15:06:07 +0800 Subject: [PATCH] [Neo Core Add] add char support (#3441) * add char support * Update src/Neo/VM/Helper.cs * add char unit test --------- Co-authored-by: Shargon --- src/Neo/VM/Helper.cs | 3 +++ tests/Neo.UnitTests/VM/UT_Helper.cs | 37 ++++++++++++++++++++++++++++- 2 files changed, 39 insertions(+), 1 deletion(-) diff --git a/src/Neo/VM/Helper.cs b/src/Neo/VM/Helper.cs index 4a82041ea5..c6423ac1f1 100644 --- a/src/Neo/VM/Helper.cs +++ b/src/Neo/VM/Helper.cs @@ -217,6 +217,9 @@ public static ScriptBuilder EmitPush(this ScriptBuilder builder, object obj) case short data: builder.EmitPush(data); break; + case char data: + builder.EmitPush((ushort)data); + break; case ushort data: builder.EmitPush(data); break; diff --git a/tests/Neo.UnitTests/VM/UT_Helper.cs b/tests/Neo.UnitTests/VM/UT_Helper.cs index 4178ed58d3..3e83256669 100644 --- a/tests/Neo.UnitTests/VM/UT_Helper.cs +++ b/tests/Neo.UnitTests/VM/UT_Helper.cs @@ -18,6 +18,7 @@ using Neo.SmartContract.Native; using Neo.VM; using Neo.VM.Types; +using Org.BouncyCastle.Asn1.Tsp; using System; using System.Collections.Generic; using System.Linq; @@ -152,7 +153,7 @@ public void TestEmitAppCall3() sb.EmitDynamicCall(UInt160.Zero, "AAAAA", true); byte[] tempArray = new byte[38]; tempArray[0] = (byte)OpCode.PUSHT; - tempArray[1] = (byte)OpCode.PUSH1;//arg.Length + tempArray[1] = (byte)OpCode.PUSH1;//arg.Length tempArray[2] = (byte)OpCode.PACK; tempArray[3] = (byte)OpCode.PUSH15;//(byte)CallFlags.All; tempArray[4] = (byte)OpCode.PUSHDATA1; @@ -401,6 +402,7 @@ public void TestEmitPush3() TestEmitPush3Byte(); TestEmitPush3Short(); TestEmitPush3Ushort(); + TestEmitPush3Char(); TestEmitPush3Int(); TestEmitPush3Uint(); TestEmitPush3Long(); @@ -472,6 +474,16 @@ private void TestEmitPush3Ushort() CollectionAssert.AreEqual(tempArray, sb.ToArray()); } + private void TestEmitPush3Char() + { + ScriptBuilder sb = new ScriptBuilder(); + char temp = char.MinValue; + VM.Helper.EmitPush(sb, temp); + byte[] tempArray = new byte[1]; + tempArray[0] = (byte)OpCode.PUSH0; + CollectionAssert.AreEqual(tempArray, sb.ToArray()); + } + private void TestEmitPush3Short() { ScriptBuilder sb = new ScriptBuilder(); @@ -630,5 +642,28 @@ private void TestToParaMeter2VMArray() Assert.AreEqual(ContractParameterType.Array, parameter.Type); Assert.AreEqual(0, ((List)parameter.Value).Count); } + + [TestMethod] + public void TestCharAsUInt16() + { + Assert.AreEqual(ushort.MaxValue, char.MaxValue); + Assert.AreEqual(ushort.MinValue, char.MinValue); + + // test every char in a loop + for (int i = ushort.MinValue; i < char.MinValue; i++) + { + var c = Convert.ToChar(i); + Assert.AreEqual(i, c); + } + + for (int i = ushort.MinValue; i < ushort.MaxValue; i++) + { + using var sbUInt16 = new ScriptBuilder(); + using var sbChar = new ScriptBuilder(); + sbUInt16.EmitPush((ushort)i); + sbChar.EmitPush(Convert.ToChar(i)); + CollectionAssert.AreEqual(sbUInt16.ToArray(), sbChar.ToArray()); + } + } } }