From f5e6d7d6afa4dcd6f92ab00a61a9d8ead8ce8981 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Felix=20Wei=C3=9F?= <72068105+Sandoun@users.noreply.github.com> Date: Fri, 16 Jun 2023 16:29:01 +0200 Subject: [PATCH] Added new tests --- MewtocolNet/Mewtocol/LinkedLists.cs | 2 +- MewtocolNet/Mewtocol/MewtocolHelpers.cs | 17 ++++++ MewtocolTests/TestHelperExtensions.cs | 74 +++++++++++++++++++++++++ MewtocolTests/TestLinkedLists.cs | 62 +++++++++++++++++++++ 4 files changed, 154 insertions(+), 1 deletion(-) create mode 100644 MewtocolTests/TestHelperExtensions.cs create mode 100644 MewtocolTests/TestLinkedLists.cs diff --git a/MewtocolNet/Mewtocol/LinkedLists.cs b/MewtocolNet/Mewtocol/LinkedLists.cs index 5554330..468a28a 100644 --- a/MewtocolNet/Mewtocol/LinkedLists.cs +++ b/MewtocolNet/Mewtocol/LinkedLists.cs @@ -5,7 +5,7 @@ namespace MewtocolNet.Links { internal class LinkedData { - internal static Dictionary ErrorCodes = new System.Collections.Generic.Dictionary { + internal static Dictionary ErrorCodes = new Dictionary { {21, "NACK error"}, {22, "WACK error"}, diff --git a/MewtocolNet/Mewtocol/MewtocolHelpers.cs b/MewtocolNet/Mewtocol/MewtocolHelpers.cs index 31a3295..11cb77e 100644 --- a/MewtocolNet/Mewtocol/MewtocolHelpers.cs +++ b/MewtocolNet/Mewtocol/MewtocolHelpers.cs @@ -24,28 +24,37 @@ namespace MewtocolNet { } + /// + /// Converts a string (after converting to upper case) to ascii bytes + /// internal static byte[] ToHexASCIIBytes (this string _str) { + ASCIIEncoding ascii = new ASCIIEncoding(); byte[] bytes = ascii.GetBytes(_str.ToUpper()); return bytes; + } internal static string BuildBCCFrame (this string asciiArr) { + Encoding ae = Encoding.ASCII; byte[] b = ae.GetBytes(asciiArr); byte xorTotalByte = 0; for(int i = 0; i < b.Length; i++) xorTotalByte ^= b[i]; return asciiArr.Insert(asciiArr.Length, xorTotalByte.ToString("X2")); + } internal static byte[] ParseDTBytes (this string _onString ,int _blockSize = 4) { + var res = new Regex(@"\%([0-9]{2})\$RD(.{"+_blockSize+"})").Match(_onString); if(res.Success) { string val = res.Groups[2].Value; return val.HexStringToByteArray(); } return null; + } 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) { + var res = new Regex(@"\%([0-9]{2})\$RC(.)").Match(_onString); if (res.Success) { string val = res.Groups[2].Value; return val == "1"; } return null; + } internal static string ParseDTString (this string _onString) { + var res = new Regex(@"\%([0-9]{2})\$RD.{8}(.*)...").Match(_onString); if(res.Success) { string val = res.Groups[2].Value; return val.GetStringFromAsciiHex()?.Trim(); } return null; + } internal static string ReverseByteOrder (this string _onString) { @@ -94,6 +107,7 @@ namespace MewtocolNet { } internal static IEnumerable SplitInParts (this string s, int partLength) { + if (s == null) throw new ArgumentNullException(nameof(s)); if (partLength <= 0) @@ -101,6 +115,7 @@ namespace MewtocolNet { for (var i = 0; i < s.Length; i += partLength) yield return s.Substring(i, Math.Min(partLength, s.Length - i)); + } internal static string BuildDTString (this string _inString, short _stringReservedSize) { @@ -167,12 +182,14 @@ namespace MewtocolNet { } internal static string ToHexString (this byte[] arr) { + StringBuilder sb = new StringBuilder(); for (int i = 0; i < arr.Length; i++) { byte b = arr[i]; sb.Append(b.ToString("X2")); } return sb.ToString(); + } internal static byte[] BigToMixedEndian (this byte[] arr) { diff --git a/MewtocolTests/TestHelperExtensions.cs b/MewtocolTests/TestHelperExtensions.cs new file mode 100644 index 0000000..d0dc5e7 --- /dev/null +++ b/MewtocolTests/TestHelperExtensions.cs @@ -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()); + + } + + } + +} \ No newline at end of file diff --git a/MewtocolTests/TestLinkedLists.cs b/MewtocolTests/TestLinkedLists.cs new file mode 100644 index 0000000..cef48f8 --- /dev/null +++ b/MewtocolTests/TestLinkedLists.cs @@ -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 { + + {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); + + } + + } + +} \ No newline at end of file