github.com/go-asm/go@v1.21.1-0.20240213172139-40c5ead50c48/cmd/compile/test/testdata/compound_test.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  // Test compound objects
     6  
     7  package main
     8  
     9  import (
    10  	"testing"
    11  )
    12  
    13  func string_ssa(a, b string, x bool) string {
    14  	s := ""
    15  	if x {
    16  		s = a
    17  	} else {
    18  		s = b
    19  	}
    20  	return s
    21  }
    22  
    23  func testString(t *testing.T) {
    24  	a := "foo"
    25  	b := "barz"
    26  	if want, got := a, string_ssa(a, b, true); got != want {
    27  		t.Errorf("string_ssa(%v, %v, true) = %v, want %v\n", a, b, got, want)
    28  	}
    29  	if want, got := b, string_ssa(a, b, false); got != want {
    30  		t.Errorf("string_ssa(%v, %v, false) = %v, want %v\n", a, b, got, want)
    31  	}
    32  }
    33  
    34  //go:noinline
    35  func complex64_ssa(a, b complex64, x bool) complex64 {
    36  	var c complex64
    37  	if x {
    38  		c = a
    39  	} else {
    40  		c = b
    41  	}
    42  	return c
    43  }
    44  
    45  //go:noinline
    46  func complex128_ssa(a, b complex128, x bool) complex128 {
    47  	var c complex128
    48  	if x {
    49  		c = a
    50  	} else {
    51  		c = b
    52  	}
    53  	return c
    54  }
    55  
    56  func testComplex64(t *testing.T) {
    57  	var a complex64 = 1 + 2i
    58  	var b complex64 = 3 + 4i
    59  
    60  	if want, got := a, complex64_ssa(a, b, true); got != want {
    61  		t.Errorf("complex64_ssa(%v, %v, true) = %v, want %v\n", a, b, got, want)
    62  	}
    63  	if want, got := b, complex64_ssa(a, b, false); got != want {
    64  		t.Errorf("complex64_ssa(%v, %v, true) = %v, want %v\n", a, b, got, want)
    65  	}
    66  }
    67  
    68  func testComplex128(t *testing.T) {
    69  	var a complex128 = 1 + 2i
    70  	var b complex128 = 3 + 4i
    71  
    72  	if want, got := a, complex128_ssa(a, b, true); got != want {
    73  		t.Errorf("complex128_ssa(%v, %v, true) = %v, want %v\n", a, b, got, want)
    74  	}
    75  	if want, got := b, complex128_ssa(a, b, false); got != want {
    76  		t.Errorf("complex128_ssa(%v, %v, true) = %v, want %v\n", a, b, got, want)
    77  	}
    78  }
    79  
    80  func slice_ssa(a, b []byte, x bool) []byte {
    81  	var s []byte
    82  	if x {
    83  		s = a
    84  	} else {
    85  		s = b
    86  	}
    87  	return s
    88  }
    89  
    90  func testSlice(t *testing.T) {
    91  	a := []byte{3, 4, 5}
    92  	b := []byte{7, 8, 9}
    93  	if want, got := byte(3), slice_ssa(a, b, true)[0]; got != want {
    94  		t.Errorf("slice_ssa(%v, %v, true) = %v, want %v\n", a, b, got, want)
    95  	}
    96  	if want, got := byte(7), slice_ssa(a, b, false)[0]; got != want {
    97  		t.Errorf("slice_ssa(%v, %v, false) = %v, want %v\n", a, b, got, want)
    98  	}
    99  }
   100  
   101  func interface_ssa(a, b interface{}, x bool) interface{} {
   102  	var s interface{}
   103  	if x {
   104  		s = a
   105  	} else {
   106  		s = b
   107  	}
   108  	return s
   109  }
   110  
   111  func testInterface(t *testing.T) {
   112  	a := interface{}(3)
   113  	b := interface{}(4)
   114  	if want, got := 3, interface_ssa(a, b, true).(int); got != want {
   115  		t.Errorf("interface_ssa(%v, %v, true) = %v, want %v\n", a, b, got, want)
   116  	}
   117  	if want, got := 4, interface_ssa(a, b, false).(int); got != want {
   118  		t.Errorf("interface_ssa(%v, %v, false) = %v, want %v\n", a, b, got, want)
   119  	}
   120  }
   121  
   122  func TestCompound(t *testing.T) {
   123  	testString(t)
   124  	testSlice(t)
   125  	testInterface(t)
   126  	testComplex64(t)
   127  	testComplex128(t)
   128  }