-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds tests.
- Loading branch information
Showing
47 changed files
with
20,319 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,16 @@ | ||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= | ||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= | ||
github.com/go-telegram/bot v1.12.1 h1:2CSwMd+g71/XrmuSpvEjLtsmkfL/s63PdnLboGJQxtw= | ||
github.com/go-telegram/bot v1.12.1/go.mod h1:i2TRs7fXWIeaceF3z7KzsMt/he0TwkVC680mvdTFYeM= | ||
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI= | ||
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= | ||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= | ||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= | ||
github.com/sethvargo/go-envconfig v1.1.0 h1:cWZiJxeTm7AlCvzGXrEXaSTCNgip5oJepekh/BOQuog= | ||
github.com/sethvargo/go-envconfig v1.1.0/go.mod h1:JLd0KFWQYzyENqnEPWWZ49i4vzZo/6nRidxI8YvGiHw= | ||
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA= | ||
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= | ||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= | ||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= | ||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= | ||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,192 @@ | ||
package main | ||
|
||
import ( | ||
"math/rand" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestWriteStrings(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
input []string | ||
expected string | ||
}{ | ||
{ | ||
name: "single string", | ||
input: []string{"hello"}, | ||
expected: "hello", | ||
}, | ||
{ | ||
name: "multiple strings", | ||
input: []string{"hello", " ", "world"}, | ||
expected: "hello world", | ||
}, | ||
{ | ||
name: "empty strings", | ||
input: []string{"", "", ""}, | ||
expected: "", | ||
}, | ||
{ | ||
name: "mixed strings", | ||
input: []string{"foo", "", "bar"}, | ||
expected: "foobar", | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
var b builder | ||
b.WriteStrings(tt.input...) | ||
assert.Equal(t, tt.expected, b.String()) | ||
}) | ||
} | ||
} | ||
func TestNewRand(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
seeds []uint64 | ||
}{ | ||
{ | ||
name: "single seed", | ||
seeds: []uint64{12345}, | ||
}, | ||
{ | ||
name: "multiple seeds", | ||
seeds: []uint64{12345, 67890, 54321}, | ||
}, | ||
{ | ||
name: "no seeds", | ||
seeds: []uint64{}, | ||
}, | ||
{ | ||
name: "all zero seeds", | ||
seeds: []uint64{0, 0, 0}, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
r := newRand(tt.seeds) | ||
assert.NotNil(t, r) | ||
assert.IsType(t, &rand.Rand{}, r) | ||
}) | ||
} | ||
} | ||
func TestPia(t *testing.T) { | ||
const n = 10000 | ||
const seed = 42 | ||
expRatios := map[string]int{ | ||
"Pia!▼(o ‵-′)ノ★": 1, | ||
"Pia!<(=o ‵-′)ノ☆": 7, | ||
} | ||
|
||
expTotal := 0 | ||
|
||
for _, v := range expRatios { | ||
expTotal += v | ||
} | ||
|
||
counts := map[string]int{ | ||
"Pia!▼(o ‵-′)ノ★": 0, | ||
"Pia!<(=o ‵-′)ノ☆": 0, | ||
} | ||
|
||
r := rand.New(rand.NewSource(seed)) | ||
for i := 0; i < n; i++ { | ||
query := "test query" | ||
ctx := &UpdateContext{ | ||
Rand: r, | ||
Query: &query, | ||
} | ||
result := pia(ctx) | ||
for key := range counts { | ||
if strings.Contains(result, key) { | ||
counts[key]++ | ||
break | ||
} | ||
} | ||
} | ||
|
||
countsTotal := 0 | ||
for _, v := range counts { | ||
countsTotal += v | ||
} | ||
|
||
for key, count := range counts { | ||
expected := float64(expRatios[key]) / float64(expTotal) | ||
actual := float64(count) / float64(countsTotal) | ||
t.Logf("%s: %d, expected: %f, actual: %f", key, count, expected, actual) | ||
assert.InDelta(t, actual, expected, expected*0.1, "Ratio mismatch for: "+key) | ||
} | ||
} | ||
|
||
func TestDivine(t *testing.T) { | ||
const iterations = 10000 | ||
const randSeed = 42 | ||
expRatios := map[string]int{ | ||
"极大吉": 7 * 1, | ||
"超大吉": 7 * 10, | ||
"特大吉": 7 * 45, | ||
"甚大吉": 7 * 120, | ||
"大吉": 7 * 210, | ||
"吉": 7 * 252, | ||
"小吉": 7 * 210, | ||
"甚小吉": 7 * 120, | ||
"特小吉": 7 * 45, | ||
"超小吉": 7 * 10, | ||
"极小吉": 7 * 1, | ||
"尚可": 2 * 1024, | ||
"极小凶": 7 * 1, | ||
"超小凶": 7 * 10, | ||
"特小凶": 7 * 45, | ||
"甚小凶": 7 * 120, | ||
"小凶": 7 * 210, | ||
"凶": 7 * 252, | ||
"大凶": 7 * 210, | ||
"甚大凶": 7 * 120, | ||
"特大凶": 7 * 45, | ||
"超大凶": 7 * 10, | ||
"极大凶": 7 * 1, | ||
} | ||
|
||
expTotal := 0 | ||
for _, v := range expRatios { | ||
expTotal += v | ||
} | ||
|
||
counts := make(map[string]int) | ||
for key := range expRatios { | ||
counts[key] = 0 | ||
} | ||
|
||
r := rand.New(rand.NewSource(randSeed)) | ||
for i := 0; i < iterations; i++ { | ||
query := "test query" | ||
ctx := &UpdateContext{ | ||
Rand: r, | ||
Query: &query, | ||
} | ||
result := divine(ctx) | ||
for key := range counts { | ||
if strings.Contains(result, key) { | ||
counts[key]++ | ||
break | ||
} | ||
} | ||
} | ||
|
||
countsTotal := 0 | ||
for _, v := range counts { | ||
countsTotal += v | ||
} | ||
|
||
for key, count := range counts { | ||
expected := float64(expRatios[key]) / float64(expTotal) | ||
actual := float64(count) / float64(countsTotal) | ||
t.Logf("%s: %d, expected: %f, actual: %f", key, count, expected, actual) | ||
assert.InDelta(t, actual, expected, expected*3, "Ratio mismatch for: "+key) | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.