Skip to content Search Apps Documentation Source Content Run Reset Eval Playground File Folder Download Copy Actions State String Boolean Number Struct Map Slice Pointer Function Closure Reference Nil Package Type Interface Unknown

Community realm. Read the code first. Community realm, deployed by its author. Read the code before you interact or send coins.

rating_test.gno

8.72 Kb · 237 lines
  1package standings
  2
  3import (
  4	"math"
  5	"strconv"
  6	"testing"
  7	"time"
  8
  9	"gno.land/p/nt/avl/v0"
 10	"gno.land/p/nt/uassert/v0"
 11	"gno.land/p/nt/urequire/v0"
 12)
 13
 14func showStanding(s standing) string {
 15	out := strconv.Itoa(s.rating) + " " + strconv.Itoa(s.wins) + "-" + strconv.Itoa(s.losses) + "-" +
 16		strconv.Itoa(s.draws) + " last " + strconv.Itoa(s.lastClan) + " per"
 17	for _, n := range s.perClan {
 18		out += " " + strconv.Itoa(n)
 19	}
 20	return out
 21}
 22
 23// seed gives a a standing at rating with one rated win as Pickers, clan 1, and its ladder
 24// key.
 25func seed(a address, rating int) {
 26	clanNumber("Pickers")
 27	saveStanding(a, &standing{rating: rating, wins: 1, lastClan: 1, perClan: []int{1}})
 28	ladder, _ = ladder.Set(ladderKey(rating, a), nil)
 29}
 30
 31// ladderKeys lists the ladder's keys in order.
 32func ladderKeys() []string {
 33	var keys []string
 34	ladder.TraverseByOffset(0, ladder.Size(), true, true, func(n *avl.Node) bool {
 35		keys = append(keys, n.Key())
 36		return false
 37	})
 38	return keys
 39}
 40
 41func TestExpectedTableMatchesFormula(t *testing.T) {
 42	for i, e := range expectedTable {
 43		d := float64(i * 25)
 44		uassert.Equal(t, int(math.Round(1000/(1+math.Pow(10, d/400)))), e)
 45	}
 46}
 47
 48func TestExpected(t *testing.T) {
 49	uassert.Equal(t, 500, expected(1000, 1000))
 50	uassert.Equal(t, 500, expected(1000, 1012), "a gap of 12 rounds to 0")
 51	uassert.Equal(t, 464, expected(1000, 1013), "a gap of 13 rounds to 25")
 52	uassert.Equal(t, 360, expected(1000, 1100))
 53	uassert.Equal(t, 640, expected(1100, 1000))
 54	uassert.Equal(t, 91, expected(1000, 1500), "the gap is capped at 400")
 55	uassert.Equal(t, 909, expected(1500, 1000))
 56}
 57
 58func TestEloChange(t *testing.T) {
 59	uassert.Equal(t, 16, eloChange(1000, 1000, 1000, playerK))
 60	uassert.Equal(t, 0, eloChange(1000, 1000, 500, playerK))
 61	uassert.Equal(t, 20, eloChange(1000, 1100, 1000, playerK))
 62	uassert.Equal(t, -12, eloChange(1000, 1100, 0, playerK))
 63	uassert.Equal(t, 4, eloChange(1000, 1100, 500, playerK))
 64	uassert.Equal(t, 12, eloChange(1100, 1000, 1000, playerK))
 65	uassert.Equal(t, -20, eloChange(1100, 1000, 0, playerK))
 66	uassert.Equal(t, 29, eloChange(1000, 1500, 1000, playerK))
 67	uassert.Equal(t, 8, eloChange(1000, 1000, 1000, clanK))
 68}
 69
 70func TestApplyChangeClamps(t *testing.T) {
 71	a, b := applyChange(1000, 1000, 16)
 72	uassert.Equal(t, 1016, a)
 73	uassert.Equal(t, 984, b)
 74	a, b = applyChange(5, 5, -16)
 75	uassert.Equal(t, 0, a, "no rating goes below 0")
 76	uassert.Equal(t, 21, b)
 77	a, b = applyChange(65530, 900, 29)
 78	uassert.Equal(t, maxRating, a, "no rating goes above what the ladder key holds")
 79	uassert.Equal(t, 871, b)
 80}
 81
 82func TestRateFirstMatch(cur realm, t *testing.T) {
 83	reset()
 84	testing.SetRealm(testing.NewCodeRealm(arenaV0))
 85	Record(cross(cur), alice, bob, "Pickers", "Ironhide", "a")
 86
 87	a := loadStanding(alice)
 88	urequire.True(t, a != nil)
 89	uassert.Equal(t, "1016 1-0-0 last 1 per 1", showStanding(*a))
 90	uassert.Equal(t, 1, a.mainClan())
 91	b := loadStanding(bob)
 92	urequire.True(t, b != nil)
 93	uassert.Equal(t, "984 0-1-0 last 2 per 0 1", showStanding(*b))
 94	uassert.Equal(t, 2, b.mainClan())
 95
 96	uassert.Equal(t, 2, ladder.Size())
 97	uassert.Equal(t, 1, rankOf(1016))
 98	uassert.Equal(t, 2, rankOf(984))
 99	urequire.Equal(t, 2, len(clans))
100	uassert.True(t, clans[0] == clanRecord{"Pickers", 1008, 1, 0, 0})
101	uassert.True(t, clans[1] == clanRecord{"Ironhide", 992, 0, 1, 0})
102}
103
104func TestRateMovesLadderKeys(cur realm, t *testing.T) {
105	reset()
106	testing.SetRealm(testing.NewCodeRealm(arenaV0))
107	Record(cross(cur), alice, bob, "Pickers", "Ironhide", "a")
108	Record(cross(cur), alice, bob, "Pickers", "Ironhide", "a")
109	a, b := loadStanding(alice), loadStanding(bob)
110	keys := ladderKeys()
111	urequire.Equal(t, 2, len(keys))
112	uassert.Equal(t, ladderKey(a.rating, alice), keys[0])
113	uassert.Equal(t, ladderKey(b.rating, bob), keys[1])
114}
115
116func TestRateDraw(cur realm, t *testing.T) {
117	reset()
118	testing.SetRealm(testing.NewCodeRealm(arenaV0))
119	Record(cross(cur), alice, bob, "Pickers", "Ironhide", "draw")
120	uassert.Equal(t, "1000 0-0-1 last 1 per 1", showStanding(*loadStanding(alice)))
121	uassert.Equal(t, "1000 0-0-1 last 2 per 0 1", showStanding(*loadStanding(bob)))
122}
123
124func TestForfeitCountsForClans(cur realm, t *testing.T) {
125	reset()
126	testing.SetRealm(testing.NewCodeRealm(arenaV0))
127	Record(cross(cur), alice, bob, "Pickers", "Ironhide", "b")
128	uassert.Equal(t, 984, loadStanding(alice).rating)
129	uassert.Equal(t, 1016, loadStanding(bob).rating)
130	uassert.True(t, clans[0] == clanRecord{"Pickers", 992, 0, 1, 0}, "the clan that ran out of time loses")
131	uassert.True(t, clans[1] == clanRecord{"Ironhide", 1008, 1, 0, 0})
132}
133
134func TestMirrorSkipsClans(cur realm, t *testing.T) {
135	reset()
136	testing.SetRealm(testing.NewCodeRealm(arenaV0))
137	Record(cross(cur), alice, bob, "Pickers", "Pickers", "a")
138	uassert.Equal(t, 1016, loadStanding(alice).rating)
139	urequire.Equal(t, 1, len(clans))
140	uassert.True(t, clans[0] == clanRecord{name: "Pickers", rating: startRating})
141}
142
143func TestSharedRank(t *testing.T) {
144	reset()
145	seed(alice, 1016)
146	seed(bob, 1016)
147	seed(carol, 984)
148	uassert.Equal(t, 1, rankOf(1016))
149	uassert.Equal(t, 3, rankOf(984))
150}
151
152func TestMainClanTie(t *testing.T) {
153	uassert.Equal(t, 2, standing{lastClan: 2, perClan: []int{2, 2, 0}}.mainClan())
154	uassert.Equal(t, 1, standing{lastClan: 1, perClan: []int{2, 2, 0}}.mainClan())
155	uassert.Equal(t, 1, standing{lastClan: 3, perClan: []int{2, 2, 1}}.mainClan(), "a tie the last clan is not in goes to first-seen order")
156	uassert.Equal(t, 0, standing{}.mainClan())
157}
158
159func TestClanNumbersFollowFirstSeen(cur realm, t *testing.T) {
160	reset()
161	testing.SetRealm(testing.NewCodeRealm(arenaV0))
162	Record(cross(cur), alice, bob, "Ironhide", "Delvers", "a")
163	first := showStanding(*loadStanding(alice))
164	Record(cross(cur), carol, dave, "Pickers", "Ironhide", "a")
165
166	urequire.Equal(t, 3, len(clans))
167	uassert.Equal(t, "Ironhide", clans[0].name)
168	uassert.Equal(t, "Delvers", clans[1].name)
169	uassert.Equal(t, "Pickers", clans[2].name)
170	uassert.Equal(t, "1016 1-0-0 last 1 per 1", first)
171	uassert.Equal(t, first, showStanding(*loadStanding(alice)), "a new clan moves no earlier count")
172	uassert.Equal(t, "1016 1-0-0 last 3 per 0 0 1", showStanding(*loadStanding(carol)))
173	uassert.Equal(t, "984 0-1-0 last 1 per 1", showStanding(*loadStanding(dave)))
174}
175
176func TestCountersSaturate(cur realm, t *testing.T) {
177	reset()
178	clanNumber("Pickers")
179	clanNumber("Ironhide")
180	saveStanding(alice, &standing{rating: startRating, wins: 65535, lastClan: 1, perClan: []int{65535, 0}})
181	clans[1].losses = 4294967295
182
183	testing.SetRealm(testing.NewCodeRealm(arenaV0))
184	Record(cross(cur), alice, bob, "Pickers", "Ironhide", "a")
185	a := loadStanding(alice)
186	uassert.Equal(t, 65535, a.wins)
187	uassert.Equal(t, 65535, a.perClan[0])
188	uassert.Equal(t, 4294967295, clans[1].losses)
189}
190
191func TestResultsLogEachRecord(cur realm, t *testing.T) {
192	reset()
193	testing.SetRealm(testing.NewCodeRealm(arenaV0))
194	Record(cross(cur), alice, bob, "Pickers", "Ironhide", "a")
195	Record(cross(cur), bob, alice, "Ironhide", "Ironhide", "draw")
196
197	urequire.Equal(t, 2, resultCount())
198	now := time.Now().Unix()
199	first := resultAt(1)
200	uassert.True(t, first == resultEntry{writer: 1, challenger: alice, acceptor: bob, clans: [2]int{1, 2}, result: 'a', time: now})
201	second := resultAt(2)
202	uassert.True(t, second == resultEntry{writer: 1, challenger: bob, acceptor: alice, clans: [2]int{2, 2}, result: 'd', time: now})
203}
204
205// TestResultsShareChunks checks that a new log entry grows a chunk string instead of adding
206// tree nodes, which would cost a rated ending far more storage.
207func TestResultsShareChunks(cur realm, t *testing.T) {
208	reset()
209	testing.SetRealm(testing.NewCodeRealm(arenaV0))
210	for i := 0; i < 65; i++ {
211		Record(cross(cur), alice, bob, "Pickers", "Ironhide", "a")
212	}
213	uassert.Equal(t, 2, results.Size(), "64 results to a chunk")
214	uassert.Equal(t, 65, resultCount())
215	uassert.True(t, resultAt(65) == resultEntry{writer: 1, challenger: alice, acceptor: bob, clans: [2]int{1, 2}, result: 'a', time: time.Now().Unix()})
216}
217
218// TestClanPastTheTableCountsForNone checks that a writer filling the clan table cannot stop
219// another writer's endings: a clan past the table rates its players and counts for no clan.
220func TestClanPastTheTableCountsForNone(cur realm, t *testing.T) {
221	reset()
222	for i := 1; i <= 255; i++ {
223		clanNumber("c" + strconv.Itoa(i))
224	}
225	testing.SetRealm(testing.NewCodeRealm(arenaV0))
226	Record(cross(cur), alice, bob, "c1", "c256", "a")
227
228	uassert.Equal(t, 255, len(clans))
229	uassert.True(t, clans[0] == clanRecord{name: "c1", rating: startRating}, "a clan's record moves only against another counted clan")
230	uassert.Equal(t, "1016 1-0-0 last 1 per 1", showStanding(*loadStanding(alice)))
231	uassert.Equal(t, "984 0-1-0 last 0 per", showStanding(*loadStanding(bob)))
232	uassert.Equal(t, 2, StandingOf(bob).Rank, "a player rated with no counted clan still has a rank")
233	uassert.Equal(t, "", StandingOf(bob).MainClan)
234	r := Results(0, 1)[0]
235	uassert.Equal(t, "c1", r.ChallengerClan)
236	uassert.Equal(t, "", r.AcceptorClan)
237}