Skip to content

Commit 7601d94

Browse files
committed
Normalize CGo test results.
This makes the result look like Go 1.14 results, by running a few regex's to wrap functions.
1 parent 9e8cc00 commit 7601d94

File tree

2 files changed

+56
-11
lines changed

2 files changed

+56
-11
lines changed

cgo/cgo_test.go

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"go/types"
1212
"io/ioutil"
1313
"path/filepath"
14+
"regexp"
1415
"runtime"
1516
"strings"
1617
"testing"
@@ -19,6 +20,34 @@ import (
1920
// Pass -update to go test to update the output of the test files.
2021
var flagUpdate = flag.Bool("update", false, "Update images based on test output.")
2122

23+
// normalizeResult normalizes test results across Go versions.
24+
// Currently, it does so by using consistent newlines, and wrapping some
25+
// function contents to be consistent with Go 1.14.
26+
func normalizeResult(result string) string {
27+
actual := strings.Replace(result, "\r\n", "\n", -1)
28+
29+
// The bitfield getter is no longer aligned with setter, so remove spaces.
30+
re := regexp.MustCompile(`(bitfield_.\(\) C\.uchar) +{`)
31+
actual = re.ReplaceAllString(actual, "$1 {")
32+
33+
// The bitfield setter is now wrapped.
34+
re = regexp.MustCompile(`(set_bitfield_.\(.+\)) +{ (.+) }`)
35+
actual = re.ReplaceAllString(actual, "$1 {\n\t$2\n}")
36+
37+
// The unionfield getters are now wrapped, _except_ for single letter fields.
38+
re = regexp.MustCompile(`(unionfield_.{2,}\(\) +[^ ]+) +{ (.+) }`)
39+
actual = re.ReplaceAllString(actual, "$1 {\n\t$2\n}")
40+
41+
// In union_union2d, the unionfield_i getter is no longer aligned with unionfield_d,
42+
// which is now wrapped. This does not affect similarly named field in other unions.
43+
re = regexp.MustCompile(`(union_union2d\) unionfield_i\(\) +[^ ]+) +{`)
44+
actual = re.ReplaceAllString(actual, "$1 {")
45+
re = regexp.MustCompile(`(union_union2d\) unionfield_d\(\) +[^ ]+) +{ (.+) }`)
46+
actual = re.ReplaceAllString(actual, "$1 {\n\t$2\n}")
47+
48+
return actual
49+
}
50+
2251
func TestCGo(t *testing.T) {
2352
var cflags = []string{"--target=armv6m-none-eabi"}
2453

@@ -74,7 +103,7 @@ func TestCGo(t *testing.T) {
74103
if err != nil {
75104
t.Errorf("could not write out CGo AST: %v", err)
76105
}
77-
actual := strings.Replace(string(buf.Bytes()), "\r\n", "\n", -1)
106+
actual := normalizeResult(string(buf.Bytes()))
78107

79108
// Read the file with the expected output, to compare against.
80109
outfile := filepath.Join("testdata", name+".out.go")

cgo/testdata/types.out.go

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -62,12 +62,16 @@ type C.union3_t = C.union_1
6262
type C.union_nested_t = C.union_3
6363
type C.unionarray_t = struct{ arr [10]C.uchar }
6464

65-
func (s *C.struct_4) bitfield_a() C.uchar { return s.__bitfield_1 & 0x1f }
66-
func (s *C.struct_4) set_bitfield_a(value C.uchar) { s.__bitfield_1 = s.__bitfield_1&^0x1f | value&0x1f<<0 }
65+
func (s *C.struct_4) bitfield_a() C.uchar { return s.__bitfield_1 & 0x1f }
66+
func (s *C.struct_4) set_bitfield_a(value C.uchar) {
67+
s.__bitfield_1 = s.__bitfield_1&^0x1f | value&0x1f<<0
68+
}
6769
func (s *C.struct_4) bitfield_b() C.uchar {
6870
return s.__bitfield_1 >> 5 & 0x1
6971
}
70-
func (s *C.struct_4) set_bitfield_b(value C.uchar) { s.__bitfield_1 = s.__bitfield_1&^0x20 | value&0x1<<5 }
72+
func (s *C.struct_4) set_bitfield_b(value C.uchar) {
73+
s.__bitfield_1 = s.__bitfield_1&^0x20 | value&0x1<<5
74+
}
7175
func (s *C.struct_4) bitfield_c() C.uchar {
7276
return s.__bitfield_1 >> 6
7377
}
@@ -100,19 +104,31 @@ func (union *C.union_1) unionfield_s() *C.short { return (*C.short)(unsafe.Point
100104

101105
type C.union_1 struct{ $union uint64 }
102106

103-
func (union *C.union_2) unionfield_area() *C.point2d_t { return (*C.point2d_t)(unsafe.Pointer(&union.$union)) }
104-
func (union *C.union_2) unionfield_solid() *C.point3d_t { return (*C.point3d_t)(unsafe.Pointer(&union.$union)) }
107+
func (union *C.union_2) unionfield_area() *C.point2d_t {
108+
return (*C.point2d_t)(unsafe.Pointer(&union.$union))
109+
}
110+
func (union *C.union_2) unionfield_solid() *C.point3d_t {
111+
return (*C.point3d_t)(unsafe.Pointer(&union.$union))
112+
}
105113

106114
type C.union_2 struct{ $union [3]uint32 }
107115

108-
func (union *C.union_3) unionfield_point() *C.point3d_t { return (*C.point3d_t)(unsafe.Pointer(&union.$union)) }
109-
func (union *C.union_3) unionfield_array() *C.unionarray_t { return (*C.unionarray_t)(unsafe.Pointer(&union.$union)) }
110-
func (union *C.union_3) unionfield_thing() *C.union3_t { return (*C.union3_t)(unsafe.Pointer(&union.$union)) }
116+
func (union *C.union_3) unionfield_point() *C.point3d_t {
117+
return (*C.point3d_t)(unsafe.Pointer(&union.$union))
118+
}
119+
func (union *C.union_3) unionfield_array() *C.unionarray_t {
120+
return (*C.unionarray_t)(unsafe.Pointer(&union.$union))
121+
}
122+
func (union *C.union_3) unionfield_thing() *C.union3_t {
123+
return (*C.union3_t)(unsafe.Pointer(&union.$union))
124+
}
111125

112126
type C.union_3 struct{ $union [2]uint64 }
113127

114-
func (union *C.union_union2d) unionfield_i() *C.int { return (*C.int)(unsafe.Pointer(&union.$union)) }
115-
func (union *C.union_union2d) unionfield_d() *[2]float64 { return (*[2]float64)(unsafe.Pointer(&union.$union)) }
128+
func (union *C.union_union2d) unionfield_i() *C.int { return (*C.int)(unsafe.Pointer(&union.$union)) }
129+
func (union *C.union_union2d) unionfield_d() *[2]float64 {
130+
return (*[2]float64)(unsafe.Pointer(&union.$union))
131+
}
116132

117133
type C.union_union2d struct{ $union [2]uint64 }
118134
type C.enum_option C.int

0 commit comments

Comments
 (0)