| 1 |
// Copyright 2016 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 |
package main |
| 6 |
|
| 7 |
import ( |
| 8 |
"math" |
| 9 |
"testing" |
| 10 |
) |
| 11 |
|
| 12 |
var tests = [...]struct { |
| 13 |
name string |
| 14 |
in float64 // used for error messages, not an input |
| 15 |
got float64 |
| 16 |
want float64 |
| 17 |
}{ |
| 18 |
{"sqrt0", 0, math.Sqrt(0), 0}, |
| 19 |
{"sqrt1", 1, math.Sqrt(1), 1}, |
| 20 |
{"sqrt2", 2, math.Sqrt(2), math.Sqrt2}, |
| 21 |
{"sqrt4", 4, math.Sqrt(4), 2}, |
| 22 |
{"sqrt100", 100, math.Sqrt(100), 10}, |
| 23 |
{"sqrt101", 101, math.Sqrt(101), 10.04987562112089}, |
| 24 |
} |
| 25 |
|
| 26 |
var nanTests = [...]struct { |
| 27 |
name string |
| 28 |
in float64 // used for error messages, not an input |
| 29 |
got float64 |
| 30 |
}{ |
| 31 |
{"sqrtNaN", math.NaN(), math.Sqrt(math.NaN())}, |
| 32 |
{"sqrtNegative", -1, math.Sqrt(-1)}, |
| 33 |
{"sqrtNegInf", math.Inf(-1), math.Sqrt(math.Inf(-1))}, |
| 34 |
} |
| 35 |
|
| 36 |
func TestSqrtConst(t *testing.T) { |
| 37 |
for _, test := range tests { |
| 38 |
if test.got != test.want { |
| 39 |
t.Errorf("%s: math.Sqrt(%f): got %f, want %f\n", test.name, test.in, test.got, test.want) |
| 40 |
} |
| 41 |
} |
| 42 |
for _, test := range nanTests { |
| 43 |
if math.IsNaN(test.got) != true { |
| 44 |
t.Errorf("%s: math.Sqrt(%f): got %f, want NaN\n", test.name, test.in, test.got) |
| 45 |
} |
| 46 |
} |
| 47 |
if got := math.Sqrt(math.Inf(1)); !math.IsInf(got, 1) { |
| 48 |
t.Errorf("math.Sqrt(+Inf), got %f, want +Inf\n", got) |
| 49 |
} |
| 50 |
} |