1 package main
2 3 import "moxie"
4 5 // Phase B Test 3: close propagation.
6 // Parent sends N values then closes the send-channel. Child recv loop
7 // drains values; the comma-ok recv returns ok=false once the parent's
8 // close marker traverses the pipe. Child writes a final completion
9 // value to the parent over a return channel so the parent can verify
10 // the count and close behavior.
11 12 func drain(in chan moxie.Int32, done chan moxie.Int32) {
13 count := int32(0)
14 sum := int32(0)
15 for {
16 v, ok := <-in
17 if !ok {
18 break
19 }
20 count++
21 sum += int32(v)
22 }
23 // Encode count in low 16 bits, sum in high 16 bits — single
24 // completion value over the return channel.
25 done <- moxie.Int32(count | (sum << 16))
26 }
27 28 func main() {
29 in := make(chan moxie.Int32)
30 done := make(chan moxie.Int32)
31 spawn(drain, in, done)
32 33 const N = int32(5)
34 expectedSum := int32(0)
35 for i := int32(1); i <= N; i++ {
36 in <- moxie.Int32(i)
37 expectedSum += i
38 }
39 close(in)
40 41 result := int32(<-done)
42 gotCount := result & 0xFFFF
43 gotSum := (result >> 16) & 0xFFFF
44 if gotCount != N || gotSum != expectedSum {
45 println("FAIL:close-propagation", gotCount, gotSum, "expected", N, expectedSum)
46 return
47 }
48 println("PASS:close-propagation")
49 }
50