github.com/ncruces/go-sqlite3@v0.15.1-0.20240520133447-53eef1510ff0/vfs/os_unix_test.go (about)

     1  //go:build unix && !(sqlite3_flock || sqlite3_nosys)
     2  
     3  package vfs
     4  
     5  import (
     6  	"crypto/rand"
     7  	"io"
     8  	"os"
     9  	"runtime"
    10  	"testing"
    11  
    12  	"golang.org/x/sys/unix"
    13  )
    14  
    15  func Test_osAllocate(t *testing.T) {
    16  	if runtime.GOOS != "linux" && runtime.GOOS != "darwin" {
    17  		t.Skip()
    18  	}
    19  
    20  	f, err := os.CreateTemp(t.TempDir(), "file")
    21  	if err != nil {
    22  		t.Fatal(err)
    23  	}
    24  
    25  	_, err = io.CopyN(f, rand.Reader, 1024*1024)
    26  	if err != nil {
    27  		t.Fatal(err)
    28  	}
    29  
    30  	n, err := f.Seek(0, io.SeekEnd)
    31  	if err != nil {
    32  		t.Fatal(err)
    33  	}
    34  	if n != 1024*1024 {
    35  		t.Fatalf("got %d, want %d", n, 1024*1024)
    36  	}
    37  
    38  	err = osAllocate(f, 16*1024*1024)
    39  	if err != nil {
    40  		t.Fatal(err)
    41  	}
    42  
    43  	var stat unix.Stat_t
    44  	err = unix.Stat(f.Name(), &stat)
    45  	if err != nil {
    46  		t.Fatal(err)
    47  	}
    48  
    49  	if stat.Blocks*512 != 16*1024*1024 {
    50  		t.Fatalf("got %d, want %d", stat.Blocks*512, 16*1024*1024)
    51  	}
    52  }