diff --git a/test/primitive/ReadAndWrite.cs b/test/primitive/ReadAndWrite.cs index d744619..8c3bc64 100644 --- a/test/primitive/ReadAndWrite.cs +++ b/test/primitive/ReadAndWrite.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.Numerics; +using System.Text; using Byter; using Xunit; @@ -8,7 +9,22 @@ namespace ByterTest.primitive; public class ReadAndWrite : IPrimitiveGet { - [Fact] + private T? Terminate(ref Primitive primitive) + { + Terminate(ref primitive); + + return default; + } + + private void Terminate(ref Primitive primitive) + { + // EXTRA + Assert.True(primitive.IsValid); + _ = primitive.Get.Bool(); + Assert.False(primitive.IsValid); + } + + [Fact] // DONE public bool Bool() { Primitive primitive = new(); @@ -23,10 +39,11 @@ public bool Bool() Assert.False(primitive.Get.Bool()); Assert.True(primitive.Get.Bool()); - return default; + return Terminate(ref primitive); + ; } - [Fact] + [Fact] // DONE public byte Byte() { Primitive primitive = new(); @@ -37,10 +54,11 @@ public byte Byte() Assert.Equal(byte.MinValue, primitive.Get.Byte()); Assert.Equal(byte.MaxValue, primitive.Get.Byte()); - return default; + return Terminate(ref primitive); + ; } - [Fact] + [Fact] // DONE public sbyte SByte() { Primitive primitive = new(); @@ -51,16 +69,32 @@ public sbyte SByte() Assert.Equal(sbyte.MinValue, primitive.Get.SByte()); Assert.Equal(sbyte.MaxValue, primitive.Get.SByte()); - return default; + return Terminate(ref primitive); } - [Fact] + [Fact] // DONE public char Char() { - return default; + Primitive primitive = new(); + + primitive.Add.Char('A'); + primitive.Add.Char('L'); + primitive.Add.Char('E'); + primitive.Add.Char('C'); + primitive.Add.Char('I'); + primitive.Add.Char('O'); + + Assert.Equal('A', primitive.Get.Char()); + Assert.Equal('L', primitive.Get.Char()); + Assert.Equal('E', primitive.Get.Char()); + Assert.Equal('C', primitive.Get.Char()); + Assert.Equal('I', primitive.Get.Char()); + Assert.Equal('O', primitive.Get.Char()); + + return Terminate(ref primitive); } - [Fact] + [Fact] // DONE public short Short() { Primitive primitive = new(); @@ -71,10 +105,11 @@ public short Short() Assert.Equal(short.MinValue, primitive.Get.Short()); Assert.Equal(short.MaxValue, primitive.Get.Short()); - return default; + return Terminate(ref primitive); + ; } - [Fact] + [Fact] // DONE public ushort UShort() { Primitive primitive = new(); @@ -85,10 +120,11 @@ public ushort UShort() Assert.Equal(ushort.MinValue, primitive.Get.UShort()); Assert.Equal(ushort.MaxValue, primitive.Get.UShort()); - return default; + return Terminate(ref primitive); + ; } - [Fact] + [Fact] // DONE public int Int() { Primitive primitive = new(); @@ -99,10 +135,11 @@ public int Int() Assert.Equal(int.MinValue, primitive.Get.Int()); Assert.Equal(int.MaxValue, primitive.Get.Int()); - return default; + return Terminate(ref primitive); + ; } - [Fact] + [Fact] // DONE public uint UInt() { Primitive primitive = new(); @@ -113,10 +150,11 @@ public uint UInt() Assert.Equal(uint.MinValue, primitive.Get.UInt()); Assert.Equal(uint.MaxValue, primitive.Get.UInt()); - return default; + return Terminate(ref primitive); + ; } - [Fact] + [Fact] // DONE public float Float() { Primitive primitive = new(); @@ -127,15 +165,11 @@ public float Float() Assert.Equal(float.MinValue, primitive.Get.Float()); Assert.Equal(float.MaxValue, primitive.Get.Float()); - return default; + return Terminate(ref primitive); + ; } - public T? Enum() - { - return default; - } - - [Fact] + [Fact] // DONE public long Long() { Primitive primitive = new(); @@ -146,10 +180,11 @@ public long Long() Assert.Equal(long.MinValue, primitive.Get.Long()); Assert.Equal(long.MaxValue, primitive.Get.Long()); - return default; + return Terminate(ref primitive); + ; } - [Fact] + [Fact] // DONE public ulong ULong() { Primitive primitive = new(); @@ -160,10 +195,11 @@ public ulong ULong() Assert.Equal(ulong.MinValue, primitive.Get.ULong()); Assert.Equal(ulong.MaxValue, primitive.Get.ULong()); - return default; + return Terminate(ref primitive); + ; } - [Fact] + [Fact] // DONE public double Double() { Primitive primitive = new(); @@ -174,16 +210,32 @@ public double Double() Assert.Equal(double.MinValue, primitive.Get.Double()); Assert.Equal(double.MaxValue, primitive.Get.Double()); - return default; + return Terminate(ref primitive); + ; } - [Fact] + [Fact] // DONE public DateTime DateTime() { - return default; + Primitive primitive = new(); + + DateTime time1 = new(1970, 1, 1); + DateTime time2 = new(2038, 1, 19); + DateTime time3 = new(2038, 1, 20); // 2038 Bug Fixed. + + primitive.Add.DateTime(time1); + primitive.Add.DateTime(time2); + primitive.Add.DateTime(time3); + + Assert.Equal(time1, primitive.Get.DateTime()); + Assert.Equal(time2, primitive.Get.DateTime()); + Assert.Equal(time3, primitive.Get.DateTime()); + + return Terminate(ref primitive); + ; } - [Fact] + [Fact] // DONE public decimal Decimal() { Primitive primitive = new(); @@ -194,12 +246,13 @@ public decimal Decimal() Assert.Equal(decimal.MinValue, primitive.Get.Decimal()); Assert.Equal(decimal.MaxValue, primitive.Get.Decimal()); - return default; + return Terminate(ref primitive); + ; } #region Unused - public string? String() + public T? Enum() { return default; } @@ -226,26 +279,105 @@ public decimal Decimal() #endregion - [Fact] + [Fact] // DONE public BigInteger BigInteger() { - return default; + const string n1 = "1111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111"; + const string n2 = "2222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222"; + const string n3 = "3333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333"; + const string n9 = "9999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999"; + + var number1 = System.Numerics.BigInteger.Parse(n1); + var number2 = System.Numerics.BigInteger.Parse(n2); + var number3 = System.Numerics.BigInteger.Parse(n3); + var number9 = System.Numerics.BigInteger.Parse(n9); + + Assert.Equal(n1, number1.ToString()); + Assert.Equal(n2, number2.ToString()); + Assert.Equal(n3, number3.ToString()); + Assert.Equal(n9, number9.ToString()); + + Primitive primitive = new(); + + primitive.Add.BigInteger(number1); + primitive.Add.BigInteger(number2); + primitive.Add.BigInteger(number3); + primitive.Add.BigInteger(number9); + + Assert.Equal(number1, primitive.Get.BigInteger()); + Assert.Equal(number2, primitive.Get.BigInteger()); + Assert.Equal(number3, primitive.Get.BigInteger()); + Assert.Equal(number9, primitive.Get.BigInteger()); + + return Terminate(ref primitive); + ; } - [Fact] + [Fact] // DONE public byte[]? Bytes() { - return default; + byte[] data1 = [1, 2, 3, 4, 6, 7, 8, 9, 0]; + byte[] data2 = "ALEC1O".GetBytes(Encoding.UTF8); + byte[] data3 = "Hello World".GetBytes(Encoding.UTF32); + + Primitive primitive = new(); + + primitive.Add.Bytes(data1); + primitive.Add.Bytes(data2); + primitive.Add.Bytes(data3); + + Assert.Equal(data1, primitive.Get.Bytes()); + Assert.Equal(data2, primitive.Get.Bytes()); + Assert.Equal(data3, primitive.Get.Bytes()); + + return Terminate(ref primitive); + ; } [Fact] public void _Enum() { + Primitive primitive = new(); + + primitive.Add.Enum(TypeCode.Char); + primitive.Add.Enum(TypeCode.Byte); + primitive.Add.Enum(TypeCode.String); + primitive.Add.Enum(TypeCode.Decimal); + + Assert.Throws(() => primitive.Add.Enum(int.MaxValue)); + + Assert.Equal(TypeCode.Char, primitive.Get.Enum()); + Assert.Equal(TypeCode.Byte, primitive.Get.Enum()); + Assert.Equal(TypeCode.String, primitive.Get.Enum()); + Assert.Equal(TypeCode.Decimal, primitive.Get.Enum()); + + Terminate(ref primitive); } - [Fact] - public void _String() + [Fact] // DONE + public string? String() { + const string text1 = "BYTER"; + const string text2 = "IS"; + const string text3 = "CREATED"; + const string text4 = "BY"; + const string text5 = "ALEC1O"; + + Primitive primitive = new(); + + primitive.Add.String(text1); + primitive.Add.String(text2); + primitive.Add.String(text3); + primitive.Add.String(text4); + primitive.Add.String(text5); + + Assert.Equal(text1, primitive.Get.String()); + Assert.Equal(text2, primitive.Get.String()); + Assert.Equal(text3, primitive.Get.String()); + Assert.Equal(text4, primitive.Get.String()); + Assert.Equal(text5, primitive.Get.String()); + + return Terminate(ref primitive); } [Fact]