1
0
Fork 0
mirror of https://github.com/beefytech/Beef.git synced 2025-06-10 20:42:21 +02:00

Add ability to get min/max values for generic int's and uint's

This commit is contained in:
Ron Zuckerman 2024-02-08 20:16:29 -06:00
parent 6302416e40
commit 484b9716db
No known key found for this signature in database
GPG key ID: 7EC17CB87028ABF8
12 changed files with 72 additions and 10 deletions

View file

@ -0,0 +1,8 @@
namespace System
{
interface IMinMaxValue<T>
{
public static T MinValue { get; }
public static T MaxValue { get; }
}
}

View file

@ -3,7 +3,7 @@ using System;
namespace System
{
#unwarn
struct Int : int, IInteger, IHashable, IFormattable, IIsNaN, IParseable<int, ParseError>, IParseable<int>
struct Int : int, IInteger, IHashable, IFormattable, IIsNaN, IParseable<int, ParseError>, IParseable<int>, IMinMaxValue<int>
{
public enum ParseError
{
@ -16,6 +16,9 @@ namespace System
public const int MaxValue = (sizeof(int) == 8) ? 0x7FFFFFFFFFFFFFFFL : 0x7FFFFFFF;
public const int MinValue = (sizeof(int) == 8) ? -0x8000000000000000L : -0x80000000;
public static int IMinMaxValue<int>.MinValue => MinValue;
public static int IMinMaxValue<int>.MaxValue => MaxValue;
public static int operator<=>(Self a, Self b)
{
return (SelfBase)a <=> (SelfBase)b;

View file

@ -3,7 +3,7 @@ using System.Globalization;
namespace System
{
#unwarn
struct Int16 : int16, IInteger, ISigned, IHashable, IFormattable, IIsNaN, IParseable<int16, ParseError>, IParseable<int16>
struct Int16 : int16, IInteger, ISigned, IHashable, IFormattable, IIsNaN, IParseable<int16, ParseError>, IParseable<int16>, IMinMaxValue<int16>
{
public enum ParseError
{
@ -16,6 +16,9 @@ namespace System
public const int16 MaxValue = 0x7FFF;
public const int16 MinValue = -0x8000;
public static int16 IMinMaxValue<int16>.MinValue => MinValue;
public static int16 IMinMaxValue<int16>.MaxValue => MaxValue;
public static int operator<=>(Self a, Self b)
{
return (SelfBase)a <=> (SelfBase)b;

View file

@ -3,7 +3,7 @@ using System.Globalization;
namespace System
{
#unwarn
struct Int32 : int32, IInteger, ISigned, IHashable, IFormattable, IIsNaN, IParseable<int32, ParseError>, IParseable<int32>
struct Int32 : int32, IInteger, ISigned, IHashable, IFormattable, IIsNaN, IParseable<int32, ParseError>, IParseable<int32>, IMinMaxValue<int32>
{
public enum ParseError
{
@ -16,6 +16,9 @@ namespace System
public const int32 MaxValue = 0x7FFFFFFF;
public const int32 MinValue = -0x80000000;
public static int32 IMinMaxValue<int32>.MinValue => MinValue;
public static int32 IMinMaxValue<int32>.MaxValue => MaxValue;
public static int operator<=>(Self a, Self b)
{
return (SelfBase)a <=> (SelfBase)b;

View file

@ -3,7 +3,7 @@ using System.Globalization;
namespace System
{
#unwarn
struct Int64 : int64, IInteger, ISigned, IFormattable, IHashable, IIsNaN, IParseable<int64, ParseError>, IParseable<int64>
struct Int64 : int64, IInteger, ISigned, IFormattable, IHashable, IIsNaN, IParseable<int64, ParseError>, IParseable<int64>, IMinMaxValue<int64>
{
public enum ParseError
{
@ -16,6 +16,9 @@ namespace System
public const int64 MaxValue = 0x7FFFFFFFFFFFFFFFL;
public const int64 MinValue = -0x8000000000000000L;
public static int64 IMinMaxValue<int64>.MinValue => MinValue;
public static int64 IMinMaxValue<int64>.MaxValue => MaxValue;
public static int operator<=>(Int64 a, Int64 b)
{
return (SelfBase)a <=> (SelfBase)b;

View file

@ -3,7 +3,7 @@ using System.Globalization;
namespace System
{
#unwarn
struct Int8 : int8, IInteger, ISigned, IHashable, IFormattable, IIsNaN, IParseable<int8, ParseError>, IParseable<int8>
struct Int8 : int8, IInteger, ISigned, IHashable, IFormattable, IIsNaN, IParseable<int8, ParseError>, IParseable<int8>, IMinMaxValue<int8>
{
public enum ParseError
{
@ -16,6 +16,9 @@ namespace System
public const int8 MaxValue = 0x7F;
public const int8 MinValue = -0x80;
public static int8 IMinMaxValue<int8>.MinValue => MinValue;
public static int8 IMinMaxValue<int8>.MaxValue => MaxValue;
public static int operator<=>(Self a, Self b)
{
return (SelfBase)a <=> (SelfBase)b;

View file

@ -1,7 +1,7 @@
namespace System
{
#unwarn
struct UInt : uint, IInteger, IUnsigned, IHashable, IFormattable, IIsNaN, IParseable<uint, ParseError>, IParseable<uint>
struct UInt : uint, IInteger, IUnsigned, IHashable, IFormattable, IIsNaN, IParseable<uint, ParseError>, IParseable<uint>, IMinMaxValue<uint>
{
public enum ParseError
{
@ -14,6 +14,9 @@ namespace System
public const uint MaxValue = (sizeof(uint) == 8) ? 0xFFFFFFFFFFFFFFFFUL : 0xFFFFFFFFL;
public const uint MinValue = 0;
public static uint IMinMaxValue<uint>.MinValue => MinValue;
public static uint IMinMaxValue<uint>.MaxValue => MaxValue;
public bool IsNull()
{
return this == 0;

View file

@ -3,7 +3,7 @@ using System.Globalization;
namespace System
{
#unwarn
struct UInt16 : uint16, IInteger, IUnsigned, IHashable, IFormattable, IIsNaN, IParseable<uint16, ParseError>, IParseable<uint16>
struct UInt16 : uint16, IInteger, IUnsigned, IHashable, IFormattable, IIsNaN, IParseable<uint16, ParseError>, IParseable<uint16>, IMinMaxValue<uint16>
{
public enum ParseError
{
@ -16,6 +16,9 @@ namespace System
public const uint16 MaxValue = 0xFFFF;
public const uint16 MinValue = 0;
public static uint16 IMinMaxValue<uint16>.MinValue => MinValue;
public static uint16 IMinMaxValue<uint16>.MaxValue => MaxValue;
public static int operator<=>(Self a, Self b)
{
return (SelfBase)a <=> (SelfBase)b;

View file

@ -3,7 +3,7 @@ using System.Globalization;
namespace System
{
#unwarn
struct UInt32 : uint32, IInteger, IUnsigned, IHashable, IFormattable, IIsNaN, IParseable<uint32, ParseError>, IParseable<uint32>
struct UInt32 : uint32, IInteger, IUnsigned, IHashable, IFormattable, IIsNaN, IParseable<uint32, ParseError>, IParseable<uint32>, IMinMaxValue<uint32>
{
public enum ParseError
{
@ -16,6 +16,9 @@ namespace System
public const uint32 MaxValue = 0xFFFFFFFFL;
public const uint32 MinValue = 0;
public static uint32 IMinMaxValue<uint32>.MinValue => MinValue;
public static uint32 IMinMaxValue<uint32>.MaxValue => MaxValue;
public static int operator<=>(Self a, Self b)
{
return (SelfBase)a <=> (SelfBase)b;

View file

@ -3,7 +3,7 @@ using System.Globalization;
namespace System
{
#unwarn
struct UInt64 : uint64, IInteger, IUnsigned, IHashable, IIsNaN, IFormattable, IParseable<uint64, ParseError>, IParseable<uint64>
struct UInt64 : uint64, IInteger, IUnsigned, IHashable, IIsNaN, IFormattable, IParseable<uint64, ParseError>, IParseable<uint64>, IMinMaxValue<uint64>
{
public enum ParseError
{
@ -16,6 +16,9 @@ namespace System
public const uint64 MaxValue = 0xFFFFFFFFFFFFFFFFUL;
public const uint64 MinValue = 0;
public static uint64 IMinMaxValue<uint64>.MinValue => MinValue;
public static uint64 IMinMaxValue<uint64>.MaxValue => MaxValue;
public static int operator<=>(UInt64 a, UInt64 b)
{
return (SelfBase)a <=> (SelfBase)b;

View file

@ -3,7 +3,7 @@ using System.Globalization;
namespace System
{
#unwarn
struct UInt8 : uint8, IInteger, IUnsigned, IHashable, IFormattable, IIsNaN, IParseable<uint8, ParseError>, IParseable<uint8>
struct UInt8 : uint8, IInteger, IUnsigned, IHashable, IFormattable, IIsNaN, IParseable<uint8, ParseError>, IParseable<uint8>, IMinMaxValue<uint8>
{
public enum ParseError
{
@ -16,6 +16,9 @@ namespace System
public const uint8 MaxValue = 0xFF;
public const uint8 MinValue = 0;
public static uint8 IMinMaxValue<uint8>.MinValue => MinValue;
public static uint8 IMinMaxValue<uint8>.MaxValue => MaxValue;
public static int operator<=>(Self a, Self b)
{
return (SelfBase)a <=> (SelfBase)b;

View file

@ -113,5 +113,29 @@ namespace Tests
Uint64ParseErrorTest("+0x", .HexNumber);
Uint64ParseErrorTest("+0X", .HexNumber);
}
public static void MinMaxTest<T>(T expectedMinValue, T expectedMaxValue)
where T : IMinMaxValue<T>
where int : operator T <=> T
{
Test.Assert(T.MinValue == expectedMinValue);
Test.Assert(T.MaxValue == expectedMaxValue);
}
[Test]
public static void TestMinMax()
{
MinMaxTest<int>(Int.MinValue, Int.MaxValue);
MinMaxTest<int8>(Int8.MinValue, Int8.MaxValue);
MinMaxTest<int16>(Int16.MinValue, Int16.MaxValue);
MinMaxTest<int32>(Int32.MinValue, Int32.MaxValue);
MinMaxTest<int64>(Int64.MinValue, Int64.MaxValue);
MinMaxTest<uint>(UInt.MinValue, UInt.MaxValue);
MinMaxTest<uint8>(UInt8.MinValue, UInt8.MaxValue);
MinMaxTest<uint16>(UInt16.MinValue, UInt16.MaxValue);
MinMaxTest<uint32>(UInt32.MinValue, UInt32.MaxValue);
MinMaxTest<uint64>(UInt64.MinValue, UInt64.MaxValue);
}
}
}