about summary refs log tree commit diff stats
path: root/baremetal/504test-screen.mu
blob: 614edd9db46d7ea297084e1291f565006b592506 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# Some primitives for checking the state of fake screen objects.

# validate data on screen regardless of attributes (color, bold, etc.)
# Mu doesn't have multi-line strings, so we provide functions for rows or portions of rows.
# Tab characters (that translate into multiple screen cells) not supported.

fn check-screen-row screen: (addr screen), y: int, expected: (addr array byte), msg: (addr array byte) {
  check-screen-row-from screen, y, 0, expected, msg
}

fn check-screen-row-from screen-on-stack: (addr screen), x: int, y: int, expected: (addr array byte), msg: (addr array byte) {
  var screen/esi: (addr screen) <- copy screen-on-stack
  var idx/ecx: int <- screen-cell-index screen, y, x
  # compare 'expected' with the screen contents starting at 'idx', grapheme by grapheme
  var e: (stream byte 0x100)
  var e-addr/edx: (addr stream byte) <- address e
  write e-addr, expected
  {
    var done?/eax: boolean <- stream-empty? e-addr
    compare done?, 0
    break-if-!=
    var _g/eax: grapheme <- screen-grapheme-at-idx screen, idx
    var g/ebx: grapheme <- copy _g
    var expected-grapheme/eax: grapheme <- read-grapheme e-addr
    # compare graphemes
    $check-screen-row-from:compare-graphemes: {
      # if expected-grapheme is space, null grapheme is also ok
      {
        compare expected-grapheme, 0x20
        break-if-!=
        compare g, 0
        break-if-= $check-screen-row-from:compare-graphemes
      }
      # if (g == expected-grapheme) print "."
      compare g, expected-grapheme
      {
        break-if-!=
        draw-text-wrapping-right-then-down-from-cursor-over-full-screen 0, ".", 3  # 3=cyan
        break $check-screen-row-from:compare-graphemes
      }
      # otherwise print an error
      count-test-failure
      draw-text-wrapping-right-then-down-from-cursor-over-full-screen 0, msg, 3  # 3=cyan
      draw-text-wrapping-right-then-down-from-cursor-over-full-screen 0, ": expected '", 3
      draw-grapheme-at-cursor 0, expected-grapheme, 3
      move-cursor-rightward-and-downward 0, 0, 0x80  # screen-width
      draw-text-wrapping-right-then-down-from-cursor-over-full-screen 0, "' at (", 3
      draw-int32-hex-wrapping-right-then-down-from-cursor-over-full-screen 0, x, 3
      draw-text-wrapping-right-then-down-from-cursor-over-full-screen 0, ", ", 3
      draw-int32-hex-wrapping-right-then-down-from-cursor-over-full-screen 0, y, 3
      draw-text-wrapping-right-then-down-from-cursor-over-full-screen 0, ") but observed '", 3
      draw-grapheme-at-cursor 0, g, 3
      move-cursor-rightward-and-downward 0, 0, 0x80  # screen-width
      draw-text-wrapping-right-then-down-from-cursor-over-full-screen 0, "'", 3
    }
    idx <- increment
    increment x
    loop
  }
}

# various variants by screen-cell attribute; spaces in the 'expected' data should not match the attribute

fn check-screen-row-in-color screen: (addr screen), fg: int, y: int, expected: (addr array byte), msg: (addr array byte) {
  check-screen-row-in-color-from screen, fg, y, 0, expected, msg
}

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) {
  var screen/esi: (addr screen) <- copy screen-on-stack
  var idx/ecx: int <- screen-cell-index screen, y, x
  # compare 'expected' with the screen contents starting at 'idx', grapheme by grapheme
  var e: (stream byte 0x100)
  var e-addr/edx: (addr stream byte) <- address e
  write e-addr, expected
  {
    var done?/eax: boolean <- stream-empty? e-addr
    compare done?, 0
    break-if-!=
    var _g/eax: grapheme <- screen-grapheme-at-idx screen, idx
    var g/ebx: grapheme <- copy _g
    var _expected-grapheme/eax: grapheme <- read-grapheme e-addr
    var expected-grapheme/edi: grapheme <- copy _expected-grapheme
    $check-screen-row-in-color-from:compare-cells: {
      # if expected-grapheme is space, null grapheme is also ok
      {
        compare expected-grapheme, 0x20
        break-if-!=
        compare g, 0
        break-if-= $check-screen-row-in-color-from:compare-cells
      }
      # if expected-grapheme is space, a different color is ok
      {
        compare expected-grapheme, 0x20
        break-if-!=
        var color/eax: int <- screen-color-at-idx screen, idx
        compare color, fg
        break-if-!= $check-screen-row-in-color-from:compare-cells
      }
      # compare graphemes
      $check-screen-row-in-color-from:compare-graphemes: {
        # if (g == expected-grapheme) print "."
        compare g, expected-grapheme
        {
          break-if-!=
          draw-text-wrapping-right-then-down-from-cursor-over-full-screen 0, ".", 3  # 3=cyan
          break $check-screen-row-in-color-from:compare-graphemes
        }
        # otherwise print an error
        count-test-failure
        draw-text-wrapping-right-then-down-from-cursor-over-full-screen 0, msg, 3  # 3=cyan
        draw-text-wrapping-right-then-down-from-cursor-over-full-screen 0, ": expected '", 3
        draw-grapheme-at-cursor 0, expected-grapheme, 3
        move-cursor-rightward-and-downward 0, 0, 0x80  # screen-width
        draw-text-wrapping-right-then-down-from-cursor-over-full-screen 0, "' at (", 3
        draw-int32-hex-wrapping-right-then-down-from-cursor-over-full-screen 0, x, 3
        draw-text-wrapping-right-then-down-from-cursor-over-full-screen 0, ", ", 3
        draw-int32-hex-wrapping-right-then-down-from-cursor-over-full-screen 0, y, 3
        draw-text-wrapping-right-then-down-from-cursor-over-full-screen 0, ") but observed '", 3
        draw-grapheme-at-cursor 0, g, 3
        move-cursor-rightward-and-downward 0, 0, 0x80  # screen-width
        draw-text-wrapping-right-then-down-from-cursor-over-full-screen 0, "'", 3
      }
      $check-screen-row-in-color-from:compare-colors: {
        var color/eax: int <- screen-color-at-idx screen, idx
        compare fg, color
        {
          break-if-!=
          draw-text-wrapping-right-then-down-from-cursor-over-full-screen 0, ".", 3  # 3=cyan
          break $check-screen-row-in-color-from:compare-colors
        }
        # otherwise print an error
        count-test-failure
        draw-text-wrapping-right-then-down-from-cursor-over-full-screen 0, msg, 3  # 3=cyan
        draw-text-wrapping-right-then-down-from-cursor-over-full-screen 0, ": expected '", 3
        draw-grapheme-at-cursor 0, expected-grapheme, 3
        move-cursor-rightward-and-downward 0, 0, 0x80  # screen-width
        draw-text-wrapping-right-then-down-from-cursor-over-full-screen 0, "' at (", 3
        draw-int32-hex-wrapping-right-then-down-from-cursor-over-full-screen 0, x, 3
        draw-text-wrapping-right-then-down-from-cursor-over-full-screen 0, ", ", 3
        draw-int32-hex-wrapping-right-then-down-from-cursor-over-full-screen 0, y, 3
        draw-text-wrapping-right-then-down-from-cursor-over-full-screen 0, ") in color ", 3
        draw-int32-hex-wrapping-right-then-down-from-cursor-over-full-screen 0, fg, 3
        draw-text-wrapping-right-then-down-from-cursor-over-full-screen 0, " but observed color ", 3
        draw-int32-hex-wrapping-right-then-down-from-cursor-over-full-screen 0, color, 3
      }
    }
    idx <- increment
    increment x
    loop
  }
}

fn test-draw-single-grapheme {
  var screen-on-stack: screen
  var screen/esi: (addr screen) <- address screen-on-stack
  initialize-screen screen, 5, 4
  var c/eax: grapheme <- copy 0x61  # 'a'
  draw-grapheme screen, c, 0, 0, 1  # color=1
  check-screen-row screen, 0, "a", "F - test-draw-single-grapheme"  # top-left corner of the screen
}

fn test-draw-multiple-graphemes {
  var screen-on-stack: screen
  var screen/esi: (addr screen) <- address screen-on-stack
  initialize-screen screen, 0x10, 4
  draw-text-wrapping-right-then-down-from-cursor-over-full-screen screen, "Hello, 世界", 1  # color=1
  check-screen-row screen, 0, "Hello, 世界", "F - test-draw-multiple-graphemes"
}