Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add byte.AsString and Map UTs #303

Merged
merged 18 commits into from
Jul 16, 2020
Merged
Show file tree
Hide file tree
Changes from 9 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
5 changes: 5 additions & 0 deletions src/Neo.Compiler.MSIL/MSIL/Conv_Multi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,11 @@ private void ConvertLdArg(ILMethod method, OpCode src, NeoMethod to, int pos)
{
Convert1by1(VM.OpCode.LDARG, src, to, new byte[] { (byte)pos });
}

if (method.paramtypes[pos].type == "System.Byte[]")
{
Insert1(VM.OpCode.CONVERT, "", to, new byte[] { (byte)VM.Types.StackItemType.Buffer });
}
}

private void ConvertStArg(OpCode src, NeoMethod to, int pos)
Expand Down
5 changes: 3 additions & 2 deletions src/Neo.SmartContract.Framework/Helper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ namespace Neo.SmartContract.Framework
public static class Helper
{
internal const string StackItemType_Integer = "0x21";
internal const string StackItemType_ByteString = "0x28";
internal const string StackItemType_Buffer = "0x30";

/// <summary>
Expand Down Expand Up @@ -37,8 +38,8 @@ public static class Helper
/// <summary>
/// Converts byte[] to string. Examples: [0x68656c6c6f] -> "hello"; [] -> ""; [0x4e656f] -> "Neo"
/// </summary>
[Script]
public extern static string AsString(this byte[] source);
[OpCode(OpCode.CONVERT, StackItemType_ByteString)]
public extern static string ToByteString(this byte[] source);

/// <summary>
/// Returns true iff a <= x && x < b. Examples: x=5 a=5 b=15 is true; x=15 a=5 b=15 is false
Expand Down
149 changes: 149 additions & 0 deletions tests/Neo.SmartContract.Framework.UnitTests/MapTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Neo.Compiler.MSIL.UnitTests.Utils;
using Neo.VM;
using Neo.VM.Types;

namespace Neo.SmartContract.Framework.UnitTests
{
[TestClass]
public class MapTest
{
private TestEngine _engine;

[TestInitialize]
public void Init()
{
_engine = new TestEngine();
_engine.AddEntryScript("./TestClasses/Contract_Map.cs");
}

[TestMethod]
public void TestByteArray()
{
_engine.Reset();
_engine.ExecuteTestCaseStandard("testByteArray");
Assert.AreEqual(VMState.FAULT, _engine.State);
}

[TestMethod]
public void TestByteArray2()
{
_engine.Reset();
StackItem key = System.Text.Encoding.ASCII.GetBytes("a");
var result = _engine.ExecuteTestCaseStandard("testByteArray2", key);
Assert.AreEqual(VMState.HALT, _engine.State);
Assert.AreEqual(1, result.Count);

var item = result.Pop();
Assert.IsInstanceOfType(item, typeof(ByteString));
// Except: {"a":"dGVzdHN0cmluZzI="}
Assert.AreEqual("7b2261223a226447567a64484e30636d6c755a7a493d227d", (item as ByteString).Span.ToHexString());
}

[TestMethod]
public void TestByteArray3()
{
_engine.Reset();
var result = _engine.ExecuteTestCaseStandard("testByteArray3");
Assert.AreEqual(VMState.HALT, _engine.State);
Assert.AreEqual(1, result.Count);

var item = result.Pop();
Assert.IsInstanceOfType(item, typeof(ByteString));
// Except: {"\u0001\u0001":"IiI="}
Assert.AreEqual("7b225c75303030315c7530303031223a224969493d227d", (item as ByteString).Span.ToHexString());
}

[TestMethod]
public void TestUnicode()
{
_engine.Reset();
StackItem key = System.Text.Encoding.UTF8.GetBytes("中");
var result = _engine.ExecuteTestCaseStandard("testUnicode", key);
Assert.AreEqual(VMState.HALT, _engine.State);
Assert.AreEqual(1, result.Count);

var item = result.Pop();
Assert.IsInstanceOfType(item, typeof(ByteString));
// Except: {"\u4E2D":"MTI5ODQwdGVzdDEwMDIyOTM5"}
Assert.AreEqual("7b225c7534453244223a224d5449354f4451776447567a644445774d4449794f544d35227d", (item as ByteString).Span.ToHexString());
}

[TestMethod]
public void TestUnicodeValue()
{
_engine.Reset();
StackItem value = System.Text.Encoding.UTF8.GetBytes("文");
var result = _engine.ExecuteTestCaseStandard("testUnicodeValue", value);
Assert.AreEqual(VMState.HALT, _engine.State);
Assert.AreEqual(1, result.Count);

var item = result.Pop();
Assert.IsInstanceOfType(item, typeof(ByteString));
// Except: {"ab":"5paH"}
Assert.AreEqual("7b226162223a2235706148227d", (item as ByteString).Span.ToHexString());
}

[TestMethod]
public void TestUnicodeKeyValue()
{
_engine.Reset();
StackItem key = System.Text.Encoding.UTF8.GetBytes("中");
StackItem value = System.Text.Encoding.UTF8.GetBytes("文");
var result = _engine.ExecuteTestCaseStandard("testUnicodeKeyValue", key, value);
Assert.AreEqual(VMState.HALT, _engine.State);
Assert.AreEqual(1, result.Count);

var item = result.Pop();
Assert.IsInstanceOfType(item, typeof(ByteString));
// Except: {"\u4E2D":"5paH"}
Assert.AreEqual("7b225c7534453244223a2235706148227d", (item as ByteString).Span.ToHexString());
}

[TestMethod]
public void TestInt()
{
_engine.Reset();
StackItem key = 1;
var result = _engine.ExecuteTestCaseStandard("testInt", key);
Assert.AreEqual(VMState.HALT, _engine.State);
Assert.AreEqual(1, result.Count);

var item = result.Pop();
Assert.IsInstanceOfType(item, typeof(ByteString));
// Except: {"\u0001":"c3RyaW5n"}
Assert.AreEqual("7b225c7530303031223a22633352796157356e227d", (item as ByteString).Span.ToHexString());
}

[TestMethod]
public void TestBool()
{
_engine.Reset();
StackItem key = true;
var result = _engine.ExecuteTestCaseStandard("testBool", key);
Assert.AreEqual(VMState.HALT, _engine.State);
Assert.AreEqual(1, result.Count);

var item = result.Pop();
Assert.IsInstanceOfType(item, typeof(ByteString));
// Except: {"\u0001":"dGVzdGJvb2w="}
Assert.AreEqual("7b225c7530303031223a226447567a64474a766232773d227d", (item as ByteString).Span.ToHexString());
}

[TestMethod]
public void TestDeserialize()
{
_engine.Reset();
var result = _engine.ExecuteTestCaseStandard("testDeserialize", "a");
Assert.AreEqual(VMState.HALT, _engine.State);
Assert.AreEqual(1, result.Count);

var item = result.Pop();
Assert.IsInstanceOfType(item, typeof(Map));
var map = item as Map;
Assert.AreEqual(1, map.Count);
Assert.IsTrue(map.ContainsKey("a"));
Assert.AreEqual((ByteString)"dGVzdGRlc2VyaWFsaXpl", map["a"]);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ namespace Neo.Compiler.MSIL.TestClasses
{
public class Contract_Enumerator : SmartContract.Framework.SmartContract
{
public static int TestNext(byte[] a)
public static int TestNext(int[] a)
shargon marked this conversation as resolved.
Show resolved Hide resolved
{
int sum = 0;
var enumerator = Enumerator<byte>.Create(a);
var enumerator = Enumerator<int>.Create(a);

while (enumerator.Next())
{
Expand All @@ -17,11 +17,11 @@ public static int TestNext(byte[] a)
return sum;
}

public static int TestConcat(byte[] a, byte[] b)
public static int TestConcat(int[] a, int[] b)
{
int sum = 0;
var enumeratorA = Enumerator<byte>.Create(a);
var enumeratorB = Enumerator<byte>.Create(b);
var enumeratorA = Enumerator<int>.Create(a);
var enumeratorB = Enumerator<int>.Create(b);
var enumeratorC = enumeratorA.Concat(enumeratorB);

while (enumeratorC.Next())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ namespace Neo.Compiler.MSIL.TestClasses
{
public class Contract_Iterator : SmartContract.Framework.SmartContract
{
public static int TestNextArray(byte[] a)
public static int TestNextArray(int[] a)
{
int sum = 0;
var iterator = Iterator<byte, byte>.Create(a);
var iterator = Iterator<int, int>.Create(a);

while (iterator.Next())
{
Expand All @@ -18,11 +18,11 @@ public static int TestNextArray(byte[] a)
return sum;
}

public static int TestConcatArray(byte[] a, byte[] b)
public static int TestConcatArray(int[] a, int[] b)
{
int sum = 0;
var iteratorA = Iterator<byte, byte>.Create(a);
var iteratorB = Iterator<byte, byte>.Create(b);
var iteratorA = Iterator<int, int>.Create(a);
var iteratorB = Iterator<int, int>.Create(b);
var iteratorC = iteratorA.Concat(iteratorB);

while (iteratorC.Next())
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
using System;
using System.Collections.Generic;
using System.Text;
using Neo.SmartContract.Framework.Services.Neo;

namespace Neo.SmartContract.Framework.UnitTests.TestClasses
{
public class Contract_Map : SmartContract
{
public static string TestByteArray()
{
Map<byte[], string> some = new Map<byte[], string>();
some[new byte[] { 0x01, 0x01 }] = Json.Serialize("");
return Json.Serialize(some);
}

public static object TestByteArray2(byte[] key)
{
Map<string, string> some = new Map<string, string>();
some[key.ToByteString()] = "teststring2";
return Json.Serialize(some);
}

public static string TestByteArray3()
{
Map<string, string> some = new Map<string, string>();
string key = new byte[] { 0x01, 0x01 }.ToByteString();
some[key] = Json.Serialize("");
return Json.Serialize(some);
}

public static string TestUnicode(string key)
{
Map<string, string> some = new Map<string, string>();
some[key] = "129840test10022939";
return Json.Serialize(some);
}

public static string TestUnicodeValue(string value)
{
Map<string, string> some = new Map<string, string>();
some["ab"] = value;
return Json.Serialize(some);
}

public static string TestUnicodeKeyValue(string key, string value)
{
Map<string, string> some = new Map<string, string>();
some[key] = value;
return Json.Serialize(some);
}

public static string TestInt(int key)
{
Map<int, string> some = new Map<int, string>();
some[key] = "string";
return Json.Serialize(some);
}

public static string TestBool(bool key)
{
Map<bool, string> some = new Map<bool, string>();
some[key] = "testbool";
return Json.Serialize(some);
}

public static object TestDeserialize(string key)
{
Map<string, string> some = new Map<string, string>();
some[key] = "testdeserialize";
string sea = Json.Serialize(some);
return Json.Deserialize(sea);
}
}
}