2021-01-19 05:40:57 -08:00
|
|
|
#pragma warning disable 168
|
|
|
|
|
2020-12-06 09:06:14 -08:00
|
|
|
using System;
|
2021-05-31 07:01:56 -07:00
|
|
|
using System.Collections;
|
2021-07-21 10:13:26 -07:00
|
|
|
using System.Diagnostics;
|
2020-12-06 09:06:14 -08:00
|
|
|
|
|
|
|
namespace Tests
|
|
|
|
{
|
|
|
|
class Loops
|
|
|
|
{
|
2021-01-19 05:40:57 -08:00
|
|
|
struct StructA
|
|
|
|
{
|
|
|
|
public int32 mA;
|
|
|
|
public int32 mB;
|
|
|
|
}
|
|
|
|
|
2021-05-31 07:01:56 -07:00
|
|
|
class EnumeratorTest : IEnumerator<int32>, IDisposable
|
|
|
|
{
|
|
|
|
public int mDispCount;
|
|
|
|
|
|
|
|
public void Dispose()
|
|
|
|
{
|
|
|
|
mDispCount++;
|
|
|
|
}
|
|
|
|
|
|
|
|
public Result<int32> GetNext()
|
|
|
|
{
|
|
|
|
return .Err;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-16 09:06:31 -07:00
|
|
|
static int sGetValCount = 0;
|
|
|
|
|
|
|
|
static int GetVal()
|
|
|
|
{
|
2021-07-20 12:28:23 -07:00
|
|
|
return 10 + sGetValCount++ / 2;
|
2021-07-16 09:06:31 -07:00
|
|
|
}
|
|
|
|
|
2020-12-06 09:06:14 -08:00
|
|
|
[Test]
|
|
|
|
public static void TestBasics()
|
|
|
|
{
|
|
|
|
while (false)
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
2021-01-19 05:40:57 -08:00
|
|
|
|
|
|
|
StructA[0] zeroLoop = default;
|
|
|
|
for (var val in zeroLoop)
|
|
|
|
{
|
|
|
|
StructA sa = val;
|
|
|
|
int idx = @val;
|
|
|
|
}
|
2021-05-31 07:01:56 -07:00
|
|
|
|
|
|
|
var e = scope EnumeratorTest();
|
|
|
|
for (var val in e)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
Test.Assert(e.mDispCount == 1);
|
|
|
|
for (var val in e)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
Test.Assert(e.mDispCount == 2);
|
|
|
|
TestEnumerator1(e);
|
|
|
|
Test.Assert(e.mDispCount == 3);
|
|
|
|
TestEnumerator2(e);
|
|
|
|
Test.Assert(e.mDispCount == 4);
|
2021-07-16 09:06:31 -07:00
|
|
|
|
|
|
|
int iterations = 0;
|
|
|
|
for (int i < GetVal())
|
|
|
|
{
|
|
|
|
iterations++;
|
|
|
|
}
|
2021-07-20 12:28:23 -07:00
|
|
|
Test.Assert(iterations == 19);
|
|
|
|
Test.Assert(sGetValCount == 20);
|
2021-07-21 07:48:37 -07:00
|
|
|
|
|
|
|
int total = 0;
|
|
|
|
for (int i in 1..<10)
|
2021-07-21 10:13:26 -07:00
|
|
|
{
|
|
|
|
if (i == 5)
|
|
|
|
Test.Assert(total == 1+2+3+4);
|
2021-07-21 07:48:37 -07:00
|
|
|
total += i;
|
2021-07-21 10:13:26 -07:00
|
|
|
}
|
2021-07-21 07:48:37 -07:00
|
|
|
Test.Assert(total == 1+2+3+4+5+6+7+8+9);
|
|
|
|
|
|
|
|
total = 0;
|
|
|
|
for (int i in 1...10)
|
2021-07-21 10:13:26 -07:00
|
|
|
{
|
|
|
|
if (i == 5)
|
|
|
|
Test.Assert(total == 1+2+3+4);
|
2021-07-21 07:48:37 -07:00
|
|
|
total += i;
|
2021-07-21 10:13:26 -07:00
|
|
|
}
|
2021-07-21 07:48:37 -07:00
|
|
|
Test.Assert(total == 1+2+3+4+5+6+7+8+9+10);
|
2021-07-21 10:13:26 -07:00
|
|
|
|
|
|
|
total = 0;
|
|
|
|
for (int i in (1..<10).Reversed)
|
|
|
|
{
|
|
|
|
if (i == 5)
|
|
|
|
Test.Assert(total == 9+8+7+6);
|
|
|
|
total += i;
|
|
|
|
|
|
|
|
}
|
|
|
|
Test.Assert(total == 9+8+7+6+5+4+3+2+1);
|
|
|
|
|
|
|
|
total = 0;
|
|
|
|
for (int i in (1...10).Reversed)
|
|
|
|
{
|
|
|
|
if (i == 5)
|
|
|
|
Test.Assert(total == 10+9+8+7+6);
|
|
|
|
total += i;
|
|
|
|
|
|
|
|
}
|
|
|
|
Test.Assert(total == 10+9+8+7+6+5+4+3+2+1);
|
|
|
|
|
|
|
|
Test.Assert(!(1...3).Contains(0));
|
|
|
|
Test.Assert((1...3).Contains(1));
|
|
|
|
Test.Assert((1...3).Contains(2));
|
|
|
|
Test.Assert((1...3).Contains(3));
|
|
|
|
Test.Assert(!(1...3).Contains(4));
|
|
|
|
|
|
|
|
Test.Assert(!(1..<3).Contains(0));
|
|
|
|
Test.Assert((1..<3).Contains(1));
|
|
|
|
Test.Assert((1..<3).Contains(2));
|
|
|
|
Test.Assert(!(1..<3).Contains(3));
|
|
|
|
|
|
|
|
Test.Assert((1...3).Contains(1...3));
|
|
|
|
Test.Assert((1...3).Contains(1...2));
|
|
|
|
Test.Assert(!(1...3).Contains(1...4));
|
|
|
|
Test.Assert((1...3).Contains(2..<3));
|
|
|
|
Test.Assert((1...3).Contains(2..<4));
|
|
|
|
Test.Assert(!(1...3).Contains(2..<5));
|
|
|
|
Test.Assert(!(1..<3).Contains(1...3));
|
|
|
|
Test.Assert((1..<3).Contains(1..<3));
|
|
|
|
Test.Assert(!(1..<3).Contains(1..<4));
|
|
|
|
|
2021-05-31 07:01:56 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
public static void TestEnumerator1(EnumeratorTest e)
|
|
|
|
{
|
|
|
|
for (var val in e)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void TestEnumerator2(EnumeratorTest e)
|
|
|
|
{
|
|
|
|
for (var val in e)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2020-12-06 09:06:14 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|