2014-04-11 Marc Glisse <marc.glisse@inria.fr>
[official-gcc.git] / libgo / go / bytes / equal_test.go
blob1bf19a74b80193c8be4d7952b18981ec0228e153
1 // Copyright 2013 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 // +build linux
7 package bytes_test
9 import (
10 . "bytes"
11 "syscall"
12 "testing"
13 "unsafe"
16 // This file tests the situation where memeq is checking
17 // data very near to a page boundary. We want to make sure
18 // equal does not read across the boundary and cause a page
19 // fault where it shouldn't.
21 // This test runs only on linux. The code being tested is
22 // not OS-specific, so it does not need to be tested on all
23 // operating systems.
25 func TestEqualNearPageBoundary(t *testing.T) {
26 pagesize := syscall.Getpagesize()
27 b := make([]byte, 4*pagesize)
28 i := pagesize
29 for ; uintptr(unsafe.Pointer(&b[i]))%uintptr(pagesize) != 0; i++ {
31 syscall.Mprotect(b[i-pagesize:i], 0)
32 syscall.Mprotect(b[i+pagesize:i+2*pagesize], 0)
33 defer syscall.Mprotect(b[i-pagesize:i], syscall.PROT_READ|syscall.PROT_WRITE)
34 defer syscall.Mprotect(b[i+pagesize:i+2*pagesize], syscall.PROT_READ|syscall.PROT_WRITE)
36 // both of these should fault
37 //pagesize += int(b[i-1])
38 //pagesize += int(b[i+pagesize])
40 for j := 0; j < pagesize; j++ {
41 b[i+j] = 'A'
43 for j := 0; j <= pagesize; j++ {
44 Equal(b[i:i+j], b[i+pagesize-j:i+pagesize])
45 Equal(b[i+pagesize-j:i+pagesize], b[i:i+j])