...

Source file src/go.uber.org/atomic/pointer_test.go

Documentation: go.uber.org/atomic

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

View as plain text