3 // Copyright 2021 The Go Authors. All rights reserved.
4 // Use of this source code is governed by a BSD-style
5 // license that can be found in the LICENSE file.
14 const maxUintptr = 1 << (8 * unsafe.Sizeof(uintptr(0)))
21 p1 := unsafe.Pointer(&p[1])
22 assert(unsafe.Add(p1, 1) == unsafe.Pointer(&p[2]))
23 assert(unsafe.Add(p1, -1) == unsafe.Pointer(&p[0]))
28 s := unsafe.Slice(&p[0], len(p))
29 assert(&s[0] == &p[0])
30 assert(len(s) == len(p))
31 assert(cap(s) == len(p))
34 mustPanic(func() { _ = unsafe.Slice((*int)(nil), 0) })
38 mustPanic(func() { _ = unsafe.Slice(new(byte), neg) })
41 var tooBig uint64 = math.MaxUint64
42 mustPanic(func() { _ = unsafe.Slice(new(byte), tooBig) })
44 // size overflows address space
45 mustPanic(func() { _ = unsafe.Slice(new(uint64), maxUintptr/8) })
46 mustPanic(func() { _ = unsafe.Slice(new(uint64), maxUintptr/8+1) })
50 func assert(ok bool) {
56 func mustPanic(f func()) {
58 assert(recover() != nil)