| 1 |
// Copyright 2025 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 |
package mmap |
| 6 |
|
| 7 |
import ( |
| 8 |
"bytes" |
| 9 |
"testing" |
| 10 |
) |
| 11 |
|
| 12 |
// TestMmap does a round trip to make sure the slice returned by |
| 13 |
// mmap contains the same data as was written to the file. It's |
| 14 |
// a test on one of the issues in #71059: on Windows we were |
| 15 |
// returning a slice containing all the data in the mmapped pages, |
| 16 |
// which could be longer than the file. |
| 17 |
func TestMmap(t *testing.T) { |
| 18 |
// Use an already existing file as our test data. Avoid creating |
| 19 |
// a temporary file so that we don't have to close the mapping on |
| 20 |
// Windows before deleting the file during test cleanup. |
| 21 |
f := "testdata/small_file.txt" |
| 22 |
|
| 23 |
want := []byte("This file is shorter than 4096 bytes.\n") |
| 24 |
|
| 25 |
data, _, err := Mmap(f) |
| 26 |
if err != nil { |
| 27 |
t.Fatalf("calling Mmap: %v", err) |
| 28 |
} |
| 29 |
if !bytes.Equal(data.Data, want) { |
| 30 |
t.Fatalf("mmapped data slice: got %q; want %q", data.Data, want) |
| 31 |
} |
| 32 |
} |