github.com/gagliardetto/golang-go@v0.0.0-20201020153340-53909ea70814/cmd/compile/internal/gc/testdata/append_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  // append_ssa.go tests append operations.
     6  package main
     7  
     8  import "testing"
     9  
    10  //go:noinline
    11  func appendOne_ssa(a []int, x int) []int {
    12  	return append(a, x)
    13  }
    14  
    15  //go:noinline
    16  func appendThree_ssa(a []int, x, y, z int) []int {
    17  	return append(a, x, y, z)
    18  }
    19  
    20  func eqBytes(a, b []int) bool {
    21  	if len(a) != len(b) {
    22  		return false
    23  	}
    24  	for i := range a {
    25  		if a[i] != b[i] {
    26  			return false
    27  		}
    28  	}
    29  	return true
    30  }
    31  
    32  func expect(t *testing.T, got, want []int) {
    33  	if eqBytes(got, want) {
    34  		return
    35  	}
    36  	t.Errorf("expected %v, got %v\n", want, got)
    37  }
    38  
    39  func testAppend(t *testing.T) {
    40  	var store [7]int
    41  	a := store[:0]
    42  
    43  	a = appendOne_ssa(a, 1)
    44  	expect(t, a, []int{1})
    45  	a = appendThree_ssa(a, 2, 3, 4)
    46  	expect(t, a, []int{1, 2, 3, 4})
    47  	a = appendThree_ssa(a, 5, 6, 7)
    48  	expect(t, a, []int{1, 2, 3, 4, 5, 6, 7})
    49  	if &a[0] != &store[0] {
    50  		t.Errorf("unnecessary grow")
    51  	}
    52  	a = appendOne_ssa(a, 8)
    53  	expect(t, a, []int{1, 2, 3, 4, 5, 6, 7, 8})
    54  	if &a[0] == &store[0] {
    55  		t.Errorf("didn't grow")
    56  	}
    57  }
    58  
    59  func TestAppend(t *testing.T) {
    60  	testAppend(t)
    61  }