1 // Copyright ©2015 The Gonum 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.
11 "golang.org/x/exp/rand"
13 "gonum.org/v1/gonum/floats"
16 type Dlartger interface {
17 Dlartg(f, g float64) (cs, sn, r float64)
20 func DlartgTest(t *testing.T, impl Dlartger) {
22 // safmn2 and safmx2 are copied from native.Dlartg.
23 safmn2 := math.Pow(dlamchB, math.Trunc(math.Log(dlamchS/dlamchE)/math.Log(dlamchB)/2))
25 rnd := rand.New(rand.NewSource(1))
26 for i := 0; i < 1000; i++ {
33 f = math.Pow(10, 10-20*rnd.Float64()) * safmx2
36 f = math.Pow(10, 10-20*rnd.Float64()) * safmn2
50 g = math.Pow(10, 10-20*rnd.Float64()) * safmx2
53 g = math.Pow(10, 10-20*rnd.Float64()) * safmn2
61 cs, sn, r := impl.Dlartg(f, g)
64 if !floats.EqualWithinAbsOrRel(math.Abs(rWant), math.Abs(r), tol, tol) {
65 t.Errorf("Case f=%v,g=%v: unexpected r. Want %v, got %v", f, g, rWant, r)
67 oneTest := cs*cs + sn*sn
68 if math.Abs(oneTest-1) > tol {
69 t.Errorf("Case f=%v,g=%v: expected cs^2+sn^2==1, got %v", f, g, oneTest)
72 zeroTest := -sn*f + cs*g
73 if math.Abs(zeroTest) > tol {
74 t.Errorf("Case f=%v,g=%v: expected zero, got %v", f, g, zeroTest)
77 if math.Abs(f) > math.Abs(g) && cs < 0 {
78 t.Errorf("Case f=%v,g=%v: unexpected negative cs %v", f, g, cs)
81 for i := 0; i < 100; i++ {
82 cs, sn, _ := impl.Dlartg(rnd.NormFloat64(), 0)
84 t.Errorf("Unexpected cs for g=0. Want 1, got %v", cs)
87 t.Errorf("Unexpected sn for g=0. Want 0, got %v", sn)
90 for i := 0; i < 100; i++ {
91 cs, sn, _ := impl.Dlartg(0, rnd.NormFloat64())
93 t.Errorf("Unexpected cs for f=0. Want 0, got %v", cs)
96 t.Errorf("Unexpected sn for f=0. Want 1, got %v", sn)