Skip to content

Commit

Permalink
[Neo Core Add] add char support (#3441)
Browse files Browse the repository at this point in the history
* add char support

* Update src/Neo/VM/Helper.cs

* add char unit test

---------

Co-authored-by: Shargon <[email protected]>
  • Loading branch information
Jim8y and shargon committed Aug 1, 2024
1 parent 450cb61 commit 6278961
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
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);
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());
}
}
}
}

0 comments on commit 6278961

Please sign in to comment.