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 "testing"
26
27 "github.com/stretchr/testify/assert"
28 "github.com/stretchr/testify/require"
29 )
30
31 func TestBool(t *testing.T) {
32 atom := NewBool(false)
33 require.False(t, atom.Toggle(), "Expected Toggle to return previous value.")
34 require.True(t, atom.Toggle(), "Expected Toggle to return previous value.")
35 require.False(t, atom.Toggle(), "Expected Toggle to return previous value.")
36 require.True(t, atom.Load(), "Unexpected state after swap.")
37
38 require.True(t, atom.CAS(true, true), "CAS should swap when old matches")
39 require.True(t, atom.Load(), "CAS should have no effect")
40 require.True(t, atom.CAS(true, false), "CAS should swap when old matches")
41 require.False(t, atom.Load(), "CAS should have modified the value")
42 require.False(t, atom.CAS(true, false), "CAS should fail on old mismatch")
43 require.False(t, atom.Load(), "CAS should not have modified the value")
44
45 atom.Store(false)
46 require.False(t, atom.Load(), "Unexpected state after store.")
47
48 prev := atom.Swap(false)
49 require.False(t, prev, "Expected Swap to return previous value.")
50
51 prev = atom.Swap(true)
52 require.False(t, prev, "Expected Swap to return previous value.")
53
54 t.Run("JSON/Marshal", func(t *testing.T) {
55 atom.Store(true)
56 bytes, err := json.Marshal(atom)
57 require.NoError(t, err, "json.Marshal errored unexpectedly.")
58 require.Equal(t, []byte("true"), bytes, "json.Marshal encoded the wrong bytes.")
59 })
60
61 t.Run("JSON/Unmarshal", func(t *testing.T) {
62 err := json.Unmarshal([]byte("false"), &atom)
63 require.NoError(t, err, "json.Unmarshal errored unexpectedly.")
64 require.False(t, atom.Load(), "json.Unmarshal didn't set the correct value.")
65 })
66
67 t.Run("JSON/Unmarshal/Error", func(t *testing.T) {
68 err := json.Unmarshal([]byte("42"), &atom)
69 require.Error(t, err, "json.Unmarshal didn't error as expected.")
70 assertErrorJSONUnmarshalType(t, err,
71 "json.Unmarshal failed with unexpected error %v, want UnmarshalTypeError.", err)
72 })
73
74 t.Run("String", func(t *testing.T) {
75 t.Run("true", func(t *testing.T) {
76 assert.Equal(t, "true", NewBool(true).String(),
77 "String() returned an unexpected value.")
78 })
79
80 t.Run("false", func(t *testing.T) {
81 var b Bool
82 assert.Equal(t, "false", b.String(),
83 "String() returned an unexpected value.")
84 })
85 })
86 }
87
88 func TestBool_InitializeDefaults(t *testing.T) {
89 tests := []struct {
90 msg string
91 newBool func() *Bool
92 }{
93 {
94 msg: "Uninitialized",
95 newBool: func() *Bool {
96 var b Bool
97 return &b
98 },
99 },
100 {
101 msg: "NewBool with default",
102 newBool: func() *Bool {
103 return NewBool(false)
104 },
105 },
106 {
107 msg: "Bool swapped with default",
108 newBool: func() *Bool {
109 b := NewBool(true)
110 b.Swap(false)
111 return b
112 },
113 },
114 {
115 msg: "Bool CAS'd with default",
116 newBool: func() *Bool {
117 b := NewBool(true)
118 b.CompareAndSwap(true, false)
119 return b
120 },
121 },
122 }
123
124 for _, tt := range tests {
125 t.Run(tt.msg, func(t *testing.T) {
126 t.Run("MarshalJSON", func(t *testing.T) {
127 b := tt.newBool()
128 marshalled, err := b.MarshalJSON()
129 require.NoError(t, err)
130 assert.Equal(t, "false", string(marshalled))
131 })
132
133 t.Run("String", func(t *testing.T) {
134 b := tt.newBool()
135 assert.Equal(t, "false", b.String())
136 })
137
138 t.Run("CompareAndSwap", func(t *testing.T) {
139 b := tt.newBool()
140 require.True(t, b.CompareAndSwap(false, true))
141 assert.Equal(t, true, b.Load())
142 })
143
144 t.Run("Swap", func(t *testing.T) {
145 b := tt.newBool()
146 assert.Equal(t, false, b.Swap(true))
147 })
148 })
149 }
150 }
151
View as plain text