Skip to content

Commit 0bd554e

Browse files
committed
Add basic raw/patch unit test and call benchmark.
1 parent 9ac9428 commit 0bd554e

File tree

7 files changed

+109
-0
lines changed

7 files changed

+109
-0
lines changed

go.mod

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,13 @@ go 1.18
55
require (
66
github.com/alecthomas/kingpin/v2 v2.4.0
77
github.com/dsnet/compress v0.0.1
8+
github.com/stretchr/testify v1.8.2
89
)
910

1011
require (
1112
github.com/alecthomas/units v0.0.0-20211218093645-b94a6e3cc137 // indirect
13+
github.com/davecgh/go-spew v1.1.1 // indirect
14+
github.com/pmezard/go-difflib v1.0.0 // indirect
1215
github.com/xhit/go-str2duration/v2 v2.1.0 // indirect
16+
gopkg.in/yaml.v3 v3.0.1 // indirect
1317
)

go.sum

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ github.com/alecthomas/units v0.0.0-20211218093645-b94a6e3cc137 h1:s6gZFSlWYmbqAu
44
github.com/alecthomas/units v0.0.0-20211218093645-b94a6e3cc137/go.mod h1:OMCwj8VM1Kc9e19TLln2VL61YJF0x1XFtfdL4JdbSyE=
55
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
66
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
7+
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
78
github.com/dsnet/compress v0.0.1 h1:PlZu0n3Tuv04TzpfPbrnI0HW/YwodEXDS+oPKahKF0Q=
89
github.com/dsnet/compress v0.0.1/go.mod h1:Aw8dCMJ7RioblQeTqt88akK31OvO8Dhf5JflhBbQEHo=
910
github.com/dsnet/golib v0.0.0-20171103203638-1ea166775780/go.mod h1:Lj+Z9rebOhdfkVLjJ8T6VcRQv3SXugXy999NBtR9aFY=
@@ -12,11 +13,19 @@ github.com/klauspost/cpuid v1.2.0/go.mod h1:Pj4uuM528wm8OyEC2QMXAi2YiTZ96dNQPGgo
1213
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
1314
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
1415
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
16+
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
17+
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
1518
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
19+
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
20+
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
1621
github.com/stretchr/testify v1.8.2 h1:+h33VjcLVPDHtOdpUCuF+7gSuG3yGIftsP1YvFihtJ8=
22+
github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
1723
github.com/ulikunitz/xz v0.5.6/go.mod h1:2bypXElzHzzJZwzH67Y6wb67pO62Rzfn7BSiF4ABRW8=
1824
github.com/xhit/go-str2duration/v2 v2.1.0 h1:lxklc02Drh6ynqX+DdPyp5pCKLUQpRT8bp8Ydu2Bstc=
1925
github.com/xhit/go-str2duration/v2 v2.1.0/go.mod h1:ohY8p+0f07DiV6Em5LKB0s2YpLtXVyJfNt1+BlmyAsU=
26+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
2027
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
2128
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
29+
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
2230
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
31+
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

internal/testing/fixtures/fixtures.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Copyright (c) 2017-2025 Carl Kittelberger.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE.txt file.
4+
5+
package fixtures
6+
7+
import _ "embed"
8+
9+
// The updated test file.
10+
//
11+
//go:embed new.txt
12+
var New []byte
13+
14+
// The original test file.
15+
//
16+
//go:embed old.txt
17+
var Old []byte
18+
19+
// A fully bsdiff-formatted patch between the original and the updated test file.
20+
//
21+
//go:embed old_to_new_patch.bin
22+
var Patch []byte

internal/testing/fixtures/new.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Lorem
2+
Ipsum
3+
Hello
4+
Set
5+
Dolem

internal/testing/fixtures/old.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Lorem
2+
Ipsum
3+
Set
4+
Dolem
87 Bytes
Binary file not shown.

raw/patch/patch_test.go

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
// Copyright (c) 2017-2025 Carl Kittelberger.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE.txt file.
4+
5+
package patch_test
6+
7+
import (
8+
"bytes"
9+
"testing"
10+
11+
"github.com/icedream/go-bsdiff/internal/testing/fixtures"
12+
"github.com/icedream/go-bsdiff/patch"
13+
"github.com/stretchr/testify/require"
14+
)
15+
16+
func TestPatch(t *testing.T) {
17+
t.Run("succeeds with valid patch", func(t *testing.T) {
18+
oldReader := bytes.NewReader(fixtures.Old)
19+
patchReader := bytes.NewReader(fixtures.Patch)
20+
var buf bytes.Buffer
21+
22+
require.NoError(t, patch.Patch(oldReader, &buf, patchReader))
23+
require.Equal(t, fixtures.New, buf.Bytes())
24+
})
25+
26+
t.Run("fails with invalid patch", func(t *testing.T) {
27+
oldReader := bytes.NewReader(fixtures.Old)
28+
patchReader := bytes.NewReader([]byte("invalid patch"))
29+
var buf bytes.Buffer
30+
31+
require.Error(t, patch.Patch(oldReader, &buf, patchReader))
32+
require.Empty(t, buf.Bytes())
33+
})
34+
35+
t.Run("fails with invalid original file", func(t *testing.T) {
36+
// TODO - bsdiff currently seems to return 0 code...?
37+
t.Skip()
38+
39+
oldReader := bytes.NewReader([]byte("bad file"))
40+
patchReader := bytes.NewReader(fixtures.Patch)
41+
var buf bytes.Buffer
42+
43+
require.Error(t, patch.Patch(oldReader, &buf, patchReader))
44+
require.Empty(t, buf.Bytes())
45+
})
46+
}
47+
48+
func BenchmarkPatch(b *testing.B) {
49+
// make sure we start in the middle of the loop from exactly 0 time passed
50+
b.StopTimer()
51+
b.ResetTimer()
52+
53+
for i := 0; i < b.N; i++ {
54+
oldReader := bytes.NewReader(fixtures.Old)
55+
patchReader := bytes.NewReader(fixtures.Patch)
56+
var buf bytes.Buffer
57+
58+
// measure actual patch
59+
b.StartTimer()
60+
err := patch.Patch(oldReader, &buf, patchReader)
61+
b.StopTimer()
62+
63+
require.NoError(b, err)
64+
}
65+
}

0 commit comments

Comments
 (0)