github.com/c-darwin/mobile@v0.0.0-20160313183840-ff625c46f7c9/bind/java/testpkg/testpkg.go (about)

     1  // Copyright 2014 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  // +build darwin linux
     6  
     7  // Package testpkg contains bound functions for testing the cgo-JNI interface.
     8  // This is used in tests of github.com/c-darwin/mobile/bind/java.
     9  package testpkg
    10  
    11  //go:generate gobind -lang=go -outdir=go_testpkg .
    12  //go:generate gobind -lang=java -outdir=. .
    13  import (
    14  	"errors"
    15  	"fmt"
    16  	"io/ioutil"
    17  	"log"
    18  	"runtime"
    19  	"time"
    20  
    21  	"github.com/c-darwin/mobile/asset"
    22  )
    23  
    24  type I interface {
    25  	F()
    26  
    27  	E() error
    28  	V() int
    29  	VE() (int, error)
    30  	I() I
    31  	S() *S
    32  	StoString(*S) string
    33  
    34  	String() string
    35  }
    36  
    37  func CallF(i I) {
    38  	i.F()
    39  }
    40  
    41  func CallE(i I) error {
    42  	return i.E()
    43  }
    44  
    45  func CallV(i I) int {
    46  	return i.V()
    47  }
    48  
    49  func CallVE(i I) (int, error) {
    50  	return i.VE()
    51  }
    52  
    53  func CallI(i I) I {
    54  	return i
    55  }
    56  
    57  func CallS(i I) *S {
    58  	return &S{}
    59  }
    60  
    61  var keep []I
    62  
    63  func Keep(i I) {
    64  	keep = append(keep, i)
    65  }
    66  
    67  var numSCollected int
    68  
    69  type S struct {
    70  	// *S already has a finalizer, so we need another object
    71  	// to count successful collections.
    72  	innerObj *int
    73  
    74  	name string
    75  }
    76  
    77  func (s *S) F() {
    78  	fmt.Printf("called F on *S{%s}\n", s.name)
    79  }
    80  
    81  func (s *S) String() string {
    82  	return s.name
    83  }
    84  
    85  func finalizeInner(a *int) {
    86  	numSCollected++
    87  }
    88  
    89  func New() *S {
    90  	s := &S{innerObj: new(int), name: "new"}
    91  	runtime.SetFinalizer(s.innerObj, finalizeInner)
    92  	return s
    93  }
    94  
    95  func GC() {
    96  	runtime.GC()
    97  	time.Sleep(10 * time.Millisecond)
    98  	runtime.GC()
    99  }
   100  
   101  func Add(x, y int) int {
   102  	return x + y
   103  }
   104  
   105  func NumSCollected() int {
   106  	return numSCollected
   107  }
   108  
   109  func StrDup(s string) string {
   110  	return s
   111  }
   112  
   113  func Negate(x bool) bool {
   114  	return !x
   115  }
   116  
   117  func Err(s string) error {
   118  	if s != "" {
   119  		return errors.New(s)
   120  	}
   121  	return nil
   122  }
   123  
   124  func BytesAppend(a []byte, b []byte) []byte {
   125  	return append(a, b...)
   126  }
   127  
   128  func AppendToString(str string, someBytes []byte) []byte {
   129  	a := []byte(str)
   130  	fmt.Printf("str=%q (len=%d), someBytes=%v (len=%d)\n", str, len(str), someBytes, len(someBytes))
   131  	return append(a, someBytes...)
   132  }
   133  
   134  func UnnamedParams(_, _ int, p0 string) int {
   135  	return len(p0)
   136  }
   137  
   138  type Node struct {
   139  	V    string
   140  	Next *Node
   141  	Err  error
   142  }
   143  
   144  func NewNode(name string) *Node {
   145  	return &Node{V: name}
   146  }
   147  
   148  func (a *Node) String() string {
   149  	if a == nil {
   150  		return "<end>"
   151  	}
   152  	return a.V + ":" + a.Next.String()
   153  }
   154  
   155  type Receiver interface {
   156  	Hello(message string)
   157  }
   158  
   159  func Hello(r Receiver, name string) {
   160  	r.Hello(fmt.Sprintf("Hello, %s!\n", name))
   161  }
   162  
   163  func GarbageCollect() {
   164  	runtime.GC()
   165  }
   166  
   167  func ReadAsset() string {
   168  	rc, err := asset.Open("hello.txt")
   169  	if err != nil {
   170  		log.Fatal(err)
   171  	}
   172  	defer rc.Close()
   173  
   174  	b, err := ioutil.ReadAll(rc)
   175  	if err != nil {
   176  		log.Fatal(err)
   177  	}
   178  	return string(b)
   179  }