File: fileinfosums_test.go

package info (click to toggle)
docker.io 20.10.24%2Bdfsg1-1%2Bdeb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 60,824 kB
  • sloc: sh: 5,621; makefile: 593; ansic: 179; python: 162; asm: 7
file content (62 lines) | stat: -rw-r--r-- 1,749 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package tarsum // import "github.com/docker/docker/pkg/tarsum"

import "testing"

func newFileInfoSums() FileInfoSums {
	return FileInfoSums{
		fileInfoSum{name: "file3", sum: "2abcdef1234567890", pos: 2},
		fileInfoSum{name: "dup1", sum: "deadbeef1", pos: 5},
		fileInfoSum{name: "file1", sum: "0abcdef1234567890", pos: 0},
		fileInfoSum{name: "file4", sum: "3abcdef1234567890", pos: 3},
		fileInfoSum{name: "dup1", sum: "deadbeef0", pos: 4},
		fileInfoSum{name: "file2", sum: "1abcdef1234567890", pos: 1},
	}
}

func TestSortFileInfoSums(t *testing.T) {
	dups := newFileInfoSums().GetAllFile("dup1")
	if len(dups) != 2 {
		t.Errorf("expected length 2, got %d", len(dups))
	}
	dups.SortByNames()
	if dups[0].Pos() != 4 {
		t.Errorf("sorted dups should be ordered by position. Expected 4, got %d", dups[0].Pos())
	}

	fis := newFileInfoSums()
	expected := "0abcdef1234567890"
	fis.SortBySums()
	got := fis[0].Sum()
	if got != expected {
		t.Errorf("Expected %q, got %q", expected, got)
	}

	fis = newFileInfoSums()
	expected = "dup1"
	fis.SortByNames()
	gotFis := fis[0]
	if gotFis.Name() != expected {
		t.Errorf("Expected %q, got %q", expected, gotFis.Name())
	}
	// since a duplicate is first, ensure it is ordered first by position too
	if gotFis.Pos() != 4 {
		t.Errorf("Expected %d, got %d", 4, gotFis.Pos())
	}

	fis = newFileInfoSums()
	fis.SortByPos()
	if fis[0].Pos() != 0 {
		t.Error("sorted fileInfoSums by Pos should order them by position.")
	}

	fis = newFileInfoSums()
	expected = "deadbeef1"
	gotFileInfoSum := fis.GetFile("dup1")
	if gotFileInfoSum.Sum() != expected {
		t.Errorf("Expected %q, got %q", expected, gotFileInfoSum)
	}
	if fis.GetFile("noPresent") != nil {
		t.Error("Should have return nil if name not found.")
	}

}