Skip to content

Commit

Permalink
Added test and comments
Browse files Browse the repository at this point in the history
  • Loading branch information
cschuchardt88 committed Sep 20, 2024
1 parent 49aea69 commit 792968d
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 3 deletions.
20 changes: 17 additions & 3 deletions src/Neo/VM/Helper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public static ScriptBuilder CreateMap<TKey, TValue>(this ScriptBuilder builder,
if (count == 0)
return builder.Emit(OpCode.NEWMAP);

foreach (var (key, value) in map)
foreach (var (key, value) in map.Reverse())
{
builder.EmitPush(value);
builder.EmitPush(key);
Expand All @@ -72,14 +72,22 @@ public static ScriptBuilder CreateMap<TKey, TValue>(this ScriptBuilder builder,
return builder.Emit(OpCode.PACKMAP);
}

/// <summary>
/// Emits the opcodes for creating a map.
/// </summary>
/// <typeparam name="TKey">The type of the key of the map.</typeparam>
/// <typeparam name="TValue">The type of the value of the map.</typeparam>
/// <param name="builder">The <see cref="ScriptBuilder"/> to be used.</param>
/// <param name="map">The key/value pairs of the map.</param>
/// <returns>The same instance as <paramref name="builder"/>.</returns>
public static ScriptBuilder CreateMap<TKey, TValue>(this ScriptBuilder builder, IReadOnlyDictionary<TKey, TValue> map)
where TKey : notnull
where TValue : notnull
{
if (map.Count == 0)
return builder.Emit(OpCode.NEWMAP);

foreach (var (key, value) in map)
foreach (var (key, value) in map.Reverse())
{
builder.EmitPush(value);
builder.EmitPush(key);
Expand All @@ -88,7 +96,13 @@ public static ScriptBuilder CreateMap<TKey, TValue>(this ScriptBuilder builder,
return builder.Emit(OpCode.PACKMAP);
}


/// <summary>
/// Emits the opcodes for creating a struct.
/// </summary>
/// <typeparam name="T">The type of the property.</typeparam>
/// <param name="builder">The <see cref="ScriptBuilder"/> to be used.</param>
/// <param name="array">The list of properties.</param>
/// <returns>The same instance as <paramref name="builder"/>.</returns>
public static ScriptBuilder CreateStruct<T>(this ScriptBuilder builder, IReadOnlyList<T> array)
where T : notnull
{
Expand Down
24 changes: 24 additions & 0 deletions tests/Neo.UnitTests/VM/UT_Helper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,30 @@ public void TestEmitArray()
Assert.AreEqual(0, engine2.ResultStack.Pop<VM.Types.Array>().Count);
}

[TestMethod]
public void TestEmitStruct()
{
var expected = new BigInteger[] { 1, 2, 3 };
var sb = new ScriptBuilder();
sb.CreateStruct(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.Struct>().Select(u => u.GetInteger()).ToArray());

expected = new BigInteger[] { };
sb = new ScriptBuilder();
sb.CreateStruct(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.Struct>().Count);
}

[TestMethod]
public void TestEmitMap()
{
Expand Down

0 comments on commit 792968d

Please sign in to comment.