github.com/cockroachdb/cockroach@v20.2.0-alpha.1+incompatible/pkg/util/syncutil/atomic_test.go (about)

     1  // Copyright 2017 The Cockroach Authors.
     2  //
     3  // Use of this software is governed by the Business Source License
     4  // included in the file licenses/BSL.txt.
     5  //
     6  // As of the Change Date specified in that file, in accordance with
     7  // the Business Source License, use of this software will be governed
     8  // by the Apache License, Version 2.0, included in the file
     9  // licenses/APL.txt.
    10  
    11  package syncutil
    12  
    13  import "testing"
    14  
    15  const magic64 = 0xdeddeadbeefbeef
    16  
    17  // Tests of correct behavior, without contention.
    18  // The loop over power-of-two values is meant to
    19  // ensure that the operations apply to the full word size.
    20  // The struct fields x.before and x.after check that the
    21  // operations do not extend past the full word size.
    22  //
    23  // Adapted from https://golang.org/src/sync/atomic/atomic_test.go
    24  func TestAtomicFloat64(t *testing.T) {
    25  	var x struct {
    26  		before AtomicFloat64
    27  		i      AtomicFloat64
    28  		after  AtomicFloat64
    29  	}
    30  	x.before = magic64
    31  	x.after = magic64
    32  	for delta := uint64(1); delta+delta > delta; delta += delta {
    33  		e := float64(delta)
    34  		StoreFloat64(&x.i, e)
    35  		a := LoadFloat64(&x.i)
    36  		if a != e {
    37  			t.Fatalf("stored=%f got=%f", e, a)
    38  		}
    39  	}
    40  	if x.before != magic64 || x.after != magic64 {
    41  		t.Fatalf("wrong magic: %#x _ %#x != %#x _ %#x", x.before, x.after, uint64(magic64), uint64(magic64))
    42  	}
    43  }