go divbyzero_test 源码

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

golang divbyzero_test 代码

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

package main

import (
	"runtime"
	"testing"
)

func checkDivByZero(f func()) (divByZero bool) {
	defer func() {
		if r := recover(); r != nil {
			if e, ok := r.(runtime.Error); ok && e.Error() == "runtime error: integer divide by zero" {
				divByZero = true
			}
		}
	}()
	f()
	return false
}

//go:noinline
func div_a(i uint, s []int) int {
	return s[i%uint(len(s))]
}

//go:noinline
func div_b(i uint, j uint) uint {
	return i / j
}

//go:noinline
func div_c(i int) int {
	return 7 / (i - i)
}

func TestDivByZero(t *testing.T) {
	if got := checkDivByZero(func() { div_b(7, 0) }); !got {
		t.Errorf("expected div by zero for b(7, 0), got no error\n")
	}
	if got := checkDivByZero(func() { div_b(7, 7) }); got {
		t.Errorf("expected no error for b(7, 7), got div by zero\n")
	}
	if got := checkDivByZero(func() { div_a(4, nil) }); !got {
		t.Errorf("expected div by zero for a(4, nil), got no error\n")
	}
	if got := checkDivByZero(func() { div_c(5) }); !got {
		t.Errorf("expected div by zero for c(5), got no error\n")
	}
}

相关信息

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  赞