Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Neo Core Add] add char support #3441

Merged
merged 7 commits into from
Aug 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/Neo/VM/Helper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
cschuchardt88 marked this conversation as resolved.
Show resolved Hide resolved
break;
case ushort data:
builder.EmitPush(data);
break;
Expand Down
37 changes: 36 additions & 1 deletion tests/Neo.UnitTests/VM/UT_Helper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -401,6 +402,7 @@ public void TestEmitPush3()
TestEmitPush3Byte();
TestEmitPush3Short();
TestEmitPush3Ushort();
TestEmitPush3Char();
TestEmitPush3Int();
TestEmitPush3Uint();
TestEmitPush3Long();
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -630,5 +642,28 @@ private void TestToParaMeter2VMArray()
Assert.AreEqual(ContractParameterType.Array, parameter.Type);
Assert.AreEqual(0, ((List<ContractParameter>)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());
}
}
}
}