1// Copyright (c) 2020 Uber Technologies, Inc. 2// 3// Permission is hereby granted, free of charge, to any person obtaining a copy 4// of this software and associated documentation files (the "Software"), to deal 5// in the Software without restriction, including without limitation the rights 6// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7// copies of the Software, and to permit persons to whom the Software is 8// furnished to do so, subject to the following conditions: 9// 10// The above copyright notice and this permission notice shall be included in 11// all copies or substantial portions of the Software. 12// 13// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 19// THE SOFTWARE. 20 21package atomic 22 23import ( 24 "bytes" 25 "io/ioutil" 26 "os" 27 "os/exec" 28 "path/filepath" 29 "reflect" 30 "testing" 31 32 "github.com/stretchr/testify/assert" 33 "github.com/stretchr/testify/require" 34) 35 36func TestNocmpComparability(t *testing.T) { 37 tests := []struct { 38 desc string 39 give interface{} 40 comparable bool 41 }{ 42 { 43 desc: "nocmp struct", 44 give: nocmp{}, 45 }, 46 { 47 desc: "struct with nocmp embedded", 48 give: struct{ nocmp }{}, 49 }, 50 { 51 desc: "pointer to struct with nocmp embedded", 52 give: &struct{ nocmp }{}, 53 comparable: true, 54 }, 55 56 // All exported types must be uncomparable. 57 {desc: "Bool", give: Bool{}}, 58 {desc: "Duration", give: Duration{}}, 59 {desc: "Error", give: Error{}}, 60 {desc: "Float64", give: Float64{}}, 61 {desc: "Int32", give: Int32{}}, 62 {desc: "Int64", give: Int64{}}, 63 {desc: "String", give: String{}}, 64 {desc: "Uint32", give: Uint32{}}, 65 {desc: "Uint64", give: Uint64{}}, 66 {desc: "Value", give: Value{}}, 67 } 68 69 for _, tt := range tests { 70 t.Run(tt.desc, func(t *testing.T) { 71 typ := reflect.TypeOf(tt.give) 72 assert.Equalf(t, tt.comparable, typ.Comparable(), 73 "type %v comparablity mismatch", typ) 74 }) 75 } 76} 77 78// nocmp must not add to the size of a struct in-memory. 79func TestNocmpSize(t *testing.T) { 80 type x struct{ _ int } 81 82 before := reflect.TypeOf(x{}).Size() 83 84 type y struct { 85 _ nocmp 86 _ x 87 } 88 89 after := reflect.TypeOf(y{}).Size() 90 91 assert.Equal(t, before, after, 92 "expected nocmp to have no effect on struct size") 93} 94 95// This test will fail to compile if we disallow copying of nocmp. 96// 97// We need to allow this so that users can do, 98// 99// var x atomic.Int32 100// x = atomic.NewInt32(1) 101func TestNocmpCopy(t *testing.T) { 102 type foo struct{ _ nocmp } 103 104 t.Run("struct copy", func(t *testing.T) { 105 a := foo{} 106 b := a 107 _ = b // unused 108 }) 109 110 t.Run("pointer copy", func(t *testing.T) { 111 a := &foo{} 112 b := *a 113 _ = b // unused 114 }) 115} 116 117// Fake go.mod with no dependencies. 118const _exampleGoMod = `module example.com/nocmp` 119 120const _badFile = `package atomic 121 122import "fmt" 123 124type Int64 struct { 125 nocmp 126 127 v int64 128} 129 130func shouldNotCompile() { 131 var x, y Int64 132 fmt.Println(x == y) 133} 134` 135 136func TestNocmpIntegration(t *testing.T) { 137 tempdir, err := ioutil.TempDir("", "nocmp") 138 require.NoError(t, err, "unable to set up temporary directory") 139 defer os.RemoveAll(tempdir) 140 141 nocmp, err := ioutil.ReadFile("nocmp.go") 142 require.NoError(t, err, "unable to read nocmp.go") 143 144 require.NoError(t, 145 ioutil.WriteFile(filepath.Join(tempdir, "go.mod"), []byte(_exampleGoMod), 0644), 146 "unable to write go.mod") 147 148 require.NoError(t, 149 ioutil.WriteFile(filepath.Join(tempdir, "nocmp.go"), nocmp, 0644), 150 "unable to write nocmp.go") 151 152 require.NoError(t, 153 ioutil.WriteFile(filepath.Join(tempdir, "bad.go"), []byte(_badFile), 0644), 154 "unable to write bad.go") 155 156 var stderr bytes.Buffer 157 cmd := exec.Command("go", "build") 158 cmd.Dir = tempdir 159 // Create a minimal build enviroment with only HOME set so that "go 160 // build" has somewhere to put the cache and other Go files in. 161 cmd.Env = []string{"HOME=" + filepath.Join(tempdir, "home")} 162 cmd.Stderr = &stderr 163 require.Error(t, cmd.Run(), "bad.go must not compile") 164 165 assert.Contains(t, stderr.String(), 166 "struct containing nocmp cannot be compared") 167} 168