mirror of
https://github.com/OpenLogics/MewtocolNet.git
synced 2025-12-06 11:11:23 +00:00
Added new tests
This commit is contained in:
@@ -5,7 +5,7 @@ namespace MewtocolNet.Links {
|
|||||||
|
|
||||||
internal class LinkedData {
|
internal class LinkedData {
|
||||||
|
|
||||||
internal static Dictionary<int, string> ErrorCodes = new System.Collections.Generic.Dictionary<int, string> {
|
internal static Dictionary<int, string> ErrorCodes = new Dictionary<int, string> {
|
||||||
|
|
||||||
{21, "NACK error"},
|
{21, "NACK error"},
|
||||||
{22, "WACK error"},
|
{22, "WACK error"},
|
||||||
|
|||||||
@@ -24,28 +24,37 @@ namespace MewtocolNet {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Converts a string (after converting to upper case) to ascii bytes
|
||||||
|
/// </summary>
|
||||||
internal static byte[] ToHexASCIIBytes (this string _str) {
|
internal static byte[] ToHexASCIIBytes (this string _str) {
|
||||||
|
|
||||||
ASCIIEncoding ascii = new ASCIIEncoding();
|
ASCIIEncoding ascii = new ASCIIEncoding();
|
||||||
byte[] bytes = ascii.GetBytes(_str.ToUpper());
|
byte[] bytes = ascii.GetBytes(_str.ToUpper());
|
||||||
return bytes;
|
return bytes;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
internal static string BuildBCCFrame (this string asciiArr) {
|
internal static string BuildBCCFrame (this string asciiArr) {
|
||||||
|
|
||||||
Encoding ae = Encoding.ASCII;
|
Encoding ae = Encoding.ASCII;
|
||||||
byte[] b = ae.GetBytes(asciiArr);
|
byte[] b = ae.GetBytes(asciiArr);
|
||||||
byte xorTotalByte = 0;
|
byte xorTotalByte = 0;
|
||||||
for(int i = 0; i < b.Length; i++)
|
for(int i = 0; i < b.Length; i++)
|
||||||
xorTotalByte ^= b[i];
|
xorTotalByte ^= b[i];
|
||||||
return asciiArr.Insert(asciiArr.Length, xorTotalByte.ToString("X2"));
|
return asciiArr.Insert(asciiArr.Length, xorTotalByte.ToString("X2"));
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
internal static byte[] ParseDTBytes (this string _onString ,int _blockSize = 4) {
|
internal static byte[] ParseDTBytes (this string _onString ,int _blockSize = 4) {
|
||||||
|
|
||||||
var res = new Regex(@"\%([0-9]{2})\$RD(.{"+_blockSize+"})").Match(_onString);
|
var res = new Regex(@"\%([0-9]{2})\$RD(.{"+_blockSize+"})").Match(_onString);
|
||||||
if(res.Success) {
|
if(res.Success) {
|
||||||
string val = res.Groups[2].Value;
|
string val = res.Groups[2].Value;
|
||||||
return val.HexStringToByteArray();
|
return val.HexStringToByteArray();
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
internal static string ParseDTByteString (this string _onString, int _blockSize = 4) {
|
internal static string ParseDTByteString (this string _onString, int _blockSize = 4) {
|
||||||
@@ -63,21 +72,25 @@ namespace MewtocolNet {
|
|||||||
}
|
}
|
||||||
|
|
||||||
internal static bool? ParseRCSingleBit (this string _onString, int _blockSize = 4) {
|
internal static bool? ParseRCSingleBit (this string _onString, int _blockSize = 4) {
|
||||||
|
|
||||||
var res = new Regex(@"\%([0-9]{2})\$RC(.)").Match(_onString);
|
var res = new Regex(@"\%([0-9]{2})\$RC(.)").Match(_onString);
|
||||||
if (res.Success) {
|
if (res.Success) {
|
||||||
string val = res.Groups[2].Value;
|
string val = res.Groups[2].Value;
|
||||||
return val == "1";
|
return val == "1";
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
internal static string ParseDTString (this string _onString) {
|
internal static string ParseDTString (this string _onString) {
|
||||||
|
|
||||||
var res = new Regex(@"\%([0-9]{2})\$RD.{8}(.*)...").Match(_onString);
|
var res = new Regex(@"\%([0-9]{2})\$RD.{8}(.*)...").Match(_onString);
|
||||||
if(res.Success) {
|
if(res.Success) {
|
||||||
string val = res.Groups[2].Value;
|
string val = res.Groups[2].Value;
|
||||||
return val.GetStringFromAsciiHex()?.Trim();
|
return val.GetStringFromAsciiHex()?.Trim();
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
internal static string ReverseByteOrder (this string _onString) {
|
internal static string ReverseByteOrder (this string _onString) {
|
||||||
@@ -94,6 +107,7 @@ namespace MewtocolNet {
|
|||||||
}
|
}
|
||||||
|
|
||||||
internal static IEnumerable<String> SplitInParts (this string s, int partLength) {
|
internal static IEnumerable<String> SplitInParts (this string s, int partLength) {
|
||||||
|
|
||||||
if (s == null)
|
if (s == null)
|
||||||
throw new ArgumentNullException(nameof(s));
|
throw new ArgumentNullException(nameof(s));
|
||||||
if (partLength <= 0)
|
if (partLength <= 0)
|
||||||
@@ -101,6 +115,7 @@ namespace MewtocolNet {
|
|||||||
|
|
||||||
for (var i = 0; i < s.Length; i += partLength)
|
for (var i = 0; i < s.Length; i += partLength)
|
||||||
yield return s.Substring(i, Math.Min(partLength, s.Length - i));
|
yield return s.Substring(i, Math.Min(partLength, s.Length - i));
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
internal static string BuildDTString (this string _inString, short _stringReservedSize) {
|
internal static string BuildDTString (this string _inString, short _stringReservedSize) {
|
||||||
@@ -167,12 +182,14 @@ namespace MewtocolNet {
|
|||||||
}
|
}
|
||||||
|
|
||||||
internal static string ToHexString (this byte[] arr) {
|
internal static string ToHexString (this byte[] arr) {
|
||||||
|
|
||||||
StringBuilder sb = new StringBuilder();
|
StringBuilder sb = new StringBuilder();
|
||||||
for (int i = 0; i < arr.Length; i++) {
|
for (int i = 0; i < arr.Length; i++) {
|
||||||
byte b = arr[i];
|
byte b = arr[i];
|
||||||
sb.Append(b.ToString("X2"));
|
sb.Append(b.ToString("X2"));
|
||||||
}
|
}
|
||||||
return sb.ToString();
|
return sb.ToString();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
internal static byte[] BigToMixedEndian (this byte[] arr) {
|
internal static byte[] BigToMixedEndian (this byte[] arr) {
|
||||||
|
|||||||
74
MewtocolTests/TestHelperExtensions.cs
Normal file
74
MewtocolTests/TestHelperExtensions.cs
Normal file
@@ -0,0 +1,74 @@
|
|||||||
|
using Xunit;
|
||||||
|
|
||||||
|
using MewtocolNet;
|
||||||
|
using MewtocolNet.Registers;
|
||||||
|
using Xunit.Abstractions;
|
||||||
|
using MewtocolNet.Links;
|
||||||
|
using System.Collections;
|
||||||
|
|
||||||
|
namespace MewtocolTests {
|
||||||
|
|
||||||
|
public class TestHelperExtensions {
|
||||||
|
|
||||||
|
private readonly ITestOutputHelper output;
|
||||||
|
|
||||||
|
public TestHelperExtensions (ITestOutputHelper output) {
|
||||||
|
this.output = output;
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact(DisplayName = nameof(MewtocolHelpers.ToBitString))]
|
||||||
|
public void ToBitStringGeneration () {
|
||||||
|
|
||||||
|
var bitarr = new BitArray(16);
|
||||||
|
bitarr[2] = true;
|
||||||
|
bitarr[5] = true;
|
||||||
|
bitarr[8] = true;
|
||||||
|
bitarr[11] = true;
|
||||||
|
bitarr[14] = true;
|
||||||
|
|
||||||
|
Assert.Equal("0010010010010010", bitarr.ToBitString());
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact(DisplayName = nameof(MewtocolHelpers.ToHexString))]
|
||||||
|
public void ToHexStringGeneration() {
|
||||||
|
|
||||||
|
var bytes = new byte[6] {
|
||||||
|
0x10,
|
||||||
|
0xAB,
|
||||||
|
0xAC,
|
||||||
|
0x32,
|
||||||
|
0x00,
|
||||||
|
0x01
|
||||||
|
};
|
||||||
|
|
||||||
|
Assert.Equal("10ABAC320001", bytes.ToHexString());
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact(DisplayName = nameof(MewtocolHelpers.ToHexASCIIBytes))]
|
||||||
|
public void ToHexASCIIBytesGeneration() {
|
||||||
|
|
||||||
|
string test = "Hello, world!";
|
||||||
|
|
||||||
|
Assert.Equal(new byte[] {
|
||||||
|
0x48,
|
||||||
|
0x45,
|
||||||
|
0x4C,
|
||||||
|
0x4C,
|
||||||
|
0x4F,
|
||||||
|
0x2C,
|
||||||
|
0x20,
|
||||||
|
0x57,
|
||||||
|
0x4F,
|
||||||
|
0x52,
|
||||||
|
0x4C,
|
||||||
|
0x44,
|
||||||
|
0x21
|
||||||
|
}, test.ToHexASCIIBytes());
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
62
MewtocolTests/TestLinkedLists.cs
Normal file
62
MewtocolTests/TestLinkedLists.cs
Normal file
@@ -0,0 +1,62 @@
|
|||||||
|
using Xunit;
|
||||||
|
|
||||||
|
using MewtocolNet;
|
||||||
|
using MewtocolNet.Registers;
|
||||||
|
using Xunit.Abstractions;
|
||||||
|
using MewtocolNet.Links;
|
||||||
|
|
||||||
|
namespace MewtocolTests {
|
||||||
|
|
||||||
|
public class TestLinkedLists {
|
||||||
|
|
||||||
|
private readonly ITestOutputHelper output;
|
||||||
|
|
||||||
|
public TestLinkedLists (ITestOutputHelper output) {
|
||||||
|
this.output = output;
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact(DisplayName = "Linked error list")]
|
||||||
|
public void NumericRegisterMewtocolIdentifiers () {
|
||||||
|
|
||||||
|
var expectedData = new Dictionary<int, string> {
|
||||||
|
|
||||||
|
{21, "NACK error"},
|
||||||
|
{22, "WACK error"},
|
||||||
|
{23, "Station number overlap"},
|
||||||
|
{24, "Transmission error"},
|
||||||
|
{25, "Hardware error"},
|
||||||
|
{26, "Station number setting error"},
|
||||||
|
{27, "Frame over error"},
|
||||||
|
{28, "No response error"},
|
||||||
|
{29, "Buffer close error"},
|
||||||
|
{30, "Timeout error"},
|
||||||
|
{32, "Transmission impossible"},
|
||||||
|
{33, "Communication stop"},
|
||||||
|
{36, "No local station"},
|
||||||
|
{38, "Other com error"},
|
||||||
|
{40, "BCC error"},
|
||||||
|
{41, "Format error"},
|
||||||
|
{42, "Not supported error"},
|
||||||
|
{43, "Procedure error"},
|
||||||
|
{50, "Link setting error"},
|
||||||
|
{51, "Simultanious operation error"},
|
||||||
|
{52, "Sending disable error"},
|
||||||
|
{53, "Busy error"},
|
||||||
|
{60, "Paramter error"},
|
||||||
|
{61, "Data error"},
|
||||||
|
{62, "Registration error"},
|
||||||
|
{63, "Mode error"},
|
||||||
|
{66, "Adress error"},
|
||||||
|
{67, "No data error"},
|
||||||
|
{72, "Timeout"},
|
||||||
|
{73, "Timeout"},
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
Assert.Equal(expectedData, LinkedData.ErrorCodes);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user