cs-codex-dist-tests/ProjectPlugins/CodexContractsPlugin/TestTokenExtensions.cs

103 lines
2.5 KiB
C#
Raw Normal View History

using System.Numerics;
namespace CodexContractsPlugin
2023-09-19 09:51:59 +00:00
{
public class TestToken : IComparable<TestToken>
{
public static BigInteger WeiFactor = new BigInteger(1000000000000000000);
public TestToken(BigInteger tstWei)
2023-09-19 09:51:59 +00:00
{
TstWei = tstWei;
Tst = tstWei / WeiFactor;
2023-09-19 09:51:59 +00:00
}
public BigInteger TstWei { get; }
public BigInteger Tst { get; }
2023-09-19 09:51:59 +00:00
public int CompareTo(TestToken? other)
{
return TstWei.CompareTo(other!.TstWei);
2023-09-19 09:51:59 +00:00
}
public override bool Equals(object? obj)
{
return obj is TestToken token && TstWei == token.TstWei;
2023-09-19 09:51:59 +00:00
}
public override int GetHashCode()
{
return HashCode.Combine(TstWei);
2023-09-19 09:51:59 +00:00
}
public override string ToString()
{
var weiOnly = TstWei % WeiFactor;
var tokens = new List<string>();
if (Tst > 0) tokens.Add($"{Tst} TST");
if (weiOnly > 0) tokens.Add($"{weiOnly} TSTWEI");
return string.Join(" + ", tokens);
}
public static TestToken operator +(TestToken a, TestToken b)
{
return new TestToken(a.TstWei + b.TstWei);
}
public static bool operator <(TestToken a, TestToken b)
{
return a.TstWei < b.TstWei;
}
public static bool operator >(TestToken a, TestToken b)
{
return a.TstWei > b.TstWei;
}
public static bool operator ==(TestToken a, TestToken b)
{
return a.TstWei == b.TstWei;
}
public static bool operator !=(TestToken a, TestToken b)
{
return a.TstWei != b.TstWei;
2023-09-19 09:51:59 +00:00
}
}
public static class TestTokensExtensions
2023-09-19 09:51:59 +00:00
{
public static TestToken TstWei(this int i)
{
return TstWei(Convert.ToDecimal(i));
}
public static TestToken TstWei(this decimal i)
2023-09-19 09:51:59 +00:00
{
return new TestToken(new BigInteger(i));
2023-09-19 09:51:59 +00:00
}
public static TestToken TstWei(this BigInteger i)
2023-09-19 09:51:59 +00:00
{
return new TestToken(i);
}
public static TestToken Tst(this int i)
{
return Tst(Convert.ToDecimal(i));
}
public static TestToken Tst(this decimal i)
{
return new TestToken(new BigInteger(i) * TestToken.WeiFactor);
}
public static TestToken Tst(this BigInteger i)
{
return new TestToken(i * TestToken.WeiFactor);
}
2023-09-19 09:51:59 +00:00
}
}