gitee.com/go-spring2/spring-base@v1.1.3/atomic/uintptr_test.go (about) 1 /* 2 * Copyright 2012-2019 the original author or authors. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * https://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package atomic_test 18 19 import ( 20 "testing" 21 "unsafe" 22 23 "gitee.com/go-spring2/spring-base/assert" 24 "gitee.com/go-spring2/spring-base/atomic" 25 ) 26 27 func TestUintptr(t *testing.T) { 28 29 // atomic.Uintptr and uintptr occupy the same space 30 assert.Equal(t, unsafe.Sizeof(atomic.Uintptr{}), uintptr(8)) 31 32 var p atomic.Uintptr 33 assert.Equal(t, p.Load(), uintptr(0)) 34 35 v := p.Add(0) 36 assert.Equal(t, v, uintptr(0)) 37 assert.Equal(t, p.Load(), uintptr(0)) 38 39 s1 := &properties{Data: 1} 40 p.Store(uintptr(unsafe.Pointer(s1))) 41 assert.Equal(t, p.Load(), uintptr(unsafe.Pointer(s1))) 42 43 s2 := &properties{Data: 2} 44 old := p.Swap(uintptr(unsafe.Pointer(s2))) 45 assert.Equal(t, old, uintptr(unsafe.Pointer(s1))) 46 assert.Equal(t, p.Load(), uintptr(unsafe.Pointer(s2))) 47 48 s3 := &properties{Data: 3} 49 swapped := p.CompareAndSwap(uintptr(unsafe.Pointer(s2)), uintptr(unsafe.Pointer(s3))) 50 assert.True(t, swapped) 51 assert.Equal(t, p.Load(), uintptr(unsafe.Pointer(s3))) 52 53 swapped = p.CompareAndSwap(uintptr(unsafe.Pointer(s2)), uintptr(unsafe.Pointer(s3))) 54 assert.False(t, swapped) 55 assert.Equal(t, p.Load(), uintptr(unsafe.Pointer(s3))) 56 }