aboutsummaryrefslogtreecommitdiff
path: root/search/search_test.go
blob: fcb2efb896d8839e579a2c5a37f332c13010afbc (plain)
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
package main

import (
	"slices"
	"testing"
)

func TestNormalizeWords(t *testing.T) {
	testCases := []struct {
		name     string
		input    []string
		expected []string
	}{
		{"simple", []string{"one", "two", "three"}, []string{"one", "three", "two"}},
		{"duplicates", []string{"one", "one", "two", "one", "three", "two"}, []string{"one", "three", "two"}},
	}
	for _, tc := range testCases {
		t.Run(tc.name, func(t *testing.T) {
			valid := normalizeWords(tc.input)
			if slices.Compare(valid, tc.expected) != 0 {
				t.Errorf("got %v, want %v", valid, tc.expected)
			}
		})
	}
}

func TestScoreIndex(t *testing.T) {
	testCases := []struct {
		name     string
		input    []string
		index    []string
		expected int
	}{
		{"simple", []string{"one", "two", "three"}, []string{"one", "three", "two"}, 3},
		{"duplicates", []string{"one", "one"}, []string{"one", "three", "two"}, 2},
		{"none", []string{"one", "two"}, []string{"three", "four", "five"}, 0},
	}
	for _, tc := range testCases {
		t.Run(tc.name, func(t *testing.T) {
			valid := scoreIndex(tc.input, tc.index)
			if valid != tc.expected {
				t.Errorf("got %v, want %v", valid, tc.expected)
			}
		})
	}
}