htpack/packed/load_test.go
Laurence Withers cd9c8b600e packed: add ability to load from memory, add MapAndLoad
The ability to load a packfile from memory means that it becomes easy to
use the "embed" package to embed a small packfile.

The refactor to allow loading from memory also makes it easy to add a
MapAndLoad implementation, which means the file only needs to be opened
/ mmap()ed once.
2026-06-06 10:16:40 +01:00

39 lines
813 B
Go

package packed
import (
"bytes"
"os"
"testing"
)
func TestMapAndLoad(t *testing.T) {
rawPack, err := os.ReadFile("testdata/helloworld.pack")
if err != nil {
t.Fatal(err)
}
rawText, err := os.ReadFile("testdata/helloworld.txt")
if err != nil {
t.Fatal(err)
}
mapped, err := MapAndLoad("testdata/helloworld.pack")
if err != nil {
t.Fatal(err)
}
defer mapped.Close()
if !bytes.Equal(mapped.Data, rawPack) {
t.Error("mapped data does not match raw file")
}
file := mapped.Directory.Files["/helloworld.txt"]
if file == nil {
t.Fatal("directory does not contain /helloworld.txt")
}
start := file.Uncompressed.Offset
end := start + file.Uncompressed.Length
if !bytes.Equal(rawText, mapped.Data[start:end]) {
t.Error("uncompressed file does not match testdata/helloworld.txt")
}
}