Files
navidrome-meilisearch/utils/random/weighted_random_chooser_test.go
Dongho Kim c251f174ed
Some checks failed
Pipeline: Test, Lint, Build / Get version info (push) Has been cancelled
Pipeline: Test, Lint, Build / Lint Go code (push) Has been cancelled
Pipeline: Test, Lint, Build / Test Go code (push) Has been cancelled
Pipeline: Test, Lint, Build / Test JS code (push) Has been cancelled
Pipeline: Test, Lint, Build / Lint i18n files (push) Has been cancelled
Pipeline: Test, Lint, Build / Check Docker configuration (push) Has been cancelled
Pipeline: Test, Lint, Build / Build (darwin/amd64) (push) Has been cancelled
Pipeline: Test, Lint, Build / Build (darwin/arm64) (push) Has been cancelled
Pipeline: Test, Lint, Build / Build (linux/386) (push) Has been cancelled
Pipeline: Test, Lint, Build / Build (linux/amd64) (push) Has been cancelled
Pipeline: Test, Lint, Build / Build (linux/arm/v5) (push) Has been cancelled
Pipeline: Test, Lint, Build / Build (linux/arm/v6) (push) Has been cancelled
Pipeline: Test, Lint, Build / Build (linux/arm/v7) (push) Has been cancelled
Pipeline: Test, Lint, Build / Build (linux/arm64) (push) Has been cancelled
Pipeline: Test, Lint, Build / Build (windows/386) (push) Has been cancelled
Pipeline: Test, Lint, Build / Build (windows/amd64) (push) Has been cancelled
Pipeline: Test, Lint, Build / Push to GHCR (push) Has been cancelled
Pipeline: Test, Lint, Build / Push to Docker Hub (push) Has been cancelled
Pipeline: Test, Lint, Build / Cleanup digest artifacts (push) Has been cancelled
Pipeline: Test, Lint, Build / Build Windows installers (push) Has been cancelled
Pipeline: Test, Lint, Build / Package/Release (push) Has been cancelled
Pipeline: Test, Lint, Build / Upload Linux PKG (push) Has been cancelled
Close stale issues and PRs / stale (push) Has been cancelled
POEditor import / update-translations (push) Has been cancelled
update
2025-12-08 16:16:23 +01:00

73 lines
1.6 KiB
Go

package random
import (
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)
var _ = Describe("WeightedChooser", func() {
var w *WeightedChooser[int]
BeforeEach(func() {
w = NewWeightedChooser[int]()
for i := 0; i < 10; i++ {
w.Add(i, i+1)
}
})
It("selects and removes a random item", func() {
Expect(w.Size()).To(Equal(10))
_, err := w.Pick()
Expect(err).ToNot(HaveOccurred())
Expect(w.Size()).To(Equal(9))
})
It("removes items", func() {
Expect(w.Size()).To(Equal(10))
for i := 0; i < 10; i++ {
Expect(w.Remove(0)).To(Succeed())
}
Expect(w.Size()).To(Equal(0))
})
It("returns error if trying to remove an invalid index", func() {
Expect(w.Size()).To(Equal(10))
Expect(w.Remove(-1)).ToNot(Succeed())
Expect(w.Remove(10000)).ToNot(Succeed())
Expect(w.Size()).To(Equal(10))
})
It("returns the sole item", func() {
ws := NewWeightedChooser[string]()
ws.Add("a", 1)
Expect(ws.Pick()).To(Equal("a"))
})
It("returns all items from the list", func() {
for i := 0; i < 10; i++ {
Expect(w.Pick()).To(BeElementOf(0, 1, 2, 3, 4, 5, 6, 7, 8, 9))
}
Expect(w.Size()).To(Equal(0))
})
It("fails when trying to choose from empty set", func() {
w = NewWeightedChooser[int]()
w.Add(1, 1)
w.Add(2, 1)
Expect(w.Pick()).To(BeElementOf(1, 2))
Expect(w.Pick()).To(BeElementOf(1, 2))
_, err := w.Pick()
Expect(err).To(HaveOccurred())
})
It("chooses based on weights", func() {
counts := [10]int{}
for i := 0; i < 200000; i++ {
c, _ := w.weightedChoice()
counts[c] = counts[c] + 1
}
for i := 0; i < 9; i++ {
Expect(counts[i]).To(BeNumerically("<", counts[i+1]))
}
})
})