https://github.com/akkartik/mu/blob/master/404stream.mu
 1 # Tests for Mu's stream primitives.
 2 
 3 fn test-stream {
 4   # - write an int to a stream, then read it back
 5   # step 1: initialize
 6   var s: (stream int 4)
 7   var s2/ecx: (addr stream int) <- address s
 8   var tmp/eax: boolean <- stream-empty? s2
 9   check-true tmp, "F - test-stream/empty?/0"
10   tmp <- stream-full? s2
11   check-false tmp, "F - test-stream/full?/0"
12   # step 2: write to stream
13   var x: int
14   copy-to x, 0x34
15   var x2/edx: (addr int) <- address x
16   write-to-stream s2, x2
17   tmp <- stream-empty? s2
18   check-false tmp, "F - test-stream/empty?/1"
19   tmp <- stream-full? s2
20   check-false tmp, "F - test-stream/full?/1"
21   # step 3: modify the value written (should make no difference)
22   copy-to x, 0
23   # step 4: read back
24   var y: int
25   var y2/ebx: (addr int) <- address y
26   read-from-stream s2, y2
27   tmp <- stream-empty? s2
28   check-true tmp, "F - test-stream/empty?/2"
29   tmp <- stream-full? s2
30   check-false tmp, "F - test-stream/full?/2"
31   # we read back what was written
32   check-ints-equal y, 0x34, "F - test-stream"
33 }
34 
35 fn test-stream-full {
36   # write an int to a stream of capacity 1
37   var s: (stream int 1)
38   var s2/ecx: (addr stream int) <- address s
39   var tmp/eax: boolean <- stream-full? s2
40   check-false tmp, "F - test-stream-full?/pre"
41   var x: int
42   var x2/edx: (addr int) <- address x
43   write-to-stream s2, x2
44   tmp <- stream-full? s2
45   check-true tmp, "F - test-stream-full?"
46 }
47 
48 fn test-fake-input-buffered-file {
49   var foo: (handle buffered-file)
50   var foo-ah/eax: (addr handle buffered-file) <- address foo
51   populate-buffered-file-containing "abc", foo-ah
52   var foo-addr/eax: (addr buffered-file) <- lookup foo
53   var s: (stream byte 0x100)
54   var result/ecx: (addr stream byte) <- address s
55   read-line-buffered foo-addr, result
56   check-stream-equal result, "abc", "F - test-fake-input-buffered-file"
57 }
58 
59 fn test-fake-output-buffered-file {
60   var foo: (handle buffered-file)
61   var foo-ah/eax: (addr handle buffered-file) <- address foo
62   new-buffered-file foo-ah
63   var foo-addr/eax: (addr buffered-file) <- lookup foo
64   write-buffered foo-addr, "abc"
65   var s: (stream byte 0x100)
66   var result/ecx: (addr stream byte) <- address s
67   read-line-buffered foo-addr, result
68   check-stream-equal result, "abc", "F - test-fake-output-buffered-file"
69 }