pointer_test.go raw
1 // Copyright (c) 2022 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 //go:build go1.18
22 // +build go1.18
23
24 package atomic
25
26 //
27 // import (
28 // "fmt"
29 // "testing"
30 //
31 // "github.com/stretchr/testify/require"
32 // )
33 //
34 // func TestPointer(t *testing.T) {
35 // type foo struct{ v int }
36 //
37 // i := foo{42}
38 // j := foo{0}
39 // k := foo{1}
40 //
41 // tests := []struct {
42 // desc string
43 // newAtomic func() *Pointer[foo]
44 // initial *foo
45 // }{
46 // {
47 // desc: "New",
48 // newAtomic: func() *Pointer[foo] {
49 // return NewPointer(&i)
50 // },
51 // initial: &i,
52 // },
53 // {
54 // desc: "New/nil",
55 // newAtomic: func() *Pointer[foo] {
56 // return NewPointer[foo](nil)
57 // },
58 // initial: nil,
59 // },
60 // {
61 // desc: "zero value",
62 // newAtomic: func() *Pointer[foo] {
63 // var p Pointer[foo]
64 // return &p
65 // },
66 // initial: nil,
67 // },
68 // }
69 //
70 // for _, tt := range tests {
71 // t.Run(tt.desc, func(t *testing.T) {
72 // t.Run("Load", func(t *testing.T) {
73 // atom := tt.newAtomic()
74 // require.Equal(t, tt.initial, atom.Load(), "Load should report nil.")
75 // })
76 //
77 // t.Run("Swap", func(t *testing.T) {
78 // atom := tt.newAtomic()
79 // require.Equal(t, tt.initial, atom.Swap(&k), "Swap didn't return the old value.")
80 // require.Equal(t, &k, atom.Load(), "Swap didn't set the correct value.")
81 // })
82 //
83 // t.Run("CAS", func(t *testing.T) {
84 // atom := tt.newAtomic()
85 // require.True(t, atom.CompareAndSwap(tt.initial, &j), "CAS didn't report a swap.")
86 // require.Equal(t, &j, atom.Load(), "CAS didn't set the correct value.")
87 // })
88 //
89 // t.Run("Store", func(t *testing.T) {
90 // atom := tt.newAtomic()
91 // atom.Store(&i)
92 // require.Equal(t, &i, atom.Load(), "Store didn't set the correct value.")
93 // })
94 // t.Run("String", func(t *testing.T) {
95 // atom := tt.newAtomic()
96 // require.Equal(t, fmt.Sprint(tt.initial), atom.String(), "String did not return the correct value.")
97 // })
98 // })
99 // }
100 // }
101