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 method to convert from byte array to bigInteger #316

Merged
merged 11 commits into from
Jul 4, 2020
Merged
Show file tree
Hide file tree
Changes from 10 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
11 changes: 11 additions & 0 deletions src/Neo.SmartContract.Framework/Helper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,21 @@ public static class Helper

/// <summary>
/// Converts byte[] to BigInteger. No guarantees are assumed regarding BigInteger working range.
/// If it's null it will return 0
/// Examples: [0x0a] -> 10; [0x80] -> -128; [] -> 0; [0xff00] -> 255
/// </summary>
[OpCode(OpCode.DUP)]
[OpCode(OpCode.ISNULL)]
[OpCode(OpCode.JMPIFNOT, "0x05")]
[OpCode(OpCode.PUSH0)]
[OpCode(OpCode.SWAP)]
[OpCode(OpCode.DROP)]
[OpCode(OpCode.CONVERT, StackItemType_Integer)]
public extern static BigInteger ToBigInteger(this byte[] source);
//{
// if (value == null) return 0;
// return value.ToBigInteger();
//}

/// <summary>
/// Converts string to byte[]. Examples: "hello" -> [0x68656c6c6f]; "" -> []; "Neo" -> [0x4e656f]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,13 @@ public static bool IfNull(object obj)

public static object NullCollationAndCollation(string code)
{
return Storage.Get(code)?.ToBigInteger() ?? 123;
return Storage.Get(code) ?? new byte[] { 123 };
}

public static object NullCollationAndCollation2(string code)
{
Storage.Put(code, "111");
return Storage.Get(code)?.ToBigInteger() ?? 123;
return Storage.Get(code) ?? new byte[] { 123 };
}
}
}
26 changes: 13 additions & 13 deletions tests/Neo.Compiler.MSIL.UnitTests/UnitTest_NULL.cs
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ public void NullCollation()
Assert.IsTrue(str == "linux");
}
}

[TestMethod]
public void NullCollationAndCollation()
{
Expand All @@ -115,13 +116,12 @@ public void NullCollationAndCollation()
Features = ContractFeatures.HasStorage
}
});
{
var result = _testengine.ExecuteTestCaseStandard("nullCollationAndCollation", "nes");
var item = result.Pop() as Integer;
var num = item.GetInteger();
Assert.IsTrue(num == 123);
}

var result = _testengine.ExecuteTestCaseStandard("nullCollationAndCollation", "nes");
var item = result.Pop() as Buffer;
Assert.AreEqual(123, item.GetSpan()[0]);
}

[TestMethod]
public void NullCollationAndCollation2()
{
Expand All @@ -135,15 +135,15 @@ public void NullCollationAndCollation2()
Features = ContractFeatures.HasStorage
}
});
{
var result = _testengine.ExecuteTestCaseStandard("nullCollationAndCollation2", "nes");
var item = result.Pop() as Integer;
var bts = System.Text.Encoding.ASCII.GetBytes("111");
var num = new System.Numerics.BigInteger(bts);

Assert.IsTrue(item.GetInteger() == num);
}
var result = _testengine.ExecuteTestCaseStandard("nullCollationAndCollation2", "nes");
var item = result.Pop() as ByteString;
var bts = System.Text.Encoding.ASCII.GetBytes("111");
var num = new System.Numerics.BigInteger(bts);

Assert.AreEqual(num, item.GetInteger());
}

[TestMethod]
public void EqualNull()
{
Expand Down
35 changes: 35 additions & 0 deletions tests/Neo.SmartContract.Framework.UnitTests/HelperTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,41 @@ public void TestHexToBytes()
Assert.AreEqual("0a0b0c0d0e0f", (item as ByteString).GetSpan().ToHexString());
}

[TestMethod]
public void TestToBigInteger()
{
// 0

_engine.Reset();
var result = _engine.ExecuteTestCaseStandard("testToBigInteger", StackItem.Null);
Assert.AreEqual(VMState.HALT, _engine.State);
Assert.AreEqual(1, result.Count);

var item = result.Pop();
Assert.IsInstanceOfType(item, typeof(Integer));
Assert.AreEqual(0, item.GetInteger());

_engine.Reset();
result = _engine.ExecuteTestCaseStandard("testToBigInteger", new ByteString(new byte[0]));
Assert.AreEqual(VMState.HALT, _engine.State);
Assert.AreEqual(1, result.Count);

item = result.Pop();
Assert.IsInstanceOfType(item, typeof(Integer));
Assert.AreEqual(0, item.GetInteger());

// Value

_engine.Reset();
result = _engine.ExecuteTestCaseStandard("testToBigInteger", new ByteString(new byte[] { 123 }));
Assert.AreEqual(VMState.HALT, _engine.State);
Assert.AreEqual(1, result.Count);

item = result.Pop();
Assert.IsInstanceOfType(item, typeof(Integer));
Assert.AreEqual(123, item.GetInteger());
}

[TestMethod]
public void TestAssert()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Neo.SmartContract.Framework;
using System.Numerics;

namespace Compiler.MSIL.TestClasses
{
Expand All @@ -18,6 +19,11 @@ public static int AssertCall(bool value)
return 5;
}

public static BigInteger TestToBigInteger(byte[] data)
{
return data.ToBigInteger();
}

public static void VoidAssertCall(bool value)
{
Assert(value == true);
Expand Down