github.com/filecoin-project/specs-actors/v4@v4.0.2/actors/util/math/expneg_test.go (about)

     1  package math_test
     2  
     3  import (
     4  	"bytes"
     5  	"fmt"
     6  	"math/big"
     7  	"testing"
     8  
     9  	"github.com/xorcare/golden"
    10  
    11  	"github.com/filecoin-project/specs-actors/v4/actors/util/math"
    12  )
    13  
    14  var Res big.Word
    15  
    16  func BenchmarkExpneg(b *testing.B) {
    17  	x := new(big.Int).SetUint64(14)
    18  	x = x.Lsh(x, math.Precision128-3) // set x to 1.75
    19  	dec := new(big.Int)
    20  	dec = dec.Div(x, big.NewInt(int64(b.N)))
    21  	b.ResetTimer()
    22  	b.ReportAllocs()
    23  	var res big.Word
    24  
    25  	for i := 0; i < b.N; i++ {
    26  		r := math.ExpNeg(x)
    27  		res += r.Bits()[0]
    28  		x.Sub(x, dec)
    29  	}
    30  	Res += res
    31  }
    32  
    33  func TestExpFunction(t *testing.T) {
    34  	const N = 256
    35  
    36  	step := big.NewInt(5)
    37  	step = step.Lsh(step, math.Precision128) // Q.128
    38  	step = step.Div(step, big.NewInt(N-1))
    39  
    40  	x := big.NewInt(0)
    41  	b := &bytes.Buffer{}
    42  
    43  	b.WriteString("x, y\n")
    44  	for i := 0; i < N; i++ {
    45  		y := math.ExpNeg(x)
    46  		fmt.Fprintf(b, "%s,%s\n", x, y)
    47  		x = x.Add(x, step)
    48  	}
    49  
    50  	golden.Assert(t, b.Bytes())
    51  }