Skip to content

Commit

Permalink
iflet match expressions test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
smaludzi committed Mar 23, 2020
1 parent cdc8ef4 commit 315c4cf
Show file tree
Hide file tree
Showing 14 changed files with 298 additions and 23 deletions.
3 changes: 2 additions & 1 deletion sample/sample414.nev
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ func main() -> int
}
else
{
print(90)
print(90);
assert(0)
};

0
Expand Down
2 changes: 1 addition & 1 deletion sample/sample415.nev
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func main() -> int
{
let o = Optional::Some(10);

print(getF(o)());
assert(getF(o)() == 10);

0
}
Expand Down
2 changes: 1 addition & 1 deletion sample/sample416.nev
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func main() -> int
let o = Optional::Some(10);
o = Optional::None;

print(getF(o)()());
assert(getF(o)()() == 1000);

0
}
Expand Down
18 changes: 9 additions & 9 deletions sample/sample417.nev
Original file line number Diff line number Diff line change
@@ -1,27 +1,27 @@

enum Number { One, Two, Three, Four }

func printNum(n : Number) -> string
func printNum(n : Number) -> int
{
if let (Number::One = n)
{
prints("Number::One\n")
prints("Number::One\n"); 1
}
else if let (Number::Two = n)
{
prints("Number::Two\n")
prints("Number::Two\n"); 2
}
else if let (Number::Three = n)
{
prints("Number::Three\n")
prints("Number::Three\n"); 3
}
else if let (Number::Four = n)
{
prints("Number::Four\n")
prints("Number::Four\n"); 4
}
else
{
prints("Unknown Number\n")
prints("Unknown Number\n"); 0
}
}

Expand All @@ -34,9 +34,9 @@ func main() -> int
{
let n = Number::One;

printNum(n);
printNum(Number::Two);
printNum(getNumber());
assert(printNum(n) == 1);
assert(printNum(Number::Two) == 2);
assert(printNum(getNumber()) == 3);

0
}
Expand Down
22 changes: 11 additions & 11 deletions sample/sample419.nev
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,27 @@ enum Number { One { n : int; },
Three,
Four }

func printNum(n : Number, m : int) -> string
func printNum(n : Number, m : int) -> int
{
if let (Number::One(n) = n)
{
prints("Number::One " + n + "\n")
prints("Number::One " + n + "\n"); n
}
else if let (Number::Two(n) = n)
{
prints("Number::Two " + n + "\n")
prints("Number::Two " + n + "\n"); n
}
else if (m == 3)
{
prints("Three " + m + "\n")
prints("Three " + m + "\n"); m
}
else if (m == 4)
{
prints("Four " + m + "\n")
prints("Four " + m + "\n"); m
}
else
{
prints("Other Number\n")
prints("Other Number\n"); 0
}
}

Expand All @@ -37,11 +37,11 @@ func main() -> int
{
let n = Number::One(1);

printNum(n, 1);
printNum(Number::Two(2), 2);
printNum(getNumber(), 3);
printNum(Number::Four, 4);
printNum(Number::Four, 5);
assert(printNum(n, 1) == 1);
assert(printNum(Number::Two(2), 2) == 2);
assert(printNum(getNumber(), 3) == 3);
assert(printNum(Number::Four, 4) == 4);
assert(printNum(Number::Four, 5) == 0);

0
}
Expand Down
24 changes: 24 additions & 0 deletions sample/sample420.nev
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@

enum Result { Ok { value : int; }, Err { msg : string; } }

func calc() -> Result
{
Result::Ok(1)
}

func main() -> int
{
let i = if let (Result::Ok = calc())
{
200
}
else
{
90
};

assert(i == 200);

0
}

21 changes: 21 additions & 0 deletions sample/sample421.nev
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@

enum Result { Ok { value : int; }, Err { msg : string; } }

func calc() -> Result
{
Result::Ok(1)
}

func main() -> int
{
let i = match calc()
{
Result::Ok -> 200;
Result::Err -> 90;
};

assert(i == 200);

0
}

21 changes: 21 additions & 0 deletions sample/sample422.nev
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@

enum Result { Ok { value : int; }, Err { msg : string; } }

func calc() -> Result
{
Result::Ok(200)
}

func main() -> int
{
let i = match calc()
{
Result::Ok(value) -> value;
Result::Err(msg) -> 90;
};

assert(i == 200);

0
}

24 changes: 24 additions & 0 deletions sample/sample423.nev
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@

enum Result { Ok { value : int; }, Err { msg : string; } }

func calc() -> Result
{
Result::Ok(200)
}

func main() -> int
{
let i = if let (Result::Ok(value) = calc())
{
value
}
else
{
90
};

assert(i == 200);

0
}

36 changes: 36 additions & 0 deletions sample/sample424.nev
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@

record Rect
{
x : int;
y : int;
}

enum Result { Ok { value : Rect; }, Err { msg : string; } }

func printRect(r : Rect) -> string
{
prints("x " + r.x + " y " + r.y + "\n")
}

func rect() -> Result
{
Result::Ok(Rect(200, 300))
}

func main() -> int
{
let i = if let (Result::Ok(rect) = rect())
{
printRect(rect);
rect.x + rect.y
}
else
{
90
};

assert(i == 500);

0
}

37 changes: 37 additions & 0 deletions sample/sample425.nev
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@

record Rect
{
x : int;
y : int;
printRect(r : Rect) -> string;
}

enum Result { Ok { value : Rect; }, Err { msg : string; } }

func printRect(r : Rect) -> string
{
prints("x " + r.x + " y " + r.y + "\n")
}

func rect() -> Result
{
Result::Ok(Rect(200, 300, printRect))
}

func main() -> int
{
let i = if let (Result::Ok(rect) = rect())
{
rect.printRect(rect);
rect.x + rect.y
}
else
{
90
};

assert(i == 500);

0
}

42 changes: 42 additions & 0 deletions sample/sample426.nev
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@

record Arr
{
arr[D] : string;
}

enum Result { Ok { value : Arr; }, Err { msg : string; } }

func printArr(arr[D] : string) -> int
{
let i = 0;

for (i = 0; i < D; i = i + 1)
{
prints(arr[i] + "\n")
};

0
}

func arr() -> Result
{
Result::Ok(Arr(["one", "two", "three", "four"] : string))
}

func main() -> int
{
let i = if let (Result::Ok(value) = arr())
{
printArr(value.arr);
1
}
else
{
0
};

assert(i == 1);

0
}

44 changes: 44 additions & 0 deletions sample/sample427.nev
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@

record Rect
{
x : int;
y : int;
}

enum Result { Ok { rects[D] : Rect; }, Err { msg : string; } }

func printRect(rects[D] : Rect) -> int
{
let i = 0;

for (i = 0; i < D; i = i + 1)
{
prints(rects[i].x + " " + rects[i].y + "\n")
};

0
}

func rects() -> Result
{
Result::Ok([Rect(100, 200), Rect(200, 300), Rect(300, 400)] : Rect)
}

func main() -> int
{
let i = 0;
let j = if let (Result::Ok(rects) = rects())
{
printRect(rects);
1
}
else
{
0
};

assert(j == 1);

0
}

Loading

0 comments on commit 315c4cf

Please sign in to comment.