Test: tile && shown tile

This commit is contained in:
2020-03-27 18:31:02 +08:00
parent 82c62402e7
commit 2671f81336
2 changed files with 308 additions and 1 deletions
+119
View File
@@ -0,0 +1,119 @@
package model
import (
"github.com/c-my/MahjongServer/config"
"testing"
)
func TestGetChowCount(t *testing.T) {
var tests = []struct {
input []ShownTile
want int
}{
{[]ShownTile{{config.ConcealedKong, []Tile{{0, 3}, {0, 3}, {0, 3}, {0, 3}}}}, 0},
{[]ShownTile{}, 0},
{[]ShownTile{{config.Chow, []Tile{{0, 3}, {0, 4}, {0, 5}}}}, 1},
{[]ShownTile{{config.Chow, []Tile{{0, 3}, {0, 4}, {0, 5}}},
{config.Chow, []Tile{{1, 5}, {1, 6}, {1, 7}}}}, 2},
{[]ShownTile{{config.Pong, []Tile{{0, 3}, {0, 3}, {0, 3}}},
{config.Pong, []Tile{{3, 1}, {3, 1}, {3, 1}}}}, 0},
}
for _, test := range tests {
if GetChowCount(test.input) != test.want {
t.Errorf("get wrong chow count")
}
}
}
func TestGetPongCount(t *testing.T) {
var tests = []struct {
input []ShownTile
want int
}{
{[]ShownTile{{config.ConcealedKong, []Tile{{0, 3}, {0, 3}, {0, 3}, {0, 3}}}}, 0},
{[]ShownTile{}, 0},
{[]ShownTile{{config.Chow, []Tile{{0, 3}, {0, 4}, {0, 5}}}}, 0},
{[]ShownTile{{config.Chow, []Tile{{0, 3}, {0, 4}, {0, 5}}},
{config.Chow, []Tile{{1, 5}, {1, 6}, {1, 7}}}}, 0},
{[]ShownTile{{config.Pong, []Tile{{0, 3}, {0, 3}, {0, 3}}}}, 1},
{[]ShownTile{{config.Pong, []Tile{{0, 3}, {0, 3}, {0, 3}}},
{config.Pong, []Tile{{3, 1}, {3, 1}, {3, 1}}}}, 2},
}
for _, test := range tests {
if GetPongCount(test.input) != test.want {
t.Errorf("get wrong pong count")
}
}
}
func TestGetExposedKongCount(t *testing.T) {
var tests = []struct {
input []ShownTile
want int
}{
{[]ShownTile{{config.ConcealedKong, []Tile{{0, 3}, {0, 3}, {0, 3}, {0, 3}}}}, 0},
{[]ShownTile{}, 0},
{[]ShownTile{{config.Chow, []Tile{{0, 3}, {0, 4}, {0, 5}}}}, 0},
{[]ShownTile{{config.Chow, []Tile{{0, 3}, {0, 4}, {0, 5}}},
{config.Chow, []Tile{{1, 5}, {1, 6}, {1, 7}}}}, 0},
{[]ShownTile{{config.Pong, []Tile{{0, 3}, {0, 3}, {0, 3}}}}, 0},
{[]ShownTile{{config.Pong, []Tile{{0, 3}, {0, 3}, {0, 3}}},
{config.Pong, []Tile{{3, 1}, {3, 1}, {3, 1}}}}, 0},
{[]ShownTile{{config.ExposedKong, []Tile{{0, 3}, {0, 3}, {0, 3}, {0, 3}}}}, 1},
{[]ShownTile{{config.ExposedKong, []Tile{{0, 3}, {0, 3}, {0, 3}, {0, 3}}},
{config.ExposedKong, []Tile{{3, 1}, {3, 1}, {3, 1}, {3, 1}}}}, 2},
}
for _, test := range tests {
if GetExposedKongCount(test.input) != test.want {
t.Errorf("get wrong exposed kong count")
}
}
}
func TestGetConcealedKongCount(t *testing.T) {
var tests = []struct {
input []ShownTile
want int
}{
{[]ShownTile{{config.ConcealedKong, []Tile{{0, 3}, {0, 3}, {0, 3}, {0, 3}}}}, 1},
{[]ShownTile{}, 0},
{[]ShownTile{{config.Chow, []Tile{{0, 3}, {0, 4}, {0, 5}}}}, 0},
{[]ShownTile{{config.Chow, []Tile{{0, 3}, {0, 4}, {0, 5}}},
{config.Chow, []Tile{{1, 5}, {1, 6}, {1, 7}}}}, 0},
{[]ShownTile{{config.Pong, []Tile{{0, 3}, {0, 3}, {0, 3}}}}, 0},
{[]ShownTile{{config.Pong, []Tile{{0, 3}, {0, 3}, {0, 3}}},
{config.Pong, []Tile{{3, 1}, {3, 1}, {3, 1}}}}, 0},
{[]ShownTile{{config.ExposedKong, []Tile{{0, 3}, {0, 3}, {0, 3}, {0, 3}}}}, 0},
{[]ShownTile{{config.ExposedKong, []Tile{{0, 3}, {0, 3}, {0, 3}, {0, 3}}},
{config.ConcealedKong, []Tile{{3, 1}, {3, 1}, {3, 1}, {3, 1}}}}, 1},
}
for _, test := range tests {
if GetConcealedKongCount(test.input) != test.want {
t.Errorf("get wrong concealed kong count")
}
}
}
func TestGetAddedKongCount(t *testing.T) {
var tests = []struct {
input []ShownTile
want int
}{
{[]ShownTile{{config.AddedKong, []Tile{{0, 3}, {0, 3}, {0, 3}, {0, 3}}}}, 1},
{[]ShownTile{}, 0},
{[]ShownTile{{config.Chow, []Tile{{0, 3}, {0, 4}, {0, 5}}}}, 0},
{[]ShownTile{{config.Chow, []Tile{{0, 3}, {0, 4}, {0, 5}}},
{config.Chow, []Tile{{1, 5}, {1, 6}, {1, 7}}}}, 0},
{[]ShownTile{{config.Pong, []Tile{{0, 3}, {0, 3}, {0, 3}}}}, 0},
{[]ShownTile{{config.Pong, []Tile{{0, 3}, {0, 3}, {0, 3}}},
{config.Pong, []Tile{{3, 1}, {3, 1}, {3, 1}}}}, 0},
{[]ShownTile{{config.ExposedKong, []Tile{{0, 3}, {0, 3}, {0, 3}, {0, 3}}}}, 0},
{[]ShownTile{{config.AddedKong, []Tile{{0, 3}, {0, 3}, {0, 3}, {0, 3}}},
{config.ConcealedKong, []Tile{{3, 1}, {3, 1}, {3, 1}, {3, 1}}}}, 1},
}
for _, test := range tests {
if GetAddedKongCount(test.input) != test.want {
t.Errorf("get wrong added-kong count")
}
}
}
+189 -1
View File
@@ -1,6 +1,9 @@
package model
import "testing"
import (
"sort"
"testing"
)
func TestNewTile(t *testing.T) {
tile := NewTile(Character, 3)
@@ -28,6 +31,11 @@ func TestTile_GetLeftTile(t *testing.T) {
t.Errorf("get wrong left tile")
}
}
var p *Tile
p = nil
if p.GetLeftTile() != nil {
t.Errorf("left of nil isn't nil")
}
}
func TestTile_GetRightTile(t *testing.T) {
@@ -49,6 +57,11 @@ func TestTile_GetRightTile(t *testing.T) {
t.Errorf("get wrong left tile")
}
}
var p *Tile
p = nil
if p.GetRightTile() != nil {
t.Errorf("right of nil isn't nil")
}
}
func TestIsAllSeqOrTriplet(t *testing.T) {
@@ -60,6 +73,14 @@ func TestIsAllSeqOrTriplet(t *testing.T) {
{2, 1}, {2, 2}, {2, 3}, {2, 6}, {2, 7}, {2, 8}},
true,
},
{[]Tile{{0, 7}, {0, 7}, {0, 7},
{2, 1}, {2, 2}, {2, 3}, {2, 6}, {2, 7}, {2, 8}},
true,
},
{[]Tile{{0, 7}, {0, 7}, {0, 8},
{2, 1}, {2, 2}, {2, 3}, {2, 6}, {2, 7}, {2, 8}},
false,
},
}
for _, test := range tests {
if IsAllSeqOrTriplet(test.tiles, 0) != test.want {
@@ -129,3 +150,170 @@ func TestGetTilePos(t *testing.T) {
}
}
}
func TestIsSequence(t *testing.T) {
var tests = []struct {
input []Tile
want bool
}{
{[]Tile{{3, 1}, {3, 1}, {3, 1}}, false},
{[]Tile{{1, 1}, {3, 1}, {2, 1}}, false},
{[]Tile{{1, 1}, {1, 1}, {1, 1}}, false},
{[]Tile{{1, 1}, {0, 1}, {1, 1}}, false},
{[]Tile{{2, 5}, {2, 6}, {2, 7}}, true},
{[]Tile{{2, 7}, {2, 8}, {2, 9}}, true},
{[]Tile{{1, 1}, {1, 2}, {1, 3}}, true},
{[]Tile{{0, 1}, {0, 2}, {0, 3}}, true},
}
for _, test := range tests {
if IsSequence(test.input[0], test.input[1], test.input[2]) != test.want {
t.Errorf("wrong judge sequence")
}
}
}
func TestHasCharacter(t *testing.T) {
var tests = []struct {
input []Tile
want bool
}{
{[]Tile{{0, 7}, {0, 8}, {0, 9},
{2, 1}, {2, 2}, {2, 3}, {2, 6}, {2, 7}, {2, 8}},
true,
},
{[]Tile{{1, 7}, {1, 8}, {1, 9},
{2, 1}, {2, 2}, {2, 3}, {2, 6},
{2, 7}, {2, 8}, {3, 1}},
false,
},
}
for _, test := range tests {
if HasCharacter(test.input) != test.want {
t.Errorf("wrong jugde existence of character tile")
}
}
}
func TestHasBamboo(t *testing.T) {
var tests = []struct {
input []Tile
want bool
}{
{[]Tile{{0, 7}, {0, 8}, {0, 9},
{2, 1}, {2, 2}, {2, 3}, {2, 6}, {2, 7}, {2, 8}},
false,
},
{[]Tile{{1, 7}, {1, 8}, {1, 9},
{2, 1}, {2, 2}, {2, 3}, {2, 6},
{2, 7}, {2, 8}, {3, 1}},
true,
},
}
for _, test := range tests {
if HasBamboo(test.input) != test.want {
t.Errorf("wrong jugde existence of bamboo tile")
}
}
}
func TestHasDot(t *testing.T) {
var tests = []struct {
input []Tile
want bool
}{
{[]Tile{{0, 7}, {0, 8}, {0, 9},
{2, 1}, {2, 2}, {2, 3}, {2, 6}, {2, 7}, {2, 8}},
true,
},
{[]Tile{{1, 7}, {1, 8}, {1, 9},
{2, 1}, {2, 2}, {2, 3}, {2, 6},
{2, 7}, {2, 8}, {3, 1}},
true,
},
}
for _, test := range tests {
if HasDot(test.input) != test.want {
t.Errorf("wrong jugde existence of dot tile")
}
}
}
func TestGetPairPos(t *testing.T) {
var tests = []struct {
input []Tile
want []int
}{
{[]Tile{}, []int{}},
{[]Tile{{0, 7}, {3, 1}, {3, 1}, {3, 1}}, []int{1}},
}
for _, test := range tests {
res := GetPairPos(test.input)
if len(res) != len(test.want) {
t.Errorf("wrong pair pos")
}
sort.Ints(res)
for i := 0; i < len(res); i++ {
if res[i] != test.want[i] {
t.Errorf("wrong pair pos")
}
}
}
}
func TestRemoveTile(t *testing.T) {
var tests = []struct {
input []Tile
tile Tile
count int
want []Tile
}{
{[]Tile{{0, 1}, {0, 2}, {0, 3}},
Tile{0, 1},
1,
[]Tile{{0, 2}, {0, 3}}},
{[]Tile{{0, 1}, {0, 2}, {0, 3}, {1, 2}, {1, 2}},
Tile{1, 2},
2,
[]Tile{{0, 1}, {0, 2}, {0, 3}}},
{[]Tile{{1, 2}, {1, 2}},
Tile{1, 2},
2,
[]Tile{}},
}
for _, test := range tests {
res := RemoveTile(test.input, test.tile, test.count)
l := len(res)
if l != len(test.want) {
t.Errorf("failed to remove tile")
}
for i := 0; i < l; i++ {
if !res[i].Equals(test.want[i]) {
t.Errorf("failed to remove tile")
}
}
}
}
func TestRemovePair(t *testing.T) {
var tests = []struct {
tiles []Tile
pos int
want []Tile
}{
{[]Tile{{0, 1}, {0, 2}, {0, 3}, {1, 2}, {1, 2}},
3,
[]Tile{{0, 1}, {0, 2}, {0, 3}}},
}
for _, test := range tests {
res := RemovePair(test.tiles, test.pos)
l := len(res)
if l != len(test.want) {
t.Errorf("failed to remove pair")
}
for i := 0; i < l; i++ {
if !res[i].Equals(test.want[i]) {
t.Errorf("failed to remove pair")
}
}
}
}