forked from neo-project/neo
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow to iterate a byte array inside the smart contract (neo-project#…
…1281) * Update changes * Fix UT * Rename * Update ByteArrayWrapper.cs * Support Neo.Iterator.Create * Add ut and Rename * Cover line * Revert rename
- Loading branch information
Showing
4 changed files
with
111 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
using Neo.VM.Types; | ||
using System; | ||
|
||
namespace Neo.SmartContract.Iterators | ||
{ | ||
internal class ByteArrayWrapper : IIterator | ||
{ | ||
private readonly byte[] array; | ||
private int index = -1; | ||
|
||
public ByteArrayWrapper(PrimitiveType value) | ||
{ | ||
this.array = value.ToByteArray().ToArray(); | ||
} | ||
|
||
public void Dispose() { } | ||
|
||
public PrimitiveType Key() | ||
{ | ||
if (index < 0) | ||
throw new InvalidOperationException(); | ||
return index; | ||
} | ||
|
||
public bool Next() | ||
{ | ||
int next = index + 1; | ||
if (next >= array.Length) | ||
return false; | ||
index = next; | ||
return true; | ||
} | ||
|
||
public StackItem Value() | ||
{ | ||
if (index < 0) | ||
throw new InvalidOperationException(); | ||
return new Integer(array[index]); | ||
} | ||
} | ||
} |
49 changes: 49 additions & 0 deletions
49
tests/neo.UnitTests/SmartContract/Iterators/UT_PrimitiveWrapper.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
using FluentAssertions; | ||
using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
using Neo.SmartContract.Iterators; | ||
using Neo.VM; | ||
using Neo.VM.Types; | ||
using System; | ||
|
||
namespace Neo.UnitTests.SmartContract.Iterators | ||
{ | ||
[TestClass] | ||
public class UT_PrimitiveWrapper | ||
{ | ||
[TestMethod] | ||
public void TestGeneratorAndDispose() | ||
{ | ||
ByteArrayWrapper arrayWrapper = new ByteArrayWrapper(new ByteArray(new byte[0])); | ||
Assert.IsNotNull(arrayWrapper); | ||
Action action = () => arrayWrapper.Dispose(); | ||
action.Should().NotThrow<Exception>(); | ||
} | ||
|
||
[TestMethod] | ||
public void TestKeyAndValue() | ||
{ | ||
ByteArrayWrapper arrayWrapper = new ByteArrayWrapper(new byte[] { 0x01, 0x02 }); | ||
Action action1 = () => arrayWrapper.Key(); | ||
action1.Should().Throw<InvalidOperationException>(); | ||
Action action2 = () => arrayWrapper.Value(); | ||
action2.Should().Throw<InvalidOperationException>(); | ||
arrayWrapper.Next(); | ||
Assert.AreEqual(0x00, arrayWrapper.Key().GetBigInteger()); | ||
Assert.AreEqual(0x01, arrayWrapper.Value()); | ||
arrayWrapper.Next(); | ||
Assert.AreEqual(0x01, arrayWrapper.Key().GetBigInteger()); | ||
Assert.AreEqual(0x02, arrayWrapper.Value()); | ||
} | ||
|
||
[TestMethod] | ||
public void TestNext() | ||
{ | ||
ByteArrayWrapper arrayWrapper = new ByteArrayWrapper(new byte[] { 0x01, 0x02 }); | ||
Assert.AreEqual(true, arrayWrapper.Next()); | ||
Assert.AreEqual(0x01, arrayWrapper.Value()); | ||
Assert.AreEqual(true, arrayWrapper.Next()); | ||
Assert.AreEqual(0x02, arrayWrapper.Value()); | ||
Assert.AreEqual(false, arrayWrapper.Next()); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters