1 //go:build wasm || ppc64 || ppc64le
2 // +build wasm ppc64 ppc64le
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.
9 // TODO(61395): move these tests to atomic_test.go once And/Or have
10 // implementations for all architectures.
14 "runtime/internal/atomic"
18 func TestAnd32(t *testing.T) {
19 // Basic sanity check.
20 x := uint32(0xffffffff)
21 for i := uint32(0); i < 32; i++ {
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)
29 // Set every bit in array to 1.
30 a := make([]uint32, 1<<12)
35 // Clear array bit-by-bit in different goroutines.
36 done := make(chan bool)
37 for i := 0; i < 32; i++ {
46 for i := 0; i < 32; i++ {
50 // Check that the array has been totally cleared.
53 t.Fatalf("a[%v] not cleared: want %#x, got %#x", i, uint32(0), v)
58 func TestAnd64(t *testing.T) {
59 // Basic sanity check.
60 x := uint64(0xffffffffffffffff)
61 for i := uint64(0); i < 64; i++ {
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)
69 // Set every bit in array to 1.
70 a := make([]uint64, 1<<12)
72 a[i] = 0xffffffffffffffff
75 // Clear array bit-by-bit in different goroutines.
76 done := make(chan bool)
77 for i := 0; i < 64; i++ {
81 atomic.And64(&a[i], m)
86 for i := 0; i < 64; i++ {
90 // Check that the array has been totally cleared.
93 t.Fatalf("a[%v] not cleared: want %#x, got %#x", i, uint64(0), v)
98 func TestOr32(t *testing.T) {
99 // Basic sanity check.
101 for i := uint32(0); i < 32; i++ {
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)
109 // Start with every bit in array set to 0.
110 a := make([]uint32, 1<<12)
112 // Set every bit in array bit-by-bit in different goroutines.
113 done := make(chan bool)
114 for i := 0; i < 32; i++ {
118 atomic.Or32(&a[i], m)
123 for i := 0; i < 32; i++ {
127 // Check that the array has been totally set.
128 for i, v := range a {
130 t.Fatalf("a[%v] not fully set: want %#x, got %#x", i, uint32(0xffffffff), v)
135 func TestOr64(t *testing.T) {
136 // Basic sanity check.
138 for i := uint64(0); i < 64; i++ {
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)
146 // Start with every bit in array set to 0.
147 a := make([]uint64, 1<<12)
149 // Set every bit in array bit-by-bit in different goroutines.
150 done := make(chan bool)
151 for i := 0; i < 64; i++ {
155 atomic.Or64(&a[i], m)
160 for i := 0; i < 64; i++ {
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)