github.com/XiaoMi/Gaea@v1.2.5/util/sync2/atomic_test.go (about) 1 /* 2 Copyright 2017 Google Inc. 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 http://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 sync2 18 19 import ( 20 "testing" 21 ) 22 23 func TestAtomicString(t *testing.T) { 24 var s AtomicString 25 if s.Get() != "" { 26 t.Errorf("want empty, got %s", s.Get()) 27 } 28 s.Set("a") 29 if s.Get() != "a" { 30 t.Errorf("want a, got %s", s.Get()) 31 } 32 if s.CompareAndSwap("b", "c") { 33 t.Errorf("want false, got true") 34 } 35 if s.Get() != "a" { 36 t.Errorf("want a, got %s", s.Get()) 37 } 38 if !s.CompareAndSwap("a", "c") { 39 t.Errorf("want true, got false") 40 } 41 if s.Get() != "c" { 42 t.Errorf("want c, got %s", s.Get()) 43 } 44 } 45 46 func TestAtomicBool(t *testing.T) { 47 b := NewAtomicBool(true) 48 if !b.Get() { 49 t.Error("b.Get: false, want true") 50 } 51 b.Set(false) 52 if b.Get() { 53 t.Error("b.Get: true, want false") 54 } 55 b.Set(true) 56 if !b.Get() { 57 t.Error("b.Get: false, want true") 58 } 59 }