From d4a231a1141c03042a0138f46cacd49531a24ec0 Mon Sep 17 00:00:00 2001 From: lights Date: Thu, 5 Mar 2020 12:41:14 +0800 Subject: [PATCH] this unittest for https://github.com/neo-project/neo-devpack-dotnet/issues/195 --- .../Neo.Compiler.MSIL.UnitTests.csproj | 6 +++ .../TestClasses/Contract_TypeConvert.cs | 51 +++++++++++++++++++ .../UnitTest_TypeConvert.cs | 31 +++++++++++ 3 files changed, 88 insertions(+) create mode 100644 tests/Neo.Compiler.MSIL.UnitTests/TestClasses/Contract_TypeConvert.cs create mode 100644 tests/Neo.Compiler.MSIL.UnitTests/UnitTest_TypeConvert.cs diff --git a/tests/Neo.Compiler.MSIL.UnitTests/Neo.Compiler.MSIL.UnitTests.csproj b/tests/Neo.Compiler.MSIL.UnitTests/Neo.Compiler.MSIL.UnitTests.csproj index 53a854964..291748acd 100644 --- a/tests/Neo.Compiler.MSIL.UnitTests/Neo.Compiler.MSIL.UnitTests.csproj +++ b/tests/Neo.Compiler.MSIL.UnitTests/Neo.Compiler.MSIL.UnitTests.csproj @@ -29,4 +29,10 @@ + + + Never + + + diff --git a/tests/Neo.Compiler.MSIL.UnitTests/TestClasses/Contract_TypeConvert.cs b/tests/Neo.Compiler.MSIL.UnitTests/TestClasses/Contract_TypeConvert.cs new file mode 100644 index 000000000..f52f0ba8f --- /dev/null +++ b/tests/Neo.Compiler.MSIL.UnitTests/TestClasses/Contract_TypeConvert.cs @@ -0,0 +1,51 @@ +using System; +using System.Collections.Generic; +using System.Text; +using System.Numerics; +using Neo.SmartContract.Framework; + +namespace Neo.Compiler.MSIL.UnitTests.TestClasses +{ + + class Contract_TypeConvert : SmartContract.Framework.SmartContract + { + //Integer = 0x21, + //ByteArray = 0x28, + [OpCode(SmartContract.Framework.OpCode.CONVERT, "0x28")] + public extern static byte[] ConvertToByteArray(object from); + + + [OpCode(SmartContract.Framework.OpCode.CONVERT, "0x21")] + public extern static BigInteger ConvertToInteger(byte[] from); + + + + + public static object testType() + { + BigInteger int0 = 0; + var bts0 = ConvertToByteArray(int0); + BigInteger int1 = 2; + var bts1 = ConvertToByteArray(int1); + + var bts2=new byte[1] { 3 }; + var int2 = ConvertToInteger(bts2); + + var bts3 = ConvertToByteArray(new byte[0]) ; + var int3 = ConvertToInteger(bts3); + + var arrobj = new object[8]; + arrobj[0] = int0; + arrobj[1] = bts0; + arrobj[2] = int1; + arrobj[3] = bts1; + arrobj[4] = bts2; + arrobj[5] = int2; + arrobj[6] = bts3; + arrobj[7] = int3; + return arrobj; + } + + + } +} diff --git a/tests/Neo.Compiler.MSIL.UnitTests/UnitTest_TypeConvert.cs b/tests/Neo.Compiler.MSIL.UnitTests/UnitTest_TypeConvert.cs new file mode 100644 index 000000000..e7f268cef --- /dev/null +++ b/tests/Neo.Compiler.MSIL.UnitTests/UnitTest_TypeConvert.cs @@ -0,0 +1,31 @@ +using Microsoft.VisualStudio.TestTools.UnitTesting; +using Neo.Compiler.MSIL.UnitTests.Utils; +using Neo.VM.Types; +using System.Linq; + +namespace Neo.Compiler.MSIL.UnitTests +{ + [TestClass] + public class UnitTest_TypeConvert + { + [TestMethod] + public void UnitTest_TestTypeConvert() + { + var testengine = new TestEngine(); + testengine.AddEntryScript("./TestClasses/Contract_TypeConvert.cs"); + var result = testengine.ExecuteTestCaseStandard("testType"); + + //test 0,1,2 + Assert.IsTrue(result.TryPop(out Array arr)); + Assert.IsTrue(arr[0].Type == StackItemType.Integer); + Assert.IsTrue(arr[1].Type == StackItemType.ByteArray); + Assert.IsTrue(arr[2].Type == StackItemType.Integer); + Assert.IsTrue(arr[3].Type == StackItemType.ByteArray); + Assert.IsTrue(arr[4].Type == StackItemType.ByteArray); + Assert.IsTrue(arr[5].Type == StackItemType.Integer); + Assert.IsTrue(arr[6].Type == StackItemType.ByteArray); + Assert.IsTrue(arr[7].Type == StackItemType.Integer); + } + + } +}