Skip to content

Commit

Permalink
Add CreateArray and CreateMap to Helper (neo-project#2145)
Browse files Browse the repository at this point in the history
  • Loading branch information
erikzhang authored and cloud8little committed Jan 24, 2021
1 parent bb52b52 commit 889d5ec
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 0 deletions.
24 changes: 24 additions & 0 deletions src/neo/VM/Helper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,30 @@ namespace Neo.VM
{
public static class Helper
{
public static ScriptBuilder CreateArray<T>(this ScriptBuilder sb, IReadOnlyList<T> list = null)
{
if (list is null || list.Count == 0)
return sb.Emit(OpCode.NEWARRAY0);
for (int i = list.Count - 1; i >= 0; i--)
sb.EmitPush(list[i]);
sb.EmitPush(list.Count);
return sb.Emit(OpCode.PACK);
}

public static ScriptBuilder CreateMap<TKey, TValue>(this ScriptBuilder sb, IReadOnlyDictionary<TKey, TValue> map = null)
{
sb.Emit(OpCode.NEWMAP);
if (map != null)
foreach (var p in map)
{
sb.Emit(OpCode.DUP);
sb.EmitPush(p.Key);
sb.EmitPush(p.Value);
sb.Emit(OpCode.SETITEM);
}
return sb;
}

public static ScriptBuilder Emit(this ScriptBuilder sb, params OpCode[] ops)
{
foreach (OpCode op in ops)
Expand Down
42 changes: 42 additions & 0 deletions tests/neo.UnitTests/VM/UT_Helper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,48 @@ public void TestEmitAppCall1()
CollectionAssert.AreEqual(tempArray, sb.ToArray());
}

[TestMethod]
public void TestEmitArray()
{
var expected = new BigInteger[] { 1, 2, 3 };
var sb = new ScriptBuilder();
sb.CreateArray(expected);

using var engine = ApplicationEngine.Create(TriggerType.Application, null, null);
engine.LoadScript(sb.ToArray());
Assert.AreEqual(VMState.HALT, engine.Execute());

CollectionAssert.AreEqual(expected, engine.ResultStack.Pop<VM.Types.Array>().Select(u => u.GetInteger()).ToArray());

expected = new BigInteger[] { };
sb = new ScriptBuilder();
sb.CreateArray(expected);

using var engine2 = ApplicationEngine.Create(TriggerType.Application, null, null);
engine2.LoadScript(sb.ToArray());
Assert.AreEqual(VMState.HALT, engine2.Execute());

Assert.AreEqual(0, engine2.ResultStack.Pop<VM.Types.Array>().Count);
}

[TestMethod]
public void TestEmitMap()
{
var expected = new Dictionary<BigInteger, BigInteger>() { { 1, 2 }, { 3, 4 } };
var sb = new ScriptBuilder();
sb.CreateMap(expected);

using var engine = ApplicationEngine.Create(TriggerType.Application, null, null);
engine.LoadScript(sb.ToArray());
Assert.AreEqual(VMState.HALT, engine.Execute());

var map = engine.ResultStack.Pop<VM.Types.Map>();
var dic = map.ToDictionary(u => u.Key, u => u.Value);

CollectionAssert.AreEqual(expected.Keys, dic.Keys.Select(u => u.GetInteger()).ToArray());
CollectionAssert.AreEqual(expected.Values, dic.Values.Select(u => u.GetInteger()).ToArray());
}

[TestMethod]
public void TestEmitAppCall2()
{
Expand Down

0 comments on commit 889d5ec

Please sign in to comment.