Skip to content

Commit

Permalink
Test com.Atomic#Swap()
Browse files Browse the repository at this point in the history
  • Loading branch information
Al2Klimov committed Jul 8, 2024
1 parent 8221f87 commit bdb92da
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions com/atomic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,32 @@ func TestAtomic_Load(t *testing.T) {
})
}
}

func TestAtomic_Swap(t *testing.T) {
subtests := []struct {
name string
init bool
io testInterface
new testInterface
}{
{"uninitialized", false, nil, (*testImpl)(nil)},
{"nil", true, (*testImpl)(nil), nil},
}

for _, st := range subtests {
t.Run(st.name, func(t *testing.T) {
var a Atomic[testInterface]
if st.init {
a.Store(st.io)
}

old, ok := a.Swap(st.new)
require.Equal(t, st.init, ok, "Swap second return value")
require.Equal(t, st.io, old, "Swap first return value")

v, ok := a.Load()
require.True(t, ok, "Load second return value")
require.Equal(t, st.new, v, "Load first return value")
})
}
}

0 comments on commit bdb92da

Please sign in to comment.