github.com/graybobo/golang.org-package-offline-cache@v0.0.0-20200626051047-6608995c132f/x/mobile/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  	"math"
    14  	"runtime"
    15  	"time"
    16  )
    17  
    18  const (
    19  	AString = "a string"
    20  	AnInt   = 7
    21  	ABool   = true
    22  	AFloat  = 0.12345
    23  
    24  	MinInt32               int32   = math.MinInt32
    25  	MaxInt32               int32   = math.MaxInt32
    26  	MinInt64                       = math.MinInt64
    27  	MaxInt64                       = math.MaxInt64
    28  	SmallestNonzeroFloat64         = math.SmallestNonzeroFloat64
    29  	MaxFloat64                     = math.MaxFloat64
    30  	SmallestNonzeroFloat32 float32 = math.SmallestNonzeroFloat64
    31  	MaxFloat32             float32 = math.MaxFloat32
    32  	Log2E                          = math.Log2E
    33  )
    34  
    35  var (
    36  	StringVar    = "a string var"
    37  	IntVar       = 77
    38  	StructVar    = &Node{V: "a struct var"}
    39  	InterfaceVar I
    40  )
    41  
    42  type I interface {
    43  	Times(v int32) int64
    44  	Error(triggerError bool) error
    45  
    46  	StringError(s string) (string, error)
    47  }
    48  
    49  type myI struct{}
    50  
    51  func (i *myI) Times(v int32) int64 {
    52  	return int64(v) * 10
    53  }
    54  
    55  func (i *myI) Error(e bool) error {
    56  	if e {
    57  		return errors.New("some error")
    58  	}
    59  	return nil
    60  }
    61  
    62  func (i *myI) StringError(s string) (string, error) {
    63  	return s, nil
    64  }
    65  
    66  func CallIError(i I, triggerError bool) error {
    67  	return i.Error(triggerError)
    68  }
    69  
    70  func CallIStringError(i I, s string) (string, error) {
    71  	return i.StringError(s)
    72  }
    73  
    74  func NewI() I {
    75  	return &myI{}
    76  }
    77  
    78  var pinnedI = make(map[int32]I)
    79  
    80  func RegisterI(idx int32, i I) {
    81  	pinnedI[idx] = i
    82  }
    83  
    84  func UnregisterI(idx int32) {
    85  	delete(pinnedI, idx)
    86  }
    87  
    88  func Multiply(idx int32, val int32) int64 {
    89  	i, ok := pinnedI[idx]
    90  	if !ok {
    91  		panic(fmt.Sprintf("unknown I with index %d", idx))
    92  	}
    93  	return i.Times(val)
    94  }
    95  
    96  func GC() {
    97  	runtime.GC()
    98  }
    99  
   100  func Hi() {
   101  	fmt.Println("Hi")
   102  }
   103  
   104  func Int(x int32) {
   105  	fmt.Println("Received int32", x)
   106  }
   107  
   108  func Sum(x, y int64) int64 {
   109  	return x + y
   110  }
   111  
   112  func Hello(s string) string {
   113  	return fmt.Sprintf("Hello, %s!", s)
   114  }
   115  
   116  func BytesAppend(a []byte, b []byte) []byte {
   117  	return append(a, b...)
   118  }
   119  
   120  func ReturnsError(b bool) (string, error) {
   121  	if b {
   122  		return "", errors.New("Error")
   123  	}
   124  	return "OK", nil
   125  }
   126  
   127  var collectS = make(chan struct{}, 100)
   128  
   129  func finalizeS(a *S) {
   130  	collectS <- struct{}{}
   131  }
   132  
   133  func CollectS(want, timeoutSec int) int {
   134  	runtime.GC()
   135  
   136  	tick := time.NewTicker(time.Duration(timeoutSec) * time.Second)
   137  	defer tick.Stop()
   138  
   139  	for i := 0; i < want; i++ {
   140  		select {
   141  		case <-collectS:
   142  		case <-tick.C:
   143  			fmt.Println("CollectS: timed out")
   144  			return i
   145  		}
   146  	}
   147  	return want
   148  }
   149  
   150  type S struct {
   151  	X, Y       float64
   152  	unexported bool
   153  }
   154  
   155  func NewS(x, y float64) *S {
   156  	s := &S{X: x, Y: y}
   157  	runtime.SetFinalizer(s, finalizeS)
   158  	return s
   159  }
   160  
   161  func (s *S) TryTwoStrings(first, second string) string {
   162  	return first + second
   163  }
   164  
   165  func (s *S) Sum() float64 {
   166  	return s.X + s.Y
   167  }
   168  
   169  func CallSSum(s *S) float64 {
   170  	return s.Sum()
   171  }
   172  
   173  type Node struct {
   174  	V   string
   175  	Err error
   176  }
   177  
   178  func NewNode(name string) *Node {
   179  	return &Node{V: name}
   180  }
   181  
   182  // issue 13004
   183  type StructThatStartsWithLetterBeforeZ struct {
   184  	Value Z
   185  }
   186  
   187  type Z interface {
   188  }
   189  
   190  func Echo(s string) string {
   191  	return s
   192  }