1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package atomic
22
23 import (
24 "encoding/json"
25 "fmt"
26 "testing"
27
28 "github.com/stretchr/testify/assert"
29 "github.com/stretchr/testify/require"
30 )
31
32 func TestUintptr(t *testing.T) {
33 atom := NewUintptr(42)
34
35 require.Equal(t, uintptr(42), atom.Load(), "Load didn't work.")
36 require.Equal(t, uintptr(46), atom.Add(4), "Add didn't work.")
37 require.Equal(t, uintptr(44), atom.Sub(2), "Sub didn't work.")
38 require.Equal(t, uintptr(45), atom.Inc(), "Inc didn't work.")
39 require.Equal(t, uintptr(44), atom.Dec(), "Dec didn't work.")
40
41 require.True(t, atom.CAS(44, 0), "CAS didn't report a swap.")
42 require.Equal(t, uintptr(0), atom.Load(), "CAS didn't set the correct value.")
43
44 require.Equal(t, uintptr(0), atom.Swap(1), "Swap didn't return the old value.")
45 require.Equal(t, uintptr(1), atom.Load(), "Swap didn't set the correct value.")
46
47 atom.Store(42)
48 require.Equal(t, uintptr(42), atom.Load(), "Store didn't set the correct value.")
49
50 t.Run("JSON/Marshal", func(t *testing.T) {
51 bytes, err := json.Marshal(atom)
52 require.NoError(t, err, "json.Marshal errored unexpectedly.")
53 require.Equal(t, []byte("42"), bytes, "json.Marshal encoded the wrong bytes.")
54 })
55
56 t.Run("JSON/Unmarshal", func(t *testing.T) {
57 err := json.Unmarshal([]byte("40"), &atom)
58 require.NoError(t, err, "json.Unmarshal errored unexpectedly.")
59 require.Equal(t, uintptr(40), atom.Load(), "json.Unmarshal didn't set the correct value.")
60 })
61
62 t.Run("JSON/Unmarshal/Error", func(t *testing.T) {
63 err := json.Unmarshal([]byte(`"40"`), &atom)
64 require.Error(t, err, "json.Unmarshal didn't error as expected.")
65 assertErrorJSONUnmarshalType(t, err,
66 "json.Unmarshal failed with unexpected error %v, want UnmarshalTypeError.", err)
67 })
68
69 t.Run("String", func(t *testing.T) {
70
71
72
73 negative := -1
74 atom := NewUintptr(uintptr(negative))
75 want := fmt.Sprint(uintptr(negative))
76 assert.Equal(t, want, atom.String(),
77 "String() returned an unexpected value.")
78 })
79 }
80
View as plain text