Source file test/func.go

     1  // run
     2  
     3  // Copyright 2009 The Go Authors. All rights reserved.
     4  // Use of this source code is governed by a BSD-style
     5  // license that can be found in the LICENSE file.
     6  
     7  // Test simple functions.
     8  
     9  package main
    10  
    11  func assertequal(is, shouldbe int, msg string) {
    12  	if is != shouldbe {
    13  		print("assertion fail", msg, "\n")
    14  		panic(1)
    15  	}
    16  }
    17  
    18  func f1() {
    19  }
    20  
    21  func f2(a int) {
    22  }
    23  
    24  func f3(a, b int) int {
    25  	return a + b
    26  }
    27  
    28  func f4(a, b int, c float32) int {
    29  	return (a+b)/2 + int(c)
    30  }
    31  
    32  func f5(a int) int {
    33  	return 5
    34  }
    35  
    36  func f6(a int) (r int) {
    37  	return 6
    38  }
    39  
    40  func f7(a int) (x int, y float32) {
    41  	return 7, 7.0
    42  }
    43  
    44  
    45  func f8(a int) (x int, y float32) {
    46  	return 8, 8.0
    47  }
    48  
    49  type T struct {
    50  	x, y int
    51  }
    52  
    53  func (t *T) m10(a int, b float32) int {
    54  	return (t.x + a) * (t.y + int(b))
    55  }
    56  
    57  
    58  func f9(a int) (i int, f float32) {
    59  	i = 9
    60  	f = 9.0
    61  	return
    62  }
    63  
    64  
    65  func main() {
    66  	f1()
    67  	f2(1)
    68  	r3 := f3(1, 2)
    69  	assertequal(r3, 3, "3")
    70  	r4 := f4(0, 2, 3.0)
    71  	assertequal(r4, 4, "4")
    72  	r5 := f5(1)
    73  	assertequal(r5, 5, "5")
    74  	r6 := f6(1)
    75  	assertequal(r6, 6, "6")
    76  	r7, s7 := f7(1)
    77  	assertequal(r7, 7, "r7")
    78  	assertequal(int(s7), 7, "s7")
    79  	r8, s8 := f8(1)
    80  	assertequal(r8, 8, "r8")
    81  	assertequal(int(s8), 8, "s8")
    82  	r9, s9 := f9(1)
    83  	assertequal(r9, 9, "r9")
    84  	assertequal(int(s9), 9, "s9")
    85  	var t *T = new(T)
    86  	t.x = 1
    87  	t.y = 2
    88  	r10 := t.m10(1, 3.0)
    89  	assertequal(r10, 10, "10")
    90  }
    91  

View as plain text