1 // Copyright (c) 2021 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 21 package atomic
22 23 import (
24 "testing"
25 "unsafe"
26 27 "github.com/stretchr/testify/require"
28 )
29 30 func TestUnsafePointer(t *testing.T) {
31 i := int64(42)
32 j := int64(0)
33 k := int64(1)
34 35 tests := []struct {
36 desc string
37 newAtomic func() *UnsafePointer
38 initial unsafe.Pointer
39 }{
40 {
41 desc: "non-empty",
42 newAtomic: func() *UnsafePointer {
43 return NewUnsafePointer(unsafe.Pointer(&i))
44 },
45 initial: unsafe.Pointer(&i),
46 },
47 {
48 desc: "nil",
49 newAtomic: func() *UnsafePointer {
50 var p UnsafePointer
51 return &p
52 },
53 initial: unsafe.Pointer(nil),
54 },
55 }
56 57 for _, tt := range tests {
58 t.Run(tt.desc, func(t *testing.T) {
59 t.Run("Load", func(t *testing.T) {
60 atom := tt.newAtomic()
61 require.Equal(t, tt.initial, atom.Load(), "Load should report nil.")
62 })
63 64 t.Run("Swap", func(t *testing.T) {
65 atom := tt.newAtomic()
66 require.Equal(t, tt.initial, atom.Swap(unsafe.Pointer(&k)), "Swap didn't return the old value.")
67 require.Equal(t, unsafe.Pointer(&k), atom.Load(), "Swap didn't set the correct value.")
68 })
69 70 t.Run("CAS", func(t *testing.T) {
71 atom := tt.newAtomic()
72 require.True(t, atom.CAS(tt.initial, unsafe.Pointer(&j)), "CAS didn't report a swap.")
73 require.Equal(t, unsafe.Pointer(&j), atom.Load(), "CAS didn't set the correct value.")
74 })
75 76 t.Run("Store", func(t *testing.T) {
77 atom := tt.newAtomic()
78 atom.Store(unsafe.Pointer(&i))
79 require.Equal(t, unsafe.Pointer(&i), atom.Load(), "Store didn't set the correct value.")
80 })
81 })
82 }
83 }
84