corpus_test.mx raw
1 package iskra
2
3 import "testing"
4
5 func TestLoadCorpus(t *testing.T) {
6 tr, err := LoadCorpus("/tmp/iskra-corpus/utf8")
7 if err != nil {
8 t.Fatal("LoadCorpus: " | err.Error())
9 }
10 if tr.EntryCount() == 0 {
11 t.Fatal("corpus tree has 0 entries")
12 }
13
14 srcCount := 0
15 irCount := 0
16 asmCount := 0
17 binCount := 0
18 for i := range tr.RecMeta {
19 m := &tr.RecMeta[i]
20 if m.Kind == KindUnknown && m.StageTag == 0 {
21 continue
22 }
23 switch m.StageTag {
24 case StageSRC:
25 srcCount++
26 case StageIR:
27 irCount++
28 case StageASM:
29 asmCount++
30 case StageBIN:
31 binCount++
32 }
33 }
34 if srcCount == 0 {
35 t.Fatal("no SRC entries")
36 }
37 if irCount == 0 {
38 t.Fatal("no IR entries")
39 }
40 if asmCount == 0 {
41 t.Fatal("no ASM entries")
42 }
43 if binCount == 0 {
44 t.Fatal("no BIN entries")
45 }
46
47 // Cross-stage links are now key-implicit. Verify at least one SRC entry
48 // has a corresponding IR entry by key lookup.
49 stageLinksFound := 0
50 for i := range tr.RecMeta {
51 m := &tr.RecMeta[i]
52 if m.StageTag != StageSRC {
53 continue
54 }
55 links := tr.StageLinksFor(uint32(i))
56 if len(links) > 0 {
57 stageLinksFound++
58 break
59 }
60 }
61 if stageLinksFound == 0 {
62 t.Fatal("no cross-stage links found")
63 }
64 }
65
66 func TestReverseLookup(t *testing.T) {
67 tr, err := LoadCorpus("/tmp/iskra-corpus/utf8")
68 if err != nil {
69 t.Fatal("LoadCorpus: " | err.Error())
70 }
71
72 // Find a BIN entry and walk back to SRC via key-implicit cross-stage lookup.
73 for i := range tr.RecMeta {
74 m := &tr.RecMeta[i]
75 if m.StageTag != StageBIN || m.Kind == KindUnknown {
76 continue
77 }
78 key, ok := tr.KeyForRecord(uint32(i))
79 if !ok {
80 continue
81 }
82 hash := KeyHash(key)
83 branch := KindToBranch(m.Kind)
84 srcKey := MakeCodeKey(StageSRC, hash)
85 srcRI := tr.LookupRecIdx(branch, srcKey)
86 if srcRI == NullLatticeRec {
87 continue
88 }
89 // Found a BIN entry with a SRC counterpart.
90 binName := tr.FormAt(uint32(i))
91 if len(binName) > 0 {
92 return // success
93 }
94 }
95 t.Fatal("no BIN→SRC reverse path found")
96 }
97