1
0
Fork 0
mirror of https://github.com/beefytech/Beef.git synced 2025-06-21 09:27:59 +02:00
Beef/IDEHelper/Tests/src/Generics.bf

125 lines
1.8 KiB
Beef
Raw Normal View History

using System;
namespace Tests
{
class Generics
{
2020-05-16 08:21:34 -07:00
class ClassA : IDisposable, LibA.IVal
{
2020-05-16 08:21:34 -07:00
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()
{
}
}
static void DoDispose<T>(mut T val) where T : IDisposable
{
val.Dispose();
}
struct Disposer<T>
{
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();
}
}
2020-02-13 07:53:11 -08:00
[Test]
public static void TestGenericDelegates()
{
delegate void(String v) dlg = scope => StrMethod;
CallGenericDelegate(dlg);
CallGenericDelegate<String>(scope => StrMethod);
}
public static void CallGenericDelegate<T>(delegate void(T v) dlg)
{
}
public static void StrMethod(String v)
{
}
[Test]
public static void TestBasics()
{
2020-05-16 08:21:34 -07:00
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);
2020-05-16 08:21:34 -07:00
LibA.LibA0.Alloc<ClassA>();
LibA.LibA0.Alloc<ClassB>();
}
}
class ConstGenerics
{
public static float GetSum<TCount>(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);
}
}
}