/
testpkg.go
206 lines (163 loc) · 3.3 KB
/
testpkg.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
// Copyright 2014 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.
// +build darwin linux
// Package testpkg contains bound functions for testing the cgo-JNI interface.
// This is used in tests of golang.org/x/mobile/bind/java.
package testpkg
//go:generate gobind -lang=go -outdir=go_testpkg .
//go:generate gobind -lang=java -outdir=. .
import (
"errors"
"fmt"
"io/ioutil"
"log"
"math"
"runtime"
"time"
"golang.org/x/mobile/asset"
)
const (
AString = "a string"
AnInt = 7
ABool = true
AFloat = 0.12345
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 = &S{name: "a struct var"}
InterfaceVar I
)
type I interface {
F()
E() error
V() int
VE() (int, error)
I() I
S() *S
StoString(*S) string
String() string
}
func CallF(i I) {
i.F()
}
func CallE(i I) error {
return i.E()
}
func CallV(i I) int {
return i.V()
}
func CallVE(i I) (int, error) {
return i.VE()
}
func CallI(i I) I {
return i
}
func CallS(i I) *S {
return &S{}
}
var keep []I
func Keep(i I) {
keep = append(keep, i)
}
var numSCollected int
type S struct {
// *S already has a finalizer, so we need another object
// to count successful collections.
innerObj *int
name string
}
func (s *S) F() {
fmt.Printf("called F on *S{%s}\n", s.name)
}
func (s *S) String() string {
return s.name
}
func finalizeInner(a *int) {
numSCollected++
}
var seq = 0
func New() *S {
s := &S{innerObj: new(int), name: fmt.Sprintf("new%d", seq)}
runtime.SetFinalizer(s.innerObj, finalizeInner)
return s
}
func GC() {
runtime.GC()
time.Sleep(10 * time.Millisecond)
runtime.GC()
}
func Add(x, y int) int {
return x + y
}
func NumSCollected() int {
return numSCollected
}
func StrDup(s string) string {
return s
}
func Negate(x bool) bool {
return !x
}
func Err(s string) error {
if s != "" {
return errors.New(s)
}
return nil
}
func BytesAppend(a []byte, b []byte) []byte {
return append(a, b...)
}
func AppendToString(str string, someBytes []byte) []byte {
a := []byte(str)
fmt.Printf("str=%q (len=%d), someBytes=%v (len=%d)\n", str, len(str), someBytes, len(someBytes))
return append(a, someBytes...)
}
func UnnamedParams(_, _ int, p0 string) int {
return len(p0)
}
type Node struct {
V string
Next *Node
Err error
}
func NewNode(name string) *Node {
return &Node{V: name}
}
func (a *Node) String() string {
if a == nil {
return "<end>"
}
return a.V + ":" + a.Next.String()
}
type Receiver interface {
Hello(message string)
}
func Hello(r Receiver, name string) {
r.Hello(fmt.Sprintf("Hello, %s!\n", name))
}
func GarbageCollect() {
runtime.GC()
}
func ReadAsset() string {
rc, err := asset.Open("hello.txt")
if err != nil {
log.Fatal(err)
}
defer rc.Close()
b, err := ioutil.ReadAll(rc)
if err != nil {
log.Fatal(err)
}
return string(b)
}