From 087d0d0419e2a0ab48e58d86fb00fb6f33bc080a Mon Sep 17 00:00:00 2001 From: Alecio Furanze Date: Mon, 6 May 2024 02:20:54 +0200 Subject: [PATCH] remove null type and improve test --- src/by/By.Add.cs | 5 ----- src/by/By.Get.cs | 19 ------------------- src/by/By.Hash.cs | 2 -- src/by/By.Types.cs | 1 - test/by/By.Hash.cs | 3 +-- test/by/By.WriteAndRead.cs | 33 +++++++++++++++++++++++++++------ 6 files changed, 28 insertions(+), 35 deletions(-) diff --git a/src/by/By.Add.cs b/src/by/By.Add.cs index 4a31669..e8d52e6 100644 --- a/src/by/By.Add.cs +++ b/src/by/By.Add.cs @@ -102,11 +102,6 @@ public void Add(T value) buffer.AddRange(BitConverter.GetBytes((double)data)); break; } - case Types.Null: - { - buffer.AddRange(BitConverter.GetBytes((sbyte)-1)); - break; - } case Types.Decimal: { Decimal.GetBits((decimal)data).ToList().ForEach(x => buffer.AddRange(BitConverter.GetBytes(x))); diff --git a/src/by/By.Get.cs b/src/by/By.Get.cs index e25531c..ad0ea88 100644 --- a/src/by/By.Get.cs +++ b/src/by/By.Get.cs @@ -186,25 +186,6 @@ public T Get() return value; } - case Types.Null: - { - if (!IsValidPrefix(type, sizeof(sbyte))) return default; - - sbyte data = (sbyte)_vault[GetIndex()]; - - if (data != -1) - { - IsValid = false; - return default; - } - - value = (T)(object)data; - - AddIndex(sizeof(sbyte)); - - return value; - } - case Types.Bytes: { if (!IsValidPrefix(type, sizeof(int))) return default; diff --git a/src/by/By.Hash.cs b/src/by/By.Hash.cs index 5836894..b52e52a 100644 --- a/src/by/By.Hash.cs +++ b/src/by/By.Hash.cs @@ -10,8 +10,6 @@ public partial class By : IBy { public static Types Hash(T value) { - if (value == null) return Types.Null; - Type type = typeof(T) == typeof(Type) ? (Type)(object)value : value.GetType(); if (type == typeof(sbyte)) return Types.Sbyte; diff --git a/src/by/By.Types.cs b/src/by/By.Types.cs index d7c56bb..8b41e88 100644 --- a/src/by/By.Types.cs +++ b/src/by/By.Types.cs @@ -18,7 +18,6 @@ public partial class By : IBy public enum Types { // 1 byte - Null = TypesPrefix + 0, Sbyte = TypesPrefix + 1, Byte = TypesPrefix + 2, Bool = TypesPrefix + 3, diff --git a/test/by/By.Hash.cs b/test/by/By.Hash.cs index ea54672..7b8e2bc 100644 --- a/test/by/By.Hash.cs +++ b/test/by/By.Hash.cs @@ -48,8 +48,7 @@ public void Start() int[] array = [1, 2, 3]; MyClass @class = new MyClass(); BigInteger big = BigInteger.One; - - Assert.Equal(By.Types.Null, By.Hash(@null)); + Assert.Equal(By.Types.Sbyte, By.Hash(@sbyte)); Assert.Equal(By.Types.Byte, By.Hash(@byte)); Assert.Equal(By.Types.Bool, By.Hash(@bool)); diff --git a/test/by/By.WriteAndRead.cs b/test/by/By.WriteAndRead.cs index dfab229..d03ff99 100644 --- a/test/by/By.WriteAndRead.cs +++ b/test/by/By.WriteAndRead.cs @@ -17,6 +17,7 @@ public void ByInt() Assert.Equal(int.MinValue, b.Get()); Assert.Equal(int.MaxValue, b.Get()); + Assert.True(b.IsValid); } [Fact] @@ -29,6 +30,7 @@ public void ByUInt() Assert.Equal(uint.MinValue, b.Get()); Assert.Equal(uint.MaxValue, b.Get()); + Assert.True(b.IsValid); } [Fact] @@ -41,6 +43,7 @@ public void ByLong() Assert.Equal(long.MinValue, b.Get()); Assert.Equal(long.MaxValue, b.Get()); + Assert.True(b.IsValid); } [Fact] @@ -53,6 +56,7 @@ public void ByULong() Assert.Equal(ulong.MinValue, b.Get()); Assert.Equal(ulong.MaxValue, b.Get()); + Assert.True(b.IsValid); } [Fact] @@ -65,6 +69,7 @@ public void ByFloat() Assert.Equal(float.MinValue, b.Get()); Assert.Equal(float.MaxValue, b.Get()); + Assert.True(b.IsValid); } [Fact] @@ -77,6 +82,7 @@ public void ByShort() Assert.Equal(short.MinValue, b.Get()); Assert.Equal(short.MaxValue, b.Get()); + Assert.True(b.IsValid); } [Fact] @@ -89,6 +95,7 @@ public void ByUShort() Assert.Equal(ushort.MinValue, b.Get()); Assert.Equal(ushort.MaxValue, b.Get()); + Assert.True(b.IsValid); } [Fact] @@ -101,6 +108,7 @@ public void Byte() Assert.Equal(byte.MinValue, b.Get()); Assert.Equal(byte.MaxValue, b.Get()); + Assert.True(b.IsValid); } [Fact] @@ -113,6 +121,7 @@ public void SByte() Assert.Equal(sbyte.MinValue, b.Get()); Assert.Equal(sbyte.MaxValue, b.Get()); + Assert.True(b.IsValid); } [Fact] @@ -125,6 +134,7 @@ public void ByDouble() Assert.Equal(double.MinValue, b.Get()); Assert.Equal(double.MaxValue, b.Get()); + Assert.True(b.IsValid); } [Fact] @@ -137,17 +147,22 @@ public void ByDecimal() Assert.Equal(decimal.MinValue, b.Get()); Assert.Equal(decimal.MaxValue, b.Get()); + Assert.True(b.IsValid); } - [Fact(Skip = "TODO: ")] + [Fact] public void ByNull() { var b = new By(); - object? @null = null; - b.Add(@null); - - Assert.Null(b.Get()); + 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] @@ -163,6 +178,7 @@ public void ByDateTime() Assert.Equal(y1970, b.Get()); Assert.Equal(y2038, b.Get()); + Assert.True(b.IsValid); } [Fact] @@ -178,6 +194,7 @@ public void ByBytes() Assert.Equal(b1, b.Get()); Assert.Equal(b2, b.Get()); + Assert.True(b.IsValid); } [Fact] @@ -198,6 +215,7 @@ public void ByChar() Assert.Equal('C', b.Get()); Assert.Equal('1', b.Get()); Assert.Equal('O', b.Get()); + Assert.True(b.IsValid); } [Fact] @@ -218,6 +236,7 @@ public void ByBool() Assert.True(b.Get()); Assert.False(b.Get()); Assert.False(b.Get()); + Assert.True(b.IsValid); } [Fact] @@ -239,6 +258,7 @@ public void ByString() Assert.Equal(s2, b.Get()); Assert.Equal(s3, b.Get()); Assert.Equal(s4, b.Get()); + Assert.True(b.IsValid); } [Fact] @@ -247,7 +267,7 @@ public void ByBigInteger() var b = new By(); const string data = "01234567891011121314151617181920212223242526272829303132"; - + BigInteger b1 = BigInteger.Parse("1970" + data); BigInteger b2 = BigInteger.Parse("2038" + data); @@ -256,5 +276,6 @@ public void ByBigInteger() Assert.Equal(b1, b.Get()); Assert.Equal(b2, b.Get()); + Assert.True(b.IsValid); } } \ No newline at end of file