Exemple #1
0
func BenchmarkSqrtUint32(b *testing.B) {
	for i := 0; i < b.N; i++ {
		for j := uint32(0x10000); j < 0x4000000; j += 0x10000 {
			_ = u32.Sqrt(j)
		}
	}
}
Exemple #2
0
func TestSqrt(t *testing.T) {
	t.Logf("Testing Sqrt\n")
	i32Max := int32(0x7FFF)
	i64Max := int64(0x7FFFFFFF)
	u32Max := uint32(0xFFFF)
	u64Max := uint64(0xFFFFFFFF)

	iMax := 1
	var uMax uint
	if iMax<<32 == 0 {
		iMax = int(i32Max)
		uMax = uint(u32Max)
	} else {
		iMax = int(i64Max)
		uMax = uint(u64Max)
	}

	for i := iMax; i > 0; i >>= 1 {
		if intgr.Sqrt(i*i) != i {
			t.Logf("intgr.Sqrt(%X*%X) == %X\n", i, i, intgr.Sqrt(i))
			t.Fail()
		}
	}
	for i := uMax; i > 0; i >>= 1 {
		if uintgr.Sqrt(i*i) != i {
			t.Logf("uintgr.Sqrt(%X*%X) == %X\n", i, i, uintgr.Sqrt(i))
			t.Fail()
		}
	}
	for i := i32Max; i > 0; i >>= 1 {
		if i32.Sqrt(i*i) != i {
			t.Logf("i32.Sqrt(%X*%X) == %X\n", i, i, i32.Sqrt(i))
			t.Fail()
		}
	}
	for i := u32Max; i > 0; i >>= 1 {
		if u32.Sqrt(i*i) != i {
			t.Logf("u32.Sqrt(%X*%X) == %X\n", i, i, u32.Sqrt(i))
			t.Fail()
		}
	}
	for i := i64Max; i > 0; i >>= 1 {
		if i64.Sqrt(i*i) != i {
			t.Logf("i64.Sqrt(%X*%X) == %X\n", i, i, i64.Sqrt(i))
			t.Fail()
		}
	}
	for i := u64Max; i > 0; i >>= 1 {
		if u64.Sqrt(i*i) != i {
			t.Logf("u64.Sqrt(%X*%X) == %X\n", i, i, u64.Sqrt(i))
			t.Fail()
		}
	}

	for i := u32Max; i > 0; i >>= 1 {
		if SqrtShiftU32(i*i) != i {
			t.Logf("SqrtShiftU32(%X*%X) == %X\n", i, i, SqrtShiftU32(i*i))
			t.Fail()
		}
	}
	for i := u64Max; i > 0; i >>= 1 {
		if SqrtShiftU64(i*i) != i {
			t.Logf("SqrtShiftU64(%X*%X) == %X\n", i, i, SqrtShiftU64(i*i))
			t.Fail()
		}
	}

	for i := u32Max; i > 0; i >>= 1 {
		if SqrtHDU32(i*i) != i {
			t.Logf("SqrtHDU32(%X*%X) == %X\n", i, i, SqrtHDU32(i*i))
			t.Fail()
		}
	}
	for i := u64Max; i > 0; i >>= 1 {
		if SqrtHDU64(i*i) != i {
			t.Logf("SqrtHDU64(%X*%X) == %X\n", i, i, SqrtHDU64(i*i))
			t.Fail()
		}
	}
}