aboutsummaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
authorGareth Pendleton <gareth.sidebottom@gmail.com>2024-09-05 11:50:48 +0100
committerGitHub <noreply@github.com>2024-09-05 11:50:48 +0100
commit363de5ebd94d6c975d4f232955050d364512d230 (patch)
tree593b748e8b4f4b8b6d6d8cae7d2d4f7231de3016 /test
parentee334747949c6696a0128d1ce709078ecc564598 (diff)
downloadgleam_stdlib-363de5ebd94d6c975d4f232955050d364512d230.tar.gz
gleam_stdlib-363de5ebd94d6c975d4f232955050d364512d230.zip
make iterator.do_try_fold tail-recursive (#689)
Co-authored-by: Gareth Pendleton <gareth.pendleton@ninthwave.co.uk>
Diffstat (limited to 'test')
-rw-r--r--test/gleam/iterator_test.gleam20
1 files changed, 18 insertions, 2 deletions
diff --git a/test/gleam/iterator_test.gleam b/test/gleam/iterator_test.gleam
index 7c3498b..6ae1d4c 100644
--- a/test/gleam/iterator_test.gleam
+++ b/test/gleam/iterator_test.gleam
@@ -4,6 +4,17 @@ import gleam/iterator.{Done, Next}
import gleam/list
import gleam/should
+@target(erlang)
+const recursion_test_cycles = 1_000_000
+
+// JavaScript engines crash when exceeding a certain stack size:
+//
+// - Chrome 106 and NodeJS V16, V18, and V19 crash around 10_000+
+// - Firefox 106 crashes around 35_000+.
+// - Safari 16 crashes around 40_000+.
+@target(javascript)
+const recursion_test_cycles = 40_000
+
// a |> from_list |> to_list == a
pub fn to_from_list_test() {
let testcase = fn(subject) {
@@ -505,7 +516,7 @@ pub fn any_test() {
// TCO test
iterator.repeat(1)
- |> iterator.take(1_000_000)
+ |> iterator.take(recursion_test_cycles)
|> iterator.any(satisfying: fn(n) { n % 2 == 0 })
|> should.be_false
}
@@ -525,7 +536,7 @@ pub fn all_test() {
// TCO test
iterator.repeat(0)
- |> iterator.take(1_000_000)
+ |> iterator.take(recursion_test_cycles)
|> iterator.all(satisfying: fn(n) { n % 2 == 0 })
|> should.be_true
}
@@ -641,6 +652,11 @@ pub fn try_fold_test() {
|> iterator.from_list()
|> iterator.try_fold(0, f)
|> should.equal(Error("tried to add an odd number"))
+
+ // TCO test
+ iterator.repeat(1)
+ |> iterator.take(recursion_test_cycles)
+ |> iterator.try_fold(0, fn(e, acc) { Ok(e + acc) })
}
pub fn first_test() {