| 1 | package test |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "testing" |
| 6 | |
| 7 | "review.coreboot.org/coreboot.git/util/intelp2m/config/p2m" |
| 8 | "review.coreboot.org/coreboot.git/util/intelp2m/platforms" |
| 9 | "review.coreboot.org/coreboot.git/util/intelp2m/platforms/common" |
| 10 | "review.coreboot.org/coreboot.git/util/intelp2m/platforms/common/fields" |
| 11 | ) |
| 12 | |
| 13 | type ( |
| 14 | Pad struct { |
| 15 | ID string |
| 16 | DW0 uint32 |
| 17 | DW1 uint32 |
| 18 | Ownership bool |
| 19 | } |
| 20 | Macro struct { |
| 21 | Short string |
| 22 | Long string |
| 23 | } |
| 24 | TestCase struct { |
| 25 | Pad |
| 26 | Macro |
| 27 | } |
| 28 | Suite []TestCase |
| 29 | ) |
| 30 | |
| 31 | func (p Pad) toShortMacro() string { |
| 32 | if err := p2m.SetFieldType("none"); err != nil { |
| 33 | panic(err) |
| 34 | } |
| 35 | p2m.Config.AutoCheck = false |
| 36 | constructor, err := platforms.GetConstructor() |
| 37 | if err != nil { |
| 38 | panic(err) |
| 39 | } |
| 40 | macro := common.CreateFrom( |
| 41 | p.ID, |
| 42 | p.Ownership, |
| 43 | constructor(p.DW0, p.DW1), |
| 44 | fields.Get(), |
| 45 | ) |
| 46 | return macro.Generate() |
| 47 | } |
| 48 | |
| 49 | func (p Pad) toLongMacro() string { |
| 50 | if err := p2m.SetFieldType("cb"); err != nil { |
| 51 | panic(err) |
| 52 | } |
| 53 | p2m.Config.AutoCheck = true |
| 54 | constructor, err := platforms.GetConstructor() |
| 55 | if err != nil { |
| 56 | panic(err) |
| 57 | } |
| 58 | macro := common.CreateFrom( |
| 59 | p.ID, |
| 60 | p.Ownership, |
| 61 | constructor(p.DW0, p.DW1), |
| 62 | fields.Get(), |
| 63 | ) |
| 64 | return macro.Generate() |
| 65 | } |
| 66 | |
| 67 | func (m *Macro) check(pad Pad) error { |
| 68 | var format string = "%s macro\nExpects: '%s'\nActually: '%s'\n\n" |
| 69 | if actually := pad.toLongMacro(); m.Long != "" && m.Long != actually { |
| 70 | return fmt.Errorf(format, "LONG", m.Long, actually) |
| 71 | } |
| 72 | if actually := pad.toShortMacro(); m.Short != "" && m.Short != actually { |
| 73 | return fmt.Errorf(format, "SHORT", m.Short, actually) |
| 74 | } |
| 75 | return nil |
| 76 | } |
| 77 | |
| 78 | func (suite Suite) Run(t *testing.T, label string) { |
| 79 | t.Run(label, func(t *testing.T) { |
| 80 | for _, testcase := range suite { |
| 81 | if err := testcase.Macro.check(testcase.Pad); err != nil { |
| 82 | t.Errorf("Test failed for pad %s\n%v", testcase.Pad.ID, err) |
| 83 | } |
| 84 | } |
| 85 | }) |
| 86 | } |