Skip to content

Commit 3692bd3

Browse files
shargonajara87
andauthored
Map constructor (#4207)
Co-authored-by: Alvaro <[email protected]>
1 parent 363d730 commit 3692bd3

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

src/Neo.VM/Types/Map.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,20 @@ public StackItem this[PrimitiveType key]
8989
/// <param name="referenceCounter">The reference counter to be used.</param>
9090
public Map(IReferenceCounter? referenceCounter = null) : base(referenceCounter) { }
9191

92+
/// <summary>
93+
/// Create a new map with the specified dictionary and reference counter.
94+
/// </summary>
95+
/// <param name="dictionary">Dictionary</param>
96+
/// <param name="referenceCounter">Reference Counter</param>
97+
public Map(IDictionary<PrimitiveType, StackItem> dictionary, IReferenceCounter? referenceCounter = null)
98+
: this(referenceCounter)
99+
{
100+
foreach (var (k, v) in dictionary)
101+
{
102+
this[k] = v;
103+
}
104+
}
105+
92106
public override void Clear()
93107
{
94108
if (IsReadOnly) throw new InvalidOperationException("The map is readonly, can not clear.");

tests/Neo.VM.Tests/UT_StackItem.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
using Microsoft.VisualStudio.TestTools.UnitTesting;
1313
using Neo.VM;
1414
using Neo.VM.Types;
15+
using System.Collections.Generic;
1516
using System.Numerics;
1617

1718
namespace Neo.Test
@@ -93,10 +94,11 @@ public void TestHashCode()
9394

9495
itemA = new Map { [true] = false, [0] = 1 };
9596
itemB = new Map { [true] = false, [0] = 1 };
96-
itemC = new Map { [true] = false, [0] = 2 };
97+
itemC = new Map(new Dictionary<PrimitiveType, StackItem>() { [true] = false, [0] = 2 });
9798

9899
Assert.AreEqual(itemB.GetHashCode(), itemA.GetHashCode());
99100
Assert.AreNotEqual(itemC.GetHashCode(), itemA.GetHashCode());
101+
Assert.HasCount(2, itemC as Map);
100102

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

0 commit comments

Comments
 (0)