1 // Copyright 2009 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
14 var typedefTests = map[string]string{
15 "t_ptr_volatile_int": "*volatile int",
16 "t_ptr_const_char": "*const char",
18 "t_ushort": "short unsigned int",
19 "t_func_int_of_float_double": "func(float, double) int",
20 "t_ptr_func_int_of_float_double": "*func(float, double) int",
21 "t_func_ptr_int_of_char_schar_uchar": "func(char, signed char, unsigned char) *int",
22 "t_func_void_of_char": "func(char) void",
23 "t_func_void_of_void": "func() void",
24 "t_func_void_of_ptr_char_dots": "func(*char, ...) void",
25 "t_my_struct": "struct my_struct {vi volatile int@0; x char@4 : 1@7; y int@4 : 4@27; array [40]long long int@8}",
26 "t_my_union": "union my_union {vi volatile int@0; x char@0 : 1@7; y int@0 : 4@28; array [40]long long int@0}",
27 "t_my_enum": "enum my_enum {e1=1; e2=2; e3=-5; e4=1000000000000000}",
28 "t_my_list": "struct list {val short int@0; next *t_my_list@8}",
29 "t_my_tree": "struct tree {left *struct tree@0; right *struct tree@8; val long long unsigned int@16}",
32 func elfData(t *testing.T, name string) *Data {
33 f, err := elf.Open(name)
45 func machoData(t *testing.T, name string) *Data {
46 f, err := macho.Open(name)
59 func TestTypedefsELF(t *testing.T) { testTypedefs(t, elfData(t, "testdata/typedef.elf")) }
61 func TestTypedefsMachO(t *testing.T) {
62 testTypedefs(t, machoData(t, "testdata/typedef.macho"))
65 func testTypedefs(t *testing.T, d *Data) {
67 seen := make(map[string]bool)
71 t.Fatal("r.Next:", err)
76 if e.Tag == TagTypedef {
77 typ, err := d.Type(e.Offset)
79 t.Fatal("d.Type:", err)
81 t1 := typ.(*TypedefType)
83 if ts, ok := t1.Type.(*StructType); ok {
86 typstr = t1.Type.String()
89 if want, ok := typedefTests[t1.Name]; ok {
91 t.Errorf("multiple definitions for %s", t1.Name)
95 t.Errorf("%s:\n\thave %s\n\twant %s", t1.Name, typstr, want)
99 if e.Tag != TagCompileUnit {
104 for k := range typedefTests {
106 t.Errorf("missing %s", k)