-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #49 from mycroes/hotfix/conversion
Hotfix conversion of float/double
- Loading branch information
Showing
6 changed files
with
277 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using Xunit; | ||
|
||
namespace Sally7.Tests; | ||
|
||
internal static class TestValues | ||
{ | ||
public static IEnumerable<byte> ByteData => | ||
Enumerable.Range(byte.MinValue, byte.MaxValue).Select(x => (byte)x); | ||
|
||
public static IEnumerable<sbyte> SByteData => | ||
Enumerable.Range(sbyte.MinValue, sbyte.MaxValue).Select(x => (sbyte)x); | ||
|
||
|
||
public static IEnumerable<short> Int16Data => ShortValues; | ||
|
||
public static IEnumerable<ushort> UInt16Data => | ||
ShortValues.Select(x => (ushort)x); | ||
|
||
public static IEnumerable<int> Int32Data => UIntValues.Select(x => (int)x); | ||
|
||
public static IEnumerable<uint> UInt32Data => UIntValues; | ||
|
||
public static IEnumerable<float> SingleData | ||
{ | ||
get | ||
{ | ||
float[] values = | ||
[ | ||
0, | ||
1, | ||
0.1f, | ||
123.45f, | ||
float.MinValue, | ||
float.MaxValue, | ||
]; | ||
|
||
return values; | ||
} | ||
} | ||
|
||
public static IEnumerable<long> Int64Data => ULongValues.Select(x => (long) x); | ||
|
||
public static IEnumerable<ulong> UInt64Data => ULongValues; | ||
|
||
public static IEnumerable<double> DoubleData | ||
{ | ||
get | ||
{ | ||
double[] values = | ||
[ | ||
0, | ||
1, | ||
0.1, | ||
123.45, | ||
0.0000001, | ||
(double) ulong.MaxValue * 33, | ||
double.MinValue, | ||
double.MaxValue, | ||
]; | ||
|
||
return values; | ||
} | ||
} | ||
|
||
private static readonly short[] ShortValues = | ||
[ | ||
0, | ||
1, | ||
123, | ||
12345, | ||
-1, | ||
-123, | ||
-12345, | ||
1 << 8, | ||
1 | 2 << 8, | ||
short.MinValue, | ||
short.MaxValue | ||
]; | ||
|
||
private static readonly uint[] UIntValues = | ||
[ | ||
uint.MinValue, | ||
uint.MaxValue, | ||
1, | ||
123, | ||
12345, | ||
1 << 8, | ||
1 << 16, | ||
1 << 24, | ||
1 | 2 << 8, | ||
1 | 2 << 8 | 3 << 16, | ||
1 | 2 << 8 | 3 << 24, | ||
1 | 2 << 8 | 3 << 16 | 4 << 24, | ||
0xf, | ||
0xf << 8, | ||
0xf << 16, | ||
0xf << 24, | ||
]; | ||
|
||
private static readonly ulong[] ULongValues = UIntValues.Select(x => (ulong)x) | ||
.SelectMany(x => new[] { x, x << 8, x << 16, x << 24, x << 32 }) | ||
.Concat(UIntValues.SelectMany(_ => UIntValues, (a, b) => (ulong)a << 32 | b)) | ||
.Concat(UIntValues.SelectMany(_ => UIntValues, (a, b) => a | (ulong)b << 32)).Distinct().ToArray(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
using System; | ||
using System.Collections; | ||
using System.Collections.Generic; | ||
using System.Reflection; | ||
using Xunit.Sdk; | ||
|
||
namespace Sally7.Tests; | ||
|
||
internal sealed class TestValuesDataAttribute : DataAttribute | ||
{ | ||
public override IEnumerable<object[]> GetData(MethodInfo testMethod) | ||
{ | ||
var paramType = testMethod.GetParameters()[0].ParameterType; | ||
var dataProperty = typeof(TestValues).GetProperty($"{paramType.Name}Data") ?? | ||
throw new ArgumentException($"No data available for type {paramType}"); | ||
|
||
var data = dataProperty.GetValue(null) as IEnumerable ?? | ||
throw new NotSupportedException($"Data from {dataProperty} could not be converted to {nameof(IEnumerable)}."); | ||
|
||
foreach (var value in data) yield return [value]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
using System; | ||
using Sally7.ValueConversion; | ||
using Xunit; | ||
|
||
namespace Sally7.Tests; | ||
|
||
public static class ToPlcConverterTests | ||
{ | ||
public class Elements | ||
{ | ||
[Theory] | ||
[TestValuesData] | ||
public void ConvertByteToPlc(byte value) | ||
{ | ||
// Arrange | ||
var converter = ConverterFactory.GetToPlcConverter<byte>(1); | ||
var buffer = new byte[sizeof(byte)]; | ||
|
||
// Act | ||
converter(value, 1, buffer); | ||
|
||
// Assert | ||
Assert.Equal([value], buffer); | ||
} | ||
|
||
[Theory] | ||
[TestValuesData] | ||
public void ConvertSByteToPlc(sbyte value) | ||
{ | ||
// Arrange | ||
var converter = ConverterFactory.GetToPlcConverter<sbyte>(1); | ||
var buffer = new byte[sizeof(sbyte)]; | ||
|
||
// Act | ||
converter(value, 1, buffer); | ||
|
||
// Assert | ||
Assert.Equal([(byte)value], buffer); | ||
} | ||
|
||
[Theory] | ||
[TestValuesData] | ||
public void ConvertInt16ToPlc(short value) | ||
{ | ||
TestConvertToPlc(value, sizeof(short), BitConverter.GetBytes); | ||
} | ||
|
||
[Theory] | ||
[TestValuesData] | ||
public void ConvertUInt16ToPlc(ushort value) | ||
{ | ||
TestConvertToPlc(value, sizeof(ushort), BitConverter.GetBytes); | ||
} | ||
|
||
[Theory] | ||
[TestValuesData] | ||
public void ConvertInt32ToPlc(int value) | ||
{ | ||
TestConvertToPlc(value, sizeof(int), BitConverter.GetBytes); | ||
} | ||
|
||
[Theory] | ||
[TestValuesData] | ||
public void ConvertUInt32ToPlc(uint value) | ||
{ | ||
TestConvertToPlc(value, sizeof(uint), BitConverter.GetBytes); | ||
} | ||
|
||
[Theory] | ||
[TestValuesData] | ||
public void ConvertSingleToPlc(float value) | ||
{ | ||
TestConvertToPlc(value, sizeof(float), BitConverter.GetBytes); | ||
} | ||
|
||
[Theory] | ||
[TestValuesData] | ||
public void ConvertInt64ToPlc(long value) | ||
{ | ||
TestConvertToPlc(value, sizeof(long), BitConverter.GetBytes); | ||
} | ||
|
||
[Theory] | ||
[TestValuesData] | ||
public void ConvertUInt64ToPlc(ulong value) | ||
{ | ||
TestConvertToPlc(value, sizeof(ulong), BitConverter.GetBytes); | ||
} | ||
|
||
[Theory] | ||
[TestValuesData] | ||
public void ConvertDoubleToPlc(double value) | ||
{ | ||
TestConvertToPlc(value, sizeof(double), BitConverter.GetBytes); | ||
} | ||
|
||
private static void TestConvertToPlc<T>(T value, int size, Func<T, byte[]> getBytes) | ||
{ | ||
// Arrange | ||
var converter = ConverterFactory.GetToPlcConverter<T>(1); | ||
var buffer = new byte[size]; | ||
|
||
// Act | ||
converter(value, 1, buffer); | ||
|
||
// Assert | ||
var bytes = getBytes.Invoke(value); | ||
if (BitConverter.IsLittleEndian) Array.Reverse(bytes); | ||
|
||
Assert.Equal(bytes, buffer); | ||
} | ||
} | ||
|
||
public class Arrays | ||
{ | ||
[Theory] | ||
[TestValuesData] | ||
public void ConvertFloatArrToPlc(float value) | ||
{ | ||
// Arrange | ||
var converter = ConverterFactory.GetToPlcConverter<float[]>(1); | ||
var buffer = new byte[sizeof(float)]; | ||
|
||
// Act | ||
converter([value], 1, buffer); | ||
|
||
// Assert | ||
var bytes = BitConverter.GetBytes(value); | ||
if (BitConverter.IsLittleEndian) Array.Reverse(bytes); | ||
|
||
Assert.Equal(bytes, buffer); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters