Merge pull request #65 from Maledong/ChangeToTask

[Update] Refactor codes according to RocherHong's changes
This commit is contained in:
lionsoul 2018-07-30 10:56:10 +08:00 committed by GitHub
commit 235cd0388c
16 changed files with 52 additions and 843 deletions

View File

@ -1,34 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6" />
</startup>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="System.Collections.Immutable" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-1.2.1.0" newVersion="1.2.1.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.IO.Compression" publicKeyToken="b77a5c561934e089" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-4.1.2.0" newVersion="4.1.2.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.IO.FileSystem" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-4.0.2.0" newVersion="4.0.2.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.IO.FileSystem.Primitives" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-4.0.2.0" newVersion="4.0.2.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Security.Cryptography.Primitives" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-4.0.1.0" newVersion="4.0.1.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Xml.XPath.XDocument" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-4.0.2.0" newVersion="4.0.2.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
</configuration>

View File

@ -1,21 +0,0 @@
using BenchmarkDotNet.Attributes;
using IP2Region;
namespace BenchmarkTest.BenchTests
{
public class BenchTestBase
{
protected DbSearcher _dbSearcher = null;
[GlobalSetup]
public void Init()
{
_dbSearcher = new DbSearcher("../../../../../data/ip2region.db");
}
[GlobalCleanup]
public void Clearup()
{
_dbSearcher.Dispose();
}
}
}

View File

@ -1,46 +0,0 @@
using BenchmarkDotNet.Attributes;
using System.Threading.Tasks;
namespace BenchmarkTest.BenchTests
{
public class NormalBenchmarkTests : BenchTestBase
{
[Params("0.0.0.0", "210.109.255.230", "192.168.0.1", "255.255.255.255", "183.196.233.159",
"77.49.66.88", "210.248.255.231", "10.10.10.10", "197.84.60.202", "35.193.251.120",
"20.108.91.101", "120.196.148.137", "249.255.250.200", "112.65.1.130")]
public string validIp = null;
[Benchmark]
public void TestSyncSpeedForMemorySearch()
{
_dbSearcher.MemorySearch(validIp);
}
[Benchmark]
public async Task TestAsyncSpeedForMemorySearch()
{
await _dbSearcher.MemorySearchAsync(validIp);
}
[Benchmark]
public void TestSyncSpeedForBinarySearch()
{
_dbSearcher.BinarySearch(validIp);
}
[Benchmark]
public async Task TestAsyncBinarySearch()
{
await _dbSearcher.BinarySearchAsync(validIp);
}
[Benchmark]
public void TestSyncSpeedForBTreeSearch()
{
_dbSearcher.BtreeSearch(validIp);
}
[Benchmark]
public async Task TestAsyncSpeedForBTreeSearch()
{
await _dbSearcher.BtreeSearchAsync(validIp);
}
}
}

View File

@ -1,154 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{053EA51E-2246-4FCB-A106-9B53E70FDC3F}</ProjectGuid>
<OutputType>Exe</OutputType>
<RootNamespace>BenchmarkTest</RootNamespace>
<AssemblyName>BenchmarkTest</AssemblyName>
<TargetFrameworkVersion>v4.6</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<TargetFrameworkProfile />
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="BenchmarkDotNet, Version=0.10.14.0, Culture=neutral, PublicKeyToken=aa0ca2f9092cefc4, processorArchitecture=MSIL">
<HintPath>..\packages\BenchmarkDotNet.0.10.14\lib\net46\BenchmarkDotNet.dll</HintPath>
</Reference>
<Reference Include="BenchmarkDotNet.Core, Version=0.10.14.0, Culture=neutral, PublicKeyToken=aa0ca2f9092cefc4, processorArchitecture=MSIL">
<HintPath>..\packages\BenchmarkDotNet.Core.0.10.14\lib\net46\BenchmarkDotNet.Core.dll</HintPath>
</Reference>
<Reference Include="BenchmarkDotNet.Toolchains.Roslyn, Version=0.10.14.0, Culture=neutral, PublicKeyToken=aa0ca2f9092cefc4, processorArchitecture=MSIL">
<HintPath>..\packages\BenchmarkDotNet.Toolchains.Roslyn.0.10.14\lib\net46\BenchmarkDotNet.Toolchains.Roslyn.dll</HintPath>
</Reference>
<Reference Include="Microsoft.CodeAnalysis, Version=2.6.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<HintPath>..\packages\Microsoft.CodeAnalysis.Common.2.6.1\lib\netstandard1.3\Microsoft.CodeAnalysis.dll</HintPath>
</Reference>
<Reference Include="Microsoft.CodeAnalysis.CSharp, Version=2.6.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<HintPath>..\packages\Microsoft.CodeAnalysis.CSharp.2.6.1\lib\netstandard1.3\Microsoft.CodeAnalysis.CSharp.dll</HintPath>
</Reference>
<Reference Include="Microsoft.DotNet.InternalAbstractions, Version=1.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60, processorArchitecture=MSIL">
<HintPath>..\packages\Microsoft.DotNet.InternalAbstractions.1.0.0\lib\net451\Microsoft.DotNet.InternalAbstractions.dll</HintPath>
</Reference>
<Reference Include="Microsoft.DotNet.PlatformAbstractions, Version=1.1.1.0, Culture=neutral, PublicKeyToken=adb9793829ddae60, processorArchitecture=MSIL">
<HintPath>..\packages\Microsoft.DotNet.PlatformAbstractions.1.1.1\lib\net451\Microsoft.DotNet.PlatformAbstractions.dll</HintPath>
</Reference>
<Reference Include="Microsoft.Win32.Registry, Version=4.0.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\packages\Microsoft.Win32.Registry.4.3.0\lib\net46\Microsoft.Win32.Registry.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.AppContext, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\packages\System.AppContext.4.3.0\lib\net46\System.AppContext.dll</HintPath>
</Reference>
<Reference Include="System.Collections.Immutable, Version=1.2.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\packages\System.Collections.Immutable.1.3.1\lib\portable-net45+win8+wp8+wpa81\System.Collections.Immutable.dll</HintPath>
</Reference>
<Reference Include="System.ComponentModel.Composition" />
<Reference Include="System.Console, Version=4.0.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\packages\System.Console.4.3.0\lib\net46\System.Console.dll</HintPath>
</Reference>
<Reference Include="System.Core" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Diagnostics.FileVersionInfo, Version=4.0.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\packages\System.Diagnostics.FileVersionInfo.4.3.0\lib\net46\System.Diagnostics.FileVersionInfo.dll</HintPath>
</Reference>
<Reference Include="System.Diagnostics.StackTrace, Version=4.0.3.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\packages\System.Diagnostics.StackTrace.4.3.0\lib\net46\System.Diagnostics.StackTrace.dll</HintPath>
</Reference>
<Reference Include="System.IO.Compression, Version=4.1.2.0, Culture=neutral, PublicKeyToken=b77a5c561934e089, processorArchitecture=MSIL">
<HintPath>..\packages\System.IO.Compression.4.3.0\lib\net46\System.IO.Compression.dll</HintPath>
</Reference>
<Reference Include="System.IO.FileSystem, Version=4.0.2.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\packages\System.IO.FileSystem.4.3.0\lib\net46\System.IO.FileSystem.dll</HintPath>
</Reference>
<Reference Include="System.IO.FileSystem.Primitives, Version=4.0.2.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\packages\System.IO.FileSystem.Primitives.4.3.0\lib\net46\System.IO.FileSystem.Primitives.dll</HintPath>
</Reference>
<Reference Include="System.Management" />
<Reference Include="System.Net.Http" />
<Reference Include="System.Numerics" />
<Reference Include="System.Reflection.Metadata, Version=1.4.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\packages\System.Reflection.Metadata.1.4.2\lib\portable-net45+win8\System.Reflection.Metadata.dll</HintPath>
</Reference>
<Reference Include="System.Security.Cryptography.Algorithms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\packages\System.Security.Cryptography.Algorithms.4.3.0\lib\net46\System.Security.Cryptography.Algorithms.dll</HintPath>
</Reference>
<Reference Include="System.Security.Cryptography.Encoding, Version=4.0.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\packages\System.Security.Cryptography.Encoding.4.3.0\lib\net46\System.Security.Cryptography.Encoding.dll</HintPath>
</Reference>
<Reference Include="System.Security.Cryptography.Primitives, Version=4.0.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\packages\System.Security.Cryptography.Primitives.4.3.0\lib\net46\System.Security.Cryptography.Primitives.dll</HintPath>
</Reference>
<Reference Include="System.Security.Cryptography.X509Certificates, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\packages\System.Security.Cryptography.X509Certificates.4.3.0\lib\net46\System.Security.Cryptography.X509Certificates.dll</HintPath>
</Reference>
<Reference Include="System.Text.Encoding.CodePages, Version=4.0.2.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\packages\System.Text.Encoding.CodePages.4.3.0\lib\net46\System.Text.Encoding.CodePages.dll</HintPath>
</Reference>
<Reference Include="System.Threading.Tasks.Extensions, Version=4.1.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
<HintPath>..\packages\System.Threading.Tasks.Extensions.4.3.0\lib\portable-net45+win8+wp8+wpa81\System.Threading.Tasks.Extensions.dll</HintPath>
</Reference>
<Reference Include="System.Threading.Thread, Version=4.0.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\packages\System.Threading.Thread.4.3.0\lib\net46\System.Threading.Thread.dll</HintPath>
</Reference>
<Reference Include="System.ValueTuple, Version=4.0.1.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
<HintPath>..\packages\System.ValueTuple.4.3.0\lib\netstandard1.0\System.ValueTuple.dll</HintPath>
</Reference>
<Reference Include="System.Xml" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Xml.ReaderWriter, Version=4.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\packages\System.Xml.ReaderWriter.4.3.0\lib\net46\System.Xml.ReaderWriter.dll</HintPath>
</Reference>
<Reference Include="System.Xml.XmlDocument, Version=4.0.2.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\packages\System.Xml.XmlDocument.4.3.0\lib\net46\System.Xml.XmlDocument.dll</HintPath>
</Reference>
<Reference Include="System.Xml.XPath, Version=4.0.2.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\packages\System.Xml.XPath.4.3.0\lib\net46\System.Xml.XPath.dll</HintPath>
</Reference>
<Reference Include="System.Xml.XPath.XDocument, Version=4.0.2.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\packages\System.Xml.XPath.XDocument.4.3.0\lib\net46\System.Xml.XPath.XDocument.dll</HintPath>
</Reference>
</ItemGroup>
<ItemGroup>
<Compile Include="BenchTests\BenchTestBase.cs" />
<Compile Include="BenchTests\NormalBenchmarkTests.cs" />
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<None Include="App.config" />
<None Include="packages.config" />
</ItemGroup>
<ItemGroup>
<Analyzer Include="..\packages\Microsoft.CodeAnalysis.Analyzers.1.1.0\analyzers\dotnet\cs\Microsoft.CodeAnalysis.Analyzers.dll" />
<Analyzer Include="..\packages\Microsoft.CodeAnalysis.Analyzers.1.1.0\analyzers\dotnet\cs\Microsoft.CodeAnalysis.CSharp.Analyzers.dll" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\IP2Region\IP2Region.csproj">
<Project>{4c6032ee-43a5-4d5c-88f8-c411069e49e0}</Project>
<Name>IP2Region</Name>
</ProjectReference>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>

View File

@ -1,13 +0,0 @@
using BenchmarkDotNet.Running;
using BenchmarkTest.BenchTests;
namespace BenchmarkTest
{
public class Program
{
public static void Main(string[] args)
{
BenchmarkRunner.Run<NormalBenchmarkTests>();
}
}
}

View File

@ -1,35 +0,0 @@
using System.Reflection;
using System.Runtime.InteropServices;
// 有关程序集的一般信息由以下
// 控制。更改这些特性值可修改
// 与程序集关联的信息。
[assembly: AssemblyTitle("BenchmarkTest")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("BenchmarkTest")]
[assembly: AssemblyCopyright("Copyright © 2018")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
// 将 ComVisible 设置为 false 会使此程序集中的类型
//对 COM 组件不可见。如果需要从 COM 访问此程序集中的类型
//请将此类型的 ComVisible 特性设置为 true。
[assembly: ComVisible(false)]
// 如果此项目向 COM 公开,则下列 GUID 用于类型库的 ID
[assembly: Guid("053ea51e-2246-4fcb-a106-9b53e70fdc3f")]
// 程序集的版本信息由下列四个值组成:
//
// 主版本
// 次版本
// 生成号
// 修订号
//
// 可以指定所有值,也可以使用以下所示的 "*" 预置版本号和修订号
// 方法是按如下所示使用“*”: :
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]

View File

@ -1,54 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="BenchmarkDotNet" version="0.10.14" targetFramework="net46" />
<package id="BenchmarkDotNet.Core" version="0.10.14" targetFramework="net46" />
<package id="BenchmarkDotNet.Toolchains.Roslyn" version="0.10.14" targetFramework="net46" />
<package id="Microsoft.CodeAnalysis.Analyzers" version="1.1.0" targetFramework="net46" />
<package id="Microsoft.CodeAnalysis.Common" version="2.6.1" targetFramework="net46" />
<package id="Microsoft.CodeAnalysis.CSharp" version="2.6.1" targetFramework="net46" />
<package id="Microsoft.DotNet.InternalAbstractions" version="1.0.0" targetFramework="net46" />
<package id="Microsoft.DotNet.PlatformAbstractions" version="1.1.1" targetFramework="net46" />
<package id="Microsoft.Win32.Registry" version="4.3.0" targetFramework="net46" />
<package id="System.AppContext" version="4.3.0" targetFramework="net46" />
<package id="System.Collections" version="4.3.0" targetFramework="net46" />
<package id="System.Collections.Concurrent" version="4.3.0" targetFramework="net46" />
<package id="System.Collections.Immutable" version="1.3.1" targetFramework="net46" />
<package id="System.Console" version="4.3.0" targetFramework="net46" />
<package id="System.Diagnostics.Debug" version="4.3.0" targetFramework="net46" />
<package id="System.Diagnostics.FileVersionInfo" version="4.3.0" targetFramework="net46" />
<package id="System.Diagnostics.StackTrace" version="4.3.0" targetFramework="net46" />
<package id="System.Diagnostics.Tools" version="4.3.0" targetFramework="net46" />
<package id="System.Dynamic.Runtime" version="4.3.0" targetFramework="net46" />
<package id="System.Globalization" version="4.3.0" targetFramework="net46" />
<package id="System.IO.Compression" version="4.3.0" targetFramework="net46" />
<package id="System.IO.FileSystem" version="4.3.0" targetFramework="net46" />
<package id="System.IO.FileSystem.Primitives" version="4.3.0" targetFramework="net46" />
<package id="System.Linq" version="4.3.0" targetFramework="net46" />
<package id="System.Linq.Expressions" version="4.3.0" targetFramework="net46" />
<package id="System.Reflection" version="4.3.0" targetFramework="net46" />
<package id="System.Reflection.Metadata" version="1.4.2" targetFramework="net46" />
<package id="System.Resources.ResourceManager" version="4.3.0" targetFramework="net46" />
<package id="System.Runtime" version="4.3.0" targetFramework="net46" />
<package id="System.Runtime.Extensions" version="4.3.0" targetFramework="net46" />
<package id="System.Runtime.InteropServices" version="4.3.0" targetFramework="net46" />
<package id="System.Runtime.Numerics" version="4.3.0" targetFramework="net46" />
<package id="System.Security.Cryptography.Algorithms" version="4.3.0" targetFramework="net46" />
<package id="System.Security.Cryptography.Encoding" version="4.3.0" targetFramework="net46" />
<package id="System.Security.Cryptography.Primitives" version="4.3.0" targetFramework="net46" />
<package id="System.Security.Cryptography.X509Certificates" version="4.3.0" targetFramework="net46" />
<package id="System.Text.Encoding" version="4.3.0" targetFramework="net46" />
<package id="System.Text.Encoding.CodePages" version="4.3.0" targetFramework="net46" />
<package id="System.Text.Encoding.Extensions" version="4.3.0" targetFramework="net46" />
<package id="System.Threading" version="4.3.0" targetFramework="net46" />
<package id="System.Threading.Tasks" version="4.3.0" targetFramework="net46" />
<package id="System.Threading.Tasks.Extensions" version="4.3.0" targetFramework="net46" />
<package id="System.Threading.Tasks.Parallel" version="4.3.0" targetFramework="net46" />
<package id="System.Threading.Thread" version="4.3.0" targetFramework="net46" />
<package id="System.ValueTuple" version="4.3.0" targetFramework="net46" />
<package id="System.Xml.ReaderWriter" version="4.3.0" targetFramework="net46" />
<package id="System.Xml.XDocument" version="4.3.0" targetFramework="net46" />
<package id="System.Xml.XmlDocument" version="4.3.0" targetFramework="net46" />
<package id="System.Xml.XmlSerializer" version="4.3.0" targetFramework="net46" />
<package id="System.Xml.XPath" version="4.3.0" targetFramework="net46" />
<package id="System.Xml.XPath.XDocument" version="4.3.0" targetFramework="net46" />
</packages>

View File

@ -0,0 +1,46 @@
using System;
using System.Threading.Tasks;
using Xunit;
namespace IP2Region.Test.xUnit
{
public class SearchTest
{
private readonly DbSearcher _search;
public SearchTest()
{
_search = new DbSearcher(Environment.CurrentDirectory + @"\DB\ip2region.db");
}
[Fact]
public void Search_Test()
{
string memResult = _search.MemorySearch("183.192.62.65").Region;
string binarySearchResult = _search.BinarySearch("183.192.62.65").Region;
string binaryTreeSearchResult = _search.BtreeSearch("183.192.62.65").Region;
Assert.NotNull(memResult);
Assert.NotNull(binarySearchResult);
Assert.NotNull(binaryTreeSearchResult);
Assert.Equal(memResult, binarySearchResult);
Assert.Equal(binaryTreeSearchResult, memResult);
}
[Fact]
public async Task SearchAsync_Test()
{
var memResult = await _search.MemorySearchAsync("183.192.62.65");
var binarySearchResult = await _search.BinarySearchAsync("183.192.62.65");
var bTreeSearchResult = await _search.BtreeSearchAsync("183.192.62.65");
Assert.NotNull(memResult.Region);
Assert.NotNull(binarySearchResult.Region);
Assert.NotNull(bTreeSearchResult.Region);
Assert.Equal(memResult.Region, binarySearchResult.Region);
Assert.Equal(bTreeSearchResult.Region, memResult.Region);
}
}
}

View File

@ -1,36 +0,0 @@
using System;
using System.Threading.Tasks;
using Xunit;
namespace IP2Region.Test.xUnit
{
public class UnitTest1
{
private readonly DbSearcher _search;
public UnitTest1()
{
_search = new DbSearcher(Environment.CurrentDirectory + @"\DB\ip2region.db");
}
[Fact]
public void Search_Test()
{
Assert.NotNull(_search.MemorySearch("183.192.62.65").Region);
Assert.NotNull(_search.MemorySearchAsync("183.192.62.65").Result.Region);
Assert.NotNull(_search.BinarySearch("183.192.62.65").Region);
Assert.NotNull(_search.BinarySearchAsync("183.192.62.65").Result.Region);
Assert.NotNull(_search.BtreeSearch("183.192.62.65").Region);
Assert.NotNull(_search.BtreeSearchAsync("183.192.62.65").Result.Region);
}
[Fact]
public async Task SearchAsync_Test()
{
var result = await _search.MemorySearchAsync("183.192.62.65");
Assert.NotNull(result.Region);
}
}
}

View File

@ -360,23 +360,23 @@ namespace IP2Region
/// <summary>
/// Get the region throught the ip address with memory binary search algorithm.
/// </summary>
public async Task<DataBlock> MemorySearchAsync(string ip)
public Task<DataBlock> MemorySearchAsync(string ip)
{
return Task.FromResult(MemorySearch(ip));
}
/// <summary>
/// Get the region throught the ip address with b-tree search algorithm.
/// </summary>
public async Task<DataBlock> BtreeSearchAsync(string ip)
public Task<DataBlock> BtreeSearchAsync(string ip)
{
return Task.FromResult(BtreeSearch(ip));
}
}
/// <summary>
/// Get the region throught the ip address with binary search algorithm.
/// </summary>
public async Task<DataBlock> BinarySearchAsync(string ip)
public Task<DataBlock> BinarySearchAsync(string ip)
{
return Task.FromResult(BinarySearch(ip));
return Task.FromResult(BinarySearch(ip));
}
#endregion
@ -397,4 +397,4 @@ namespace IP2Region
}
}
}
}

View File

@ -1,50 +0,0 @@
using IP2Region;
using IP2Region.Models;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace UnitTests
{
/// <summary>
/// This test class is mainly used to test your exception types.
/// It should throw exceptions of differnt kinds of types on certain occations.
/// </summary>
[TestClass]
public class ExceptionTest
{
/// <summary>
/// Path error should throw "DbMakerConfigException"
/// </summary>
[TestMethod]
[ExpectedException(typeof(DbMakerConfigException))]
public void TestDbMakerConfigException()
{
using (var _dbSearcher = new DbSearcher(new DbConfig(255), "../../../../../data/ip2region.db")) { }
}
/// <summary>
/// Invalid IP should throw "IPInValidException"
/// </summary>
[TestMethod]
public void TestInvalidIP()
{
using (var _dbSearcher = new DbSearcher("../../../../../data/ip2region.db"))
{
var invalidIps = new string[] { "256.255.1.1", "-1.0.0.0", "192.168.4", "x.y.z" };
var counter = 0;
foreach (var item in invalidIps)
{
try
{
_dbSearcher.MemorySearch(item);
}
catch (IPInValidException)
{
counter++;
}
}
Assert.AreEqual(counter, 4);
}
}
}
}

View File

@ -1,148 +0,0 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using IP2Region;
using System.Threading.Tasks;
namespace UnitTests
{
/// <summary>
/// This class is used to test with normal IPs (Chinese, Foreign and Special).
/// Use async functions for testing.
/// </summary>
[TestClass]
public class NormalAsyncTest
{
private static DbSearcher _dbSearcher = null;
[ClassInitialize]
public static void Init(TestContext context)
{
_dbSearcher = new DbSearcher("../../../../../data/ip2region.db");
}
[ClassCleanup]
public static void ClearUp()
{
_dbSearcher.Dispose();
}
#region Normal Valid Chinese IP Test Cases
[TestMethod]
[TestCategory("Async Chinese Ip Test")]
public async Task AsyncBinarySearchTestForChinese()
{
const int cityId = 2163;
const string region = "中国|0|广东省|深圳市|鹏博士";
var result = await _dbSearcher.AsyncBinarySearch("101.105.35.57");
Assert.AreEqual(result.CityID, cityId);
Assert.AreEqual(result.Region, region);
}
[TestMethod]
[TestCategory("Async Chinese Ip Test")]
public async Task AsyncBtreeSearchTestForChinese()
{
const int cityId = 2163;
const string region = "中国|0|广东省|深圳市|鹏博士";
var result = await _dbSearcher.AsyncBtreeSearch("101.105.35.57");
Assert.AreEqual(result.CityID, cityId);
Assert.AreEqual(result.Region, region);
}
[TestMethod]
[TestCategory("Async Chinese Ip Test")]
public async Task AsyncMemorySearchTestForChinese()
{
const int cityId = 2163;
const string region = "中国|0|广东省|深圳市|鹏博士";
var result = await _dbSearcher.AsyncMemorySearch("101.105.35.57");
Assert.AreEqual(result.CityID, cityId);
Assert.AreEqual(result.Region, region);
}
#endregion
#region Normal Valid Foreign IP Test Cases
[TestMethod]
[TestCategory("Async Foreign Ip Test")]
public async Task AsyncBinarySearchTestForForeign()
{
const int cityId = 71;
const string region = "荷兰|0|0|0|0";
var result = await _dbSearcher.AsyncBinarySearch("86.84.21.60");
Assert.AreEqual(result.CityID, cityId);
Assert.AreEqual(result.Region, region);
}
[TestMethod]
[TestCategory("Async Foreign Ip Test")]
public async Task AsyncBtreeSearchTestForForeign()
{
const int cityId = 71;
const string region = "荷兰|0|0|0|0";
var result = await _dbSearcher.AsyncBtreeSearch("86.84.21.60");
Assert.AreEqual(result.CityID, cityId);
Assert.AreEqual(result.Region, region);
}
[TestMethod]
[TestCategory("Async Foreign Ip Test")]
public async Task AsyncMemorySearchTestForForeign()
{
const int cityId = 71;
const string region = "荷兰|0|0|0|0";
var result = await _dbSearcher.AsyncMemorySearch("86.84.21.60");
Assert.AreEqual(result.CityID, cityId);
Assert.AreEqual(result.Region, region);
}
#endregion
#region Normal Valid Special Ip Test Cases
[TestMethod]
[TestCategory("Async Special Ip Test")]
public async Task AsyncBinarySearchTestForSpecial()
{
const int cityId = 0;
const string region = "0|0|0|内网IP|内网IP";
string[] specialIps = new string[] { "255.255.255.255", "0.0.0.0" };
foreach (var item in specialIps)
{
var result = await _dbSearcher.AsyncBinarySearch(item);
Assert.AreEqual(result.CityID, cityId);
Assert.AreEqual(result.Region, region);
}
}
[TestMethod]
[TestCategory("Async Special Ip Test")]
public async Task AsyncBtreeSearchTestForSpecial()
{
const int cityId = 0;
const string region = "0|0|0|内网IP|内网IP";
string[] specialIps = new string[] { "255.255.255.255", "0.0.0.0" };
foreach (var item in specialIps)
{
var result = await _dbSearcher.AsyncBtreeSearch(item);
Assert.AreEqual(result.CityID, cityId);
Assert.AreEqual(result.Region, region);
}
}
[TestMethod]
[TestCategory("Async Special Ip Test")]
public async Task AsyncMemorySearchTestForSpecial()
{
const int cityId = 0;
const string region = "0|0|0|内网IP|内网IP";
string[] specialIps = new string[] { "255.255.255.255", "0.0.0.0" };
foreach (var item in specialIps)
{
var result = await _dbSearcher.AsyncMemorySearch(item);
Assert.AreEqual(result.CityID, cityId);
Assert.AreEqual(result.Region, region);
}
}
#endregion
}
}

View File

@ -1,146 +0,0 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using IP2Region;
namespace UnitTests
{
/// <summary>
/// This class is used to test with normal IPs (Chinese, Foreign and Special).
/// Use sync functions for testing.
/// </summary>
[TestClass]
public class NormalSyncTest
{
private static DbSearcher _dbSearcher = null;
[ClassInitialize]
public static void Init(TestContext context)
{
_dbSearcher = new DbSearcher("../../../../../data/ip2region.db");
}
[ClassCleanup]
public static void ClearUp()
{
_dbSearcher.Dispose();
}
#region Normal Valid Chinese IP Test Cases
[TestMethod]
[TestCategory("Chinese Ip Test")]
public void BinarySearchTestForChinese()
{
const int cityId = 2163;
const string region = "中国|0|广东省|深圳市|鹏博士";
var result = _dbSearcher.BinarySearch("101.105.35.57");
Assert.AreEqual(result.CityID, cityId);
Assert.AreEqual(result.Region, region);
}
[TestMethod]
[TestCategory("Chinese Ip Test")]
public void BtreeSearchTestForChinese()
{
const int cityId = 2163;
const string region = "中国|0|广东省|深圳市|鹏博士";
var result = _dbSearcher.BtreeSearch("101.105.35.57");
Assert.AreEqual(result.CityID, cityId);
Assert.AreEqual(result.Region, region);
}
[TestMethod]
[TestCategory("Chinese Ip Test")]
public void MemorySearchTestForChinese()
{
const int cityId = 2163;
const string region = "中国|0|广东省|深圳市|鹏博士";
var result = _dbSearcher.MemorySearch("101.105.35.57");
Assert.AreEqual(result.CityID, cityId);
Assert.AreEqual(result.Region, region);
}
#endregion
#region Normal Valid Foreign IP Test Cases
[TestMethod]
[TestCategory("Foreign Ip Test")]
public void BinarySearchTestForForeign()
{
const int cityId = 71;
const string region = "荷兰|0|0|0|0";
var result = _dbSearcher.BinarySearch("86.84.21.60");
Assert.AreEqual(result.CityID, cityId);
Assert.AreEqual(result.Region, region);
}
[TestMethod]
[TestCategory("Foreign Ip Test")]
public void BtreeSearchTestForForeign()
{
const int cityId = 71;
const string region = "荷兰|0|0|0|0";
var result = _dbSearcher.BtreeSearch("86.84.21.60");
Assert.AreEqual(result.CityID, cityId);
Assert.AreEqual(result.Region, region);
}
[TestMethod]
[TestCategory("Foreign Ip Test")]
public void MemorySearchTestForForeign()
{
const int cityId = 71;
const string region = "荷兰|0|0|0|0";
var result = _dbSearcher.MemorySearch("86.84.21.60");
Assert.AreEqual(result.CityID, cityId);
Assert.AreEqual(result.Region, region);
}
#endregion
#region Normal Valid Special Ip Test Cases
[TestMethod]
[TestCategory("Special Ip Test")]
public void BinarySearchTestForSpecial()
{
const int cityId = 0;
const string region = "0|0|0|内网IP|内网IP";
string[] specialIps = new string[] { "255.255.255.255", "0.0.0.0" };
foreach (var item in specialIps)
{
var result = _dbSearcher.BinarySearch(item);
Assert.AreEqual(result.CityID, cityId);
Assert.AreEqual(result.Region, region);
}
}
[TestMethod]
[TestCategory("Special Ip Test")]
public void BtreeSearchTestForSpecial()
{
const int cityId = 0;
const string region = "0|0|0|内网IP|内网IP";
string[] specialIps = new string[] { "255.255.255.255", "0.0.0.0" };
foreach (var item in specialIps)
{
var result = _dbSearcher.BtreeSearch(item);
Assert.AreEqual(result.CityID, cityId);
Assert.AreEqual(result.Region, region);
}
}
[TestMethod]
[TestCategory("Special Ip Test")]
public void MemorySearchTestForSpecial()
{
const int cityId = 0;
const string region = "0|0|0|内网IP|内网IP";
string[] specialIps = new string[] { "255.255.255.255", "0.0.0.0" };
foreach (var item in specialIps)
{
var result = _dbSearcher.MemorySearch(item);
Assert.AreEqual(result.CityID, cityId);
Assert.AreEqual(result.Region, region);
}
}
#endregion
}
}

View File

@ -1,19 +0,0 @@
using System.Reflection;
using System.Runtime.InteropServices;
[assembly: AssemblyTitle("UnitTests")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("UnitTests")]
[assembly: AssemblyCopyright("Copyright © 2018")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
[assembly: ComVisible(false)]
[assembly: Guid("38abc99f-cc14-47e3-9eeb-06c67d5ac33f")]
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]

View File

@ -1,76 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="..\packages\MSTest.TestAdapter.1.2.1\build\net45\MSTest.TestAdapter.props" Condition="Exists('..\packages\MSTest.TestAdapter.1.2.1\build\net45\MSTest.TestAdapter.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{38ABC99F-CC14-47E3-9EEB-06C67D5AC33F}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>UnitTests</RootNamespace>
<AssemblyName>UnitTests</AssemblyName>
<TargetFrameworkVersion>v4.6</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<ProjectTypeGuids>{3AC096D0-A1C2-E12C-1390-A8335801FDAB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
<VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">15.0</VisualStudioVersion>
<VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath>
<ReferencePath>$(ProgramFiles)\Common Files\microsoft shared\VSTT\$(VisualStudioVersion)\UITestExtensionPackages</ReferencePath>
<IsCodedUITest>False</IsCodedUITest>
<TestProjectType>UnitTest</TestProjectType>
<NuGetPackageImportStamp>
</NuGetPackageImportStamp>
<TargetFrameworkProfile />
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</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>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="Microsoft.VisualStudio.TestPlatform.TestFramework, Version=14.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\packages\MSTest.TestFramework.1.2.1\lib\net45\Microsoft.VisualStudio.TestPlatform.TestFramework.dll</HintPath>
</Reference>
<Reference Include="Microsoft.VisualStudio.TestPlatform.TestFramework.Extensions, Version=14.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\packages\MSTest.TestFramework.1.2.1\lib\net45\Microsoft.VisualStudio.TestPlatform.TestFramework.Extensions.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
</ItemGroup>
<ItemGroup>
<Compile Include="NormalAsyncTest.cs" />
<Compile Include="NormalSyncTest.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="ExceptionTest.cs" />
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\IP2Region\IP2Region.csproj">
<Project>{4c6032ee-43a5-4d5c-88f8-c411069e49e0}</Project>
<Name>IP2Region</Name>
</ProjectReference>
</ItemGroup>
<Import Project="$(VSToolsPath)\TeamTest\Microsoft.TestTools.targets" Condition="Exists('$(VSToolsPath)\TeamTest\Microsoft.TestTools.targets')" />
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
<PropertyGroup>
<ErrorText>这台计算机上缺少此项目引用的 NuGet 程序包。使用“NuGet 程序包还原”可下载这些程序包。有关更多信息,请参见 http://go.microsoft.com/fwlink/?LinkID=322105。缺少的文件是 {0}。</ErrorText>
</PropertyGroup>
<Error Condition="!Exists('..\packages\MSTest.TestAdapter.1.2.1\build\net45\MSTest.TestAdapter.props')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\MSTest.TestAdapter.1.2.1\build\net45\MSTest.TestAdapter.props'))" />
<Error Condition="!Exists('..\packages\MSTest.TestAdapter.1.2.1\build\net45\MSTest.TestAdapter.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\MSTest.TestAdapter.1.2.1\build\net45\MSTest.TestAdapter.targets'))" />
</Target>
<Import Project="..\packages\MSTest.TestAdapter.1.2.1\build\net45\MSTest.TestAdapter.targets" Condition="Exists('..\packages\MSTest.TestAdapter.1.2.1\build\net45\MSTest.TestAdapter.targets')" />
</Project>

View File

@ -1,5 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="MSTest.TestAdapter" version="1.2.1" targetFramework="net45" />
<package id="MSTest.TestFramework" version="1.2.1" targetFramework="net45" />
</packages>