#pragma warning disable 168 using System; using System.Collections; namespace LibA { extension LibA1 : IDisposable { public void Dispose() { } public static int operator<=>(Self lhs, Self rhs) { return 0; } } } namespace Tests { class Generics { class ClassA : IDisposable, LibA.IVal { int LibA.IVal.Val { get { return 123; } set { } } void IDisposable.Dispose() { } } class ClassB : IDisposable, LibA.IVal { public int Val { get { return 234; } set { } } public void Dispose() { } } class Singleton where T : Singleton { public static T mInstance; protected this() { mInstance = (T)this; } } class ClassC : Singleton { } class ClassD { public static int operator<=>(Self lhs, Self rhs) { return 0; } } static void DoDispose(mut T val) where T : IDisposable { val.Dispose(); } struct Disposer { static void UseDispose(IDisposable disp) { } static void DoDisposeA(mut T val) where T : IDisposable { val.Dispose(); UseDispose(val); } static void DoDisposeB(mut T val) where T : IDisposable { val.Dispose(); } } [Test] public static void TestGenericDelegates() { delegate void(String v) dlg = scope => StrMethod; CallGenericDelegate(dlg); CallGenericDelegate(scope => StrMethod); } public static void CallGenericDelegate(delegate void(T v) dlg) { } public static void StrMethod(String v) { } public static int MethodA(T val) where T : var { return 1; } public static int MethodA(T val) where T : struct { return 2; } public static int MethodA(T val) where T : enum { int val2 = (int)val; T val3 = 0; return 3; } public static int MethodA(T val) where T : interface { return 4; } public struct Entry { public static int operator<=>(Entry lhs, Entry rhs) { return 0; } } public static void Alloc0() where T : new, delete, IDisposable { alloctype(T) val = new T(); val.Dispose(); delete val; } public static void Alloc1() where T : new, delete, IDisposable, Object { alloctype(T) val = new T(); T val2 = val; val.Dispose(); delete val; } public static void Alloc2() where T : new, delete, IDisposable, struct { alloctype(T) val = new T(); T* val2 = val; val2.Dispose(); delete val; } public class ClassE { public static Self Instance = new ClassE() ~ delete _; public int CreateSystem() { return 999; } } public void TestCast(T val) where T : class where TI : interface { TI iface = val as TI; } public void MethodA(List list) { } public void MethodA(Span list) { } public void MethodB() { List list = null; MethodA(list); } public static void MethodC() { } public static void MethodD(delegate void() dlg) { } public static void MethodD(delegate void(T1) dlg) { } static T2 MethodE(T val) where T : concrete, IEnumerable where T2 : operator T2 + T2 { T2 total = default; for (var v in val) { total += v; } return total; } static int MethodF(IEnumerable val) { return 0; } public static TResult Sum(this T it, TDlg dlg) where T: concrete, IEnumerable where TDlg: delegate TResult(TElem) where TResult: operator TResult + TResult { var result = default(TResult); for(var elem in it) result += dlg(elem); return result; } [Test] public static void TestBasics() { MethodD(scope => MethodC); List list = scope .(); list.Sort(); List floatList = scope .() {1, 2, 3}; ClassA ca = scope .(); ClassB cb = scope .(); Test.Assert(LibA.LibA0.GetVal(ca) == 123); Test.Assert(LibA.LibA0.GetVal(cb) == 234); LibA.LibA0.Dispose(ca); LibA.LibA0.Dispose(cb); LibA.LibA0.Alloc(); LibA.LibA0.Alloc(); IDisposable iDisp = null; Test.Assert(MethodA("") == 1); Test.Assert(MethodA(1.2f) == 2); Test.Assert(MethodA(TypeCode.Boolean) == 3); Test.Assert(MethodA(iDisp) == 4); ClassC cc = scope .(); Test.Assert(ClassC.mInstance == cc); LibA.LibA1 la1 = scope .(); LibA.LibA1 la1b = scope .(); LibA.LibA2.DoDispose(la1); Test.Assert(!LibA.LibA2.DoDispose2(la1)); Test.Assert(la1 == la1b); Test.Assert(!LibA.LibA2.CheckEq(la1, la1b)); ClassD cd = scope .(); ClassD cd2 = scope .(); Test.Assert(LibA.LibA2.CheckEq(cd, cd2)); Test.Assert(ClassE.Instance.CreateSystem() == 999); /*IEnumerable ie = floatList; Test.Assert( [IgnoreErrors(true)] { Test.Assert(MethodE(ie) == 8); true } == false);*/ Test.Assert(MethodE(floatList) == 6); Test.Assert(MethodF(floatList) == 0); Test.Assert(floatList.Sum((x) => x * 2) == 12); } } class ConstGenerics { public static float GetSum(float[TCount] vals) where TCount : const int { float total = 0; for (int i < vals.Count) total += vals[i]; return total; } [Test] public static void TestBasics() { float[5] fVals = .(10, 20, 30, 40, 50); float totals = GetSum(fVals); Test.Assert(totals == 10+20+30+40+50); } public static mixin TransformArray(Input[InputSize] array, delegate void(Input, ref Output) predicate) where InputSize : const int where Output : new, class { Output[2] output = default; for (int i = 0; i < array.Count; i++) { output[i] = scope:mixin Output(); predicate(array[i], ref output[i]); } output } [Test] public static void TestSizedArrays() { int[2] arr = .(2, 4); delegate void(int, ref String) toString = scope (i, str) => { i.ToString(str); }; List l2 = scope .(); l2.Add(TransformArray!(arr, toString)); Test.Assert(l2.Front[0] == "2"); Test.Assert(l2.Front[1] == "4"); } } }