From e66146b36feea69c74bdd577158aca89b687d522 Mon Sep 17 00:00:00 2001 From: Alecio Furanze Date: Tue, 21 May 2024 22:28:26 +0200 Subject: [PATCH] delete ``by`` the ``primitive`` base --- src/by/By.Add.cs | 139 ------------- src/by/By.Constructors.cs | 24 --- src/by/By.Exception.cs | 16 -- src/by/By.Get.cs | 277 -------------------------- src/by/By.Hash.cs | 41 ---- src/by/By.Reset.cs | 36 ---- src/by/By.Types.cs | 57 ------ src/by/By.cs | 13 -- src/by/IBy.cs | 17 -- test/by/By.Hash.cs | 75 ------- test/by/By.WriteAndRead.cs | 397 ------------------------------------- 11 files changed, 1092 deletions(-) delete mode 100644 src/by/By.Add.cs delete mode 100644 src/by/By.Constructors.cs delete mode 100644 src/by/By.Exception.cs delete mode 100644 src/by/By.Get.cs delete mode 100644 src/by/By.Hash.cs delete mode 100644 src/by/By.Reset.cs delete mode 100644 src/by/By.Types.cs delete mode 100644 src/by/By.cs delete mode 100644 src/by/IBy.cs delete mode 100644 test/by/By.Hash.cs delete mode 100644 test/by/By.WriteAndRead.cs diff --git a/src/by/By.Add.cs b/src/by/By.Add.cs deleted file mode 100644 index 1d5de26..0000000 --- a/src/by/By.Add.cs +++ /dev/null @@ -1,139 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Numerics; -using System.Text; - -namespace Byter -{ - public partial class By : IBy - { - public void Add(T value) - { - Types type = Hash(value); - - object data = value; - byte prefix = (byte)type; - List buffer = new List(); - - switch (type) - { - case Types.Bool: - { - buffer.AddRange(BitConverter.GetBytes((bool)data)); - break; - } - case Types.Byte: - { - buffer.Add((byte)data); - break; - } - case Types.Char: - { - buffer.AddRange(BitConverter.GetBytes((char)data)); - break; - } - case Types.Bytes: - { - var bytes = (byte[])data; - var size = BitConverter.GetBytes(bytes.Length); - buffer.AddRange(size); - buffer.AddRange(bytes); - break; - } - case Types.Float: - { - buffer.AddRange(BitConverter.GetBytes((float)data)); - break; - } - case Types.Int: - { - buffer.AddRange(BitConverter.GetBytes((int)data)); - break; - } - case Types.Uint: - { - buffer.AddRange(BitConverter.GetBytes((uint)data)); - break; - } - case Types.Ulong: - { - buffer.AddRange(BitConverter.GetBytes((ulong)data)); - break; - } - case Types.Ushort: - { - buffer.AddRange(BitConverter.GetBytes((ushort)data)); - break; - } - case Types.Short: - { - buffer.AddRange(BitConverter.GetBytes((short)data)); - break; - } - case Types.Sbyte: - { - buffer.Add((byte)(sbyte)data); - break; - } - case Types.BigInteger: - { - var bytes = ((BigInteger)data).ToByteArray(); - var size = BitConverter.GetBytes(bytes.Length); - buffer.AddRange(size); - buffer.AddRange(bytes); - break; - } - case Types.Long: - { - buffer.AddRange(BitConverter.GetBytes((long)data)); - break; - } - case Types.String: - { - var bytes = ((string)data).GetBytes(Encoding.UTF8); - var size = BitConverter.GetBytes(bytes.Length); - buffer.AddRange(size); - buffer.AddRange(bytes); - break; - } - case Types.Double: - { - buffer.AddRange(BitConverter.GetBytes((double)data)); - break; - } - case Types.Decimal: - { - Decimal.GetBits((decimal)data).ToList().ForEach(x => buffer.AddRange(BitConverter.GetBytes(x))); - break; - } - case Types.DateTime: - { - buffer.AddRange(BitConverter.GetBytes(((DateTime)data).ToBinary())); - break; - } - case Types.Enum: - { - buffer.AddRange(BitConverter.GetBytes((int)data)); - break; - } - case Types.Array: - { - By b = new By(); - dynamic[] objects = data as dynamic[]; - buffer.AddRange(BitConverter.GetBytes(objects?.Length ?? 0)); - objects?.ToList().ForEach((x) => b.Add(x as object)); - buffer.AddRange(b.Buffer); - break; - } - default: - { - throw new NotImplementedException(); - } - } - - _vault.Add(prefix); - _vault.AddRange(buffer); - } - } -} \ No newline at end of file diff --git a/src/by/By.Constructors.cs b/src/by/By.Constructors.cs deleted file mode 100644 index f3fb5d4..0000000 --- a/src/by/By.Constructors.cs +++ /dev/null @@ -1,24 +0,0 @@ -using System; -using System.Collections.Generic; - -namespace Byter -{ - public partial class By : IBy - { - private By(int index, bool isValid, byte[] buffer) - { - _vault = new List(); - _vault.AddRange(buffer); - Index = index; - IsValid = isValid; - } - - public By() : this(Array.Empty()) - { - } - - public By(byte[] buffer) : this(0, true, buffer) - { - } - } -} \ No newline at end of file diff --git a/src/by/By.Exception.cs b/src/by/By.Exception.cs deleted file mode 100644 index 0f3a0b1..0000000 --- a/src/by/By.Exception.cs +++ /dev/null @@ -1,16 +0,0 @@ -using System; -using System.Linq; - -namespace Byter -{ - public partial class By : IBy - { - private static void ThrowExceptionIfInvalidType(Type type) - { - if (type == typeof(object)) - { - throw new NotSupportedException($"[Byter.By] Error: {type} Isn't supported!"); - } - } - } -} \ No newline at end of file diff --git a/src/by/By.Get.cs b/src/by/By.Get.cs deleted file mode 100644 index 3f2c95e..0000000 --- a/src/by/By.Get.cs +++ /dev/null @@ -1,277 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Drawing; -using System.Linq; -using System.Numerics; -using System.Text; - -namespace Byter -{ - public partial class By : IBy - { - public T Get() - { - if (Buffer.Length <= 0 || Index >= Buffer.Length) - { - IsValid = false; - return default; - } - - Types type = Hash(typeof(T)); - - T value; - - switch (type) - { - case Types.Int: - { - if (!IsValidPrefix(type, sizeof(int))) return default; - - value = (T)(object)BitConverter.ToInt32(Buffer, GetIndex()); - - AddIndex(sizeof(int)); - - return value; - } - - case Types.Uint: - { - if (!IsValidPrefix(type, sizeof(uint))) return default; - - value = (T)(object)BitConverter.ToUInt32(Buffer, GetIndex()); - - AddIndex(sizeof(uint)); - - return value; - } - - case Types.Float: - { - if (!IsValidPrefix(type, sizeof(float))) return default; - - value = (T)(object)BitConverter.ToSingle(Buffer, GetIndex()); - - AddIndex(sizeof(float)); - - return value; - } - - case Types.Long: - { - if (!IsValidPrefix(type, sizeof(long))) return default; - - value = (T)(object)BitConverter.ToInt64(Buffer, GetIndex()); - - AddIndex(sizeof(long)); - - return value; - } - - case Types.Ulong: - { - if (!IsValidPrefix(type, sizeof(ulong))) return default; - - value = (T)(object)BitConverter.ToUInt64(Buffer, GetIndex()); - - AddIndex(sizeof(ulong)); - - return value; - } - case Types.Bool: - { - if (!IsValidPrefix(type, sizeof(bool))) return default; - - value = (T)(object)BitConverter.ToBoolean(Buffer, GetIndex()); - - AddIndex(sizeof(bool)); - - return value; - } - - case Types.Char: - { - if (!IsValidPrefix(type, sizeof(char))) return default; - - value = (T)(object)BitConverter.ToChar(Buffer, GetIndex()); - - AddIndex(sizeof(char)); - - return value; - } - - case Types.Short: - { - if (!IsValidPrefix(type, sizeof(short))) return default; - - value = (T)(object)BitConverter.ToInt16(Buffer, GetIndex()); - - AddIndex(sizeof(short)); - - return value; - } - - case Types.Ushort: - { - if (!IsValidPrefix(type, sizeof(short))) return default; - - value = (T)(object)BitConverter.ToUInt16(Buffer, GetIndex()); - - AddIndex(sizeof(short)); - - return value; - } - - case Types.Double: - { - if (!IsValidPrefix(type, sizeof(double))) return default; - - value = (T)(object)BitConverter.ToDouble(Buffer, GetIndex()); - - AddIndex(sizeof(double)); - - return value; - } - - case Types.DateTime: - { - if (!IsValidPrefix(type, sizeof(long))) return default; - - value = (T)(object)DateTime.FromBinary(BitConverter.ToInt64(Buffer, GetIndex())); - - AddIndex(sizeof(long)); - - return value; - } - - case Types.Decimal: - { - if (!IsValidPrefix(type, sizeof(decimal))) return default; - - byte[] bits = _vault.GetRange(GetIndex(), sizeof(decimal)).ToArray(); - - int[] binary = - { - BitConverter.ToInt32(bits, sizeof(int) * 0), - BitConverter.ToInt32(bits, sizeof(int) * 1), - BitConverter.ToInt32(bits, sizeof(int) * 2), - BitConverter.ToInt32(bits, sizeof(int) * 3), - }; - - value = (T)(object)new decimal(binary); - - AddIndex(sizeof(decimal)); - - return value; - } - - case Types.Byte: - { - if (!IsValidPrefix(type, sizeof(byte))) return default; - - value = (T)(object)(byte)_vault[GetIndex()]; - - AddIndex(sizeof(byte)); - - return value; - } - - case Types.Sbyte: - { - if (!IsValidPrefix(type, sizeof(sbyte))) return default; - - value = (T)(object)(sbyte)_vault[GetIndex()]; - - AddIndex(sizeof(sbyte)); - - return value; - } - - case Types.Bytes: - { - if (!IsValidPrefix(type, sizeof(int))) return default; - - int size = BitConverter.ToInt32(Buffer, GetIndex()); - - int index = Index + sizeof(sbyte) + sizeof(int); - - value = (T)(object)(byte[])_vault.GetRange(index, size).ToArray(); - - AddIndex(sizeof(int) + size); - - return value; - } - - case Types.String: - { - if (!IsValidPrefix(type, sizeof(int))) return default; - - int size = BitConverter.ToInt32(Buffer, GetIndex()); - - int index = Index + sizeof(sbyte) + sizeof(int); - - value = (T)(object)(string)_vault.GetRange(index, size).ToArray().GetString(Encoding.UTF8); - - AddIndex(sizeof(int) + size); - - return value; - } - - case Types.BigInteger: - { - if (!IsValidPrefix(type, sizeof(int))) return default; - - int size = BitConverter.ToInt32(Buffer, GetIndex()); - - int index = Index + sizeof(sbyte) + sizeof(int); - - value = (T)(object)(BigInteger)new BigInteger(_vault.GetRange(index, size).ToArray()); - - AddIndex(sizeof(int) + size); - - return value; - } - case Types.Enum: - { - if (!IsValidPrefix(type, sizeof(int))) return default; - - value = (T)(object)BitConverter.ToInt32(Buffer, GetIndex()); - - AddIndex(sizeof(int)); - - return value; - } - - default: - throw new NotImplementedException($"{type}"); - } - } - - private bool IsValidPrefix(Types type, int size) - { - if (Index + size + sizeof(sbyte) > _vault.Count) - { - IsValid = false; - return false; - } - - Types target = (Types)(sbyte)_vault[Index]; - if (type != target) - { - IsValid = false; - return false; - } - - return true; - } - - private void AddIndex(int size) - { - Index += sizeof(sbyte) + size; - } - - private int GetIndex() - { - return Index + sizeof(sbyte); - } - } -} \ No newline at end of file diff --git a/src/by/By.Hash.cs b/src/by/By.Hash.cs deleted file mode 100644 index b52e52a..0000000 --- a/src/by/By.Hash.cs +++ /dev/null @@ -1,41 +0,0 @@ -using System; -using System.Collections; -using System.Collections.Generic; -using System.Linq; -using System.Numerics; - -namespace Byter -{ - public partial class By : IBy - { - public static Types Hash(T value) - { - Type type = typeof(T) == typeof(Type) ? (Type)(object)value : value.GetType(); - - if (type == typeof(sbyte)) return Types.Sbyte; - if (type == typeof(byte)) return Types.Byte; - if (type == typeof(bool)) return Types.Bool; - if (type == typeof(char)) return Types.Char; - if (type == typeof(short)) return Types.Short; - if (type == typeof(ushort)) return Types.Ushort; - if (type == typeof(uint)) return Types.Uint; - if (type == typeof(int)) return Types.Int; - if (type == typeof(float)) return Types.Float; - if (type.IsEnum) return Types.Enum; - if (type == typeof(long)) return Types.Long; - if (type == typeof(ulong)) return Types.Ulong; - if (type == typeof(double)) return Types.Double; - if (type == typeof(decimal)) return Types.Decimal; - if (type == typeof(byte[])) return Types.Bytes; - if (type == typeof(string)) return Types.String; - if (type == typeof(BigInteger)) return Types.BigInteger; - if (type == typeof(DateTime)) return Types.DateTime; - if (type.IsArray) return Types.Array; - if (value is IList) return Types.List; - if (type.IsValueType && !type.IsEnum && !type.IsPrimitive) return Types.Struct; - if (type.IsClass) return Types.Class; - - throw new NotImplementedException($"[{nameof(By)}.{nameof(Hash)}] Error: {type} Isn't implemented"); - } - } -} \ No newline at end of file diff --git a/src/by/By.Reset.cs b/src/by/By.Reset.cs deleted file mode 100644 index 8fd3d1e..0000000 --- a/src/by/By.Reset.cs +++ /dev/null @@ -1,36 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; - -namespace Byter -{ - public partial class By : IBy - { - public void Reset() - { - InternalReset(Array.Empty()); - } - - public void Reset(byte[] buffer) - { - InternalReset((buffer == null || buffer.Length <= 0) ? Array.Empty() : buffer.ToArray()); - } - - public void Reset(List buffer) - { - InternalReset((buffer == null || buffer.Count <= 0) ? Array.Empty() : buffer.ToArray()); - } - - private void InternalReset(byte[] buffer) - { - Index = 0; - IsValid = true; - _vault.Clear(); - - if (buffer != null && buffer.Length >= 1) - { - _vault.AddRange(buffer); - } - } - } -} \ No newline at end of file diff --git a/src/by/By.Types.cs b/src/by/By.Types.cs deleted file mode 100644 index 8b41e88..0000000 --- a/src/by/By.Types.cs +++ /dev/null @@ -1,57 +0,0 @@ -namespace Byter -{ - public partial class By : IBy - { - /// - /// -45 - /// - private const sbyte TypesPrefix = - ( - // XXXTentacion: Jahseh Dwayne Ricardo Onfroy - // January 23, 1998 - June 18, 2018 (age 20 years) - +(01 + 23 + 1998) - (06 + 18 + 2018) - // Juice WRLD: Jarad Anthony Higgins - // December 2, 1998 - December 8, 2019 (age 21 years) - + (12 + 2 + 1998) - (12 + 06 + 2019) - ); - - public enum Types - { - // 1 byte - Sbyte = TypesPrefix + 1, - Byte = TypesPrefix + 2, - Bool = TypesPrefix + 3, - - // 2 bytes - Char = TypesPrefix + 4, - Short = TypesPrefix + 5, - Ushort = TypesPrefix + 6, - - // 4 bytes - Uint = TypesPrefix + 7, - Int = TypesPrefix + 8, - Float = TypesPrefix + 9, - Enum = TypesPrefix + 10, - - // 8 bytes - Long = TypesPrefix + 11, - Ulong = TypesPrefix + 12, - Double = TypesPrefix + 13, - - // 16 bytes - Decimal = TypesPrefix + 14, - - // dynamic - Bytes = TypesPrefix + 15, - String = TypesPrefix + 16, - BigInteger = TypesPrefix + 18, - - // Symbols - Class = TypesPrefix + 19, - Struct = TypesPrefix + 20, - Array = TypesPrefix + 21, - DateTime = TypesPrefix + 22, - List = TypesPrefix + 23, - }; - } -} \ No newline at end of file diff --git a/src/by/By.cs b/src/by/By.cs deleted file mode 100644 index decb9e5..0000000 --- a/src/by/By.cs +++ /dev/null @@ -1,13 +0,0 @@ -using System; -using System.Collections.Generic; - -namespace Byter -{ - public partial class By : IBy - { - private readonly List _vault; - public int Index { get; private set; } - public bool IsValid { get; private set; } - public byte[] Buffer => _vault.ToArray(); - } -} \ No newline at end of file diff --git a/src/by/IBy.cs b/src/by/IBy.cs deleted file mode 100644 index 1e201a5..0000000 --- a/src/by/IBy.cs +++ /dev/null @@ -1,17 +0,0 @@ -using System.Collections.Generic; - -namespace Byter -{ - internal interface IBy - { - int Index { get; } - bool IsValid { get; } - byte[] Buffer { get; } - - void Reset(); - void Reset(byte[] buffer); - void Reset(List buffer); - void Add(T value); - T Get(); - } -} \ No newline at end of file diff --git a/test/by/By.Hash.cs b/test/by/By.Hash.cs deleted file mode 100644 index 7b8e2bc..0000000 --- a/test/by/By.Hash.cs +++ /dev/null @@ -1,75 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Net.Sockets; -using System.Numerics; -using System.Text; -using Byter; -using Xunit; - -namespace ByterTest.by; - -public class ByHash -{ - struct MyStruct - { - private const float A = -1; - private const float B = -1; - } - - class MyClass - { - private const float A = -1; - private const float B = -1; - } - - [Fact] - public void Start() - { - object? @null = null; - sbyte @sbyte = -32; - byte @byte = 42; - bool @bool = true; - char @char = 'A'; - int @int = int.MaxValue; - float @float = float.MinValue; - uint @uint = uint.MaxValue; - long @long = long.MaxValue; - ulong @ulong = ulong.MaxValue; - DateTime date = DateTime.Now; - short @short = short.MaxValue; - ushort @ushort = ushort.MaxValue; - string @string = Guid.NewGuid().ToString(); - double @double = double.MaxValue; - decimal @decimal = decimal.MaxValue; - SocketType @enum = SocketType.Stream; - byte[] bytes = "By"u8.ToArray(); - MyStruct @struct = new MyStruct(); - List list = [1, 2, 3]; - int[] array = [1, 2, 3]; - MyClass @class = new MyClass(); - BigInteger big = BigInteger.One; - - Assert.Equal(By.Types.Sbyte, By.Hash(@sbyte)); - Assert.Equal(By.Types.Byte, By.Hash(@byte)); - Assert.Equal(By.Types.Bool, By.Hash(@bool)); - Assert.Equal(By.Types.Char, By.Hash(@char)); - Assert.Equal(By.Types.Short, By.Hash(@short)); - Assert.Equal(By.Types.Ushort, By.Hash(@ushort)); - Assert.Equal(By.Types.Uint, By.Hash(@uint)); - Assert.Equal(By.Types.Int, By.Hash(@int)); - Assert.Equal(By.Types.Float, By.Hash(@float)); - Assert.Equal(By.Types.Enum, By.Hash(@enum)); - Assert.Equal(By.Types.Long, By.Hash(@long)); - Assert.Equal(By.Types.Ulong, By.Hash(@ulong)); - Assert.Equal(By.Types.Double, By.Hash(@double)); - Assert.Equal(By.Types.Decimal, By.Hash(@decimal)); - Assert.Equal(By.Types.Bytes, By.Hash(bytes)); - Assert.Equal(By.Types.String, By.Hash(@string)); - Assert.Equal(By.Types.BigInteger, By.Hash(@big)); - Assert.Equal(By.Types.Class, By.Hash(@class)); - Assert.Equal(By.Types.Struct, By.Hash(@struct)); - Assert.Equal(By.Types.DateTime, By.Hash(date)); - Assert.Equal(By.Types.Array, By.Hash(@array)); - Assert.Equal(By.Types.List, By.Hash(list)); - } -} \ No newline at end of file diff --git a/test/by/By.WriteAndRead.cs b/test/by/By.WriteAndRead.cs deleted file mode 100644 index 4abcb7d..0000000 --- a/test/by/By.WriteAndRead.cs +++ /dev/null @@ -1,397 +0,0 @@ -using System; -using System.Net.Sockets; -using System.Numerics; -using Byter; -using Xunit; - -namespace ByterTest.by; - -public class ByWriteAndRead -{ - public ByWriteAndRead() - { - - } - - [Fact] - public void ByInt() - { - var b = new By(); - - b.Add(int.MinValue); - b.Add(int.MaxValue); - - Assert.Equal(int.MinValue, b.Get()); - Assert.Equal(int.MaxValue, b.Get()); - Assert.True(b.IsValid); - } - - [Fact] - public void ByUInt() - { - var b = new By(); - - b.Add(uint.MinValue); - b.Add(uint.MaxValue); - - Assert.Equal(uint.MinValue, b.Get()); - Assert.Equal(uint.MaxValue, b.Get()); - Assert.True(b.IsValid); - } - - [Fact] - public void ByLong() - { - var b = new By(); - - b.Add(long.MinValue); - b.Add(long.MaxValue); - - Assert.Equal(long.MinValue, b.Get()); - Assert.Equal(long.MaxValue, b.Get()); - Assert.True(b.IsValid); - } - - [Fact] - public void ByULong() - { - var b = new By(); - - b.Add(ulong.MinValue); - b.Add(ulong.MaxValue); - - Assert.Equal(ulong.MinValue, b.Get()); - Assert.Equal(ulong.MaxValue, b.Get()); - Assert.True(b.IsValid); - } - - [Fact] - public void ByFloat() - { - var b = new By(); - - b.Add(float.MinValue); - b.Add(float.MaxValue); - - Assert.Equal(float.MinValue, b.Get()); - Assert.Equal(float.MaxValue, b.Get()); - Assert.True(b.IsValid); - } - - [Fact] - public void ByShort() - { - var b = new By(); - - b.Add(short.MinValue); - b.Add(short.MaxValue); - - Assert.Equal(short.MinValue, b.Get()); - Assert.Equal(short.MaxValue, b.Get()); - Assert.True(b.IsValid); - } - - [Fact] - public void ByUShort() - { - var b = new By(); - - b.Add(ushort.MinValue); - b.Add(ushort.MaxValue); - - Assert.Equal(ushort.MinValue, b.Get()); - Assert.Equal(ushort.MaxValue, b.Get()); - Assert.True(b.IsValid); - } - - [Fact] - public void Byte() - { - var b = new By(); - - b.Add(byte.MinValue); - b.Add(byte.MaxValue); - - Assert.Equal(byte.MinValue, b.Get()); - Assert.Equal(byte.MaxValue, b.Get()); - Assert.True(b.IsValid); - } - - [Fact] - public void SByte() - { - var b = new By(); - - b.Add(sbyte.MinValue); - b.Add(sbyte.MaxValue); - - Assert.Equal(sbyte.MinValue, b.Get()); - Assert.Equal(sbyte.MaxValue, b.Get()); - Assert.True(b.IsValid); - } - - [Fact] - public void ByDouble() - { - var b = new By(); - - b.Add(double.MinValue); - b.Add(double.MaxValue); - - Assert.Equal(double.MinValue, b.Get()); - Assert.Equal(double.MaxValue, b.Get()); - Assert.True(b.IsValid); - } - - [Fact] - public void ByDecimal() - { - var b = new By(); - - b.Add(decimal.MinValue); - b.Add(decimal.MaxValue); - - Assert.Equal(decimal.MinValue, b.Get()); - Assert.Equal(decimal.MaxValue, b.Get()); - Assert.True(b.IsValid); - } - - [Fact] - public void ByNull() - { - var b = new By(); - - b.Add(string.Empty); - b.Add(Array.Empty()); - b.Add(BigInteger.Zero); - - Assert.Equal(string.Empty, b.Get()); - Assert.Equal(Array.Empty(), b.Get()); - Assert.Equal(BigInteger.Zero, b.Get()); - Assert.True(b.IsValid); - } - - [Fact] - public void ByDateTime() - { - var b = new By(); - - var y1970 = new DateTime(1970, 1, 1, 0, 0, 0, 0, 0); - var y2038 = new DateTime(2038, 1, 1, 0, 0, 0, 0, 0); - - b.Add(y1970); - b.Add(y2038); - - Assert.Equal(y1970, b.Get()); - Assert.Equal(y2038, b.Get()); - Assert.True(b.IsValid); - } - - [Fact] - public void ByBytes() - { - var b = new By(); - - byte[] b1 = [1, 2, 3, 4, 5, 6, 7, 8, 9]; - byte[] b2 = [9, 8, 7, 6, 5, 4, 3, 2, 1]; - - b.Add(b1); - b.Add(b2); - - Assert.Equal(b1, b.Get()); - Assert.Equal(b2, b.Get()); - Assert.True(b.IsValid); - } - - [Fact] - public void ByChar() - { - var b = new By(); - - b.Add('A'); - b.Add('L'); - b.Add('E'); - b.Add('C'); - b.Add('1'); - b.Add('O'); - - Assert.Equal('A', b.Get()); - Assert.Equal('L', b.Get()); - Assert.Equal('E', b.Get()); - Assert.Equal('C', b.Get()); - Assert.Equal('1', b.Get()); - Assert.Equal('O', b.Get()); - Assert.True(b.IsValid); - } - - [Fact] - public void ByBool() - { - var b = new By(); - - b.Add(true); - b.Add(true); - b.Add(false); - b.Add(true); - b.Add(false); - b.Add(false); - - Assert.True(b.Get()); - Assert.True(b.Get()); - Assert.False(b.Get()); - Assert.True(b.Get()); - Assert.False(b.Get()); - Assert.False(b.Get()); - Assert.True(b.IsValid); - } - - [Fact] - public void ByString() - { - var b = new By(); - - var s1 = Guid.NewGuid().ToString(); - var s2 = Guid.NewGuid().ToString(); - var s3 = Guid.NewGuid().ToString(); - var s4 = Guid.NewGuid().ToString(); - - b.Add(s1); - b.Add(s2); - b.Add(s3); - b.Add(s4); - - Assert.Equal(s1, b.Get()); - Assert.Equal(s2, b.Get()); - Assert.Equal(s3, b.Get()); - Assert.Equal(s4, b.Get()); - Assert.True(b.IsValid); - } - - [Fact] - public void ByBigInteger() - { - var b = new By(); - - const string data = "01234567891011121314151617181920212223242526272829303132"; - - BigInteger b1 = BigInteger.Parse("1970" + data); - BigInteger b2 = BigInteger.Parse("2038" + data); - - b.Add(b1); - b.Add(b2); - - Assert.Equal(b1, b.Get()); - Assert.Equal(b2, b.Get()); - Assert.True(b.IsValid); - } - - [Fact] - public void ByEnum() - { - By b = new By(); - - b.Add(SocketType.Unknown); - b.Add(SocketType.Stream); - b.Add(SocketType.Dgram); - b.Add(SocketType.Raw); - b.Add(SocketType.Rdm); - b.Add(SocketType.Seqpacket); - - Assert.Equal(SocketType.Unknown, b.Get()); - Assert.Equal(SocketType.Stream, b.Get()); - Assert.Equal(SocketType.Dgram, b.Get()); - Assert.Equal(SocketType.Raw, b.Get()); - Assert.Equal(SocketType.Rdm, b.Get()); - Assert.Equal(SocketType.Seqpacket, b.Get()); - - Assert.True(b.IsValid); - } - - [Fact(Skip = "TODO")] - public void ByArray() - { - int[] intArray = - { - 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 - }; - - char[] charArray = - { - 'A', 'L', 'E', 'C', 'I', 'O', ' ', 'F', 'U', 'R', 'A', 'N', 'Z', 'E' - }; - - By b = new By(); - - b.Add(intArray); - b.Add(charArray); - - Assert.Equal(intArray, b.Get()); - Assert.Equal(charArray, b.Get()); - - Assert.True(b.IsValid); - } - - [Fact(Skip = "TODO")] - public void ByList() - { - } - - [Fact(Skip = "TODO")] - public void ByStruct() - { - } - - - public class MyClass - { - public int MyInt { get; set; } - public string MyString { get; set; } - public byte MyByte { get; set; } - public char MyChar { get; set; } - public sbyte MySbyte { get; set; } - public byte[] MyBytes { get; set; } - public long MyLong { get; set; } - public double MyDouble { get; set; } - public bool MyBool { get; set; } - public float MyFloat { get; set; } - public DateTime DateTime { get; set; } - public decimal MyDecimal { get; set; } - public MyClass2 MyNewClass2 { get; set; } - - public class MyClass2 - { - public string MyString { get; set; } - public double MyDouble { get; set; } - } - } - - [Fact(Skip = "TODO")] - public void ByClass() - { - MyClass myClass = new() - { - MyInt = int.MaxValue, - MyString = "alecio", - MyByte = byte.MaxValue, - MyChar = 'A', - MySbyte = sbyte.MinValue, - MyBytes = [1, 2, 3, 4], - MyLong = long.MaxValue, - MyDouble = double.MaxValue, - MyBool = true, - MyFloat = float.MaxValue, - DateTime = new DateTime(1970, 1, 1), - MyDecimal = decimal.MaxValue, - MyNewClass2 = new MyClass.MyClass2() - { - MyString = "Byter", - MyDouble = double.MinValue, - } - }; - - By b = new By(); - b.Add(myClass); - - Assert.Equal(myClass, b.Get()); - } -} \ No newline at end of file