2020-05-13 16:28:42 -07:00
|
|
|
using System;
|
|
|
|
|
|
|
|
namespace Tests
|
|
|
|
{
|
|
|
|
class Indexers
|
|
|
|
{
|
|
|
|
class ClassA
|
|
|
|
{
|
|
|
|
public virtual int this[int index]
|
|
|
|
{
|
|
|
|
get
|
|
|
|
{
|
|
|
|
return 123;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class ClassB : ClassA
|
|
|
|
{
|
|
|
|
public override int this[int index]
|
|
|
|
{
|
|
|
|
get
|
|
|
|
{
|
|
|
|
return 234;
|
|
|
|
}
|
|
|
|
}
|
2021-11-15 15:01:48 -08:00
|
|
|
|
|
|
|
public int BaseIndex
|
|
|
|
{
|
|
|
|
get
|
|
|
|
{
|
|
|
|
return base[2];
|
|
|
|
}
|
|
|
|
}
|
2020-05-13 16:28:42 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
class ClassC : ClassB
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-08-29 14:18:05 -07:00
|
|
|
struct StructA
|
|
|
|
{
|
2021-11-15 15:01:48 -08:00
|
|
|
public int mA;
|
2020-08-29 14:18:05 -07:00
|
|
|
|
|
|
|
public int this[int index]
|
|
|
|
{
|
|
|
|
get
|
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
get mut
|
|
|
|
{
|
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
}
|
2021-11-15 15:01:48 -08:00
|
|
|
|
|
|
|
public int this[params int[] indices]
|
|
|
|
{
|
|
|
|
get mut
|
|
|
|
{
|
|
|
|
int total = 0;
|
|
|
|
for (var i in indices)
|
|
|
|
total += i;
|
|
|
|
mA += total;
|
|
|
|
return total;
|
|
|
|
}
|
|
|
|
|
|
|
|
set mut
|
|
|
|
{
|
|
|
|
for (var i in indices)
|
|
|
|
mA += i;
|
|
|
|
mA += value * 1000;
|
|
|
|
}
|
|
|
|
}
|
2020-08-29 14:18:05 -07:00
|
|
|
}
|
|
|
|
|
2020-05-13 16:28:42 -07:00
|
|
|
[Test]
|
2020-10-22 17:25:19 -07:00
|
|
|
public static void TestBasics()
|
2020-05-13 16:28:42 -07:00
|
|
|
{
|
|
|
|
ClassB cc = scope ClassC();
|
|
|
|
ClassB cb = cc;
|
|
|
|
ClassA ca = cb;
|
|
|
|
int value = cc[0];
|
|
|
|
Test.Assert(value == 234);
|
|
|
|
value = cb[0];
|
|
|
|
Test.Assert(value == 234);
|
|
|
|
value = ca[0];
|
|
|
|
Test.Assert(value == 234);
|
2021-11-15 15:01:48 -08:00
|
|
|
value = cb.BaseIndex;
|
|
|
|
Test.Assert(value == 123);
|
2020-08-29 14:18:05 -07:00
|
|
|
|
|
|
|
StructA sa = default;
|
|
|
|
let sa2 = sa;
|
2020-10-22 17:25:19 -07:00
|
|
|
|
2020-08-29 14:18:05 -07:00
|
|
|
Test.Assert(sa[0] == 2);
|
|
|
|
Test.Assert(sa2[0] == 1);
|
2021-11-15 15:01:48 -08:00
|
|
|
|
|
|
|
sa[3, 4, 5] = 9;
|
|
|
|
Test.Assert(sa.mA == 9012);
|
|
|
|
int a = sa[3, 4, 5];
|
|
|
|
Test.Assert(a == 12);
|
|
|
|
Test.Assert(sa.mA == 9024);
|
2020-05-13 16:28:42 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|