forked from mkrautz/objc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtypeinfo_386_test.go
99 lines (83 loc) · 2.18 KB
/
typeinfo_386_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
// Copyright (c) 2013 The 'objc' Package Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package objc
import (
"bytes"
"errors"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"testing"
)
var prog = `
#include <objc/runtime.h>
#include <stdio.h>
#import <Foundation/Foundation.h>
@interface TestObject : NSObject {
}
- (void) testMethod:(__TEST_TYPE__)arg;
@end
@implementation TestObject
- (void) testMethod:(__TEST_TYPE__)arg {
}
@end
int main(int argc, char *argv[]) {
Method m = class_getInstanceMethod([TestObject class], @selector(testMethod:));;
printf("%s\n", method_getTypeEncoding(m));
}`
// typeInfoForCType runs clang on the `prog' test
// program above to determine the correct typeinfo
// kinds for 386.
func typeInfoForCType(ctype string) (string, error) {
tempDir, err := ioutil.TempDir("", "typeinfo")
if err != nil {
return "", err
}
defer os.RemoveAll(tempDir)
progName := filepath.Join(tempDir, "prog")
args := []string{
"-x", "objective-c",
"-arch", "i386",
"-lobjc",
"-D__TEST_TYPE__=" + ctype,
"-", // stdout
"-o", progName,
}
cmd := exec.Command("clang", args...)
cmd.Stdin = bytes.NewBufferString(prog)
buf, err := cmd.CombinedOutput()
if err != nil {
return "", errors.New(string(buf))
}
buf, err = exec.Command(progName).Output()
if err != nil {
return "", err
}
return string(buf), nil
}
// expectTypeInfoMatch calls t.Fatlaf if the the ctype does
// not match the expected encoding, 'enc'.
func expectTypeInfoMatch(t *testing.T, ctype string, enc string) {
typeInfo, err := typeInfoForCType(ctype)
if err != nil {
t.Fatalf("unable to exec test program: %v", err)
return
}
sti := simplifyTypeInfo(typeInfo)
prefix := sti[0:3]
if prefix != "v@:" {
t.Fatalf("bad prefix: %v", prefix)
}
actualEnc := string(sti[3])
if enc != actualEnc {
t.Fatalf("bad typeinfo for %v; got %v, expected %v", ctype, actualEnc, enc)
}
}
// TestTypeInfo386 tests that our expectations for
// type encodings are actually correct.
func TestTypeInfo386(t *testing.T) {
expectTypeInfoMatch(t, "uint64_t", encULongLong)
expectTypeInfoMatch(t, "int64_t", encLongLong)
}