1
0
Fork 0
mirror of https://github.com/beefytech/Beef.git synced 2025-06-18 16:10:26 +02:00
Beef/IDEHelper/Tests/src/Indexers.bf

108 lines
1.4 KiB
Beef
Raw Normal View History

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
{
}
struct StructA
{
2021-11-15 15:01:48 -08:00
public int mA;
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-05-13 16:28:42 -07:00
[Test]
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);
StructA sa = default;
let sa2 = sa;
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
}
}
}