Skip to content

Commit

Permalink
[variants] More unboxed variant tests (#217)
Browse files Browse the repository at this point in the history
  • Loading branch information
btwj authored Feb 8, 2024
1 parent 25df72c commit 4ed2cc1
Show file tree
Hide file tree
Showing 59 changed files with 1,184 additions and 1 deletion.
18 changes: 18 additions & 0 deletions test/fail/eq00.v3
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
//@execute =35

type U {
case A(x: int);
case B(y: int);
}

def f() -> (U, U) {
return (U.A(12), U.B(23));
}

def main() -> int {
var a = f();
var b = f();

if (a != b) return -1;
return U.A.!(a.0).x + U.B.!(a.1).y;
}
2 changes: 1 addition & 1 deletion test/gc/variants.gc
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@
../variants/unboxed01.v3
../variants/unboxed02.v3
../variants/unboxed03.v3
../variants/unboxed_mixed_array00.v3
../variants/ub_mixed_array00.v3
../variants/v01.v3
../variants/v13.v3
../variants/v14.v3
Expand Down
8 changes: 8 additions & 0 deletions test/variants/ub_array03.v3
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
//@execute 0=-11; 1=-51111
type U(x: int, y: int, z: int) #unboxed { }
def x = [U(11, 22, 44), U(3333, 1111, 55555)];

def main(a: int) -> int {
var u = x[a];
return u.x + u.y - u.z;
}
30 changes: 30 additions & 0 deletions test/variants/ub_cast00.v3
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
//@execute 0=12;1=78;2=123
type A00 #unboxed {
case X(x: i32, y: i64, z: i32);
case Y(x: i32, y: i32);
case Z(y: i32);
}

def x = A00.!(A00.X(12, 34, 56));
def y = A00.!(A00.Y(78, 90));
def z = A00.!(A00.Z(123));

def f(a: int) -> A00 {
match (a) {
0 => return x;
1 => return y;
2 => return z;
}
return A00.Z(123);
}

def main(a: int) -> int {
if (f(a) != f(a)) return -1;

match (a) {
0 => return A00.X.!(f(a)).x;
1 => return A00.Y.!(f(a)).x;
2 => return A00.Z.!(f(a)).y;
}
return 0;
}
30 changes: 30 additions & 0 deletions test/variants/ub_cell00.v3
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
//@execute 0=100; 1=101
type MutCell<T> #unboxed {
case Array(x: Array<T>, i: int) { }
case Func(getf: void -> T, setf: T -> void) { }

def get() -> T {
match (this) {
Array(x, i) => return x[i];
Func(getf, setf) => return getf();
}
}
def set(v: T) {
match (this) {
Array(x, i) => x[i] = v;
Func(getf, setf) => return setf(v);
}
}
}
var y = 99;
def get_y() -> int { return y; }
def set_y(v: int) { y = v; }
def cells = [
MutCell<int>.Array([0], 0),
MutCell<int>.Func(get_y, set_y)
];
def main(a: int) -> int {
var cell = cells[a];
cell.set(a + 100);
return cell.get();
}
21 changes: 21 additions & 0 deletions test/variants/ub_cell01.v3
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
//@execute 0=123; 1=-1
type Cell #unboxed {
case Array(x: Array<int>);
case Empty;

def get() -> int {
match (this) {
Array(x) => return x[0];
Empty => return -1;
}
}
}
def cells = [
Cell.Array([123]),
Cell.Empty
];

def main(a: int) -> int {
var cell = cells[a];
return cell.get();
}
17 changes: 17 additions & 0 deletions test/variants/ub_cell02.v3
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
//@execute 0=123
type Cell #unboxed {
case Array(x: int);
case Empty;

def get() -> int {
match (this) {
Array(x) => return x;
Empty => return -1;
}
}
}

def main(a: int) -> int {
def cell = Cell.Array(123);
return cell.get();
}
22 changes: 22 additions & 0 deletions test/variants/ub_cell03.v3
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
//@execute 0=123
type Cell #unboxed {
case Array(x: Array<int>);
case Empty;
}

def get(c: Cell) -> int {
match (c) {
Array(x) => return x[0];
Empty => return -1;
}
}

def cells = [
Cell.Array([123]),
Cell.Empty
];

def main(a: int) -> int {
var cell = cells[a];
return get(cell);
}
23 changes: 23 additions & 0 deletions test/variants/ub_eq00.v3
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
//@execute 0=true;1=false;2=true;3=false
type A00 #unboxed {
case X;
case Y(x: int);
}

def eq = A00.==;

def main(a: int) -> bool {
var a00_x: A00 = A00.X;
var a00_x2: A00 = A00.X;
var a00_y: A00 = A00.Y(12);
var a00_y2: A00 = A00.Y(12);
var a00_y3: A00 = A00.Y(13);

match (a) {
0 => return eq(a00_x, a00_x2);
1 => return eq(a00_x, a00_y);
2 => return eq(a00_y, a00_y2);
3 => return eq(a00_y, a00_y3);
}
return false;
}
27 changes: 27 additions & 0 deletions test/variants/ub_eq01.v3
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
//@execute =0
type A00 #unboxed {
case X;
case Y(x: int);
}

def eq(a: A00, b: A00) -> bool {
return a == b;
}

def f() -> A00 {
return A00.X;
}

def main() -> int {
var x: A00 = A00.X;
if (!eq(x, A00.X)) return -1;

x = A00.Y(12);
if (eq(x, A00.X)) return -2;
if (!eq(x, A00.Y(12))) return -3;

x = f();
if (!eq(x, A00.X)) return -4;

return 0;
}
14 changes: 14 additions & 0 deletions test/variants/ub_eq03.v3
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
//@execute 0=true
type A00 #unboxed {
case X(x: i32, y: i64, z: i32);
case Y(x: i32, y: i32);
case Z(y: i64);
}

def arr = [A00.X(12, 34, 56), A00.Y(12, 34), A00.Z(12)];

def main(a: int) -> bool {
if (arr[0] != arr[0]) return false;

return true;
}
17 changes: 17 additions & 0 deletions test/variants/ub_eq04.v3
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
//@execute 0=true;1=false;2=true;3=false;4=true;5=false
type A #unboxed {
case X(b: B);
case Y;
}

type B {
case X(x: int);
case Y(y: int);
}

def arr1 = [A.X(B.X(12)), A.X(B.X(12)), A.X(B.Y(34)), A.X(B.Y(56)), A.Y, A.Y];
def arr2 = [A.X(B.X(12)), A.X(B.X(34)), A.X(B.Y(34)), A.Y, A.Y, A.X(B.X(7))];

def main(a: int) -> bool {
return arr1[a] == arr2[a];
}
17 changes: 17 additions & 0 deletions test/variants/ub_eq05.v3
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
//@execute 0=true;1=false;2=true;3=false;4=true;5=false
type A {
case X(b: B);
case Y(y: int) #unboxed;
}

type B {
case X(x: int);
case Y(y: int);
}

def arr1 = [A.X(B.X(12)), A.X(B.X(12)), A.X(B.Y(34)), A.X(B.Y(56)), A.Y(99), A.Y(99)];
def arr2 = [A.X(B.X(12)), A.X(B.X(34)), A.X(B.Y(34)), A.Y(99), A.Y(99), A.X(B.X(7))];

def main(a: int) -> bool {
return arr1[a] == arr2[a];
}
15 changes: 15 additions & 0 deletions test/variants/ub_eq06.v3
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
//@execute 0=true;1=true;2=true;3=true;4=true;5=true
type T {
case A(x: int);
case B(y: int);
case C(a: Array<int>) #unboxed;
}

def a: Array<int> = [];
def b: Array<int> = [1, 2];
def arr1 = [T.A(0), T.A(10), T.B(20), T.B(30), T.C(a), T.C(b)];
def arr2 = [T.A(0), T.A(10), T.B(20), T.B(30), T.C(a), T.C(b)];

def main(a: int) -> bool {
return arr1[a] == arr2[a];
}
15 changes: 15 additions & 0 deletions test/variants/ub_eq07.v3
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
//@execute 0=false;1=false;2=false;3=false;4=false;5=false
type T {
case A(x: int);
case B(y: int);
case C(a: Array<int>) #unboxed;
}

def a: Array<int> = [];
def b: Array<int> = [1, 2];
def arr1 = [T.A(0), T.A(10), T.B(20), T.B(30), T.C(a), T.C(b)];
def arr2 = [T.B(20), T.B(30), T.C(a), T.C(b), T.A(0), T.A(10)];

def main(a: int) -> bool {
return arr1[a] == arr2[a];
}
22 changes: 22 additions & 0 deletions test/variants/ub_fun00.v3
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
//@execute 0=123;1=456
type A00 #unboxed {
case X {
def val() -> i32 { return 123; }
}
case Y(a: i32) {
def val() -> i32 { return a; }
}

def val() -> i32;
}

def main(a: int) -> int {
var a00_x: A00 = A00.X;
var a00_y: A00 = A00.Y(456);

match (a) {
0 => return a00_x.val();
1 => return a00_y.val();
_ => return 0;
}
}
12 changes: 12 additions & 0 deletions test/variants/ub_fun01.v3
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
//@execute =35
type A00(x: int, y: int) #unboxed {
def f() -> int {
return x + y;
}
}

def main() -> i32 {
var x = A00(12, 23);

return x.f();
}
29 changes: 29 additions & 0 deletions test/variants/ub_fun02.v3
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
//@execute 0=123;1=456;2=789;3=345;4=-456
type A00 #unboxed {
case X {
def val() -> i32 { return 123; }
def f() -> i32 { return 345; }
def g() -> i32 { return 789; }
}
case Y(a: i32) {
def val() -> i32 { return a; }
def f() -> i32 { return -a; }
}

def val() -> i32;
def f() -> i32;
}

def main(a: int) -> i32 {
var a00_x: A00 = A00.X;
var a00_y: A00 = A00.Y(456);

match (a) {
0 => return a00_x.val();
1 => return a00_y.val();
2 => return A00.X.!(a00_x).g();
3 => return a00_x.f();
4 => return a00_y.f();
_ => return 0;
}
}
Loading

0 comments on commit 4ed2cc1

Please sign in to comment.