https://github.com/akkartik/mu/blob/main/baremetal/504test-screen.mu
  1 # Some primitives for checking the state of fake screen objects.
  2 
  3 # validate data on screen regardless of attributes (color, bold, etc.)
  4 # Mu doesn't have multi-line strings, so we provide functions for rows or portions of rows.
  5 # Tab characters (that translate into multiple screen cells) not supported.
  6 
  7 fn check-screen-row screen: (addr screen), y: int, expected: (addr array byte), msg: (addr array byte) {
  8   check-screen-row-from screen, 0/x, y, expected, msg
  9 }
 10 
 11 fn check-screen-row-from screen-on-stack: (addr screen), x: int, y: int, expected: (addr array byte), msg: (addr array byte) {
 12   var screen/esi: (addr screen) <- copy screen-on-stack
 13   var idx/ecx: int <- screen-cell-index screen, x, y
 14   # compare 'expected' with the screen contents starting at 'idx', grapheme by grapheme
 15   var e: (stream byte 0x100)
 16   var e-addr/edx: (addr stream byte) <- address e
 17   write e-addr, expected
 18   {
 19     var done?/eax: boolean <- stream-empty? e-addr
 20     compare done?, 0
 21     break-if-!=
 22     var _g/eax: grapheme <- screen-grapheme-at-idx screen, idx
 23     var g/ebx: grapheme <- copy _g
 24     var expected-grapheme/eax: grapheme <- read-grapheme e-addr
 25     # compare graphemes
 26     $check-screen-row-from:compare-graphemes: {
 27       # if expected-grapheme is space, null grapheme is also ok
 28       {
 29         compare expected-grapheme, 0x20
 30         break-if-!=
 31         compare g, 0
 32         break-if-= $check-screen-row-from:compare-graphemes
 33       }
 34       # if (g == expected-grapheme) print "."
 35       compare g, expected-grapheme
 36       {
 37         break-if-!=
 38         draw-text-wrapping-right-then-down-from-cursor-over-full-screen 0/screen, ".", 3/fg/cyan, 0/bg
 39         break $check-screen-row-from:compare-graphemes
 40       }
 41       # otherwise print an error
 42       count-test-failure
 43       draw-text-wrapping-right-then-down-from-cursor-over-full-screen 0/screen, msg, 3/fg/cyan, 0/bg
 44       draw-text-wrapping-right-then-down-from-cursor-over-full-screen 0/screen, ": expected '", 3/fg/cyan, 0/bg
 45       draw-grapheme-at-cursor 0/screen, expected-grapheme, 3/cyan, 0/bg
 46       move-cursor-rightward-and-downward 0/screen, 0/xmin, 0x80/xmax=screen-width
 47       draw-text-wrapping-right-then-down-from-cursor-over-full-screen 0/screen, "' at (", 3/fg/cyan, 0/bg
 48       draw-int32-hex-wrapping-right-then-down-from-cursor-over-full-screen 0/screen, x, 3/fg/cyan, 0/bg
 49       draw-text-wrapping-right-then-down-from-cursor-over-full-screen 0/screen, ", ", 3/fg/cyan, 0/bg
 50       draw-int32-hex-wrapping-right-then-down-from-cursor-over-full-screen 0/screen, y, 3/fg/cyan, 0/bg
 51       draw-text-wrapping-right-then-down-from-cursor-over-full-screen 0/screen, ") but observed '", 3/fg/cyan, 0/bg
 52       draw-grapheme-at-cursor 0/screen, g, 3/cyan, 0/bg
 53       move-cursor-rightward-and-downward 0/screen, 0/xmin, 0x80/xmax=screen-width
 54       draw-text-wrapping-right-then-down-from-cursor-over-full-screen 0/screen, "'", 3/fg/cyan, 0/bg
 55     }
 56     idx <- increment
 57     increment x
 58     loop
 59   }
 60 }
 61 
 62 # various variants by screen-cell attribute; spaces in the 'expected' data should not match the attribute
 63 
 64 fn check-screen-row-in-color screen: (addr screen), fg: int, y: int, expected: (addr array byte), msg: (addr array byte) {
 65   check-screen-row-in-color-from screen, fg, y, 0/x, expected, msg
 66 }
 67 
 68 fn check-screen-row-in-color-from screen-on-stack: (addr screen), fg: int, y: int, x: int, expected: (addr array byte), msg: (addr array byte) {
 69   var screen/esi: (addr screen) <- copy screen-on-stack
 70   var idx/ecx: int <- screen-cell-index screen, x, y
 71   # compare 'expected' with the screen contents starting at 'idx', grapheme by grapheme
 72   var e: (stream byte 0x100)
 73   var e-addr/edx: (addr stream byte) <- address e
 74   write e-addr, expected
 75   {
 76     var done?/eax: boolean <- stream-empty? e-addr
 77     compare done?, 0
 78     break-if-!=
 79     var _g/eax: grapheme <- screen-grapheme-at-idx screen, idx
 80     var g/ebx: grapheme <- copy _g
 81     var _expected-grapheme/eax: grapheme <- read-grapheme e-addr
 82     var expected-grapheme/edi: grapheme <- copy _expected-grapheme
 83     $check-screen-row-in-color-from:compare-cells: {
 84       # if expected-grapheme is space, null grapheme is also ok
 85       {
 86         compare expected-grapheme, 0x20
 87         break-if-!=
 88         compare g, 0
 89         break-if-= $check-screen-row-in-color-from:compare-cells
 90       }
 91       # if expected-grapheme is space, a different color is ok
 92       {
 93         compare expected-grapheme, 0x20
 94         break-if-!=
 95         var color/eax: int <- screen-color-at-idx screen, idx
 96         compare color, fg
 97         break-if-!= $check-screen-row-in-color-from:compare-cells
 98       }
 99       # compare graphemes
100       $check-screen-row-in-color-from:compare-graphemes: {
101         # if (g == expected-grapheme) print "."
102         compare g, expected-grapheme
103         {
104           break-if-!=
105           draw-text-wrapping-right-then-down-from-cursor-over-full-screen 0/screen, ".", 3/fg/cyan, 0/bg
106           break $check-screen-row-in-color-from:compare-graphemes
107         }
108         # otherwise print an error
109         count-test-failure
110         draw-text-wrapping-right-then-down-from-cursor-over-full-screen 0/screen, msg, 3/fg/cyan, 0/bg
111         draw-text-wrapping-right-then-down-from-cursor-over-full-screen 0/screen, ": expected '", 3/fg/cyan, 0/bg
112         draw-grapheme-at-cursor 0/screen, expected-grapheme, 3/cyan, 0/bg
113         move-cursor-rightward-and-downward 0/screen, 0/xmin, 0x80/xmax=screen-width
114         draw-text-wrapping-right-then-down-from-cursor-over-full-screen 0/screen, "' at (", 3/fg/cyan, 0/bg
115         draw-int32-hex-wrapping-right-then-down-from-cursor-over-full-screen 0/screen, x, 3/fg/cyan, 0/bg
116         draw-text-wrapping-right-then-down-from-cursor-over-full-screen 0/screen, ", ", 3/fg/cyan, 0/bg
117         draw-int32-hex-wrapping-right-then-down-from-cursor-over-full-screen 0/screen, y, 3/fg/cyan, 0/bg
118         draw-text-wrapping-right-then-down-from-cursor-over-full-screen 0/screen, ") but observed '", 3/fg/cyan, 0/bg
119         draw-grapheme-at-cursor 0/screen, g, 3/cyan, 0/bg
120         move-cursor-rightward-and-downward 0/screen, 0/xmin, 0x80/xmax=screen-width
121         draw-text-wrapping-right-then-down-from-cursor-over-full-screen 0/screen, "'", 3/fg/cyan, 0/bg
122       }
123       $check-screen-row-in-color-from:compare-colors: {
124         var color/eax: int <- screen-color-at-idx screen, idx
125         compare fg, color
126         {
127           break-if-!=
128           draw-text-wrapping-right-then-down-from-cursor-over-full-screen 0/screen, ".", 3/fg/cyan, 0/bg
129           break $check-screen-row-in-color-from:compare-colors
130         }
131         # otherwise print an error
132         count-test-failure
133         draw-text-wrapping-right-then-down-from-cursor-over-full-screen 0/screen, msg, 3/fg/cyan, 0/bg
134         draw-text-wrapping-right-then-down-from-cursor-over-full-screen 0/screen, ": expected '", 3/fg/cyan, 0/bg
135         draw-grapheme-at-cursor 0/screen, expected-grapheme, 3/cyan, 0/bg
136         move-cursor-rightward-and-downward 0/screen, 0/xmin, 0x80/xmax=screen-width
137         draw-text-wrapping-right-then-down-from-cursor-over-full-screen 0/screen, "' at (", 3/fg/cyan, 0/bg
138         draw-int32-hex-wrapping-right-then-down-from-cursor-over-full-screen 0/screen, x, 3/fg/cyan, 0/bg
139         draw-text-wrapping-right-then-down-from-cursor-over-full-screen 0/screen, ", ", 3/fg/cyan, 0/bg
140         draw-int32-hex-wrapping-right-then-down-from-cursor-over-full-screen 0/screen, y, 3/fg/cyan, 0/bg
141         draw-text-wrapping-right-then-down-from-cursor-over-full-screen 0/screen, ") in color ", 3/fg/cyan, 0/bg
142         draw-int32-hex-wrapping-right-then-down-from-cursor-over-full-screen 0/screen, fg, 3/fg/cyan, 0/bg
143         draw-text-wrapping-right-then-down-from-cursor-over-full-screen 0/screen, " but observed color ", 3/fg/cyan, 0/bg
144         draw-int32-hex-wrapping-right-then-down-from-cursor-over-full-screen 0/screen, color, 3/fg/cyan, 0/bg
145       }
146     }
147     idx <- increment
148     increment x
149     loop
150   }
151 }
152 
153 fn check-screen-row-in-background-color screen: (addr screen), bg: int, y: int, expected: (addr array byte), msg: (addr array byte) {
154   check-screen-row-in-background-color-from screen, bg, y, 0/x, expected, msg
155 }
156 
157 fn check-screen-row-in-background-color-from screen-on-stack: (addr screen), bg: int, y: int, x: int, expected: (addr array byte), msg: (addr array byte) {
158   var screen/esi: (addr screen) <- copy screen-on-stack
159   var idx/ecx: int <- screen-cell-index screen, x, y
160   # compare 'expected' with the screen contents starting at 'idx', grapheme by grapheme
161   var e: (stream byte 0x100)
162   var e-addr/edx: (addr stream byte) <- address e
163   write e-addr, expected
164   {
165     var done?/eax: boolean <- stream-empty? e-addr
166     compare done?, 0
167     break-if-!=
168     var _g/eax: grapheme <- screen-grapheme-at-idx screen, idx
169     var g/ebx: grapheme <- copy _g
170     var _expected-grapheme/eax: grapheme <- read-grapheme e-addr
171     var expected-grapheme/edi: grapheme <- copy _expected-grapheme
172     $check-screen-row-in-background-color-from:compare-cells: {
173       # if expected-grapheme is space, null grapheme is also ok
174       {
175         compare expected-grapheme, 0x20
176         break-if-!=
177         compare g, 0
178         break-if-= $check-screen-row-in-background-color-from:compare-cells
179       }
180       # if expected-grapheme is space, a different background-color is ok
181       {
182         compare expected-grapheme, 0x20
183         break-if-!=
184         var background-color/eax: int <- screen-background-color-at-idx screen, idx
185         compare background-color, bg
186         break-if-!= $check-screen-row-in-background-color-from:compare-cells
187       }
188       # compare graphemes
189       $check-screen-row-in-background-color-from:compare-graphemes: {
190         # if (g == expected-grapheme) print "."
191         compare g, expected-grapheme
192         {
193           break-if-!=
194           draw-text-wrapping-right-then-down-from-cursor-over-full-screen 0/screen, ".", 3/fg/cyan, 0/bg
195           break $check-screen-row-in-background-color-from:compare-graphemes
196         }
197         # otherwise print an error
198         count-test-failure
199         draw-text-wrapping-right-then-down-from-cursor-over-full-screen 0/screen, msg, 3/fg/cyan, 0/bg
200         draw-text-wrapping-right-then-down-from-cursor-over-full-screen 0/screen, ": expected '", 3/fg/cyan, 0/bg
201         draw-grapheme-at-cursor 0/screen, expected-grapheme, 3/cyan, 0/bg
202         move-cursor-rightward-and-downward 0/screen, 0/xmin, 0x80/xmax=screen-width
203         draw-text-wrapping-right-then-down-from-cursor-over-full-screen 0/screen, "' at (", 3/fg/cyan, 0/bg
204         draw-int32-hex-wrapping-right-then-down-from-cursor-over-full-screen 0/screen, x, 3/fg/cyan, 0/bg
205         draw-text-wrapping-right-then-down-from-cursor-over-full-screen 0/screen, ", ", 3/fg/cyan, 0/bg
206         draw-int32-hex-wrapping-right-then-down-from-cursor-over-full-screen 0/screen, y, 3/fg/cyan, 0/bg
207         draw-text-wrapping-right-then-down-from-cursor-over-full-screen 0/screen, ") but observed '", 3/fg/cyan, 0/bg
208         draw-grapheme-at-cursor 0/screen, g, 3/cyan, 0/bg
209         move-cursor-rightward-and-downward 0/screen, 0/xmin, 0x80/xmax=screen-width
210         draw-text-wrapping-right-then-down-from-cursor-over-full-screen 0/screen, "'", 3/fg/cyan, 0/bg
211       }
212       $check-screen-row-in-background-color-from:compare-background-colors: {
213         var background-color/eax: int <- screen-background-color-at-idx screen, idx
214         compare bg, background-color
215         {
216           break-if-!=
217           draw-text-wrapping-right-then-down-from-cursor-over-full-screen 0/screen, ".", 3/fg/cyan, 0/bg
218           break $check-screen-row-in-background-color-from:compare-background-colors
219         }
220         # otherwise print an error
221         count-test-failure
222         draw-text-wrapping-right-then-down-from-cursor-over-full-screen 0/screen, msg, 3/fg/cyan, 0/bg
223         draw-text-wrapping-right-then-down-from-cursor-over-full-screen 0/screen, ": expected '", 3/fg/cyan, 0/bg
224         draw-grapheme-at-cursor 0/screen, expected-grapheme, 3/cyan, 0/bg
225         move-cursor-rightward-and-downward 0/screen, 0/xmin, 0x80/xmax=screen-width
226         draw-text-wrapping-right-then-down-from-cursor-over-full-screen 0/screen, "' at (", 3/fg/cyan, 0/bg
227         draw-int32-hex-wrapping-right-then-down-from-cursor-over-full-screen 0/screen, x, 3/fg/cyan, 0/bg
228         draw-text-wrapping-right-then-down-from-cursor-over-full-screen 0/screen, ", ", 3/fg/cyan, 0/bg
229         draw-int32-hex-wrapping-right-then-down-from-cursor-over-full-screen 0/screen, y, 3/fg/cyan, 0/bg
230         draw-text-wrapping-right-then-down-from-cursor-over-full-screen 0/screen, ") in background-color ", 3/fg/cyan, 0/bg
231         draw-int32-hex-wrapping-right-then-down-from-cursor-over-full-screen 0/screen, bg, 3/fg/cyan, 0/bg
232         draw-text-wrapping-right-then-down-from-cursor-over-full-screen 0/screen, " but observed background-color ", 3/fg/cyan, 0/bg
233         draw-int32-hex-wrapping-right-then-down-from-cursor-over-full-screen 0/screen, background-color, 3/fg/cyan, 0/bg
234       }
235     }
236     idx <- increment
237     increment x
238     loop
239   }
240 }
241 
242 # helpers for checking just background color, not screen contents
243 # these can validate bg for spaces
244 
245 fn check-background-color-in-screen-row screen: (addr screen), bg: int, y: int, expected-bitmap: (addr array byte), msg: (addr array byte) {
246   check-background-color-in-screen-row-from screen, bg, y, 0/x, expected-bitmap, msg
247 }
248 
249 fn check-background-color-in-screen-row-from screen-on-stack: (addr screen), bg: int, y: int, x: int, expected-bitmap: (addr array byte), msg: (addr array byte) {
250   var screen/esi: (addr screen) <- copy screen-on-stack
251   var idx/ecx: int <- screen-cell-index screen, x, y
252   # compare background color where 'expected-bitmap' is a non-space
253   var e: (stream byte 0x100)
254   var e-addr/edx: (addr stream byte) <- address e
255   write e-addr, expected-bitmap
256   {
257     var done?/eax: boolean <- stream-empty? e-addr
258     compare done?, 0
259     break-if-!=
260     var _expected-bit/eax: grapheme <- read-grapheme e-addr
261     var expected-bit/edi: grapheme <- copy _expected-bit
262     $check-background-color-in-screen-row-from:compare-cells: {
263       var background-color/eax: int <- screen-background-color-at-idx screen, idx
264       # if expected-bit is space, assert that background is NOT bg
265       compare expected-bit, 0x20
266       {
267         break-if-!=
268         compare background-color, bg
269         break-if-!= $check-background-color-in-screen-row-from:compare-cells
270         count-test-failure
271         draw-text-wrapping-right-then-down-from-cursor-over-full-screen 0/screen, msg, 3/fg/cyan, 0/bg
272         draw-text-wrapping-right-then-down-from-cursor-over-full-screen 0/screen, ": expected (", 3/fg/cyan, 0/bg
273         draw-int32-hex-wrapping-right-then-down-from-cursor-over-full-screen 0/screen, x, 3/fg/cyan, 0/bg
274         draw-text-wrapping-right-then-down-from-cursor-over-full-screen 0/screen, ", ", 3/fg/cyan, 0/bg
275         draw-int32-hex-wrapping-right-then-down-from-cursor-over-full-screen 0/screen, y, 3/fg/cyan, 0/bg
276         draw-text-wrapping-right-then-down-from-cursor-over-full-screen 0/screen, ") to not be in background-color ", 3/fg/cyan, 0/bg
277         draw-int32-hex-wrapping-right-then-down-from-cursor-over-full-screen 0/screen, bg, 3/fg/cyan, 0/bg
278         break $check-background-color-in-screen-row-from:compare-cells
279       }
280       # otherwise assert that background IS bg
281       compare background-color, bg
282       break-if-= $check-background-color-in-screen-row-from:compare-cells
283       count-test-failure
284       draw-text-wrapping-right-then-down-from-cursor-over-full-screen 0/screen, msg, 3/fg/cyan, 0/bg
285       draw-text-wrapping-right-then-down-from-cursor-over-full-screen 0/screen, ": expected (", 3/fg/cyan, 0/bg
286       draw-int32-hex-wrapping-right-then-down-from-cursor-over-full-screen 0/screen, x, 3/fg/cyan, 0/bg
287       draw-text-wrapping-right-then-down-from-cursor-over-full-screen 0/screen, ", ", 3/fg/cyan, 0/bg
288       draw-int32-hex-wrapping-right-then-down-from-cursor-over-full-screen 0/screen, y, 3/fg/cyan, 0/bg
289       draw-text-wrapping-right-then-down-from-cursor-over-full-screen 0/screen, ") in background-color ", 3/fg/cyan, 0/bg
290       draw-int32-hex-wrapping-right-then-down-from-cursor-over-full-screen 0/screen, bg, 3/fg/cyan, 0/bg
291       draw-text-wrapping-right-then-down-from-cursor-over-full-screen 0/screen, " but observed background-color ", 3/fg/cyan, 0/bg
292       draw-int32-hex-wrapping-right-then-down-from-cursor-over-full-screen 0/screen, background-color, 3/fg/cyan, 0/bg
293     }
294     idx <- increment
295     increment x
296     loop
297   }
298 }
299 
300 fn test-draw-single-grapheme {
301   var screen-on-stack: screen
302   var screen/esi: (addr screen) <- address screen-on-stack
303   initialize-screen screen, 5, 4
304   draw-code-point screen, 0x61/a, 0/x, 0/y, 1/fg, 2/bg
305   check-screen-row screen, 0/y, "a", "F - test-draw-single-grapheme"  # top-left corner of the screen
306   check-screen-row-in-color screen, 1/fg, 0/y, "a", "F - test-draw-single-grapheme-fg"
307   check-screen-row-in-background-color screen, 2/bg, 0/y, "a", "F - test-draw-single-grapheme-bg"
308   check-background-color-in-screen-row screen, 2/bg, 0/y, "x ", "F - test-draw-single-grapheme-bg2"
309 }
310 
311 fn test-draw-multiple-graphemes {
312   var screen-on-stack: screen
313   var screen/esi: (addr screen) <- address screen-on-stack
314   initialize-screen screen, 0x10/rows, 4/cols
315   draw-text-wrapping-right-then-down-from-cursor-over-full-screen screen, "Hello, 世界", 1/fg, 2/bg
316   check-screen-row screen, 0/y, "Hello, 世界", "F - test-draw-multiple-graphemes"
317   check-screen-row-in-color screen, 1/fg, 0/y, "Hello, 世界", "F - test-draw-multiple-graphemes-fg"
318   check-background-color-in-screen-row screen, 2/bg, 0/y, "xxxxxxxxx ", "F - test-draw-multiple-graphemes-bg2"
319 }