diff --git a/test/stdlib/Stream.fram b/test/stdlib/Stream.fram new file mode 100644 index 00000000..4d10561a --- /dev/null +++ b/test/stdlib/Stream.fram @@ -0,0 +1,113 @@ +import /List +import /Stream +import open Testing + +let _ = +testSuite "conversions" (fn _ => + testCase "fromList > toList" (fn _ => + let xs = [1, 2, 3, 4] in + assertEq xs (Stream.toList (Stream.fromList xs))); + + testCase "toList > fromList" (fn _ => + let xs = Stream.cons 10 (Stream.cons 20 Stream.empty) in + assertEq xs (Stream.fromList (Stream.toList xs)))); + +testCase "unfold" (fn _ => + let f x = if 2 == x then None else Some (x, 1 + x) in + assertEq (Stream.fromList [0, 1]) (Stream.unfold 0 f)); + +testSuite "isEmpty" (fn _ => + testCase "true" (fn _ => + assertTrue (Stream.isEmpty (Stream.empty {X=Int}))); + + testCase "false" (fn _ => + assertFalse (Stream.isEmpty (Stream.fromList [1])))); + +testCase "singleton" (fn _ => + assertEq (Stream.fromList [1]) (Stream.singleton 1)); + +testCase "cons" (fn _ => + assertEq + (Stream.cons 3 (Stream.fromList [2, 1])) + (Stream.fromList [3, 2, 1])); + +testSuite "uncons" (fn _ => + testCase "non empty" (fn _ => + assertTrue + match Stream.uncons (Stream.fromList [1, 2, 3]) with + | Some (x, _) => 1 == x + | None => False + end); + + testCase "empty" (fn _ => + assertTrue + match Stream.uncons (Stream.empty {X=Int}) with + | None => True + | _ => False + end)); + +testCase "map" (fn _ => + assertEq + (Stream.fromList ["1", "2"]) + (Stream.map (fn x => (x : Int).toString) (Stream.fromList [1, 2]))); + +testCase "append" (fn _ => + assertEq + (Stream.append (Stream.fromList [1, 2, 3]) (Stream.fromList [4, 5, 6])) + (Stream.fromList [1, 2, 3, 4, 5, 6])); + +testCase "concatMap" (fn _ => + assertEq + (Stream.fromList [1, 2, 3] + >.concatMap (fn (x : Int) => Stream.fromList [x + 1, x - 1])) + (Stream.fromList [2, 0, 3, 1, 4, 2])); + +testCase "takeWhile" (fn _ => + assertEq + (Stream.takeWhile + (fn (x : Int) => x <= 2) + (Stream.fromList [1, 2, 3, 4, 1])) + (Stream.fromList [1, 2])); + +testCase "filter" (fn _ => + assertEq + (Stream.filter (fn (x : Int) => x <= 2) (Stream.fromList [1, 2, 3, 4, 1])) + (Stream.fromList [1, 2, 1])); + +testCase "foldRight" (fn _ => + assertEq + (Stream.foldRight + (fn (x : Int) y => x + y) (Stream.fromList [1, 2, 3, 4]) 0) + 10); + +testSuite "foldRight1Err" (fn _ => + let ~onError () = 42 in + + testCase "non empty" (fn _ => + assertEq + (Stream.foldRight1Err + (fn (x : Int) y => x + y) (Stream.fromList [1, 2, 3, 4])) + 10); + + testCase "empty" (fn _ => + assertEq + (Stream.foldRight1Err + (fn (x : Int) y => x + y) (Stream.empty {X=Int})) + 42)); + + +testSuite "equal" (fn _ => + + testCase "true" (fn _ => + assertTrue + (Stream.equal + (Stream.fromList [1, 2, 3]) + (Stream.fromList [1, 2, 3]))); + + testCase "false" (fn _ => + assertFalse + (Stream.equal + (Stream.fromList [1, 2, 3]) + (Stream.fromList [4, 5, 6])))); + +() diff --git a/test/stdlib/TestAll.fram b/test/stdlib/TestAll.fram index 4d987099..0134517f 100644 --- a/test/stdlib/TestAll.fram +++ b/test/stdlib/TestAll.fram @@ -4,6 +4,7 @@ import Base/Pair import Base/Show import Base/ToString import List +import Stream import Map import Mutable import Prelude diff --git a/test/stdlib/stdlib0007_Stream.fram b/test/stdlib/stdlib0007_Stream.fram deleted file mode 100644 index d1a4cdfa..00000000 --- a/test/stdlib/stdlib0007_Stream.fram +++ /dev/null @@ -1,72 +0,0 @@ -import List -import Stream - -let _ = assert {msg="fromList > toList"} - (let xs = [1, 2, 3, 4] in - xs == Stream.toList (Stream.fromList xs)) - -let _ = assert {msg="toList > fromList"} - (let xs = Stream.cons 10 (Stream.cons 20 (Stream.empty)) in - xs == Stream.fromList (Stream.toList xs)) - -let _ = - let f x = if 2 == x then None else Some (x, 1 + x) in - assert {msg="unfold"} - (Stream.fromList [0, 1] == Stream.unfold 0 f) - -let _ = assert {msg="isEmpty"} (Stream.isEmpty (Stream.empty {X=Int})) -let _ = assert {msg="isEmpty"} (not (Stream.isEmpty (Stream.fromList [1]))) - -let _ = assert {msg="singleton"} (Stream.singleton 1 == Stream.fromList [1]) - -let _ = assert {msg="cons"} - (Stream.cons 3 (Stream.fromList [2, 1]) == Stream.fromList [3, 2, 1]) - -let _ = assert {msg="uncons"} - match Stream.uncons (Stream.fromList [1, 2, 3]) with - | Some (x, _) => 1 == x - | None => False - end -let _ = assert {msg="uncons"} - match Stream.uncons (Stream.empty {X=Int}) with - | None => True - | _ => False - end - -let _ = assert {msg="map"} - (Stream.fromList ["1", "2"] - == Stream.map (fn x => (x : Int).toString) (Stream.fromList [1, 2])) - -let _ = assert {msg="append"} - (Stream.append (Stream.fromList [1, 2, 3]) (Stream.fromList [4, 5, 6]) - == Stream.fromList [1, 2, 3, 4, 5, 6]) - -let _ = assert {msg="concatMap"} - (Stream.fromList [1, 2, 3] >.concatMap (fn (x : Int) => Stream.fromList [x + 1, x - 1]) - == Stream.fromList [2, 0, 3, 1, 4, 2]) - -let _ = assert {msg="takeWhile"} - (Stream.takeWhile (fn (x : Int) => x <= 2) (Stream.fromList [1, 2, 3, 4, 1]) - == Stream.fromList [1, 2]) - -let _ = assert {msg="filter"} - (Stream.filter (fn (x : Int) => x <= 2) (Stream.fromList [1, 2, 3, 4, 1]) - == Stream.fromList [1, 2, 1]) - -let _ = assert {msg="foldRight"} - (Stream.foldRight (fn (x : Int) y => x + y) (Stream.fromList [1, 2, 3, 4]) 0 - == 10) - -let _ = assert {msg="foldRight1Err"} - (Stream.foldRight1Err {~onError = fn _ => 42} - (fn (x : Int) y => x + y) (Stream.fromList [1, 2, 3, 4]) - == 10) -let _ = assert {msg="foldRight1Err"} - (Stream.foldRight1Err {~onError = fn _ => 42} - (fn (x : Int) y => x + y) (Stream.empty {X=Int}) - == 42) - -let _ = assert {msg="equal"} - (Stream.equal (Stream.fromList [1, 2, 3]) (Stream.fromList [1, 2, 3])) -let _ = assert {msg="equal"} - (not (Stream.equal (Stream.fromList [1, 2, 3]) (Stream.fromList [4, 5, 6]))) diff --git a/test/test_suite b/test/test_suite index e80d58ff..20dc71f1 100644 --- a/test/test_suite +++ b/test/test_suite @@ -4,7 +4,6 @@ function simple_run_tests { simple_test $file done } - run_with_flags simple_run_tests "-no-prelude -no-stdlib" function simple_examples { @@ -19,7 +18,6 @@ run_with_flags simple_examples "" function simple_stdlib_tests { simple_test test/stdlib/TestAll.fram - simple_test test/stdlib/stdlib0007_Stream.fram } run_with_flags simple_stdlib_tests ""