...
1
16
17 package pool
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
60 if b.CompareAndSwap(false, true) {
61 t.Error("b.CompareAndSwap false, true should fail")
62 }
63 if !b.CompareAndSwap(true, false) {
64 t.Error("b.CompareAndSwap true, false should succeed")
65 }
66 if !b.CompareAndSwap(false, false) {
67 t.Error("b.CompareAndSwap false, false should NOP")
68 }
69 if !b.CompareAndSwap(false, true) {
70 t.Error("b.CompareAndSwap false, true should succeed")
71 }
72 if !b.CompareAndSwap(true, true) {
73 t.Error("b.CompareAndSwap true, true should NOP")
74 }
75 }
76
View as plain text