Skip to content

Commit

Permalink
test EnsureIntegerInRange
Browse files Browse the repository at this point in the history
  • Loading branch information
Hecate2 committed Sep 19, 2024
1 parent 11a01b2 commit b136d01
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 0 deletions.
8 changes: 8 additions & 0 deletions tests/Neo.Compiler.CSharp.TestContracts/Contract_Overflow.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace Neo.Compiler.CSharp.TestContracts
{
public class Contract_Overflow : SmartContract.Framework.SmartContract
{
public static int Add(int a, int b) => a + b;
public static int Mul(int a, int b) => a * b;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
using Neo.Cryptography.ECC;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Numerics;

namespace Neo.SmartContract.Testing;

public abstract class Contract_Overflow(Neo.SmartContract.Testing.SmartContractInitialize initialize) : Neo.SmartContract.Testing.SmartContract(initialize), IContractInfo
{
#region Compiled data

public static Neo.SmartContract.Manifest.ContractManifest Manifest => Neo.SmartContract.Manifest.ContractManifest.Parse(@"{""name"":""Contract_Overflow"",""groups"":[],""features"":{},""supportedstandards"":[],""abi"":{""methods"":[{""name"":""add"",""parameters"":[{""name"":""a"",""type"":""Integer""},{""name"":""b"",""type"":""Integer""}],""returntype"":""Integer"",""offset"":0,""safe"":false},{""name"":""mul"",""parameters"":[{""name"":""a"",""type"":""Integer""},{""name"":""b"",""type"":""Integer""}],""returntype"":""Integer"",""offset"":53,""safe"":false}],""events"":[]},""permissions"":[],""trusts"":[],""extra"":{""nef"":{""optimization"":""All""}}}");

/// <summary>
/// Optimization: "All"
/// </summary>
public static Neo.SmartContract.NefFile Nef => Neo.IO.Helper.AsSerializable<Neo.SmartContract.NefFile>(Convert.FromBase64String(@"TkVGM1Rlc3RpbmdFbmdpbmUAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAGpXAAJ4eZ5KAgAAAIAuBCIKSgL///9/Mh4D/////wAAAACRSgL///9/MgwDAAAAAAEAAACfQFcAAnh5oEoCAAAAgC4EIgpKAv///38yHgP/////AAAAAJFKAv///38yDAMAAAAAAQAAAJ9AhqjPTQ=="));

#endregion

#region Unsafe methods

/// <summary>
/// Unsafe method
/// </summary>
[DisplayName("add")]
public abstract BigInteger? Add(BigInteger? a, BigInteger? b);

/// <summary>
/// Unsafe method
/// </summary>
[DisplayName("mul")]
public abstract BigInteger? Mul(BigInteger? a, BigInteger? b);

#endregion

}
32 changes: 32 additions & 0 deletions tests/Neo.Compiler.CSharp.UnitTests/UnitTest_Overflow.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Neo.SmartContract.Testing;
using Neo.SmartContract.Testing.Exceptions;
using System.Numerics;

namespace Neo.Compiler.CSharp.UnitTests
{
[TestClass]
public class UnitTest_Overflow : DebugAndTestBase<Contract_Overflow>
{
[TestMethod]
public void Test_Add()
{
Assert.AreEqual(unchecked(int.MinValue - 1), Contract.Add(int.MinValue, -1));
Assert.AreEqual(unchecked(int.MaxValue + 1), Contract.Add(int.MaxValue, 1));
Assert.AreEqual(unchecked(int.MinValue - int.MaxValue), Contract.Add(int.MinValue, -int.MaxValue));
Assert.AreEqual(unchecked(int.MaxValue - int.MinValue), Contract.Add(int.MaxValue, unchecked(-int.MinValue)));
}

[TestMethod]
public void Test_Mul()
{
Assert.AreEqual(unchecked(int.MinValue * 2), Contract.Mul(int.MinValue, 2));
Assert.AreEqual(unchecked(int.MinValue * (-2)), Contract.Mul(int.MinValue, -2));
Assert.AreEqual(unchecked(int.MaxValue * 2), Contract.Mul(int.MaxValue, 2));
Assert.AreEqual(unchecked(int.MaxValue * (-2)), Contract.Mul(int.MaxValue, -2));
Assert.AreEqual(unchecked(int.MinValue * int.MaxValue), Contract.Mul(int.MinValue, int.MaxValue));
Assert.AreEqual(unchecked(int.MinValue * (-int.MaxValue)), Contract.Mul(int.MinValue, -int.MaxValue));
Assert.AreEqual(unchecked((-int.MinValue) * int.MaxValue), Contract.Mul(unchecked(-int.MinValue), int.MaxValue));
}
}
}

0 comments on commit b136d01

Please sign in to comment.