Skip to content

Commit

Permalink
upgrade primitive-testes
Browse files Browse the repository at this point in the history
  • Loading branch information
alec1o committed May 19, 2024
1 parent 179fe77 commit 88645f7
Showing 1 changed file with 172 additions and 40 deletions.
212 changes: 172 additions & 40 deletions test/primitive/ReadAndWrite.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,30 @@
using System;
using System.Collections.Generic;
using System.Numerics;
using System.Text;
using Byter;
using Xunit;

namespace ByterTest.primitive;

public class ReadAndWrite : IPrimitiveGet
{
[Fact]
private T? Terminate<T>(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();
Expand All @@ -23,10 +39,11 @@ public bool Bool()
Assert.False(primitive.Get.Bool());
Assert.True(primitive.Get.Bool());

return default;
return Terminate<bool>(ref primitive);
;
}

[Fact]
[Fact] // DONE
public byte Byte()
{
Primitive primitive = new();
Expand All @@ -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<byte>(ref primitive);
;
}

[Fact]
[Fact] // DONE
public sbyte SByte()
{
Primitive primitive = new();
Expand All @@ -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<sbyte>(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<char>(ref primitive);
}

[Fact]
[Fact] // DONE
public short Short()
{
Primitive primitive = new();
Expand All @@ -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<short>(ref primitive);
;
}

[Fact]
[Fact] // DONE
public ushort UShort()
{
Primitive primitive = new();
Expand All @@ -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<ushort>(ref primitive);
;
}

[Fact]
[Fact] // DONE
public int Int()
{
Primitive primitive = new();
Expand All @@ -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<int>(ref primitive);
;
}

[Fact]
[Fact] // DONE
public uint UInt()
{
Primitive primitive = new();
Expand All @@ -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<uint>(ref primitive);
;
}

[Fact]
[Fact] // DONE
public float Float()
{
Primitive primitive = new();
Expand All @@ -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<float>(ref primitive);
;
}

public T? Enum<T>()
{
return default;
}

[Fact]
[Fact] // DONE
public long Long()
{
Primitive primitive = new();
Expand All @@ -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<long>(ref primitive);
;
}

[Fact]
[Fact] // DONE
public ulong ULong()
{
Primitive primitive = new();
Expand All @@ -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<ulong>(ref primitive);
;
}

[Fact]
[Fact] // DONE
public double Double()
{
Primitive primitive = new();
Expand All @@ -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<double>(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<DateTime>(ref primitive);
;
}

[Fact]
[Fact] // DONE
public decimal Decimal()
{
Primitive primitive = new();
Expand All @@ -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<decimal>(ref primitive);
;
}

#region Unused

public string? String()
public T? Enum<T>()
{
return default;
}
Expand All @@ -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<BigInteger>(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<byte[]>(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<InvalidOperationException>(() => primitive.Add.Enum(int.MaxValue));

Assert.Equal(TypeCode.Char, primitive.Get.Enum<TypeCode>());
Assert.Equal(TypeCode.Byte, primitive.Get.Enum<TypeCode>());
Assert.Equal(TypeCode.String, primitive.Get.Enum<TypeCode>());
Assert.Equal(TypeCode.Decimal, primitive.Get.Enum<TypeCode>());

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<string>(ref primitive);
}

[Fact]
Expand Down

0 comments on commit 88645f7

Please sign in to comment.