diff --git a/BeefLibs/corlib/src/IMinMaxValue.bf b/BeefLibs/corlib/src/IMinMaxValue.bf new file mode 100644 index 00000000..f7b536d5 --- /dev/null +++ b/BeefLibs/corlib/src/IMinMaxValue.bf @@ -0,0 +1,8 @@ +namespace System +{ + interface IMinMaxValue + { + public static T MinValue { get; } + public static T MaxValue { get; } + } +} diff --git a/BeefLibs/corlib/src/Int.bf b/BeefLibs/corlib/src/Int.bf index c2f929e1..3a72fad2 100644 --- a/BeefLibs/corlib/src/Int.bf +++ b/BeefLibs/corlib/src/Int.bf @@ -3,7 +3,7 @@ using System; namespace System { #unwarn - struct Int : int, IInteger, IHashable, IFormattable, IIsNaN, IParseable, IParseable + struct Int : int, IInteger, IHashable, IFormattable, IIsNaN, IParseable, IParseable, IMinMaxValue { 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.MinValue => MinValue; + public static int IMinMaxValue.MaxValue => MaxValue; + public static int operator<=>(Self a, Self b) { return (SelfBase)a <=> (SelfBase)b; diff --git a/BeefLibs/corlib/src/Int16.bf b/BeefLibs/corlib/src/Int16.bf index a092c211..8930231a 100644 --- a/BeefLibs/corlib/src/Int16.bf +++ b/BeefLibs/corlib/src/Int16.bf @@ -3,7 +3,7 @@ using System.Globalization; namespace System { #unwarn - struct Int16 : int16, IInteger, ISigned, IHashable, IFormattable, IIsNaN, IParseable, IParseable + struct Int16 : int16, IInteger, ISigned, IHashable, IFormattable, IIsNaN, IParseable, IParseable, IMinMaxValue { public enum ParseError { @@ -16,6 +16,9 @@ namespace System public const int16 MaxValue = 0x7FFF; public const int16 MinValue = -0x8000; + public static int16 IMinMaxValue.MinValue => MinValue; + public static int16 IMinMaxValue.MaxValue => MaxValue; + public static int operator<=>(Self a, Self b) { return (SelfBase)a <=> (SelfBase)b; diff --git a/BeefLibs/corlib/src/Int32.bf b/BeefLibs/corlib/src/Int32.bf index 3dc6b6c2..405965db 100644 --- a/BeefLibs/corlib/src/Int32.bf +++ b/BeefLibs/corlib/src/Int32.bf @@ -3,7 +3,7 @@ using System.Globalization; namespace System { #unwarn - struct Int32 : int32, IInteger, ISigned, IHashable, IFormattable, IIsNaN, IParseable, IParseable + struct Int32 : int32, IInteger, ISigned, IHashable, IFormattable, IIsNaN, IParseable, IParseable, IMinMaxValue { public enum ParseError { @@ -16,6 +16,9 @@ namespace System public const int32 MaxValue = 0x7FFFFFFF; public const int32 MinValue = -0x80000000; + public static int32 IMinMaxValue.MinValue => MinValue; + public static int32 IMinMaxValue.MaxValue => MaxValue; + public static int operator<=>(Self a, Self b) { return (SelfBase)a <=> (SelfBase)b; diff --git a/BeefLibs/corlib/src/Int64.bf b/BeefLibs/corlib/src/Int64.bf index ac427ab7..e361cf19 100644 --- a/BeefLibs/corlib/src/Int64.bf +++ b/BeefLibs/corlib/src/Int64.bf @@ -3,7 +3,7 @@ using System.Globalization; namespace System { #unwarn - struct Int64 : int64, IInteger, ISigned, IFormattable, IHashable, IIsNaN, IParseable, IParseable + struct Int64 : int64, IInteger, ISigned, IFormattable, IHashable, IIsNaN, IParseable, IParseable, IMinMaxValue { public enum ParseError { @@ -16,6 +16,9 @@ namespace System public const int64 MaxValue = 0x7FFFFFFFFFFFFFFFL; public const int64 MinValue = -0x8000000000000000L; + public static int64 IMinMaxValue.MinValue => MinValue; + public static int64 IMinMaxValue.MaxValue => MaxValue; + public static int operator<=>(Int64 a, Int64 b) { return (SelfBase)a <=> (SelfBase)b; diff --git a/BeefLibs/corlib/src/Int8.bf b/BeefLibs/corlib/src/Int8.bf index cca29c69..e2967d66 100644 --- a/BeefLibs/corlib/src/Int8.bf +++ b/BeefLibs/corlib/src/Int8.bf @@ -3,7 +3,7 @@ using System.Globalization; namespace System { #unwarn - struct Int8 : int8, IInteger, ISigned, IHashable, IFormattable, IIsNaN, IParseable, IParseable + struct Int8 : int8, IInteger, ISigned, IHashable, IFormattable, IIsNaN, IParseable, IParseable, IMinMaxValue { public enum ParseError { @@ -16,6 +16,9 @@ namespace System public const int8 MaxValue = 0x7F; public const int8 MinValue = -0x80; + public static int8 IMinMaxValue.MinValue => MinValue; + public static int8 IMinMaxValue.MaxValue => MaxValue; + public static int operator<=>(Self a, Self b) { return (SelfBase)a <=> (SelfBase)b; diff --git a/BeefLibs/corlib/src/UInt.bf b/BeefLibs/corlib/src/UInt.bf index 3932fa22..5e9bd284 100644 --- a/BeefLibs/corlib/src/UInt.bf +++ b/BeefLibs/corlib/src/UInt.bf @@ -1,7 +1,7 @@ namespace System { #unwarn - struct UInt : uint, IInteger, IUnsigned, IHashable, IFormattable, IIsNaN, IParseable, IParseable + struct UInt : uint, IInteger, IUnsigned, IHashable, IFormattable, IIsNaN, IParseable, IParseable, IMinMaxValue { 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.MinValue => MinValue; + public static uint IMinMaxValue.MaxValue => MaxValue; + public bool IsNull() { return this == 0; diff --git a/BeefLibs/corlib/src/UInt16.bf b/BeefLibs/corlib/src/UInt16.bf index 15370e58..de951593 100644 --- a/BeefLibs/corlib/src/UInt16.bf +++ b/BeefLibs/corlib/src/UInt16.bf @@ -3,7 +3,7 @@ using System.Globalization; namespace System { #unwarn - struct UInt16 : uint16, IInteger, IUnsigned, IHashable, IFormattable, IIsNaN, IParseable, IParseable + struct UInt16 : uint16, IInteger, IUnsigned, IHashable, IFormattable, IIsNaN, IParseable, IParseable, IMinMaxValue { public enum ParseError { @@ -16,6 +16,9 @@ namespace System public const uint16 MaxValue = 0xFFFF; public const uint16 MinValue = 0; + public static uint16 IMinMaxValue.MinValue => MinValue; + public static uint16 IMinMaxValue.MaxValue => MaxValue; + public static int operator<=>(Self a, Self b) { return (SelfBase)a <=> (SelfBase)b; diff --git a/BeefLibs/corlib/src/UInt32.bf b/BeefLibs/corlib/src/UInt32.bf index e5ffe9e2..d174ae0c 100644 --- a/BeefLibs/corlib/src/UInt32.bf +++ b/BeefLibs/corlib/src/UInt32.bf @@ -3,7 +3,7 @@ using System.Globalization; namespace System { #unwarn - struct UInt32 : uint32, IInteger, IUnsigned, IHashable, IFormattable, IIsNaN, IParseable, IParseable + struct UInt32 : uint32, IInteger, IUnsigned, IHashable, IFormattable, IIsNaN, IParseable, IParseable, IMinMaxValue { public enum ParseError { @@ -16,6 +16,9 @@ namespace System public const uint32 MaxValue = 0xFFFFFFFFL; public const uint32 MinValue = 0; + public static uint32 IMinMaxValue.MinValue => MinValue; + public static uint32 IMinMaxValue.MaxValue => MaxValue; + public static int operator<=>(Self a, Self b) { return (SelfBase)a <=> (SelfBase)b; diff --git a/BeefLibs/corlib/src/UInt64.bf b/BeefLibs/corlib/src/UInt64.bf index 89d7e990..5301efa0 100644 --- a/BeefLibs/corlib/src/UInt64.bf +++ b/BeefLibs/corlib/src/UInt64.bf @@ -3,7 +3,7 @@ using System.Globalization; namespace System { #unwarn - struct UInt64 : uint64, IInteger, IUnsigned, IHashable, IIsNaN, IFormattable, IParseable, IParseable + struct UInt64 : uint64, IInteger, IUnsigned, IHashable, IIsNaN, IFormattable, IParseable, IParseable, IMinMaxValue { public enum ParseError { @@ -16,6 +16,9 @@ namespace System public const uint64 MaxValue = 0xFFFFFFFFFFFFFFFFUL; public const uint64 MinValue = 0; + public static uint64 IMinMaxValue.MinValue => MinValue; + public static uint64 IMinMaxValue.MaxValue => MaxValue; + public static int operator<=>(UInt64 a, UInt64 b) { return (SelfBase)a <=> (SelfBase)b; diff --git a/BeefLibs/corlib/src/UInt8.bf b/BeefLibs/corlib/src/UInt8.bf index b780fd50..cb7ec6b6 100644 --- a/BeefLibs/corlib/src/UInt8.bf +++ b/BeefLibs/corlib/src/UInt8.bf @@ -3,7 +3,7 @@ using System.Globalization; namespace System { #unwarn - struct UInt8 : uint8, IInteger, IUnsigned, IHashable, IFormattable, IIsNaN, IParseable, IParseable + struct UInt8 : uint8, IInteger, IUnsigned, IHashable, IFormattable, IIsNaN, IParseable, IParseable, IMinMaxValue { public enum ParseError { @@ -16,6 +16,9 @@ namespace System public const uint8 MaxValue = 0xFF; public const uint8 MinValue = 0; + public static uint8 IMinMaxValue.MinValue => MinValue; + public static uint8 IMinMaxValue.MaxValue => MaxValue; + public static int operator<=>(Self a, Self b) { return (SelfBase)a <=> (SelfBase)b; diff --git a/IDEHelper/Tests/src/Ints.bf b/IDEHelper/Tests/src/Ints.bf index f1ea9d3c..b33c17df 100644 --- a/IDEHelper/Tests/src/Ints.bf +++ b/IDEHelper/Tests/src/Ints.bf @@ -113,5 +113,29 @@ namespace Tests Uint64ParseErrorTest("+0x", .HexNumber); Uint64ParseErrorTest("+0X", .HexNumber); } + + public static void MinMaxTest(T expectedMinValue, T expectedMaxValue) + where T : IMinMaxValue + where int : operator T <=> T + { + Test.Assert(T.MinValue == expectedMinValue); + Test.Assert(T.MaxValue == expectedMaxValue); + } + + [Test] + public static void TestMinMax() + { + MinMaxTest(Int.MinValue, Int.MaxValue); + MinMaxTest(Int8.MinValue, Int8.MaxValue); + MinMaxTest(Int16.MinValue, Int16.MaxValue); + MinMaxTest(Int32.MinValue, Int32.MaxValue); + MinMaxTest(Int64.MinValue, Int64.MaxValue); + + MinMaxTest(UInt.MinValue, UInt.MaxValue); + MinMaxTest(UInt8.MinValue, UInt8.MaxValue); + MinMaxTest(UInt16.MinValue, UInt16.MaxValue); + MinMaxTest(UInt32.MinValue, UInt32.MaxValue); + MinMaxTest(UInt64.MinValue, UInt64.MaxValue); + } } }