]> Cypherpunks.ru repositories - gostls13.git/blob - src/runtime/internal/atomic/atomic_andor_test.go
runtime/internal/atomic: add ppc64x operators for And/Or
[gostls13.git] / src / runtime / internal / atomic / atomic_andor_test.go
1 //go:build wasm || ppc64 || ppc64le
2 // +build wasm ppc64 ppc64le
3
4 //
5 // Copyright 2023 The Go Authors. All rights reserved.
6 // Use of this source code is governed by a BSD-style
7 // license that can be found in the LICENSE file.
8
9 // TODO(61395): move these tests to atomic_test.go once And/Or have
10 // implementations for all architectures.
11 package atomic_test
12
13 import (
14         "runtime/internal/atomic"
15         "testing"
16 )
17
18 func TestAnd32(t *testing.T) {
19         // Basic sanity check.
20         x := uint32(0xffffffff)
21         for i := uint32(0); i < 32; i++ {
22                 old := x
23                 v := atomic.And32(&x, ^(1 << i))
24                 if r := uint32(0xffffffff) << (i + 1); x != r || v != old {
25                         t.Fatalf("clearing bit %#x: want %#x, got new %#x and old %#v", uint32(1<<i), r, x, v)
26                 }
27         }
28
29         // Set every bit in array to 1.
30         a := make([]uint32, 1<<12)
31         for i := range a {
32                 a[i] = 0xffffffff
33         }
34
35         // Clear array bit-by-bit in different goroutines.
36         done := make(chan bool)
37         for i := 0; i < 32; i++ {
38                 m := ^uint32(1 << i)
39                 go func() {
40                         for i := range a {
41                                 atomic.And(&a[i], m)
42                         }
43                         done <- true
44                 }()
45         }
46         for i := 0; i < 32; i++ {
47                 <-done
48         }
49
50         // Check that the array has been totally cleared.
51         for i, v := range a {
52                 if v != 0 {
53                         t.Fatalf("a[%v] not cleared: want %#x, got %#x", i, uint32(0), v)
54                 }
55         }
56 }
57
58 func TestAnd64(t *testing.T) {
59         // Basic sanity check.
60         x := uint64(0xffffffffffffffff)
61         for i := uint64(0); i < 64; i++ {
62                 old := x
63                 v := atomic.And64(&x, ^(1 << i))
64                 if r := uint64(0xffffffffffffffff) << (i + 1); x != r || v != old {
65                         t.Fatalf("clearing bit %#x: want %#x, got new %#x and old %#v", uint64(1<<i), r, x, v)
66                 }
67         }
68
69         // Set every bit in array to 1.
70         a := make([]uint64, 1<<12)
71         for i := range a {
72                 a[i] = 0xffffffffffffffff
73         }
74
75         // Clear array bit-by-bit in different goroutines.
76         done := make(chan bool)
77         for i := 0; i < 64; i++ {
78                 m := ^uint64(1 << i)
79                 go func() {
80                         for i := range a {
81                                 atomic.And64(&a[i], m)
82                         }
83                         done <- true
84                 }()
85         }
86         for i := 0; i < 64; i++ {
87                 <-done
88         }
89
90         // Check that the array has been totally cleared.
91         for i, v := range a {
92                 if v != 0 {
93                         t.Fatalf("a[%v] not cleared: want %#x, got %#x", i, uint64(0), v)
94                 }
95         }
96 }
97
98 func TestOr32(t *testing.T) {
99         // Basic sanity check.
100         x := uint32(0)
101         for i := uint32(0); i < 32; i++ {
102                 old := x
103                 v := atomic.Or32(&x, 1<<i)
104                 if r := (uint32(1) << (i + 1)) - 1; x != r || v != old {
105                         t.Fatalf("setting bit %#x: want %#x, got new %#x and old %#v", uint32(1<<i), r, x, v)
106                 }
107         }
108
109         // Start with every bit in array set to 0.
110         a := make([]uint32, 1<<12)
111
112         // Set every bit in array bit-by-bit in different goroutines.
113         done := make(chan bool)
114         for i := 0; i < 32; i++ {
115                 m := uint32(1 << i)
116                 go func() {
117                         for i := range a {
118                                 atomic.Or32(&a[i], m)
119                         }
120                         done <- true
121                 }()
122         }
123         for i := 0; i < 32; i++ {
124                 <-done
125         }
126
127         // Check that the array has been totally set.
128         for i, v := range a {
129                 if v != 0xffffffff {
130                         t.Fatalf("a[%v] not fully set: want %#x, got %#x", i, uint32(0xffffffff), v)
131                 }
132         }
133 }
134
135 func TestOr64(t *testing.T) {
136         // Basic sanity check.
137         x := uint64(0)
138         for i := uint64(0); i < 64; i++ {
139                 old := x
140                 v := atomic.Or64(&x, 1<<i)
141                 if r := (uint64(1) << (i + 1)) - 1; x != r || v != old {
142                         t.Fatalf("setting bit %#x: want %#x, got new %#x and old %#v", uint64(1<<i), r, x, v)
143                 }
144         }
145
146         // Start with every bit in array set to 0.
147         a := make([]uint64, 1<<12)
148
149         // Set every bit in array bit-by-bit in different goroutines.
150         done := make(chan bool)
151         for i := 0; i < 64; i++ {
152                 m := uint64(1 << i)
153                 go func() {
154                         for i := range a {
155                                 atomic.Or64(&a[i], m)
156                         }
157                         done <- true
158                 }()
159         }
160         for i := 0; i < 64; i++ {
161                 <-done
162         }
163
164         // Check that the array has been totally set.
165         for i, v := range a {
166                 if v != 0xffffffffffffffff {
167                         t.Fatalf("a[%v] not fully set: want %#x, got %#x", i, uint64(0xffffffffffffffff), v)
168                 }
169         }
170 }