Skip to content

Commit

Permalink
Merge branch 'develop' into 'master'
Browse files Browse the repository at this point in the history
Develop

See merge request company-projects/Meadow!45
  • Loading branch information
David Pokora committed Oct 7, 2018
2 parents 9f32379 + 54f8359 commit a794672
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 1 deletion.
34 changes: 34 additions & 0 deletions src/Meadow.Core.Test/UIntTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,40 @@ public void ByteCast()
Assert.Equal(num, (byte)bigNum);
}

[Fact]
public void DivideRounded()
{
// Test division with a cleanly divided number.
UInt256 dividend = 30;
UInt256 divisor = 3;
UInt256 result = UInt256.DivideRounded(dividend, divisor);
Assert.Equal(10, result);

// Test division with remainder (under midpoint, rounds down).
dividend = 29;
divisor = 4;
result = UInt256.DivideRounded(dividend, divisor);
Assert.Equal(7, result);

// Test division with remainder (at midpoint, rounds up).
dividend = 30;
divisor = 4;
result = UInt256.DivideRounded(dividend, divisor);
Assert.Equal(8, result);

// Test division with remainder (over midpoint, rounds up).
dividend = 31;
divisor = 4;
result = UInt256.DivideRounded(dividend, divisor);
Assert.Equal(8, result);

// Test division with zero integer (stays at zero).
dividend = 0;
divisor = 4;
result = UInt256.DivideRounded(dividend, divisor);
Assert.Equal(0, result);
}

[Fact]
public void WeiTest()
{
Expand Down
3 changes: 2 additions & 1 deletion src/Meadow.Core/EthTypes/UInt256.cs
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,8 @@ public static UInt256 DivRem(UInt256 dividend, UInt256 divisor, out UInt256 rema
/// </summary>
public static UInt256 DivideRounded(UInt256 dividend, UInt256 divisor)
{
return Math.Round((decimal)dividend / (decimal)divisor);
// Perform rounded integer division on large number
return (dividend / divisor) + ((dividend % divisor) / (divisor / 2));
}

public static explicit operator decimal(UInt256 value) => (decimal)value.ToBigInteger();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,4 +161,11 @@ contract VarAnalysisContract
{
nestedMapping[key1][key2] = enumValue;
}

function testExternalCallDataArgs(address[] testArr, uint256 u1, uint256 u2) external {
// Arguments in this context (external) should be resolved from calldata, not memory)
for(uint i=0; i< testArr.length; i+=1) {
require(testArr[i] != address(0));
}
}
}
7 changes: 7 additions & 0 deletions src/Meadow.DebugExampleTests/DebuggerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -128,5 +128,12 @@ public async Task TestMappings()
// TODO: Verify variables.
Assert.Inconclusive();
}

[TestMethod]
public async Task TestExternalCallDataArgs()
{
Address[] addresses = new Address[] { Accounts[0] };
await _contract.testExternalCallDataArgs(addresses, 10, 10);
}
}
}

0 comments on commit a794672

Please sign in to comment.