Source file src/cmd/fix/cftype_test.go

     1  // Copyright 2017 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 main
     6  
     7  func init() {
     8  	addTestCases(cftypeTests, cftypefix)
     9  }
    10  
    11  var cftypeTests = []testCase{
    12  	{
    13  		Name: "cftype.localVariable",
    14  		In: `package main
    15  
    16  // typedef const void *CFTypeRef;
    17  import "C"
    18  
    19  func f() {
    20  	var x C.CFTypeRef = nil
    21  	x = nil
    22  	x, x = nil, nil
    23  }
    24  `,
    25  		Out: `package main
    26  
    27  // typedef const void *CFTypeRef;
    28  import "C"
    29  
    30  func f() {
    31  	var x C.CFTypeRef = 0
    32  	x = 0
    33  	x, x = 0, 0
    34  }
    35  `,
    36  	},
    37  	{
    38  		Name: "cftype.globalVariable",
    39  		In: `package main
    40  
    41  // typedef const void *CFTypeRef;
    42  import "C"
    43  
    44  var x C.CFTypeRef = nil
    45  
    46  func f() {
    47  	x = nil
    48  }
    49  `,
    50  		Out: `package main
    51  
    52  // typedef const void *CFTypeRef;
    53  import "C"
    54  
    55  var x C.CFTypeRef = 0
    56  
    57  func f() {
    58  	x = 0
    59  }
    60  `,
    61  	},
    62  	{
    63  		Name: "cftype.EqualArgument",
    64  		In: `package main
    65  
    66  // typedef const void *CFTypeRef;
    67  import "C"
    68  
    69  var x C.CFTypeRef
    70  var y = x == nil
    71  var z = x != nil
    72  `,
    73  		Out: `package main
    74  
    75  // typedef const void *CFTypeRef;
    76  import "C"
    77  
    78  var x C.CFTypeRef
    79  var y = x == 0
    80  var z = x != 0
    81  `,
    82  	},
    83  	{
    84  		Name: "cftype.StructField",
    85  		In: `package main
    86  
    87  // typedef const void *CFTypeRef;
    88  import "C"
    89  
    90  type T struct {
    91  	x C.CFTypeRef
    92  }
    93  
    94  var t = T{x: nil}
    95  `,
    96  		Out: `package main
    97  
    98  // typedef const void *CFTypeRef;
    99  import "C"
   100  
   101  type T struct {
   102  	x C.CFTypeRef
   103  }
   104  
   105  var t = T{x: 0}
   106  `,
   107  	},
   108  	{
   109  		Name: "cftype.FunctionArgument",
   110  		In: `package main
   111  
   112  // typedef const void *CFTypeRef;
   113  import "C"
   114  
   115  func f(x C.CFTypeRef) {
   116  }
   117  
   118  func g() {
   119  	f(nil)
   120  }
   121  `,
   122  		Out: `package main
   123  
   124  // typedef const void *CFTypeRef;
   125  import "C"
   126  
   127  func f(x C.CFTypeRef) {
   128  }
   129  
   130  func g() {
   131  	f(0)
   132  }
   133  `,
   134  	},
   135  	{
   136  		Name: "cftype.ArrayElement",
   137  		In: `package main
   138  
   139  // typedef const void *CFTypeRef;
   140  import "C"
   141  
   142  var x = [3]C.CFTypeRef{nil, nil, nil}
   143  `,
   144  		Out: `package main
   145  
   146  // typedef const void *CFTypeRef;
   147  import "C"
   148  
   149  var x = [3]C.CFTypeRef{0, 0, 0}
   150  `,
   151  	},
   152  	{
   153  		Name: "cftype.SliceElement",
   154  		In: `package main
   155  
   156  // typedef const void *CFTypeRef;
   157  import "C"
   158  
   159  var x = []C.CFTypeRef{nil, nil, nil}
   160  `,
   161  		Out: `package main
   162  
   163  // typedef const void *CFTypeRef;
   164  import "C"
   165  
   166  var x = []C.CFTypeRef{0, 0, 0}
   167  `,
   168  	},
   169  	{
   170  		Name: "cftype.MapKey",
   171  		In: `package main
   172  
   173  // typedef const void *CFTypeRef;
   174  import "C"
   175  
   176  var x = map[C.CFTypeRef]int{nil: 0}
   177  `,
   178  		Out: `package main
   179  
   180  // typedef const void *CFTypeRef;
   181  import "C"
   182  
   183  var x = map[C.CFTypeRef]int{0: 0}
   184  `,
   185  	},
   186  	{
   187  		Name: "cftype.MapValue",
   188  		In: `package main
   189  
   190  // typedef const void *CFTypeRef;
   191  import "C"
   192  
   193  var x = map[int]C.CFTypeRef{0: nil}
   194  `,
   195  		Out: `package main
   196  
   197  // typedef const void *CFTypeRef;
   198  import "C"
   199  
   200  var x = map[int]C.CFTypeRef{0: 0}
   201  `,
   202  	},
   203  	{
   204  		Name: "cftype.Conversion1",
   205  		In: `package main
   206  
   207  // typedef const void *CFTypeRef;
   208  import "C"
   209  
   210  var x C.CFTypeRef
   211  var y = (*unsafe.Pointer)(&x)
   212  `,
   213  		Out: `package main
   214  
   215  // typedef const void *CFTypeRef;
   216  import "C"
   217  
   218  var x C.CFTypeRef
   219  var y = (*unsafe.Pointer)(unsafe.Pointer(&x))
   220  `,
   221  	},
   222  	{
   223  		Name: "cftype.Conversion2",
   224  		In: `package main
   225  
   226  // typedef const void *CFTypeRef;
   227  import "C"
   228  
   229  var x unsafe.Pointer
   230  var y = (*C.CFTypeRef)(&x)
   231  `,
   232  		Out: `package main
   233  
   234  // typedef const void *CFTypeRef;
   235  import "C"
   236  
   237  var x unsafe.Pointer
   238  var y = (*C.CFTypeRef)(unsafe.Pointer(&x))
   239  `,
   240  	},
   241  }
   242  

View as plain text