From 34b8d8ff1f9d65c08a9156d72f08cf548183c6f4 Mon Sep 17 00:00:00 2001 From: JP Appel Date: Sun, 27 Apr 2025 00:49:27 -0400 Subject: Large commit; many features --- pkg/index/index_test.go | 138 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 138 insertions(+) create mode 100644 pkg/index/index_test.go (limited to 'pkg/index/index_test.go') diff --git a/pkg/index/index_test.go b/pkg/index/index_test.go new file mode 100644 index 0000000..0b5d2f2 --- /dev/null +++ b/pkg/index/index_test.go @@ -0,0 +1,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) + } + } + }) + } +} -- cgit v1.2.3