2021-01-06 21:19:40 +01:00
|
|
|
const n = 1000
|
|
|
|
|
2021-01-09 02:43:48 +01:00
|
|
|
fn f(ch chan f64) {
|
|
|
|
mut s := 0.
|
2021-01-06 21:19:40 +01:00
|
|
|
for _ in 0 .. n {
|
|
|
|
s += <-ch
|
|
|
|
}
|
2021-01-09 02:43:48 +01:00
|
|
|
assert s == f64(n * (n + 1) / 2)
|
2021-01-06 21:19:40 +01:00
|
|
|
ch.close()
|
|
|
|
}
|
|
|
|
|
2021-01-09 02:43:48 +01:00
|
|
|
fn do_send(ch chan f64, val f64) ?f64 {
|
2021-01-06 21:19:40 +01:00
|
|
|
ch <- val ?
|
2021-01-09 02:43:48 +01:00
|
|
|
return val + 1.0
|
2021-01-06 21:19:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
fn test_push_propargate() {
|
2021-01-09 02:43:48 +01:00
|
|
|
ch := chan f64{}
|
2021-01-06 21:19:40 +01:00
|
|
|
go f(ch)
|
2021-01-09 02:43:48 +01:00
|
|
|
mut s := 1.0
|
2021-01-06 21:19:40 +01:00
|
|
|
for {
|
|
|
|
s = do_send(ch, s) or {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2021-01-09 02:43:48 +01:00
|
|
|
assert s == f64(n + 1)
|
2021-01-06 21:19:40 +01:00
|
|
|
}
|