-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathcode_load_test.go
252 lines (222 loc) · 6.13 KB
/
code_load_test.go
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
package graphite
import (
"encoding/json"
"encoding/xml"
"log"
"reflect"
"strings"
"testing"
testdata "github.com/benoitkugler/textlayout-testdata/graphite"
)
func TestCodeString(t *testing.T) {
o := ocPUSH_BYTE
if o.String() != "PUSH_BYTE" {
t.Fatal()
}
}
func TestSimpleInstructions(t *testing.T) {
o := func(v opcode) byte { return byte(v) }
valid := []byte{
o(ocPUSH_BYTE), 43,
o(ocPUSH_BYTE), 42,
o(ocPUSH_BYTE), 11, o(ocPUSH_BYTE), 13, o(ocADD),
o(ocPUSH_BYTE), 4, o(ocSUB),
o(ocCOND),
// o(PUSH_LONG), 0x80, 0x50, 0x00, 0x00,
// o(PUSH_LONG), 0xff, 0xff, 0xff, 0xff,
// o(DIV),
o(ocPOP_RET),
}
simpleUnderflow := []byte{
o(ocPUSH_BYTE), 43,
o(ocPUSH_BYTE), 42,
o(ocPUSH_BYTE), 11, o(ocPUSH_BYTE), 13, o(ocADD),
o(ocPUSH_BYTE), 4, o(ocSUB),
o(ocCOND),
o(ocPUSH_LONG), 0x80, 0x00, 0x00, 0x00,
o(ocPUSH_LONG), 0xff, 0xff, 0xff, 0xff,
o(ocDIV),
o(ocCOND), // Uncomment to cause an underflow
o(ocPOP_RET),
}
invalidDiv := []byte{
o(ocPUSH_BYTE), 43,
o(ocPUSH_BYTE), 42,
o(ocPUSH_LONG), 1, 2, 3, 4,
o(ocPUSH_BYTE), 11, o(ocPUSH_BYTE), 13, o(ocADD),
o(ocPUSH_BYTE), 4, o(ocSUB),
o(ocCOND),
o(ocPUSH_LONG), 0x80, 0x00, 0x00, 0x00,
o(ocPUSH_LONG), 0xff, 0xff, 0xff, 0xff,
o(ocDIV),
o(ocPOP_RET),
}
context := codeContext{NumAttributes: 8, NumFeatures: 1}
prog, err := newCode(false, valid, 0, 0, context, false)
if err != nil {
t.Fatalf("unexpected error: %s", err)
}
_, err = newCode(false, simpleUnderflow, 0, 0, context, false)
if err != underfullStack {
t.Fatalf("expected underfull error, got %s", err)
}
progOverflow, err := newCode(false, invalidDiv, 0, 0, context, false)
if err != nil {
t.Fatalf("unexpected error: %s", err)
}
sm := newSlotMap(new(Segment), false, 0)
m := newMachine(&sm)
m.map_.pushSlot(new(Slot))
out, _, err := m.run(&prog, 1)
if err != nil {
t.Fatalf("unexpected error: %s", err)
}
if out != 42 {
t.Fatalf("expected 42, got %d", out)
}
if _, _, err := m.run(&progOverflow, 1); err != machineDiedEarly {
t.Fatalf("expected machine_died_early error, got %s", err)
}
}
type expectedRule struct {
action []opcode
constraint []opcode
preContext uint8
sortKey uint16
}
// load a .ttx file, produced by fonttools
func readExpectedOpCodes(filename string) [][]expectedRule {
data, err := testdata.Files.ReadFile(filename)
if err != nil {
log.Fatal(err)
}
type xmlDoc struct {
Passes []struct {
Rules []struct {
Action string `xml:"action"`
Constraint string `xml:"constraint"`
PreContext uint8 `xml:"precontext,attr"`
SortKey uint16 `xml:"sortkey,attr"`
} `xml:"rules>rule"`
Index int `xml:"_index,attr"`
} `xml:"Silf>silf>passes>pass"`
}
var doc xmlDoc
err = xml.Unmarshal(data, &doc)
if err != nil {
log.Fatal(err)
}
parseOpCodes := func(s string) []opcode {
chunks := strings.Split(s, "\n")
var out []opcode
mainLoop:
for _, c := range chunks {
c = strings.TrimSpace(c)
if c == "" {
continue
}
// remove the arguments
if i := strings.IndexByte(c, '('); i != -1 {
c = c[:i]
}
for opc, data := range opcodeTable {
if data.name == c {
out = append(out, opcode(opc))
continue mainLoop
}
}
log.Fatalf("unknown opcode: %s", c)
}
return out
}
var out [][]expectedRule
for _, pass := range doc.Passes {
var rules []expectedRule
for _, rule := range pass.Rules {
r := expectedRule{
preContext: rule.PreContext,
sortKey: rule.SortKey,
action: parseOpCodes(rule.Action),
constraint: parseOpCodes(rule.Constraint),
}
rules = append(rules, r)
}
out = append(out, rules)
}
return out
}
func instrsToOpcodes(l []instr) (out []opcode) {
for _, v := range l {
out = append(out, v.code)
}
return out
}
func TestOpCodesValues(t *testing.T) {
testFontOpCodes(t, "Annapurnarc2")
testFontOpCodes(t, "MagyarLinLibertineG")
}
func testFontOpCodes(t *testing.T, fontName string) {
b, err := testdata.Files.ReadFile(fontName + "_bytecodes.json")
if err != nil {
t.Fatal(err)
}
var passes []extractedPass
if err = json.Unmarshal(b, &passes); err != nil {
t.Fatal(err)
}
expected := readExpectedOpCodes(fontName + ".ttx")
if len(passes) != len(expected) {
t.Fatal("invalid length")
}
for i, exp := range expected {
input := passes[i]
if len(input.Rules) != len(exp) {
t.Fatal("invalid length")
}
for j, rule := range exp {
inputRule := input.Rules[j]
context := input.Context
if rule.preContext != inputRule.PreContext {
t.Fatalf("expected %d, got %d", rule.preContext, inputRule.PreContext)
}
if rule.sortKey != inputRule.SortKey {
t.Fatalf("expected %d, got %d", rule.sortKey, inputRule.SortKey)
}
if len(rule.action) != 0 { // test action rule
code := inputRule.Action
loadedCode, err := newCode(false, code, inputRule.PreContext, inputRule.SortKey, context, true)
if err != nil {
t.Fatalf("unexpected error on code %v", code)
}
expectedOpCodes := rule.action
if got := instrsToOpcodes(loadedCode.instrs); !reflect.DeepEqual(got, expectedOpCodes) {
t.Fatalf("expected %v, got %v", expectedOpCodes, got)
}
}
if len(rule.constraint) != 0 { // test constraint rule
code := inputRule.Constraint
loadedCode, err := newCode(true, code, inputRule.PreContext, inputRule.SortKey, context, true)
if err != nil {
t.Fatalf("unexpected error on code %v", code)
}
expectedOpCodes := rule.constraint
if got := instrsToOpcodes(loadedCode.instrs); !reflect.DeepEqual(got, expectedOpCodes) {
t.Fatalf("pass %d, rule %d : expected %v, got %v", i, j, expectedOpCodes, got)
}
}
}
}
}
func TestOpCodesAnalysis(t *testing.T) {
ft := loadGraphite(t, "MagyarLinLibertineG.ttf")
got := instrsToOpcodes(ft.silf[0].passes[0].ruleTable[194].action.instrs)
// extracted from running graphite test magyar1
expected := []opcode{
ocTEMP_COPY, ocPUT_GLYPH, ocNEXT, ocTEMP_COPY, ocPUT_GLYPH, ocNEXT, ocINSERT, ocPUT_COPY,
ocNEXT, ocINSERT, ocPUT_COPY, ocNEXT, ocINSERT, ocPUT_GLYPH, ocNEXT, ocINSERT, ocPUT_GLYPH, ocNEXT,
ocPUSH_BYTE, ocPOP_RET,
}
if !reflect.DeepEqual(got, expected) {
t.Fatalf("expected\n%v\n got \n%v", expected, got)
}
}