go sqrtConst_test 源码

  • 2022-07-15
  • 浏览 (953)

golang sqrtConst_test 代码

文件路径:/src/cmd/compile/internal/test/testdata/sqrtConst_test.go

// Copyright 2016 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package main

import (
	"math"
	"testing"
)

var tests = [...]struct {
	name string
	in   float64 // used for error messages, not an input
	got  float64
	want float64
}{
	{"sqrt0", 0, math.Sqrt(0), 0},
	{"sqrt1", 1, math.Sqrt(1), 1},
	{"sqrt2", 2, math.Sqrt(2), math.Sqrt2},
	{"sqrt4", 4, math.Sqrt(4), 2},
	{"sqrt100", 100, math.Sqrt(100), 10},
	{"sqrt101", 101, math.Sqrt(101), 10.04987562112089},
}

var nanTests = [...]struct {
	name string
	in   float64 // used for error messages, not an input
	got  float64
}{
	{"sqrtNaN", math.NaN(), math.Sqrt(math.NaN())},
	{"sqrtNegative", -1, math.Sqrt(-1)},
	{"sqrtNegInf", math.Inf(-1), math.Sqrt(math.Inf(-1))},
}

func TestSqrtConst(t *testing.T) {
	for _, test := range tests {
		if test.got != test.want {
			t.Errorf("%s: math.Sqrt(%f): got %f, want %f\n", test.name, test.in, test.got, test.want)
		}
	}
	for _, test := range nanTests {
		if math.IsNaN(test.got) != true {
			t.Errorf("%s: math.Sqrt(%f): got %f, want NaN\n", test.name, test.in, test.got)
		}
	}
	if got := math.Sqrt(math.Inf(1)); !math.IsInf(got, 1) {
		t.Errorf("math.Sqrt(+Inf), got %f, want +Inf\n", got)
	}
}

相关信息

go 源码目录

相关文章

go addressed_test 源码

go append_test 源码

go arithBoundary_test 源码

go arithConst_test 源码

go arith_test 源码

go array_test 源码

go assert_test 源码

go break_test 源码

go chan_test 源码

go closure_test 源码

0  赞