Skip to content

Commit

Permalink
remove null type and improve test
Browse files Browse the repository at this point in the history
  • Loading branch information
alec1o committed May 6, 2024
1 parent 4a1a5dc commit 087d0d0
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 35 deletions.
5 changes: 0 additions & 5 deletions src/by/By.Add.cs
Original file line number Diff line number Diff line change
Expand Up @@ -102,11 +102,6 @@ public void Add<T>(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)));
Expand Down
19 changes: 0 additions & 19 deletions src/by/By.Get.cs
Original file line number Diff line number Diff line change
Expand Up @@ -186,25 +186,6 @@ public T Get<T>()
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;
Expand Down
2 changes: 0 additions & 2 deletions src/by/By.Hash.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ public partial class By : IBy
{
public static Types Hash<T>(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;
Expand Down
1 change: 0 additions & 1 deletion src/by/By.Types.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
3 changes: 1 addition & 2 deletions test/by/By.Hash.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
33 changes: 27 additions & 6 deletions test/by/By.WriteAndRead.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public void ByInt()

Assert.Equal(int.MinValue, b.Get<int>());
Assert.Equal(int.MaxValue, b.Get<int>());
Assert.True(b.IsValid);
}

[Fact]
Expand All @@ -29,6 +30,7 @@ public void ByUInt()

Assert.Equal(uint.MinValue, b.Get<uint>());
Assert.Equal(uint.MaxValue, b.Get<uint>());
Assert.True(b.IsValid);
}

[Fact]
Expand All @@ -41,6 +43,7 @@ public void ByLong()

Assert.Equal(long.MinValue, b.Get<long>());
Assert.Equal(long.MaxValue, b.Get<long>());
Assert.True(b.IsValid);
}

[Fact]
Expand All @@ -53,6 +56,7 @@ public void ByULong()

Assert.Equal(ulong.MinValue, b.Get<ulong>());
Assert.Equal(ulong.MaxValue, b.Get<ulong>());
Assert.True(b.IsValid);
}

[Fact]
Expand All @@ -65,6 +69,7 @@ public void ByFloat()

Assert.Equal(float.MinValue, b.Get<float>());
Assert.Equal(float.MaxValue, b.Get<float>());
Assert.True(b.IsValid);
}

[Fact]
Expand All @@ -77,6 +82,7 @@ public void ByShort()

Assert.Equal(short.MinValue, b.Get<short>());
Assert.Equal(short.MaxValue, b.Get<short>());
Assert.True(b.IsValid);
}

[Fact]
Expand All @@ -89,6 +95,7 @@ public void ByUShort()

Assert.Equal(ushort.MinValue, b.Get<ushort>());
Assert.Equal(ushort.MaxValue, b.Get<ushort>());
Assert.True(b.IsValid);
}

[Fact]
Expand All @@ -101,6 +108,7 @@ public void Byte()

Assert.Equal(byte.MinValue, b.Get<byte>());
Assert.Equal(byte.MaxValue, b.Get<byte>());
Assert.True(b.IsValid);
}

[Fact]
Expand All @@ -113,6 +121,7 @@ public void SByte()

Assert.Equal(sbyte.MinValue, b.Get<sbyte>());
Assert.Equal(sbyte.MaxValue, b.Get<sbyte>());
Assert.True(b.IsValid);
}

[Fact]
Expand All @@ -125,6 +134,7 @@ public void ByDouble()

Assert.Equal(double.MinValue, b.Get<double>());
Assert.Equal(double.MaxValue, b.Get<double>());
Assert.True(b.IsValid);
}

[Fact]
Expand All @@ -137,17 +147,22 @@ public void ByDecimal()

Assert.Equal(decimal.MinValue, b.Get<decimal>());
Assert.Equal(decimal.MaxValue, b.Get<decimal>());
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<object>());
b.Add(string.Empty);
b.Add(Array.Empty<byte>());
b.Add(BigInteger.Zero);

Assert.Equal(string.Empty, b.Get<string>());
Assert.Equal(Array.Empty<byte>(), b.Get<byte[]>());
Assert.Equal(BigInteger.Zero, b.Get<BigInteger>());
Assert.True(b.IsValid);
}

[Fact]
Expand All @@ -163,6 +178,7 @@ public void ByDateTime()

Assert.Equal(y1970, b.Get<DateTime>());
Assert.Equal(y2038, b.Get<DateTime>());
Assert.True(b.IsValid);
}

[Fact]
Expand All @@ -178,6 +194,7 @@ public void ByBytes()

Assert.Equal(b1, b.Get<byte[]>());
Assert.Equal(b2, b.Get<byte[]>());
Assert.True(b.IsValid);
}

[Fact]
Expand All @@ -198,6 +215,7 @@ public void ByChar()
Assert.Equal('C', b.Get<char>());
Assert.Equal('1', b.Get<char>());
Assert.Equal('O', b.Get<char>());
Assert.True(b.IsValid);
}

[Fact]
Expand All @@ -218,6 +236,7 @@ public void ByBool()
Assert.True(b.Get<bool>());
Assert.False(b.Get<bool>());
Assert.False(b.Get<bool>());
Assert.True(b.IsValid);
}

[Fact]
Expand All @@ -239,6 +258,7 @@ public void ByString()
Assert.Equal(s2, b.Get<string>());
Assert.Equal(s3, b.Get<string>());
Assert.Equal(s4, b.Get<string>());
Assert.True(b.IsValid);
}

[Fact]
Expand All @@ -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);

Expand All @@ -256,5 +276,6 @@ public void ByBigInteger()

Assert.Equal(b1, b.Get<BigInteger>());
Assert.Equal(b2, b.Get<BigInteger>());
Assert.True(b.IsValid);
}
}

0 comments on commit 087d0d0

Please sign in to comment.