1 // Copyright 2019 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.
5 // +build darwin dragonfly freebsd openbsd netbsd
18 "golang.org/x/sys/unix"
21 func TestGetdirentries(t *testing.T) {
22 for _, count := range []int{10, 1000} {
23 t.Run(fmt.Sprintf("n=%d", count), func(t *testing.T) {
24 testGetdirentries(t, count)
28 func testGetdirentries(t *testing.T, count int) {
29 if count > 100 && testing.Short() && os.Getenv("GO_BUILDER_NAME") == "" {
30 t.Skip("skipping in -short mode")
32 d, err := ioutil.TempDir("", "getdirentries-test")
34 t.Fatalf("Tempdir: %v", err)
38 for i := 0; i < count; i++ {
39 names = append(names, fmt.Sprintf("file%03d", i))
42 // Make files in the temp directory
43 for _, name := range names {
44 err := ioutil.WriteFile(filepath.Join(d, name), []byte("data"), 0)
46 t.Fatalf("WriteFile: %v", err)
50 // Read files using Getdirentries
51 fd, err := unix.Open(d, unix.O_RDONLY, 0)
53 t.Fatalf("Open: %v", err)
58 names2 := make([]string, 0, count)
60 n, err := unix.Getdirentries(fd, buf[:], &base)
62 t.Fatalf("Getdirentries: %v", err)
70 bc, _, names2 = unix.ParseDirent(data, -1, names2)
71 if bc == 0 && len(data) > 0 {
72 t.Fatal("no progress")
80 if strings.Join(names, ":") != strings.Join(names2, ":") {
81 t.Errorf("names don't match\n names: %q\nnames2: %q", names, names2)