github.com/c-darwin/mobile@v0.0.0-20160313183840-ff625c46f7c9/bind/objc/testpkg/testpkg.go (about) 1 // Copyright 2015 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. 4 5 package testpkg 6 7 //go:generate gobind -lang=go -outdir=go_testpkg . 8 //go:generate gobind -lang=objc -outdir=objc_testpkg . 9 10 import ( 11 "errors" 12 "fmt" 13 "runtime" 14 "time" 15 ) 16 17 type I interface { 18 Times(v int32) int64 19 Error(triggerError bool) error 20 } 21 22 type myI struct{} 23 24 func (i *myI) Times(v int32) int64 { 25 return int64(v) * 10 26 } 27 28 func (i *myI) Error(e bool) error { 29 if e { 30 return errors.New("some error") 31 } 32 return nil 33 } 34 35 func CallIError(i I, triggerError bool) error { 36 return i.Error(triggerError) 37 } 38 39 func NewI() I { 40 return &myI{} 41 } 42 43 var pinnedI = make(map[int32]I) 44 45 func RegisterI(idx int32, i I) { 46 pinnedI[idx] = i 47 } 48 49 func UnregisterI(idx int32) { 50 delete(pinnedI, idx) 51 } 52 53 func Multiply(idx int32, val int32) int64 { 54 i, ok := pinnedI[idx] 55 if !ok { 56 panic(fmt.Sprintf("unknown I with index %d", i)) 57 } 58 return i.Times(val) 59 } 60 61 func GC() { 62 runtime.GC() 63 } 64 65 func Hi() { 66 fmt.Println("Hi") 67 } 68 69 func Int(x int32) { 70 fmt.Println("Received int32", x) 71 } 72 73 func Sum(x, y int64) int64 { 74 return x + y 75 } 76 77 func Hello(s string) string { 78 return fmt.Sprintf("Hello, %s!", s) 79 } 80 81 func BytesAppend(a []byte, b []byte) []byte { 82 return append(a, b...) 83 } 84 85 func ReturnsError(b bool) (string, error) { 86 if b { 87 return "", errors.New("Error") 88 } 89 return "OK", nil 90 } 91 92 var collectS = make(chan struct{}, 100) 93 94 func finalizeS(a *S) { 95 collectS <- struct{}{} 96 } 97 98 func CollectS(want, timeoutSec int) int { 99 runtime.GC() 100 101 tick := time.NewTicker(time.Duration(timeoutSec) * time.Second) 102 defer tick.Stop() 103 104 for i := 0; i < want; i++ { 105 select { 106 case <-collectS: 107 case <-tick.C: 108 fmt.Println("CollectS: timed out") 109 return i 110 } 111 } 112 return want 113 } 114 115 type S struct { 116 X, Y float64 117 unexported bool 118 } 119 120 func NewS(x, y float64) *S { 121 s := &S{X: x, Y: y} 122 runtime.SetFinalizer(s, finalizeS) 123 return s 124 } 125 126 func (s *S) TryTwoStrings(first, second string) string { 127 return first + second 128 } 129 130 func (s *S) Sum() float64 { 131 return s.X + s.Y 132 } 133 134 func CallSSum(s *S) float64 { 135 return s.Sum() 136 } 137 138 type Node struct { 139 V string 140 Err error 141 } 142 143 func NewNode(name string) *Node { 144 return &Node{V: name} 145 }