Skip to content

Commit

Permalink
wip: Add ConverterFactory and tests
Browse files Browse the repository at this point in the history
The ConverterFactory will be used with a restructure of the DataItem
class to avoid converter lookup on every DataItem read/write. It should
also help in performing conversions for derived types such as enums and
arrays.
  • Loading branch information
mycroes committed Jun 17, 2018
1 parent 7e0d20a commit ea7e112
Show file tree
Hide file tree
Showing 6 changed files with 224 additions and 0 deletions.
85 changes: 85 additions & 0 deletions Sally7.Tests/ConversionTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
using System;
using Sally7.ValueConversion;
using Xunit;

namespace Sally7.Tests
{
public class ConversionTests
{
private readonly ConverterFactory factory = new ConverterFactory();

[Theory]
[InlineData(0)]
[InlineData(1)]
[InlineData(-1)]
[InlineData((int) short.MinValue)]
[InlineData((int) short.MaxValue)]
[InlineData(int.MaxValue)]
[InlineData(int.MinValue)]
public void ConvertToInt(int value)
{
var bytes = BitConverter.GetBytes(value);
if (BitConverter.IsLittleEndian) Array.Reverse(bytes);

var converter = ConverterFactory.GetFromPlcConverter<int>();
var result = converter(bytes, sizeof(int));

Assert.Equal(value, result);
}

[Theory]
[InlineData(0f)]
[InlineData(1f)]
[InlineData(0.1f)]
[InlineData(float.MinValue)]
[InlineData(float.MaxValue)]
public void ConvertToFloat(float value)
{
var bytes = BitConverter.GetBytes(value);
if (BitConverter.IsLittleEndian) Array.Reverse(bytes);

var converter = ConverterFactory.GetFromPlcConverter<float>();
var result = converter(bytes, sizeof(float));

Assert.Equal(value, result);
}

[Theory]
[InlineData(EnumOfInt.Zero)]
[InlineData(EnumOfInt.One)]
[InlineData(EnumOfInt.Two)]
[InlineData(EnumOfInt.Three)]
[InlineData(EnumOfInt.Four)]
[InlineData(EnumOfInt.Five)]
[InlineData(EnumOfInt.Six)]
[InlineData(EnumOfInt.Seven)]
[InlineData(EnumOfInt.Eight)]
[InlineData(EnumOfInt.Nine)]
[InlineData(EnumOfInt.Ten)]
public void ConvertToEnumOfInt(EnumOfInt value)
{
var bytes = BitConverter.GetBytes((int) value);
if (BitConverter.IsLittleEndian) Array.Reverse(bytes);

var converter = ConverterFactory.GetFromPlcConverter<EnumOfInt>();
var result = converter(bytes, sizeof(int));

Assert.Equal(value, result);
}

public enum EnumOfInt
{
Zero,
One,
Two,
Three,
Four,
Five,
Six,
Seven,
Eight,
Nine,
Ten
}
}
}
36 changes: 36 additions & 0 deletions Sally7.Tests/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("Sally7.Tests")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("Sally7.Tests")]
[assembly: AssemblyCopyright("Copyright © 2018")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]

// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]

// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("f3e2b5a4-be11-47e6-ab9f-36a9ee069a9e")]

// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
61 changes: 61 additions & 0 deletions Sally7.Tests/Sally7.Tests.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?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>{F3E2B5A4-BE11-47E6-AB9F-36A9EE069A9E}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>Sally7.Tests</RootNamespace>
<AssemblyName>Sally7.Tests</AssemblyName>
<TargetFrameworkVersion>v4.6.2</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
</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>
<LangVersion>latest</LangVersion>
</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>
<LangVersion>latest</LangVersion>
<AllowUnsafeBlocks>false</AllowUnsafeBlocks>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Net.Http" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="ConversionTests.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="xunit">
<Version>2.3.1</Version>
</PackageReference>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Sally7\Sally7.csproj">
<Project>{51C6FF09-A5C3-424C-A098-BEFF4EBA9293}</Project>
<Name>Sally7</Name>
</ProjectReference>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>
6 changes: 6 additions & 0 deletions Sally7.sln
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Sally7", "Sally7\Sally7.csp
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Sally7.Benchmarks", "Sally7.Benchmarks\Sally7.Benchmarks.csproj", "{1B669D60-6229-4244-9DA6-A112BDAB664B}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Sally7.Tests", "Sally7.Tests\Sally7.Tests.csproj", "{F3E2B5A4-BE11-47E6-AB9F-36A9EE069A9E}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -21,6 +23,10 @@ Global
{1B669D60-6229-4244-9DA6-A112BDAB664B}.Debug|Any CPU.Build.0 = Debug|Any CPU
{1B669D60-6229-4244-9DA6-A112BDAB664B}.Release|Any CPU.ActiveCfg = Release|Any CPU
{1B669D60-6229-4244-9DA6-A112BDAB664B}.Release|Any CPU.Build.0 = Release|Any CPU
{F3E2B5A4-BE11-47E6-AB9F-36A9EE069A9E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{F3E2B5A4-BE11-47E6-AB9F-36A9EE069A9E}.Debug|Any CPU.Build.0 = Debug|Any CPU
{F3E2B5A4-BE11-47E6-AB9F-36A9EE069A9E}.Release|Any CPU.ActiveCfg = Release|Any CPU
{F3E2B5A4-BE11-47E6-AB9F-36A9EE069A9E}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
1 change: 1 addition & 0 deletions Sally7/Properties/InternalsVisibility.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[assembly: System.Runtime.CompilerServices.InternalsVisibleTo("Sally7.Tests")]
35 changes: 35 additions & 0 deletions Sally7/ValueConversion/ConverterFactory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using System;
using System.Runtime.CompilerServices;

namespace Sally7.ValueConversion
{
public delegate int ConvertToS7<TValue>(in TValue value, in int length, in Span<byte> output);

public delegate TValue ConvertFromS7<out TValue>(in Span<byte> input, in int length);

internal class ConverterFactory
{
public static ConvertFromS7<TValue> GetFromPlcConverter<TValue>() => Unsafe.As<ConvertFromS7<TValue>>(GetFromPlcConverter(typeof(TValue)));

private static Delegate GetFromPlcConverter(Type type)
{
if (type.IsEnum) type = Enum.GetUnderlyingType(type);

if (type == typeof(int)) return new ConvertFromS7<int>(ConvertToInt);
if (type == typeof(float)) return new ConvertFromS7<float>(ConvertToIntSized<float>);

throw new NotImplementedException();
}

private static int ConvertToInt(in Span<byte> input, in int length)
{
return input[0] << 24 | input[1] << 16 | input[2] << 8 | input[3];
}

private static TValue ConvertToIntSized<TValue>(in Span<byte> input, in int length)
{
var value = ConvertToInt(input, length);
return Unsafe.As<int, TValue>(ref value);
}
}
}

0 comments on commit ea7e112

Please sign in to comment.