Home | History | Annotate | Download | only in runtime
      1 // Copyright 2015 The Go Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style
      3 // license that can be found in the LICENSE file.
      4 
      5 // A copy of Sqrt tests from the math package to test the
      6 // purely integer arithmetic implementation in sqrt.go.
      7 
      8 package runtime_test
      9 
     10 import (
     11 	"math"
     12 	"runtime"
     13 	"testing"
     14 )
     15 
     16 func SqrtRT(x float64) float64 {
     17 	return math.Float64frombits(runtime.Sqrt(math.Float64bits(x)))
     18 }
     19 
     20 func TestSqrt(t *testing.T) {
     21 	for i := 0; i < len(vf); i++ {
     22 		a := math.Abs(vf[i])
     23 		if f := SqrtRT(a); sqrt[i] != f {
     24 			t.Errorf("Sqrt(%g) = %g, want %g", a, f, sqrt[i])
     25 		}
     26 	}
     27 	for i := 0; i < len(vfsqrtSC); i++ {
     28 		if f := SqrtRT(vfsqrtSC[i]); !alike(sqrtSC[i], f) {
     29 			t.Errorf("Sqrt(%g) = %g, want %g", vfsqrtSC[i], f, sqrtSC[i])
     30 		}
     31 	}
     32 }
     33 
     34 func alike(a, b float64) bool {
     35 	switch {
     36 	case math.IsNaN(a) && math.IsNaN(b):
     37 		return true
     38 	case a == b:
     39 		return math.Signbit(a) == math.Signbit(b)
     40 	}
     41 	return false
     42 }
     43 
     44 var vf = []float64{
     45 	4.9790119248836735e+00,
     46 	7.7388724745781045e+00,
     47 	-2.7688005719200159e-01,
     48 	-5.0106036182710749e+00,
     49 	9.6362937071984173e+00,
     50 	2.9263772392439646e+00,
     51 	5.2290834314593066e+00,
     52 	2.7279399104360102e+00,
     53 	1.8253080916808550e+00,
     54 	-8.6859247685756013e+00,
     55 }
     56 
     57 var sqrt = []float64{
     58 	2.2313699659365484748756904e+00,
     59 	2.7818829009464263511285458e+00,
     60 	5.2619393496314796848143251e-01,
     61 	2.2384377628763938724244104e+00,
     62 	3.1042380236055381099288487e+00,
     63 	1.7106657298385224403917771e+00,
     64 	2.286718922705479046148059e+00,
     65 	1.6516476350711159636222979e+00,
     66 	1.3510396336454586262419247e+00,
     67 	2.9471892997524949215723329e+00,
     68 }
     69 
     70 var vfsqrtSC = []float64{
     71 	math.Inf(-1),
     72 	-math.Pi,
     73 	math.Copysign(0, -1),
     74 	0,
     75 	math.Inf(1),
     76 	math.NaN(),
     77 	math.Float64frombits(2),
     78 }
     79 var sqrtSC = []float64{
     80 	math.NaN(),
     81 	math.NaN(),
     82 	math.Copysign(0, -1),
     83 	0,
     84 	math.Inf(1),
     85 	math.NaN(),
     86 	3.1434555694052576e-162,
     87 }
     88