Skip to content

Commit

Permalink
test suite for portable lib
Browse files Browse the repository at this point in the history
  • Loading branch information
NicolasDorier committed Dec 7, 2014
1 parent f60b903 commit 91465a2
Show file tree
Hide file tree
Showing 9 changed files with 160 additions and 16 deletions.
15 changes: 10 additions & 5 deletions NBitcoin.Portable.Tests/NBitcoin.Portable.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,22 @@
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<DefineConstants>DEBUG;TRACE;PORTABLE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<DefineConstants>TRACE;PORTABLE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Numerics" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
Expand Down Expand Up @@ -75,9 +76,6 @@
<Compile Include="..\NBitcoin.Tests\key_tests.cs">
<Link>key_tests.cs</Link>
</Compile>
<Compile Include="..\NBitcoin.Tests\RepositoryTests.cs">
<Link>RepositoryTests.cs</Link>
</Compile>
<Compile Include="..\NBitcoin.Tests\script_tests.cs">
<Link>script_tests.cs</Link>
</Compile>
Expand Down Expand Up @@ -106,6 +104,10 @@
<Link>util_tests.cs</Link>
</Compile>
<Compile Include="Properties\AssemblyInfo.cs" />
<None Include="..\NBitcoin.Tests\data\**\*.*">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<Link>data\%(RecursiveDir)%(Filename)%(Extension)</Link>
</None>
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
Expand All @@ -120,6 +122,9 @@
<Name>NBitcoin.Portable</Name>
</ProjectReference>
</ItemGroup>
<ItemGroup>
<Folder Include="Data\" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
Expand Down
20 changes: 20 additions & 0 deletions NBitcoin.Portable/NBitcoin.Portable.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,18 @@
<Compile Include="..\NBitcoin\BIP38\BitcoinEncryptedSecret.cs">
<Link>BitcoinEncryptedSecret.cs</Link>
</Compile>
<Compile Include="..\NBitcoin\Coins.cs">
<Link>Coins.cs</Link>
</Compile>
<Compile Include="..\NBitcoin\CoinsView.cs">
<Link>CoinsView.cs</Link>
</Compile>
<Compile Include="..\NBitcoin\MedianFilter.cs">
<Link>MedianFilter.cs</Link>
<AutoGen>True</AutoGen>
<DesignTime>True</DesignTime>
<DependentUpon>MedianFilter.tt</DependentUpon>
</Compile>
<Compile Include="..\NBitcoin\Properties\AssemblyInfo.cs">
<Link>Properties\AssemblyInfo.cs</Link>
</Compile>
Expand Down Expand Up @@ -308,6 +320,11 @@
</Reference>
</ItemGroup>
<ItemGroup>
<None Include="..\NBitcoin\MedianFilter.tt">
<Link>MedianFilter.tt</Link>
<Generator>TextTemplatingFileGenerator</Generator>
<LastGenOutput>MedianFilter.cs</LastGenOutput>
</None>
<None Include="packages.config" />
</ItemGroup>
<ItemGroup>
Expand All @@ -316,6 +333,9 @@
<Name>NBitcoin.Portable.BouncyCastle</Name>
</ProjectReference>
</ItemGroup>
<ItemGroup>
<Service Include="{508349B6-6B84-4DF5-91F0-309BEEBAD82D}" />
</ItemGroup>
<Import Project="$(MSBuildExtensionsPath32)\Microsoft\Portable\$(TargetFrameworkVersion)\Microsoft.Portable.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
Expand Down
7 changes: 3 additions & 4 deletions NBitcoin.Tests/ChainTests.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using NBitcoin.Scanning;
using System;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
Expand Down Expand Up @@ -80,7 +79,7 @@ public void CanParseRandomScripts()
new Script(bytes).ToString();
}
}

#if !PORTABLE
[Fact]
[Trait("UnitTest", "UnitTest")]
public void CanBuildConcurrentChain()
Expand Down Expand Up @@ -123,7 +122,7 @@ public void CanBuildConcurrentChain()
Assert.Equal(cchain.GetBlock(6), b6b);
Assert.Equal(cchain.GetBlock(5), b5b);
}

#endif
private ChainedBlock AddBlock(Chain chain)
{
BlockHeader header = new BlockHeader();
Expand Down
7 changes: 4 additions & 3 deletions NBitcoin.Tests/ColoredCoinsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ public AssetId Id
}
}

#if !PORTABLE
//[Fact]
public void TestFun()
{
Expand All @@ -124,7 +125,7 @@ public void TestFun()

Assert.True(colored.ToBytes().SequenceEqual(prismColored.ToBytes()));
}

#endif
[Fact]
[Trait("UnitTest", "UnitTest")]
//https://github.com/OpenAssets/open-assets-protocol/blob/master/specification.mediawiki
Expand Down Expand Up @@ -238,7 +239,7 @@ private static Transaction CreateSpecTransaction(NoSqlColoredTransactionReposito
return testedTx;
}


#if !PORTABLE
[Fact]
[Trait("UnitTest", "UnitTest")]
public void CanFetchTransactionFromCoinprism()
Expand All @@ -255,7 +256,7 @@ private void CanFetchTransactionFromCoinprismCore(string test)
var actual = new CoinprismColoredTransactionRepository().Get(tester.TestedTxId);
Assert.True(actual.ToBytes().SequenceEqual(expected.ToBytes()));
}

#endif
//https://www.coinprism.info/tx/b4399a545c4ddd640920d63af75e7367fe4d94b2d7f7a3423105e25ac5f165a6
//Asset Id : 3QzJDrSsi4Pm2DhcZFXR9MGJsXXtsYhUsq
//1BvvRfz4XnxSWJ524TusetYKrtZnAbgV3r to 18Jcv42cRknPmxrQPb2zSBuEVWq3egjCKq
Expand Down
2 changes: 2 additions & 0 deletions NBitcoin.Tests/util_tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,7 @@ public void CanDetectBase58NetworkAndType()
ExpectedType = (Type)null,
Network = (Network)null
},
#if !PORTABLE
new
{
Base58 = "6PYLtMnXvfG3oJde97zRyLYFZCYizPU5T3LwgdYJz1fRhh16bU7u6PPmY7",
Expand All @@ -376,6 +377,7 @@ public void CanDetectBase58NetworkAndType()
ExpectedType = typeof(BitcoinConfirmationCode),
Network = Network.Main
},
#endif
new
{
Base58 = "xprv9s21ZrQH143K3Gx1VAAD1ueDmwoPQUApekxWYSJ1f4W4m1nUPpRGdV5sTVhixZJT5cP2NqtEMZ2mrwHdW5RWpohCwspWidCpcLALvioXDyz",
Expand Down
4 changes: 2 additions & 2 deletions NBitcoin/Coins.cs
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ private void Cleanup()
}
}


#if !PORTABLE
public bool Spend(int position, out TxInUndo undo)
{
undo = null;
Expand All @@ -155,7 +155,7 @@ public bool Spend(int position)
TxInUndo undo;
return Spend(position, out undo);
}

#endif
#region IBitcoinSerializable Members

public void ReadWrite(BitcoinStream stream)
Expand Down
98 changes: 98 additions & 0 deletions NBitcoin/MedianFilter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace NBitcoin
{
public class MedianFilterInt32
{
Queue<Int32> vValues;
Queue<Int32> vSorted;
uint nSize;

public MedianFilterInt32(uint size, Int32 initialValue)
{
nSize = size;
vValues = new Queue<Int32>((int)size);
vValues.Enqueue(initialValue);
vSorted = new Queue<Int32>(vValues);
}

public Int32 Median
{
get
{
int size = vSorted.Count;
if(size <= 0)
throw new InvalidOperationException("size <= 0");

var sortedList = vSorted.ToList();
if(size % 2 == 1)
{
return sortedList[size / 2];
}
else // Even number of elements
{
return (sortedList[size / 2 - 1] + sortedList[size / 2]) / 2;
}
}
}

public void Input(Int32 value)
{
if(vValues.Count == nSize)
{
vValues.Dequeue();
}
vValues.Enqueue(value);
vSorted = new Queue<Int32>(vValues.OrderBy(o => o));
}
}
public class MedianFilterInt64
{
Queue<Int64> vValues;
Queue<Int64> vSorted;
uint nSize;

public MedianFilterInt64(uint size, Int64 initialValue)
{
nSize = size;
vValues = new Queue<Int64>((int)size);
vValues.Enqueue(initialValue);
vSorted = new Queue<Int64>(vValues);
}

public Int64 Median
{
get
{
int size = vSorted.Count;
if(size <= 0)
throw new InvalidOperationException("size <= 0");

var sortedList = vSorted.ToList();
if(size % 2 == 1)
{
return sortedList[size / 2];
}
else // Even number of elements
{
return (sortedList[size / 2 - 1] + sortedList[size / 2]) / 2;
}
}
}

public void Input(Int64 value)
{
if(vValues.Count == nSize)
{
vValues.Dequeue();
}
vValues.Enqueue(value);
vSorted = new Queue<Int64>(vValues.OrderBy(o => o));
}
}
}
21 changes: 21 additions & 0 deletions NBitcoin/RandomUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,21 @@

namespace NBitcoin
{
public class UnsecureRandom : RandomUtils.IRandom
{
Random _Rand = new Random();
#region IRandom Members

public void GetBytes(byte[] output)
{
lock(_Rand)
{
_Rand.NextBytes(output);
}
}

#endregion
}
public class RandomUtils
{
#if !USEBC
Expand All @@ -34,6 +49,12 @@ public void GetBytes(byte[] output)

#endregion
}
#endif
#if USEBC && DEBUG
static RandomUtils()
{
Random = new UnsecureRandom();
}
#endif
public interface IRandom
{
Expand Down
2 changes: 0 additions & 2 deletions NBitcoin/StandardScripts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,6 @@ private static bool IsStandardScriptSig(Script scriptSig, Script scriptPubKey)
return template.CheckScriptSig(scriptSig, scriptPubKey);
}

#if !PORTABLE
//
// Check transaction inputs, and make sure any
// pay-to-script-hash transactions are evaluating IsStandard scripts
Expand Down Expand Up @@ -161,6 +160,5 @@ public static bool AreInputsStandard(Transaction tx, CoinsView coinsView)

return true;
}
#endif
}
}

0 comments on commit 91465a2

Please sign in to comment.