...

Source file src/cmd/compile/internal/test/align_test.go

Documentation: cmd/compile/internal/test

     1  // Copyright 2021 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 to make sure that equality functions (and hash
     6  // functions) don't do unaligned reads on architectures
     7  // that can't do unaligned reads. See issue 46283.
     8  
     9  package test
    10  
    11  import "testing"
    12  
    13  type T1 struct {
    14  	x          float32
    15  	a, b, c, d int16 // memequal64
    16  }
    17  type T2 struct {
    18  	x          float32
    19  	a, b, c, d int32 // memequal128
    20  }
    21  
    22  type A2 [2]byte // eq uses a 2-byte load
    23  type A4 [4]byte // eq uses a 4-byte load
    24  type A8 [8]byte // eq uses an 8-byte load
    25  
    26  //go:noinline
    27  func cmpT1(p, q *T1) {
    28  	if *p != *q {
    29  		panic("comparison test wrong")
    30  	}
    31  }
    32  
    33  //go:noinline
    34  func cmpT2(p, q *T2) {
    35  	if *p != *q {
    36  		panic("comparison test wrong")
    37  	}
    38  }
    39  
    40  //go:noinline
    41  func cmpA2(p, q *A2) {
    42  	if *p != *q {
    43  		panic("comparison test wrong")
    44  	}
    45  }
    46  
    47  //go:noinline
    48  func cmpA4(p, q *A4) {
    49  	if *p != *q {
    50  		panic("comparison test wrong")
    51  	}
    52  }
    53  
    54  //go:noinline
    55  func cmpA8(p, q *A8) {
    56  	if *p != *q {
    57  		panic("comparison test wrong")
    58  	}
    59  }
    60  
    61  func TestAlignEqual(t *testing.T) {
    62  	cmpT1(&T1{}, &T1{})
    63  	cmpT2(&T2{}, &T2{})
    64  
    65  	m1 := map[T1]bool{}
    66  	m1[T1{}] = true
    67  	m1[T1{}] = false
    68  	if len(m1) != 1 {
    69  		t.Fatalf("len(m1)=%d, want 1", len(m1))
    70  	}
    71  	m2 := map[T2]bool{}
    72  	m2[T2{}] = true
    73  	m2[T2{}] = false
    74  	if len(m2) != 1 {
    75  		t.Fatalf("len(m2)=%d, want 1", len(m2))
    76  	}
    77  
    78  	type X2 struct {
    79  		y byte
    80  		z A2
    81  	}
    82  	var x2 X2
    83  	cmpA2(&x2.z, &A2{})
    84  	type X4 struct {
    85  		y byte
    86  		z A4
    87  	}
    88  	var x4 X4
    89  	cmpA4(&x4.z, &A4{})
    90  	type X8 struct {
    91  		y byte
    92  		z A8
    93  	}
    94  	var x8 X8
    95  	cmpA8(&x8.z, &A8{})
    96  }
    97  

View as plain text