2024-11-20 11:33:28 -05:00
|
|
|
#pragma warning disable 168
|
|
|
|
|
2021-06-19 09:29:36 -07:00
|
|
|
using System;
|
|
|
|
|
|
|
|
namespace Tests
|
|
|
|
{
|
|
|
|
class Switches
|
|
|
|
{
|
2024-11-01 19:01:21 -04:00
|
|
|
enum Shape
|
|
|
|
{
|
|
|
|
case Rectangle(int x, int y, int width, int height);
|
|
|
|
case Circle(int x, int y, int radius);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int Switch0(Result<int> res)
|
2021-06-19 09:29:36 -07:00
|
|
|
{
|
|
|
|
switch (res)
|
|
|
|
{
|
|
|
|
case .Ok(let a):
|
|
|
|
return 0;
|
|
|
|
case .Err(let b):
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
2024-11-01 19:01:21 -04:00
|
|
|
|
|
|
|
static int Switch1(Shape shape)
|
|
|
|
{
|
|
|
|
switch (shape)
|
|
|
|
{
|
2024-11-01 19:09:30 -04:00
|
|
|
case .Circle(let x, let y, let radius) when x == 10:
|
2024-11-01 19:01:21 -04:00
|
|
|
return 12;
|
|
|
|
default:
|
|
|
|
return 23;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2025-02-04 10:23:22 -08:00
|
|
|
enum ETest
|
|
|
|
{
|
|
|
|
case A(int a);
|
|
|
|
case B(float f);
|
|
|
|
case C;
|
|
|
|
}
|
|
|
|
|
2024-11-01 19:01:21 -04:00
|
|
|
[Test]
|
|
|
|
public static void TestBasics()
|
|
|
|
{
|
|
|
|
Result<int> val0 = .Ok(1);
|
|
|
|
Test.Assert(Switch0(val0) == 0);
|
|
|
|
val0 = .Err;
|
|
|
|
Test.Assert(Switch0(val0) == 1);
|
|
|
|
|
|
|
|
Shape shape = .Circle(10, 20, 30);
|
|
|
|
Test.Assert(Switch1(shape) == 12);
|
2024-11-20 11:33:28 -05:00
|
|
|
|
|
|
|
int val = 123;
|
|
|
|
int result = 0;
|
|
|
|
switch (val)
|
|
|
|
{
|
|
|
|
case 0:
|
|
|
|
result = 1;
|
|
|
|
default:
|
|
|
|
SWITCH2:
|
|
|
|
switch (val)
|
|
|
|
{
|
|
|
|
case 2:
|
|
|
|
result = 2;
|
|
|
|
default:
|
|
|
|
result = 3;
|
|
|
|
break SWITCH2;
|
|
|
|
}
|
|
|
|
|
|
|
|
result = 4;
|
|
|
|
}
|
|
|
|
Test.Assert(result == 4);
|
2025-02-04 10:23:22 -08:00
|
|
|
|
|
|
|
result = 0;
|
|
|
|
const int constVal = 123;
|
|
|
|
switch (constVal)
|
|
|
|
{
|
|
|
|
case 10:
|
|
|
|
result = 1;
|
|
|
|
case 123:
|
|
|
|
result = 2;
|
|
|
|
default:
|
|
|
|
result = 3;
|
|
|
|
}
|
|
|
|
Test.Assert(result == 2);
|
|
|
|
|
|
|
|
result = 99;
|
|
|
|
const Result<int> iResult = .Err;
|
|
|
|
bool eq = iResult case .Ok(ref result);
|
|
|
|
Test.Assert(result == 99);
|
|
|
|
|
2025-02-22 09:56:39 -08:00
|
|
|
if (iResult not case .Ok(var result2))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Test.FatalError();
|
|
|
|
}
|
|
|
|
Test.Assert(result2 == 0);
|
|
|
|
|
2025-02-04 10:23:22 -08:00
|
|
|
const ETest t = .B(234.5f);
|
|
|
|
switch (t)
|
|
|
|
{
|
|
|
|
case .A(let a):
|
|
|
|
result = 1;
|
|
|
|
case .B(let b):
|
|
|
|
result = (.)b;
|
|
|
|
case .C:
|
|
|
|
result = 3;
|
|
|
|
default:
|
|
|
|
result = 4;
|
|
|
|
}
|
|
|
|
Test.Assert(result == 234);
|
2024-11-01 19:01:21 -04:00
|
|
|
}
|
2021-06-19 09:29:36 -07:00
|
|
|
}
|
|
|
|
}
|