2020-09-28 12:41:42 -07:00
|
|
|
#pragma warning disable 168
|
|
|
|
using System;
|
|
|
|
namespace Tests
|
|
|
|
{
|
|
|
|
class ConstExprs
|
|
|
|
{
|
|
|
|
enum EnumA
|
|
|
|
{
|
|
|
|
A,
|
|
|
|
B,
|
|
|
|
C
|
|
|
|
}
|
|
|
|
|
|
|
|
class ClassA<T, TSize> where TSize : const int
|
|
|
|
{
|
|
|
|
public int GetVal()
|
|
|
|
{
|
|
|
|
return TSize;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-31 07:56:57 -05:00
|
|
|
class ClassB<T, TSize> where TSize : const int8
|
2020-09-28 12:41:42 -07:00
|
|
|
{
|
2020-09-28 12:45:56 -07:00
|
|
|
ClassA<T, TSize> mVal = new ClassA<T, const TSize>() ~ delete _;
|
|
|
|
var mVal2 = new ClassA<T, const TSize + 100>() ~ delete _;
|
2021-12-31 07:56:57 -05:00
|
|
|
|
2020-09-28 12:41:42 -07:00
|
|
|
public int GetVal()
|
|
|
|
{
|
2021-12-31 07:56:57 -05:00
|
|
|
return mVal.GetVal();
|
2020-09-28 12:41:42 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
public int GetVal2()
|
|
|
|
{
|
|
|
|
return mVal2.GetVal();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class ClassC<TEnum> where TEnum : const EnumA
|
|
|
|
{
|
|
|
|
public int Test()
|
|
|
|
{
|
|
|
|
EnumA ea = TEnum;
|
|
|
|
if (TEnum == .A)
|
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-13 06:12:12 -05:00
|
|
|
struct TestRangedArray<T, TRange> where TRange : const var
|
|
|
|
{
|
|
|
|
[OnCompile(.TypeInit), Comptime]
|
|
|
|
static void TypeInit()
|
|
|
|
{
|
|
|
|
if (TRange is var)
|
|
|
|
return;
|
|
|
|
|
|
|
|
int rangeStart = 0;
|
|
|
|
int rangeEnd = 0;
|
|
|
|
|
|
|
|
if (ClosedRange range = TRange as ClosedRange?)
|
|
|
|
{
|
|
|
|
rangeStart = range.Start;
|
|
|
|
rangeEnd = range.End+1;
|
|
|
|
}
|
|
|
|
else if (Range range = TRange as Range?)
|
|
|
|
{
|
|
|
|
rangeStart = range.Start;
|
|
|
|
rangeEnd = range.End;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Compiler.EmitTypeBody(typeof(Self), scope $"""
|
|
|
|
public const String cError = "Invalid type: {TRange}";
|
|
|
|
""");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Compiler.EmitTypeBody(typeof(Self), scope $"""
|
|
|
|
public const int cRangeStart = {rangeStart};
|
|
|
|
public const int cRangeEnd = {rangeEnd};
|
|
|
|
public T[{rangeEnd-rangeStart}] mData;
|
|
|
|
""");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-28 12:41:42 -07:00
|
|
|
[Test]
|
|
|
|
public static void TestBasics()
|
|
|
|
{
|
|
|
|
ClassB<float, const 123> cb = scope .();
|
|
|
|
Test.Assert(cb.GetVal() == 123);
|
|
|
|
Test.Assert(cb.GetVal2() == 223);
|
|
|
|
|
2021-12-31 07:56:57 -05:00
|
|
|
ClassB<float, const -45> cb2 = scope .();
|
|
|
|
Test.Assert(cb2.GetVal() == -45);
|
|
|
|
Test.Assert(cb2.GetVal2() == 55);
|
|
|
|
|
2020-09-28 12:41:42 -07:00
|
|
|
ClassC<const EnumA.A> cc = scope .();
|
|
|
|
Test.Assert(cc.Test() == 1);
|
2024-02-13 06:12:12 -05:00
|
|
|
|
|
|
|
Test.Assert(TestRangedArray<int32, -3...3>.cRangeEnd - TestRangedArray<int32, -3...3>.cRangeStart == 7);
|
|
|
|
Test.Assert(TestRangedArray<int32, -3...>.cError == "Invalid type: -3...^1");
|
2020-09-28 12:41:42 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|