github.com/graybobo/golang.org-package-offline-cache@v0.0.0-20200626051047-6608995c132f/x/mobile/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 golang.org/x/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  	"math"
    19  	"runtime"
    20  	"time"
    21  
    22  	"golang.org/x/mobile/asset"
    23  )
    24  
    25  const (
    26  	AString = "a string"
    27  	AnInt   = 7
    28  	ABool   = true
    29  	AFloat  = 0.12345
    30  
    31  	MinInt32               int32   = math.MinInt32
    32  	MaxInt32               int32   = math.MaxInt32
    33  	MinInt64                       = math.MinInt64
    34  	MaxInt64                       = math.MaxInt64
    35  	SmallestNonzeroFloat64         = math.SmallestNonzeroFloat64
    36  	MaxFloat64                     = math.MaxFloat64
    37  	SmallestNonzeroFloat32 float32 = math.SmallestNonzeroFloat64
    38  	MaxFloat32             float32 = math.MaxFloat32
    39  	Log2E                          = math.Log2E
    40  )
    41  
    42  var (
    43  	StringVar    = "a string var"
    44  	IntVar       = 77
    45  	StructVar    = &S{name: "a struct var"}
    46  	InterfaceVar I
    47  )
    48  
    49  type I interface {
    50  	F()
    51  
    52  	E() error
    53  	V() int
    54  	VE() (int, error)
    55  	I() I
    56  	S() *S
    57  	StoString(*S) string
    58  
    59  	String() string
    60  }
    61  
    62  func CallF(i I) {
    63  	i.F()
    64  }
    65  
    66  func CallE(i I) error {
    67  	return i.E()
    68  }
    69  
    70  func CallV(i I) int {
    71  	return i.V()
    72  }
    73  
    74  func CallVE(i I) (int, error) {
    75  	return i.VE()
    76  }
    77  
    78  func CallI(i I) I {
    79  	return i
    80  }
    81  
    82  func CallS(i I) *S {
    83  	return &S{}
    84  }
    85  
    86  var keep []I
    87  
    88  func Keep(i I) {
    89  	keep = append(keep, i)
    90  }
    91  
    92  var numSCollected int
    93  
    94  type S struct {
    95  	// *S already has a finalizer, so we need another object
    96  	// to count successful collections.
    97  	innerObj *int
    98  
    99  	name string
   100  }
   101  
   102  func (s *S) F() {
   103  	fmt.Printf("called F on *S{%s}\n", s.name)
   104  }
   105  
   106  func (s *S) String() string {
   107  	return s.name
   108  }
   109  
   110  func finalizeInner(a *int) {
   111  	numSCollected++
   112  }
   113  
   114  var seq = 0
   115  
   116  func New() *S {
   117  	s := &S{innerObj: new(int), name: fmt.Sprintf("new%d", seq)}
   118  	runtime.SetFinalizer(s.innerObj, finalizeInner)
   119  	return s
   120  }
   121  
   122  func GC() {
   123  	runtime.GC()
   124  	time.Sleep(10 * time.Millisecond)
   125  	runtime.GC()
   126  }
   127  
   128  func Add(x, y int) int {
   129  	return x + y
   130  }
   131  
   132  func NumSCollected() int {
   133  	return numSCollected
   134  }
   135  
   136  func StrDup(s string) string {
   137  	return s
   138  }
   139  
   140  func Negate(x bool) bool {
   141  	return !x
   142  }
   143  
   144  func Err(s string) error {
   145  	if s != "" {
   146  		return errors.New(s)
   147  	}
   148  	return nil
   149  }
   150  
   151  func BytesAppend(a []byte, b []byte) []byte {
   152  	return append(a, b...)
   153  }
   154  
   155  func AppendToString(str string, someBytes []byte) []byte {
   156  	a := []byte(str)
   157  	fmt.Printf("str=%q (len=%d), someBytes=%v (len=%d)\n", str, len(str), someBytes, len(someBytes))
   158  	return append(a, someBytes...)
   159  }
   160  
   161  func UnnamedParams(_, _ int, p0 string) int {
   162  	return len(p0)
   163  }
   164  
   165  type Node struct {
   166  	V    string
   167  	Next *Node
   168  	Err  error
   169  }
   170  
   171  func NewNode(name string) *Node {
   172  	return &Node{V: name}
   173  }
   174  
   175  func (a *Node) String() string {
   176  	if a == nil {
   177  		return "<end>"
   178  	}
   179  	return a.V + ":" + a.Next.String()
   180  }
   181  
   182  type Receiver interface {
   183  	Hello(message string)
   184  }
   185  
   186  func Hello(r Receiver, name string) {
   187  	r.Hello(fmt.Sprintf("Hello, %s!\n", name))
   188  }
   189  
   190  func GarbageCollect() {
   191  	runtime.GC()
   192  }
   193  
   194  func ReadAsset() string {
   195  	rc, err := asset.Open("hello.txt")
   196  	if err != nil {
   197  		log.Fatal(err)
   198  	}
   199  	defer rc.Close()
   200  
   201  	b, err := ioutil.ReadAll(rc)
   202  	if err != nil {
   203  		log.Fatal(err)
   204  	}
   205  	return string(b)
   206  }