Skip to content

Commit

Permalink
Core unit tests (neo-project#26)
Browse files Browse the repository at this point in the history
* Recreate unit tests on new branch, after source repo rebranding and renaming, faster than fixing a load of merge conflicts

* improve the culture testing of AssetState.cs GetName
  • Loading branch information
AshRolls authored and Erik Zhang committed Jul 14, 2017
1 parent 07dd1f5 commit 10e3659
Show file tree
Hide file tree
Showing 7 changed files with 1,128 additions and 0 deletions.
22 changes: 22 additions & 0 deletions neo.UnitTests/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
AntShares Unit Tests
====================

This project is a work in progress, aiming to provide unit test coverage for the core AntShares code.

Please note that we are aware that we are not using proper isolation / dependency injection / mocking techniques in these tests. To do that would require larger reworks of the base code which is a change for a later date in discussion with the core team, at the moment we are just aiming to get some basic coverage going.

Structure
====================

We use built in Visual Studio functionality with MSTest and the Microsoft.VisualStudio.TestPlatform.TestFramework package.

To run the tests, build the solution to discover tests, then view and run the tests from the 'Test Explorer' window within Visual Studio.
OR
With .NET Core SDK installed, use the CLI to navigate to the src\AntShares.UnitTest and use the command 'dotnet restore' to get packages, followed by 'dotnet test' to run tests.

Coverage
====================

* Core
* AccountState.cs
* AssetState.cs
22 changes: 22 additions & 0 deletions neo.UnitTests/TestUtils.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Neo.UnitTest
{
public static class TestUtils
{
public static byte[] GetByteArray(int length, byte firstByte)
{
byte[] array = new byte[length];
array[0] = firstByte;
for (int i = 1; i < length; i++)
{
array[i] = 0x20;
}
return array;
}
}
}
273 changes: 273 additions & 0 deletions neo.UnitTests/UT_AccountState.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,273 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using FluentAssertions;
using Neo.Cryptography.ECC;
using System.Collections.Generic;
using System.IO;
using System.Text;
using Neo.Core;
using Neo.UnitTest;

namespace Neo.UnitTests
{
[TestClass]
public class UT_AccountState
{
AccountState uut;

[TestInitialize]
public void TestSetup()
{
uut = new AccountState();
}

[TestMethod]
public void ScriptHash_Get()
{
uut.ScriptHash.Should().BeNull();
}

[TestMethod]
public void ScriptHash_Set()
{
UInt160 val = new UInt160();
uut.ScriptHash = val;
uut.ScriptHash.Should().Be(val);
}

[TestMethod]
public void IsFrozen_Get()
{
uut.IsFrozen.Should().Be(false);
}

[TestMethod]
public void IsFrozen_Set()
{
uut.IsFrozen = true;
uut.IsFrozen.Should().Be(true);
}

[TestMethod]
public void Votes_Get()
{
uut.Votes.Should().BeNull();
}

[TestMethod]
public void Votes_Set()
{
ECPoint val = new ECPoint();
ECPoint[] array = new ECPoint[] { val };
uut.Votes = array;
uut.Votes[0].Should().Be(val);
}

[TestMethod]
public void Balances_Get()
{
uut.Balances.Should().BeNull();
}

[TestMethod]
public void Balances_Set()
{
UInt256 key = new UInt256();
Fixed8 val = new Fixed8();
Dictionary<UInt256, Fixed8> dict = new Dictionary<UInt256, Fixed8>();
dict.Add(key, val);
uut.Balances = dict;
uut.Balances[key].Should().Be(val);
}

[TestMethod]
public void Size_Get_0_Votes_0_Balances()
{
UInt160 val = new UInt160();
ECPoint[] array = new ECPoint[0];
Dictionary<UInt256, Fixed8> dict = new Dictionary<UInt256, Fixed8>();

uut.ScriptHash = val;
uut.Votes = array;
uut.Balances = dict;

uut.Size.Should().Be(24); // 1 + 20 + 1 + 1 + 1 + 0 * (32 + 8)
}

[TestMethod]
public void Size_Get_1_Vote_0_Balances()
{
UInt160 val = new UInt160();
ECPoint[] array = new ECPoint[] { new ECPoint() };
Dictionary<UInt256, Fixed8> dict = new Dictionary<UInt256, Fixed8>();

uut.ScriptHash = val;
uut.Votes = array;
uut.Balances = dict;

uut.Size.Should().Be(25); // 1 + 20 + 1 + 2 + 1 + 0 * (32 + 8)
}

[TestMethod]
public void Size_Get_5_Votes_0_Balances()
{
UInt160 val = new UInt160();
ECPoint[] array = new ECPoint[] { new ECPoint(), new ECPoint(), new ECPoint(), new ECPoint(), new ECPoint() };
Dictionary<UInt256, Fixed8> dict = new Dictionary<UInt256, Fixed8>();

uut.ScriptHash = val;
uut.Votes = array;
uut.Balances = dict;

uut.Size.Should().Be(29); // 1 + 20 + 1 + 6 + 1 + 0 * (32 + 8)
}

[TestMethod]
public void Size_Get_0_Votes_1_Balance()
{
UInt160 val = new UInt160();
ECPoint[] array = new ECPoint[0];
Dictionary<UInt256, Fixed8> dict = new Dictionary<UInt256, Fixed8>();
dict.Add(new UInt256(), new Fixed8());

uut.ScriptHash = val;
uut.Votes = array;
uut.Balances = dict;

uut.Size.Should().Be(64); // 1 + 20 + 1 + 1 + 1 + 1 * (32 + 8)
}

[TestMethod]
public void Size_Get_0_Votes_5_Balance()
{
UInt160 val = new UInt160();
ECPoint[] array = new ECPoint[0];
Dictionary<UInt256, Fixed8> dict = new Dictionary<UInt256, Fixed8>();
dict.Add(new UInt256(), new Fixed8());
dict.Add(new UInt256(TestUtils.GetByteArray(32, 0x20)), new Fixed8());
dict.Add(new UInt256(TestUtils.GetByteArray(32, 0x21)), new Fixed8());
dict.Add(new UInt256(TestUtils.GetByteArray(32, 0x22)), new Fixed8());
dict.Add(new UInt256(TestUtils.GetByteArray(32, 0x23)), new Fixed8());

uut.ScriptHash = val;
uut.Votes = array;
uut.Balances = dict;

uut.Size.Should().Be(224); // 1 + 20 + 1 + 1 + 1 + 5 * (32 + 8)
}

[TestMethod]
public void Size_Get_5_Votes_5_Balance()
{
UInt160 val = new UInt160();
ECPoint[] array = new ECPoint[] { new ECPoint(), new ECPoint(), new ECPoint(), new ECPoint(), new ECPoint() };
Dictionary<UInt256, Fixed8> dict = new Dictionary<UInt256, Fixed8>();
dict.Add(new UInt256(), new Fixed8());
dict.Add(new UInt256(TestUtils.GetByteArray(32, 0x20)), new Fixed8());
dict.Add(new UInt256(TestUtils.GetByteArray(32, 0x21)), new Fixed8());
dict.Add(new UInt256(TestUtils.GetByteArray(32, 0x22)), new Fixed8());
dict.Add(new UInt256(TestUtils.GetByteArray(32, 0x23)), new Fixed8());

uut.ScriptHash = val;
uut.Votes = array;
uut.Balances = dict;

uut.Size.Should().Be(229); // 1 + 20 + 1 + 6 + 1 + 5 * (32 + 8)
}

private void setupAccountStateWithValues(AccountState accState, out UInt160 scriptHashVal, out ECPoint votesVal, out UInt256 key, out Fixed8 dictVal)
{
scriptHashVal = new UInt160();
accState.ScriptHash = scriptHashVal;
accState.IsFrozen = true;
votesVal = new ECPoint();
ECPoint[] array = new ECPoint[] { votesVal };
accState.Votes = array;
key = new UInt256();
dictVal = new Fixed8();
Dictionary<UInt256, Fixed8> dict = new Dictionary<UInt256, Fixed8>();
dict.Add(key, dictVal);
accState.Balances = dict;
}

[TestMethod]
public void Clone()
{
UInt160 scriptHashVal;
ECPoint votesVal;
UInt256 key;
Fixed8 dictVal;
setupAccountStateWithValues(uut, out scriptHashVal, out votesVal, out key, out dictVal);

AccountState newAs = ((ICloneable<AccountState>)uut).Clone();
newAs.ScriptHash.Should().Be(scriptHashVal);
newAs.IsFrozen.Should().Be(true);
newAs.Votes[0].Should().Be(votesVal);
newAs.Balances.Count.Should().Be(1);
newAs.Balances.Should().ContainKey(key);
newAs.Balances[key].Should().Be(dictVal);
}

[TestMethod]
public void FromReplica()
{
AccountState accState = new AccountState();
UInt160 scriptHashVal;
ECPoint votesVal;
UInt256 key;
Fixed8 dictVal;
setupAccountStateWithValues(accState, out scriptHashVal, out votesVal, out key, out dictVal);

((ICloneable<AccountState>)uut).FromReplica(accState);
uut.ScriptHash.Should().Be(scriptHashVal);
uut.IsFrozen.Should().Be(true);
uut.Votes[0].Should().Be(votesVal);
uut.Balances.Count.Should().Be(1);
uut.Balances.Should().ContainKey(key);
uut.Balances[key].Should().Be(dictVal);
}

[TestMethod]
public void Deserialize()
{
byte[] data = new byte[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0 };
int index = 0;
using (MemoryStream ms = new MemoryStream(data, index, data.Length - index, false))
{
using (BinaryReader reader = new BinaryReader(ms))
{
uut.Deserialize(reader);
}
}
uut.IsFrozen.Should().Be(true);
}


[TestMethod]
public void Serialize()
{
UInt160 scriptHashVal;
ECPoint votesVal;
UInt256 key;
Fixed8 dictVal;
setupAccountStateWithValues(uut, out scriptHashVal, out votesVal, out key, out dictVal);

byte[] data;
using (MemoryStream stream = new MemoryStream())
{
using (BinaryWriter writer = new BinaryWriter(stream, Encoding.ASCII, true))
{
uut.Serialize(writer);
data = stream.ToArray();
}
}

byte[] requiredData = new byte[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0 };

data.Length.Should().Be(25);
for (int i=0; i<25; i++)
{
data[i].Should().Be(requiredData[i]);
}
}
}
}
Loading

0 comments on commit 10e3659

Please sign in to comment.