Skip to content

Commit

Permalink
Adding tests
Browse files Browse the repository at this point in the history
Adds tests.
  • Loading branch information
mys721tx committed Dec 26, 2024
1 parent 4fe979a commit 4b2badf
Show file tree
Hide file tree
Showing 47 changed files with 20,319 additions and 0 deletions.
7 changes: 7 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,11 @@ go 1.23
require (
github.com/go-telegram/bot v1.12.1
github.com/sethvargo/go-envconfig v1.1.0
github.com/stretchr/testify v1.10.0
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
10 changes: 10 additions & 0 deletions go.sum
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=
52 changes: 52 additions & 0 deletions pgb.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,29 @@ type builder struct {
strings.Builder
}

// WriteStrings writes multiple strings to the builder.
// It takes a variadic parameter of strings and writes each one
// sequentially using the WriteString method.
//
// Parameters:
//
// strs - a variadic list of strings to be written to the builder.
func (b *builder) WriteStrings(strs ...string) {
for _, s := range strs {
b.WriteString(s)
}
}

// newRand creates a new instance of rand.Rand seeded with the XOR combination
// of the provided seeds. It takes a slice of uint64 seeds, combines them using
// the XOR operation, and returns a pointer to a new rand.Rand object seeded
// with the resulting value.
//
// Parameters:
// - seeds: A slice of uint64 values used to seed the random number generator.
//
// Returns:
// - A pointer to a new rand.Rand object seeded with the combined seed value.
func newRand(seeds []uint64) *rand.Rand {
var s uint64

Expand All @@ -50,6 +67,15 @@ func newRand(seeds []uint64) *rand.Rand {
return rand.New(rand.NewSource(int64(s)))
}

// pia generates a string based on the provided UpdateContext.
// It randomly selects one of two possible pia (slap) actions and appends the query from the context.
//
// Parameters:
// - ctx: A pointer to an UpdateContext containing the query and random number generator.
//
// Returns:
//
// A string that includes a randomly selected pia prefix and the query from the context.
func pia(ctx *UpdateContext) string {
var b builder

Expand All @@ -65,6 +91,16 @@ func pia(ctx *UpdateContext) string {
return b.String()
}

// divine generates a divination result based on the provided UpdateContext.
// It constructs a string that includes the query and the result of the divination.
// The result is determined by generating random numbers and mapping them to specific outcomes.
// The outcomes are categorized as "吉" (good) or "凶" (bad) with varying degrees of intensity.
//
// Parameters:
// - ctx: A pointer to an UpdateContext which contains the query and a random number generator.
//
// Returns:
// - A string representing the divination result.
func divine(ctx *UpdateContext) string {
var omen, mult string
var b builder
Expand Down Expand Up @@ -140,6 +176,22 @@ func main() {
}
}

// handler processes an incoming inline query from a bot and generates a response.
// It uses the query details and current time to create a unique context for the query,
// then generates a set of inline query results based on this context and sends them back.
//
// Parameters:
// - ctx: The context for the request, used for cancellation and deadlines.
// - b: The bot instance handling the request.
// - update: The update containing the inline query to be processed.
//
// The function performs the following steps:
// 1. Checks if the update contains an inline query. If not, it returns immediately.
// 2. Creates a SHA-256 hash based on the user's ID, current time truncated to 30 minutes, and the query text.
// 3. Uses the hash to seed a random number generator.
// 4. Creates an UpdateContext with the random number generator and query text.
// 5. Generates a set of inline query results using the UpdateContext.
// 6. Sends the generated results back to the bot as a response to the inline query.
func handler(ctx context.Context, b *bot.Bot, update *models.Update) {
if update.InlineQuery == nil {
return
Expand Down
192 changes: 192 additions & 0 deletions pgb_test.go
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)
}
}
15 changes: 15 additions & 0 deletions vendor/github.com/davecgh/go-spew/LICENSE

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 4b2badf

Please sign in to comment.