Source file test/interface/struct.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 interface values containing structures.
     8  
     9  package main
    10  
    11  import "os"
    12  
    13  var fail int
    14  
    15  func check(b bool, msg string) {
    16  	if (!b) {
    17  		println("failure in", msg)
    18  		fail++
    19  	}
    20  }
    21  
    22  type I1 interface { Get() int; Put(int) }
    23  
    24  type S1 struct { i int }
    25  func (p S1) Get() int { return p.i }
    26  func (p S1) Put(i int) { p.i = i }
    27  
    28  func f1() {
    29  	s := S1{1}
    30  	var i I1 = s
    31  	i.Put(2)
    32  	check(i.Get() == 1, "f1 i")
    33  	check(s.i == 1, "f1 s")
    34  }
    35  
    36  func f2() {
    37  	s := S1{1}
    38  	var i I1 = &s
    39  	i.Put(2)
    40  	check(i.Get() == 1, "f2 i")
    41  	check(s.i == 1, "f2 s")
    42  }
    43  
    44  func f3() {
    45  	s := &S1{1}
    46  	var i I1 = s
    47  	i.Put(2)
    48  	check(i.Get() == 1, "f3 i")
    49  	check(s.i == 1, "f3 s")
    50  }
    51  
    52  type S2 struct { i int }
    53  func (p *S2) Get() int { return p.i }
    54  func (p *S2) Put(i int) { p.i = i }
    55  
    56  // Disallowed by restriction of values going to pointer receivers
    57  // func f4() {
    58  //	 s := S2{1}
    59  //	 var i I1 = s
    60  //	 i.Put(2)
    61  //	 check(i.Get() == 2, "f4 i")
    62  //	 check(s.i == 1, "f4 s")
    63  // }
    64  
    65  func f5() {
    66  	s := S2{1}
    67  	var i I1 = &s
    68  	i.Put(2)
    69  	check(i.Get() == 2, "f5 i")
    70  	check(s.i == 2, "f5 s")
    71  }
    72  
    73  func f6() {
    74  	s := &S2{1}
    75  	var i I1 = s
    76  	i.Put(2)
    77  	check(i.Get() == 2, "f6 i")
    78  	check(s.i == 2, "f6 s")
    79  }
    80  
    81  type I2 interface { Get() int64; Put(int64) }
    82  
    83  type S3 struct { i, j, k, l int64 }
    84  func (p S3) Get() int64 { return p.l }
    85  func (p S3) Put(i int64) { p.l = i }
    86  
    87  func f7() {
    88  	s := S3{1, 2, 3, 4}
    89  	var i I2 = s
    90  	i.Put(5)
    91  	check(i.Get() == 4, "f7 i")
    92  	check(s.l == 4, "f7 s")
    93  }
    94  
    95  func f8() {
    96  	s := S3{1, 2, 3, 4}
    97  	var i I2 = &s
    98  	i.Put(5)
    99  	check(i.Get() == 4, "f8 i")
   100  	check(s.l == 4, "f8 s")
   101  }
   102  
   103  func f9() {
   104  	s := &S3{1, 2, 3, 4}
   105  	var i I2 = s
   106  	i.Put(5)
   107  	check(i.Get() == 4, "f9 i")
   108  	check(s.l == 4, "f9 s")
   109  }
   110  
   111  type S4 struct { i, j, k, l int64 }
   112  func (p *S4) Get() int64 { return p.l }
   113  func (p *S4) Put(i int64) { p.l = i }
   114  
   115  // Disallowed by restriction of values going to pointer receivers
   116  // func f10() {
   117  //	 s := S4{1, 2, 3, 4}
   118  //	 var i I2 = s
   119  //	 i.Put(5)
   120  //	 check(i.Get() == 5, "f10 i")
   121  //	 check(s.l == 4, "f10 s")
   122  // }
   123  
   124  func f11() {
   125  	s := S4{1, 2, 3, 4}
   126  	var i I2 = &s
   127  	i.Put(5)
   128  	check(i.Get() == 5, "f11 i")
   129  	check(s.l == 5, "f11 s")
   130  }
   131  
   132  func f12() {
   133  	s := &S4{1, 2, 3, 4}
   134  	var i I2 = s
   135  	i.Put(5)
   136  	check(i.Get() == 5, "f12 i")
   137  	check(s.l == 5, "f12 s")
   138  }
   139  
   140  func main() {
   141  	f1()
   142  	f2()
   143  	f3()
   144  //	f4()
   145  	f5()
   146  	f6()
   147  	f7()
   148  	f8()
   149  	f9()
   150  //	f10()
   151  	f11()
   152  	f12()
   153  	if fail > 0 {
   154  		os.Exit(1)
   155  	}
   156  }
   157  

View as plain text