]> Cypherpunks repositories - gostls13.git/commitdiff
archive: use slices and maps to clean up tests
authorapocelipes <seve3r@outlook.com>
Wed, 24 Jul 2024 10:24:16 +0000 (10:24 +0000)
committerGopher Robot <gobot@golang.org>
Thu, 25 Jul 2024 00:25:45 +0000 (00:25 +0000)
Replace reflect.DeepEqual with slices.Equal/maps.Equal, which is
much faster.

Clean up some unnecessary helper functions.

Change-Id: I9b94bd43886302b9b327539ab065a435ce0d75d9
GitHub-Last-Rev: b9ca21f165bcc5e45733e6a511a2344b1aa4a281
GitHub-Pull-Request: golang/go#67607
Reviewed-on: https://go-review.googlesource.com/c/go/+/587936
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Ian Lance Taylor <iant@google.com>
Reviewed-by: Dmitri Shuralyov <dmitshur@google.com>
Auto-Submit: Ian Lance Taylor <iant@google.com>
Reviewed-by: Joseph Tsai <joetsai@digital-static.net>
src/archive/tar/reader_test.go
src/archive/tar/tar_test.go
src/archive/tar/writer_test.go
src/archive/zip/reader_test.go

index 7e0462c3f883f6df84b19aab97b08c6b9d4a4638..cc49fe3641c45b14b496c1c1e522b3be749e2c70 100644 (file)
@@ -11,10 +11,12 @@ import (
        "errors"
        "fmt"
        "io"
+       "maps"
        "math"
        "os"
        "path"
        "reflect"
+       "slices"
        "strconv"
        "strings"
        "testing"
@@ -1017,7 +1019,7 @@ func TestParsePAX(t *testing.T) {
        for i, v := range vectors {
                r := strings.NewReader(v.in)
                got, err := parsePAX(r)
-               if !reflect.DeepEqual(got, v.want) && !(len(got) == 0 && len(v.want) == 0) {
+               if !maps.Equal(got, v.want) && !(len(got) == 0 && len(v.want) == 0) {
                        t.Errorf("test %d, parsePAX():\ngot  %v\nwant %v", i, got, v.want)
                }
                if ok := err == nil; ok != v.ok {
@@ -1134,7 +1136,7 @@ func TestReadOldGNUSparseMap(t *testing.T) {
                v.input = v.input[copy(blk[:], v.input):]
                tr := Reader{r: bytes.NewReader(v.input)}
                got, err := tr.readOldGNUSparseMap(&hdr, &blk)
-               if !equalSparseEntries(got, v.wantMap) {
+               if !slices.Equal(got, v.wantMap) {
                        t.Errorf("test %d, readOldGNUSparseMap(): got %v, want %v", i, got, v.wantMap)
                }
                if err != v.wantErr {
@@ -1325,7 +1327,7 @@ func TestReadGNUSparsePAXHeaders(t *testing.T) {
                r := strings.NewReader(v.inputData + "#") // Add canary byte
                tr := Reader{curr: &regFileReader{r, int64(r.Len())}}
                got, err := tr.readGNUSparsePAXHeaders(&hdr)
-               if !equalSparseEntries(got, v.wantMap) {
+               if !slices.Equal(got, v.wantMap) {
                        t.Errorf("test %d, readGNUSparsePAXHeaders(): got %v, want %v", i, got, v.wantMap)
                }
                if err != v.wantErr {
index 7398e7602ab4d836afbdddfaec8467f1db1d229d..372f1737ed5f731007ac6bfb1ad961341e403300 100644 (file)
@@ -11,11 +11,13 @@ import (
        "internal/testenv"
        "io"
        "io/fs"
+       "maps"
        "math"
        "os"
        "path"
        "path/filepath"
        "reflect"
+       "slices"
        "strings"
        "testing"
        "time"
@@ -98,10 +100,6 @@ func (f *testFile) Seek(pos int64, whence int) (int64, error) {
        return f.pos, nil
 }
 
-func equalSparseEntries(x, y []sparseEntry) bool {
-       return (len(x) == 0 && len(y) == 0) || reflect.DeepEqual(x, y)
-}
-
 func TestSparseEntries(t *testing.T) {
        vectors := []struct {
                in   []sparseEntry
@@ -198,11 +196,11 @@ func TestSparseEntries(t *testing.T) {
                        continue
                }
                gotAligned := alignSparseEntries(append([]sparseEntry{}, v.in...), v.size)
-               if !equalSparseEntries(gotAligned, v.wantAligned) {
+               if !slices.Equal(gotAligned, v.wantAligned) {
                        t.Errorf("test %d, alignSparseEntries():\ngot  %v\nwant %v", i, gotAligned, v.wantAligned)
                }
                gotInverted := invertSparseEntries(append([]sparseEntry{}, v.in...), v.size)
-               if !equalSparseEntries(gotInverted, v.wantInverted) {
+               if !slices.Equal(gotInverted, v.wantInverted) {
                        t.Errorf("test %d, inverseSparseEntries():\ngot  %v\nwant %v", i, gotInverted, v.wantInverted)
                }
        }
@@ -744,7 +742,7 @@ func TestHeaderAllowedFormats(t *testing.T) {
                if formats != v.formats {
                        t.Errorf("test %d, allowedFormats(): got %v, want %v", i, formats, v.formats)
                }
-               if formats&FormatPAX > 0 && !reflect.DeepEqual(paxHdrs, v.paxHdrs) && !(len(paxHdrs) == 0 && len(v.paxHdrs) == 0) {
+               if formats&FormatPAX > 0 && !maps.Equal(paxHdrs, v.paxHdrs) && !(len(paxHdrs) == 0 && len(v.paxHdrs) == 0) {
                        t.Errorf("test %d, allowedFormats():\ngot  %v\nwant %s", i, paxHdrs, v.paxHdrs)
                }
                if (formats != FormatUnknown) && (err != nil) {
index 9c3bcea7675016482813472c636cf40caf206d23..9542abe3e765e9fb6bcb567aef366647c6cf9ae9 100644 (file)
@@ -10,9 +10,9 @@ import (
        "errors"
        "io"
        "io/fs"
+       "maps"
        "os"
        "path"
-       "reflect"
        "slices"
        "strings"
        "testing"
@@ -702,7 +702,7 @@ func TestPaxXattrs(t *testing.T) {
        if err != nil {
                t.Fatal(err)
        }
-       if !reflect.DeepEqual(hdr.Xattrs, xattrs) {
+       if !maps.Equal(hdr.Xattrs, xattrs) {
                t.Fatalf("xattrs did not survive round trip: got %+v, want %+v",
                        hdr.Xattrs, xattrs)
        }
index 00e5ec3e0586103fbb6a1767e00ceb8b20c2033b..bfa35c992a924c0717e473c57b9c9f8d6f2d6500 100644 (file)
@@ -13,8 +13,8 @@ import (
        "io/fs"
        "os"
        "path/filepath"
-       "reflect"
        "regexp"
+       "slices"
        "strings"
        "testing"
        "testing/fstest"
@@ -1274,7 +1274,7 @@ func TestFSWalk(t *testing.T) {
                        } else if !test.wantErr && sawErr {
                                t.Error("unexpected error")
                        }
-                       if test.want != nil && !reflect.DeepEqual(files, test.want) {
+                       if test.want != nil && !slices.Equal(files, test.want) {
                                t.Errorf("got %v want %v", files, test.want)
                        }
                })
@@ -1580,7 +1580,7 @@ func TestCVE202141772(t *testing.T) {
                        t.Errorf("Opening %q with fs.FS API succeeded", f.Name)
                }
        }
-       if !reflect.DeepEqual(names, entryNames) {
+       if !slices.Equal(names, entryNames) {
                t.Errorf("Unexpected file entries: %q", names)
        }
        if _, err := r.Open(""); err == nil {
@@ -1693,7 +1693,7 @@ func TestInsecurePaths(t *testing.T) {
                for _, f := range zr.File {
                        gotPaths = append(gotPaths, f.Name)
                }
-               if !reflect.DeepEqual(gotPaths, []string{path}) {
+               if !slices.Equal(gotPaths, []string{path}) {
                        t.Errorf("NewReader for archive with file %q: got files %q", path, gotPaths)
                        continue
                }
@@ -1718,7 +1718,7 @@ func TestDisableInsecurePathCheck(t *testing.T) {
        for _, f := range zr.File {
                gotPaths = append(gotPaths, f.Name)
        }
-       if want := []string{name}; !reflect.DeepEqual(gotPaths, want) {
+       if want := []string{name}; !slices.Equal(gotPaths, want) {
                t.Errorf("NewReader with zipinsecurepath=1: got files %q, want %q", gotPaths, want)
        }
 }