diff --git a/bind/java/seq_test.go b/bind/java/seq_test.go index fb2a245..22a29d0 100644 --- a/bind/java/seq_test.go +++ b/bind/java/seq_test.go @@ -19,7 +19,7 @@ import ( // This requires the gradle command in PATH and // the Android SDK whose path is available through ANDROID_HOME environment variable. func TestJavaSeqTest(t *testing.T) { - runTest(t, "golang.org/x/mobile/bind/java/testpkg", "SeqTest") + runTest(t, "golang.org/x/mobile/bind/testpkg", "SeqTest") } // TestJavaSeqBench runs java test SeqBench.java, with the same diff --git a/bind/objc/SeqTest.m b/bind/objc/SeqTest.m index 15c75ee..69bda57 100644 --- a/bind/objc/SeqTest.m +++ b/bind/objc/SeqTest.m @@ -9,7 +9,7 @@ #import "testpkg/Testpkg.h" // Objective-C implementation of testpkg.I. -@interface Number : NSObject { +@interface Number : NSObject { } @property int32_t value; @@ -96,13 +96,13 @@ static int numI = 0; GoTestpkgInt(42); } -- (void)testSum { - int64_t sum = GoTestpkgSum(31, 21); +- (void)testAdd { + int64_t sum = GoTestpkgAdd(31, 21); XCTAssertEqual(sum, 52, @"GoTestpkgSum(31, 21) = %lld, want 52\n", sum); } - (void)testHello:(NSString *)input { - NSString *got = GoTestpkgHello(input); + NSString *got = GoTestpkgAppendHello(input); NSString *want = [NSString stringWithFormat:@"Hello, %@!", input]; XCTAssertEqualObjects(got, want, @"want %@\nGoTestpkgHello(%@)= %@", want, input, got); } @@ -117,23 +117,23 @@ static int numI = 0; - (void)testString { NSString *input = @""; - NSString *got = GoTestpkgEcho(input); + NSString *got = GoTestpkgStrDup(input); XCTAssertEqualObjects(got, input, @"want %@\nGoTestpkgEcho(%@)= %@", input, input, got); input = @"FOO"; - got = GoTestpkgEcho(input); + got = GoTestpkgStrDup(input); XCTAssertEqualObjects(got, input, @"want %@\nGoTestpkgEcho(%@)= %@", input, input, got); } - (void)testStruct { - GoTestpkgS *s = GoTestpkgNewS(10.0, 100.0); - XCTAssertNotNil(s, @"GoTestpkgNewS returned NULL"); + GoTestpkgS2 *s = GoTestpkgNewS2(10.0, 100.0); + XCTAssertNotNil(s, @"GoTestpkgNewS2 returned NULL"); double x = [s x]; double y = [s y]; double sum = [s sum]; XCTAssertTrue(x == 10.0 && y == 100.0 && sum == 110.0, - @"GoTestpkgS(10.0, 100.0).X=%f Y=%f SUM=%f; want 10, 100, 110", x, y, sum); + @"GoTestpkgS2(10.0, 100.0).X=%f Y=%f SUM=%f; want 10, 100, 110", x, y, sum); double sum2 = GoTestpkgCallSSum(s); XCTAssertEqual(sum, sum2, @"GoTestpkgCallSSum(s)=%f; want %f as returned by s.Sum", sum2, sum); @@ -144,7 +144,7 @@ static int numI = 0; y = [s y]; sum = [s sum]; XCTAssertTrue(x == 7 && y == 70 && sum == 77, - @"GoTestpkgS(7, 70).X=%f Y=%f SUM=%f; want 7, 70, 77", x, y, sum); + @"GoTestpkgS2(7, 70).X=%f Y=%f SUM=%f; want 7, 70, 77", x, y, sum); NSString *first = @"trytwotested"; NSString *second = @"test"; @@ -159,7 +159,7 @@ static int numI = 0; } GoTestpkgGC(); - int numS = GoTestpkgCollectS( + int numS = GoTestpkgCollectS2( 1, 10); // within 10 seconds, collect the S used in testStruct. XCTAssertEqual(numS, 1, @"%d S objects were collected; S used in testStruct is supposed to " @"be collected.", @@ -178,7 +178,7 @@ static int numI = 0; - (void)testInterface { // Test Go object implementing testpkg.I is handled correctly. - id goObj = GoTestpkgNewI(); + id goObj = GoTestpkgNewI(); int64_t got = [goObj times:10]; XCTAssertEqual(got, 100, @"GoTestpkgNewI().times(10) = %lld; want %d", got, 100); int32_t key = -1; @@ -268,20 +268,20 @@ static int numI = 0; i = [GoTestpkg intVar]; XCTAssertEqual(i, 7777, @"GoTestpkg.intVar = %lld, want 7777", i); - GoTestpkgNode *n0 = GoTestpkg.structVar; - XCTAssertEqualObjects(n0.v, @"a struct var", @"GoTestpkg.structVar = %@, want 'a struct var'", n0.v); + GoTestpkgNode *n0 = GoTestpkg.nodeVar; + XCTAssertEqualObjects(n0.v, @"a struct var", @"GoTestpkg.NodeVar = %@, want 'a struct var'", n0.v); GoTestpkgNode *n1 = GoTestpkgNewNode(@"a new struct var"); - GoTestpkg.structVar = n1; - GoTestpkgNode *n2 = GoTestpkg.structVar; - XCTAssertEqualObjects(n2.v, @"a new struct var", @"GoTestpkg.StructVar = %@, want 'a new struct var'", n2.v); + GoTestpkg.nodeVar = n1; + GoTestpkgNode *n2 = GoTestpkg.nodeVar; + XCTAssertEqualObjects(n2.v, @"a new struct var", @"GoTestpkg.NodeVar = %@, want 'a new struct var'", n2.v); Number *num = [[Number alloc] init]; num.value = 12345; - GoTestpkg.interfaceVar = num; - id iface = GoTestpkg.interfaceVar; + GoTestpkg.interfaceVar2 = num; + id iface = GoTestpkg.interfaceVar2; int64_t x = [iface times:10]; int64_t y = [num times:10]; - XCTAssertEqual(x, y, @"GoTestpkg.InterfaceVar Times 10 = %lld, want %lld", x, y); + XCTAssertEqual(x, y, @"GoTestpkg.InterfaceVar2 Times 10 = %lld, want %lld", x, y); } - (void)testIssue12403 { @@ -296,15 +296,15 @@ static int numI = 0; XCTAssertEqualObjects(ret, @"OK", @"GoTestpkgCallIStringError(Number, 'number') returned unexpected results %@", ret); } -- (void)testStringDup:(NSString *)want { - NSString *got = GoTestpkgStringDup(want); - XCTAssertEqualObjects(want, got, @"StringDup returned %@; expected %@", got, want); +- (void)testStrDup:(NSString *)want { + NSString *got = GoTestpkgStrDup(want); + XCTAssertEqualObjects(want, got, @"StrDup returned %@; expected %@", got, want); } - (void)testUnicodeStrings { - [self testStringDup:@"abcxyz09{}"]; - [self testStringDup:@"Hello, 世界"]; - [self testStringDup:@"\uffff\U00010000\U00010001\U00012345\U0010ffff"]; + [self testStrDup:@"abcxyz09{}"]; + [self testStrDup:@"Hello, 世界"]; + [self testStrDup:@"\uffff\U00010000\U00010001\U00012345\U0010ffff"]; } - (void)testByteArrayRead { diff --git a/bind/objc/seq_test.go b/bind/objc/seq_test.go index be7f68f..6ef1bee 100644 --- a/bind/objc/seq_test.go +++ b/bind/objc/seq_test.go @@ -37,7 +37,7 @@ var destination = flag.String("device", "platform=iOS Simulator,name=iPhone 6s P // TestObjcSeqTest runs ObjC test SeqTest.m. // This requires the xcode command lines tools. func TestObjcSeqTest(t *testing.T) { - runTest(t, "golang.org/x/mobile/bind/objc/testpkg", "xcodetest", "SeqTest.m", false) + runTest(t, "golang.org/x/mobile/bind/testpkg", "xcodetest", "SeqTest.m", false) } // TestObjcSeqBench runs ObjC test SeqBench.m. diff --git a/bind/objc/testpkg/testpkg.go b/bind/objc/testpkg/testpkg.go deleted file mode 100644 index 776a953..0000000 --- a/bind/objc/testpkg/testpkg.go +++ /dev/null @@ -1,235 +0,0 @@ -// Copyright 2015 The Go 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 testpkg - -//go:generate gobind -lang=go -outdir=go_testpkg golang.org/x/mobile/bind/objc/testpkg -//go:generate gobind -lang=objc -outdir=go_testpkg golang.org/x/mobile/bind/objc/testpkg - -import ( - "errors" - "fmt" - "math" - "runtime" - "time" -) - -const ( - AString = "a string" - AnInt = 7 - ABool = true - AFloat = 0.12345 - - ALongString = "LongString,LongString,LongString,LongString,LongString,LongString,LongString,LongString,LongString,LongString,LongString,LongString,LongString,LongString,LongString,LongString,LongString,LongString,LongString,LongString,LongString,LongString,LongString,LongString,LongString,LongString,LongString,LongString,LongString,LongString,LongString,LongString,LongString,LongString,LongString,LongString,LongString,LongString,LongString,LongString,LongString,LongString,LongString,LongString,LongString,LongString,LongString,LongString,LongString,LongString,LongString,LongString,LongString,LongString" - - MinInt32 int32 = math.MinInt32 - MaxInt32 int32 = math.MaxInt32 - MinInt64 = math.MinInt64 - MaxInt64 = math.MaxInt64 - SmallestNonzeroFloat64 = math.SmallestNonzeroFloat64 - MaxFloat64 = math.MaxFloat64 - SmallestNonzeroFloat32 float32 = math.SmallestNonzeroFloat64 - MaxFloat32 float32 = math.MaxFloat32 - Log2E = math.Log2E -) - -var ( - StringVar = "a string var" - IntVar = 77 - StructVar = &Node{V: "a struct var"} - InterfaceVar I -) - -type I interface { - Times(v int32) int64 - Error(triggerError bool) error - - StringError(s string) (string, error) -} - -type myI struct{} - -func (i *myI) Times(v int32) int64 { - return int64(v) * 10 -} - -func (i *myI) Error(e bool) error { - if e { - return errors.New("some error") - } - return nil -} - -func (i *myI) StringError(s string) (string, error) { - return s, nil -} - -func CallIError(i I, triggerError bool) error { - return i.Error(triggerError) -} - -func CallIStringError(i I, s string) (string, error) { - return i.StringError(s) -} - -func NewI() I { - return &myI{} -} - -var pinnedI = make(map[int32]I) - -func RegisterI(idx int32, i I) { - pinnedI[idx] = i -} - -func UnregisterI(idx int32) { - delete(pinnedI, idx) -} - -func Multiply(idx int32, val int32) int64 { - i, ok := pinnedI[idx] - if !ok { - panic(fmt.Sprintf("unknown I with index %d", idx)) - } - return i.Times(val) -} - -func GC() { - runtime.GC() - // Allow for finalizers to run and release any foreign references - time.Sleep(100 * time.Millisecond) -} - -func Hi() { - fmt.Println("Hi") -} - -func Int(x int32) { - fmt.Println("Received int32", x) -} - -func Sum(x, y int64) int64 { - return x + y -} - -func Hello(s string) string { - return fmt.Sprintf("Hello, %s!", s) -} - -func BytesAppend(a []byte, b []byte) []byte { - return append(a, b...) -} - -func ReturnsError(b bool) (string, error) { - if b { - return "", errors.New("Error") - } - return "OK", nil -} - -var collectS = make(chan struct{}, 100) - -func finalizeS(a *S) { - collectS <- struct{}{} -} - -func CollectS(want, timeoutSec int) int { - runtime.GC() - - tick := time.NewTicker(time.Duration(timeoutSec) * time.Second) - defer tick.Stop() - - for i := 0; i < want; i++ { - select { - case <-collectS: - case <-tick.C: - fmt.Println("CollectS: timed out") - return i - } - } - return want -} - -type S struct { - X, Y float64 - unexported bool -} - -func NewS(x, y float64) *S { - s := &S{X: x, Y: y} - runtime.SetFinalizer(s, finalizeS) - return s -} - -func (s *S) TryTwoStrings(first, second string) string { - return first + second -} - -func (s *S) Sum() float64 { - return s.X + s.Y -} - -func CallSSum(s *S) float64 { - return s.Sum() -} - -type Node struct { - V string - Err error -} - -func NewNode(name string) *Node { - return &Node{V: name} -} - -// issue 13004 -type StructThatStartsWithLetterBeforeZ struct { - Value Z -} - -type Z interface { -} - -func Echo(s string) string { - return s -} - -type NullTest interface { - Null() NullTest -} - -func NewNullInterface() I { - return nil -} - -func NewNullStruct() *S { - return nil -} - -func CallWithNull(_null NullTest, nuller NullTest) bool { - return _null == nil && nuller.Null() == nil -} - -func ReadIntoByteArray(s []byte) (int, error) { - if len(s) != cap(s) { - return 0, fmt.Errorf("cap %d != len %d", cap(s), len(s)) - } - for i := 0; i < len(s); i++ { - s[i] = byte(i) - } - return len(s), nil -} - -// Issue #13033 -type NullFieldStruct struct { - F *S -} - -func NewNullFieldStruct() *NullFieldStruct { - return &NullFieldStruct{} -} - -func StringDup(s string) string { - return s -} diff --git a/bind/testdata/basictypes.go b/bind/testdata/basictypes.go index 6d9577b..1fc02e6 100644 --- a/bind/testdata/basictypes.go +++ b/bind/testdata/basictypes.go @@ -5,12 +5,13 @@ package basictypes const ( - AString = "a string" - AnInt = 7 - AnInt2 = 1<<63 - 1 - AFloat = 0.2015 - ARune = rune(32) - ABool = true + AString = "a string" + AnInt = 7 + AnInt2 = 1<<63 - 1 + AFloat = 0.2015 + ARune = rune(32) + ABool = true + ALongString = "LongString,LongString,LongString,LongString,LongString,LongString,LongString,LongString,LongString,LongString,LongString,LongString,LongString,LongString,LongString,LongString,LongString,LongString,LongString,LongString,LongString,LongString,LongString,LongString,LongString,LongString,LongString,LongString,LongString,LongString,LongString,LongString,LongString,LongString,LongString,LongString,LongString,LongString,LongString,LongString,LongString,LongString,LongString,LongString,LongString,LongString,LongString,LongString,LongString,LongString,LongString,LongString,LongString,LongString" ) func Ints(x int8, y int16, z int32, t int64, u int) {} diff --git a/bind/testdata/basictypes.java.golden b/bind/testdata/basictypes.java.golden index f3fecc9..489d5fe 100644 --- a/bind/testdata/basictypes.java.golden +++ b/bind/testdata/basictypes.java.golden @@ -18,6 +18,7 @@ public abstract class Basictypes { public static final boolean ABool = true; public static final double AFloat = 0.2015; + public static final String ALongString = "LongString,LongString,LongString,LongString,LongString,LongString,LongString,LongString,LongString,LongString,LongString,LongString,LongString,LongString,LongString,LongString,LongString,LongString,LongString,LongString,LongString,LongString,LongString,LongString,LongString,LongString,LongString,LongString,LongString,LongString,LongString,LongString,LongString,LongString,LongString,LongString,LongString,LongString,LongString,LongString,LongString,LongString,LongString,LongString,LongString,LongString,LongString,LongString,LongString,LongString,LongString,LongString,LongString,LongString"; public static final int ARune = 32; public static final String AString = "a string"; public static final long AnInt = 7L; diff --git a/bind/testdata/basictypes.objc.h.golden b/bind/testdata/basictypes.objc.h.golden index 966b037..707e6ce 100644 --- a/bind/testdata/basictypes.objc.h.golden +++ b/bind/testdata/basictypes.objc.h.golden @@ -10,6 +10,7 @@ FOUNDATION_EXPORT const BOOL GoBasictypesABool; FOUNDATION_EXPORT const double GoBasictypesAFloat; +FOUNDATION_EXPORT NSString* const GoBasictypesALongString; FOUNDATION_EXPORT const int32_t GoBasictypesARune; FOUNDATION_EXPORT NSString* const GoBasictypesAString; FOUNDATION_EXPORT const int64_t GoBasictypesAnInt; diff --git a/bind/testdata/basictypes.objc.m.golden b/bind/testdata/basictypes.objc.m.golden index 8ee9e62..d3b7b4e 100644 --- a/bind/testdata/basictypes.objc.m.golden +++ b/bind/testdata/basictypes.objc.m.golden @@ -16,6 +16,7 @@ static NSString* errDomain = @"go.basictypes"; const BOOL GoBasictypesABool = YES; const double GoBasictypesAFloat = 0.2015; +NSString* const GoBasictypesALongString = @"LongString,LongString,LongString,LongString,LongString,LongString,LongString,LongString,LongString,LongString,LongString,LongString,LongString,LongString,LongString,LongString,LongString,LongString,LongString,LongString,LongString,LongString,LongString,LongString,LongString,LongString,LongString,LongString,LongString,LongString,LongString,LongString,LongString,LongString,LongString,LongString,LongString,LongString,LongString,LongString,LongString,LongString,LongString,LongString,LongString,LongString,LongString,LongString,LongString,LongString,LongString,LongString,LongString,LongString"; const int32_t GoBasictypesARune = 32; NSString* const GoBasictypesAString = @"a string"; const int64_t GoBasictypesAnInt = 7LL; diff --git a/bind/java/testpkg/assets/hello.txt b/bind/testpkg/assets/hello.txt similarity index 100% rename from bind/java/testpkg/assets/hello.txt rename to bind/testpkg/assets/hello.txt diff --git a/bind/java/testpkg/testpkg.go b/bind/testpkg/testpkg.go similarity index 69% rename from bind/java/testpkg/testpkg.go rename to bind/testpkg/testpkg.go index 66f6f82..2333b24 100644 --- a/bind/java/testpkg/testpkg.go +++ b/bind/testpkg/testpkg.go @@ -28,8 +28,6 @@ const ( ABool = true AFloat = 0.12345 - ALongString = "LongString,LongString,LongString,LongString,LongString,LongString,LongString,LongString,LongString,LongString,LongString,LongString,LongString,LongString,LongString,LongString,LongString,LongString,LongString,LongString,LongString,LongString,LongString,LongString,LongString,LongString,LongString,LongString,LongString,LongString,LongString,LongString,LongString,LongString,LongString,LongString,LongString,LongString,LongString,LongString,LongString,LongString,LongString,LongString,LongString,LongString,LongString,LongString,LongString,LongString,LongString,LongString,LongString,LongString" - MinInt32 int32 = math.MinInt32 MaxInt32 int32 = math.MaxInt32 MinInt64 = math.MinInt64 @@ -42,10 +40,12 @@ const ( ) var ( - StringVar = "a string var" - IntVar = 77 - StructVar = &S{name: "a struct var"} - InterfaceVar I + StringVar = "a string var" + IntVar = 77 + StructVar = &S{name: "a struct var"} + InterfaceVar I + InterfaceVar2 I2 + NodeVar = &Node{V: "a struct var"} ) type I interface { @@ -283,3 +283,131 @@ func GoroutineCallback(r Receiver) { }() <-done } + +func Hi() { + fmt.Println("Hi") +} + +func Int(x int32) { + fmt.Println("Received int32", x) +} + +type I2 interface { + Times(v int32) int64 + Error(triggerError bool) error + + StringError(s string) (string, error) +} + +type myI2 struct{} + +func (_ *myI2) Times(v int32) int64 { + return int64(v) * 10 +} + +func (_ *myI2) Error(e bool) error { + if e { + return errors.New("some error") + } + return nil +} + +func (_ *myI2) StringError(s string) (string, error) { + return s, nil +} + +func CallIError(i I2, triggerError bool) error { + return i.Error(triggerError) +} + +func CallIStringError(i I2, s string) (string, error) { + return i.StringError(s) +} + +func NewI() I2 { + return &myI2{} +} + +var pinnedI = make(map[int32]I2) + +func RegisterI(idx int32, i I2) { + pinnedI[idx] = i +} + +func UnregisterI(idx int32) { + delete(pinnedI, idx) +} + +func Multiply(idx int32, val int32) int64 { + i, ok := pinnedI[idx] + if !ok { + panic(fmt.Sprintf("unknown I2 with index %d", idx)) + } + return i.Times(val) +} + +func AppendHello(s string) string { + return fmt.Sprintf("Hello, %s!", s) +} + +func ReturnsError(b bool) (string, error) { + if b { + return "", errors.New("Error") + } + return "OK", nil +} + +var collectS2 = make(chan struct{}, 100) + +func finalizeS(a *S2) { + collectS2 <- struct{}{} +} + +func CollectS2(want, timeoutSec int) int { + runtime.GC() + + tick := time.NewTicker(time.Duration(timeoutSec) * time.Second) + defer tick.Stop() + + for i := 0; i < want; i++ { + select { + case <-collectS2: + case <-tick.C: + fmt.Println("CollectS: timed out") + return i + } + } + return want +} + +type S2 struct { + X, Y float64 + unexported bool +} + +func NewS2(x, y float64) *S2 { + s := &S2{X: x, Y: y} + runtime.SetFinalizer(s, finalizeS) + return s +} + +func (_ *S2) TryTwoStrings(first, second string) string { + return first + second +} + +func (s *S2) Sum() float64 { + return s.X + s.Y +} + +func CallSSum(s *S2) float64 { + return s.Sum() +} + +// Issue #13033 +type NullFieldStruct struct { + F *S +} + +func NewNullFieldStruct() *NullFieldStruct { + return &NullFieldStruct{} +}