Add tests

This commit is contained in:
Hamcha 2019-05-31 10:57:51 +02:00
parent f869bf7425
commit 55a3754a23
Signed by: hamcha
GPG key ID: A40413D21021EAEE
3 changed files with 144 additions and 3 deletions

View file

@ -0,0 +1,4 @@
# draft-mlp
Example command that generates random MLP:CCG booster packs for a given set.
This is mostly done to test the library itself.

3
set.go
View file

@ -12,13 +12,14 @@ type Set interface {
type GenericSet struct {
Cards []Card
PackSize int
Alternates []AlternateProvider
}
// PackSchema returns the pack schema from a booster containing all possible cards
func (g *GenericSet) PackSchema() PackSchema {
return PackSchema{
Slots: []PackSlot{
{Amount: g.PackSize, Provider: g.RandomProvider()},
{Amount: g.PackSize, Provider: g.RandomProvider(), Alternate: g.Alternates},
},
}
}

136
set_test.go Normal file
View file

@ -0,0 +1,136 @@
package draft
import (
"fmt"
"math/rand"
"testing"
)
// TestSetRepeatable makes sure that a set can generate more cards than it contains
func TestSetRepeatable(t *testing.T) {
const PACKSIZE = 5
s := &GenericSet{
Cards: []Card{{ID: "a"}, {ID: "b"}, {ID: "c"}},
PackSize: PACKSIZE,
}
// Create a pack
pack := MakePack(s)
if len(pack) < PACKSIZE {
t.Errorf("Pack expected to contain %d cards, contains %d", PACKSIZE, len(pack))
}
// Check that all cards have something in it
for i, card := range pack {
if card.ID == "" {
t.Errorf("Pack contains \"empty\" card")
}
if card.ID != "a" && card.ID != "b" && card.ID != "c" {
t.Errorf("Pack contains unexpected card %s at index %d, not contained in pool", card.ID, i)
}
}
}
// TestAlternateProviders tests alternate providers
func TestAlternateProviders(t *testing.T) {
customProvider := func(n int) []Card {
out := make([]Card, n)
for n := range out {
out[n] = Card{ID: "x"}
}
return out
}
s := &GenericSet{
Cards: []Card{{ID: "a"}, {ID: "b"}, {ID: "c"}},
PackSize: 1,
Alternates: []AlternateProvider{
{
Probability: 0.3,
Provider: customProvider,
},
},
}
// Use set seed for getting deterministic results
rand.Seed(0)
nonalternates, alternates := 0, 0
for i := 0; i < 500000; i++ {
pack := MakePack(s)
if pack[0].ID == "x" {
alternates++
} else {
nonalternates++
}
}
// Calculate distribution
distribution := float32(float32(alternates) / (float32(nonalternates) + float32(alternates)))
// After 500k packs, I'm expecting distribution to be within 2%
if distribution < 0.29 || distribution > 0.31 {
t.Errorf("Distribution is sketchy after 500k packs: %f%%", distribution*100)
}
}
// TestCubeOverflow makes sure cubes stop providing cards as they are exhausted instead of going out of bound
func TestCubeOverflow(t *testing.T) {
const PACKSIZE = 5
c := &GenericCube{
Cards: []Card{
{ID: "a"}, {ID: "b"}, {ID: "c"}, {ID: "d"}, {ID: "e"},
{ID: "f"}, {ID: "g"}, {ID: "h"}, {ID: "i"},
},
PackSize: PACKSIZE,
}
pack1 := MakePack(c)
pack2 := MakePack(c)
// Pack 2 can only contain 4 cards, as there are not enough cards to fill it
if len(pack2) >= PACKSIZE {
t.Errorf("Pack 2 expected to contain only 4 cards, has %d", len(pack2))
}
// Check for duplicates
allcards := append(pack1, pack2...)
uniq := sliceUniq(allcards)
if len(allcards) != len(uniq) {
t.Errorf("Duplicate cards found across packs")
}
}
// ExampleGenericSet is an example usage of the Set APIs to make packs
func ExampleGenericSet() {
// Create a set with some items
s := &GenericSet{
Cards: []Card{
{ID: "a"},
{ID: "b"},
{ID: "c"},
},
PackSize: 5,
}
// Create a pack
pack := MakePack(s)
// Print cards in pack
for i, card := range pack {
fmt.Printf("Card #%d: %s\n", i, card.ID)
}
}
// https://gist.github.com/alioygur/16c66b4249cb42715091fe010eec7e33
func sliceUniq(s []Card) []Card {
for i := 0; i < len(s); i++ {
for i2 := i + 1; i2 < len(s); i2++ {
if s[i].ID == s[i2].ID {
// delete
s = append(s[:i2], s[i2+1:]...)
i2--
}
}
}
return s
}