aboutsummaryrefslogtreecommitdiffstats
path: root/pkg/index/index_test.go
blob: 0b5d2f259f4b01cd8156b49075b8594dbdc4f74e (plain) (blame)
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
package index

import (
	"fmt"
	"os"
	"slices"
	"testing"
)

var indexCases map[string]func(t *testing.T) Index

func init() {
	indexCases = make(map[string]func(t *testing.T) Index)

	indexCases["single file"] = func(t *testing.T) Index {
		root := t.TempDir()
		index := Index{Root: root, Filters: []DocFilter{NewExtensionFilter(".md")}}

		f, err := os.Create(root + "/a_file.md")
		if err != nil {
			t.Fatal(err)
		}
		f.WriteString("some file contents\n")

		return index
	}

	indexCases["large file"] = func(t *testing.T) Index {
		root := t.TempDir()
		index := Index{Root: root}

		return index
	}

	indexCases["worker saturation"] = func(t *testing.T) Index {
		root := t.TempDir()
		index := Index{Root: root}

		permission := os.FileMode(0o777)
		for _, dirName := range []string{"a", "b", "c", "d", "e", "f"} {
			dir := root + "/" + dirName
			if err := os.Mkdir(dir, permission); err != nil {
				t.Fatal(err)
			}
			for i := range 8 {
				fName := fmt.Sprint(dirName, i)
				f, err := os.Create(dir + "/" + fName)
				if err != nil {
					t.Fatal(err)
				}
				f.WriteString(fName)
			}
		}

		return index
	}
}

func TestIndex_Traverse(t *testing.T) {
	tests := []struct {
		name       string
		indexCase  func(t *testing.T) Index
		numWorkers uint
		want       []string
	}{
		{name: "single file", indexCase: indexCases["single file"], numWorkers: 2, want: []string{"a_file.md"}},
		{name: "saturation test", indexCase: indexCases["worker saturation"], numWorkers: 2, want: []string{
			"a/a0", "a/a1", "a/a2", "a/a3", "a/a4", "a/a5", "a/a6", "a/a7",
			"b/b0", "b/b1", "b/b2", "b/b3", "b/b4", "b/b5", "b/b6", "b/b7",
			"c/c0", "c/c1", "c/c2", "c/c3", "c/c4", "c/c5", "c/c6", "c/c7",
			"d/d0", "d/d1", "d/d2", "d/d3", "d/d4", "d/d5", "d/d6", "d/d7",
			"e/e0", "e/e1", "e/e2", "e/e3", "e/e4", "e/e5", "e/e6", "e/e7",
			"f/f0", "f/f1", "f/f2", "f/f3", "f/f4", "f/f5", "f/f6", "f/f7",
		}},
	}
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			idx := tt.indexCase(t)
			got := idx.Traverse(tt.numWorkers)

			slices.Sort(got)
			slices.Sort(tt.want)

			n := min(len(got), len(tt.want))
			if len(got) != len(tt.want) {
				t.Errorf("Wanted %v got %v paths", len(tt.want), len(got))
				t.Logf("Checking up to %d values", n)
			}

			for i := range n {
				gotPath := got[i]
				wantPath := idx.Root + "/" + tt.want[i]
				if gotPath != wantPath {
					t.Errorf("At %d wanted %v, got %v", i, wantPath, gotPath)
				}
			}
		})
	}
}

func TestIndex_Filter(t *testing.T) {
	tests := []struct {
		name       string
		paths      []string
		indexCase  func(t *testing.T) Index
		numWorkers uint
		want       []string
	}{
		{"single file", []string{"a_file.md"}, indexCases["single file"], 2, []string{"a_file.md"}},
	}
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			idx := tt.indexCase(t)
			for i, path := range tt.paths {
				tt.paths[i] = idx.Root + "/" + path
			}

			got := idx.Filter(tt.paths, tt.numWorkers)

			slices.Sort(got)
			slices.Sort(tt.want)

			n := min(len(got), len(tt.want))
			if len(got) != len(tt.want) {
				t.Errorf("Wanted %v got %v paths", len(tt.want), len(got))
				t.Logf("Checking up to %d values", n)
			}

			for i := range n {
				gotPath := got[i]
				wantPath := idx.Root + "/" + tt.want[i]
				if gotPath != wantPath {
					t.Errorf("At %d wanted %v, got %v", i, wantPath, gotPath)
				}
			}
		})
	}
}