Skip to content
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
14 changes: 14 additions & 0 deletions src/Neo.VM/Types/Map.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,20 @@ public StackItem this[PrimitiveType key]
/// <param name="referenceCounter">The reference counter to be used.</param>
public Map(IReferenceCounter? referenceCounter = null) : base(referenceCounter) { }

/// <summary>
/// Create a new map with the specified dictionary and reference counter.
/// </summary>
/// <param name="dictionary">Dictionary</param>
/// <param name="referenceCounter">Reference Counter</param>
public Map(IDictionary<PrimitiveType, StackItem> dictionary, IReferenceCounter? referenceCounter = null)
: this(referenceCounter)
{
foreach (var (k, v) in dictionary)
{
this[k] = v;
}
}

public override void Clear()
{
if (IsReadOnly) throw new InvalidOperationException("The map is readonly, can not clear.");
Expand Down
4 changes: 3 additions & 1 deletion tests/Neo.VM.Tests/UT_StackItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Neo.VM;
using Neo.VM.Types;
using System.Collections.Generic;
using System.Numerics;

namespace Neo.Test
Expand Down Expand Up @@ -93,10 +94,11 @@ public void TestHashCode()

itemA = new Map { [true] = false, [0] = 1 };
itemB = new Map { [true] = false, [0] = 1 };
itemC = new Map { [true] = false, [0] = 2 };
itemC = new Map(new Dictionary<PrimitiveType, StackItem>() { [true] = false, [0] = 2 });

Assert.AreEqual(itemB.GetHashCode(), itemA.GetHashCode());
Assert.AreNotEqual(itemC.GetHashCode(), itemA.GetHashCode());
Assert.HasCount(2, itemC as Map);

// Test CompoundType GetHashCode for subitems
var junk = new Array { true, false, 0 };
Expand Down