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.

tally_test.gno

3.38 Kb · 118 lines
  1package rulecalc
  2
  3import "testing"
  4
  5// two criteria, weights 3 and 1
  6func ws(tie TieBreak, quorum int) Rule {
  7	return Rule{Method: WeightedSum, Criteria: []Criterion{{"a", 3}, {"b", 1}}, Quorum: quorum, Tie: tie}
  8}
  9
 10func sheet(ev string, scores ...[]int) Sheet {
 11	return Sheet{Evaluator: address(ev), Scores: scores}
 12}
 13
 14func TestComputeWeightedSum(t *testing.T) {
 15	// app0: 3*5+1*5=20 and 3*4+1*0=12 -> 32 ; app1: 3*6+1*0=18 and 3*6+1*2=20 -> 38
 16	sheets := []Sheet{
 17		sheet("e1", []int{5, 5}, []int{6, 0}),
 18		sheet("e2", []int{4, 0}, []int{6, 2}),
 19	}
 20	w, paid := Compute(ws(LowestID, 2), 2, sheets)
 21	if !paid || w != 1 {
 22		t.Fatalf("want winner 1 paid, got %d %v", w, paid)
 23	}
 24	tot := Totals(ws(LowestID, 2), 2, sheets)
 25	if len(tot) != 2 || tot[0] != 32 || tot[1] != 38 {
 26		t.Fatalf("totals: want [32 38], got %v", tot)
 27	}
 28}
 29
 30func TestComputeMajorityFirst(t *testing.T) {
 31	r := ws(LowestID, 3)
 32	r.Method = MajorityFirst
 33	// sheet firsts: e1 -> 0, e2 -> 1, e3 -> 1. Weighted sums would favor 0 heavily.
 34	sheets := []Sheet{
 35		sheet("e1", []int{10, 10}, []int{0, 0}),
 36		sheet("e2", []int{5, 0}, []int{6, 0}),
 37		sheet("e3", []int{5, 0}, []int{6, 0}),
 38	}
 39	w, paid := Compute(r, 2, sheets)
 40	if !paid || w != 1 {
 41		t.Fatalf("want winner 1 paid, got %d %v", w, paid)
 42	}
 43	tot := Totals(r, 2, sheets)
 44	if tot[0] != 1 || tot[1] != 2 {
 45		t.Fatalf("first-place counts: want [1 2], got %v", tot)
 46	}
 47}
 48
 49func TestComputeMajorityFirstInSheetTieLowestID(t *testing.T) {
 50	r := ws(LowestID, 1)
 51	r.Method = MajorityFirst
 52	// in-sheet tie between app0 and app1 -> first place is app0
 53	w, paid := Compute(r, 2, []Sheet{sheet("e1", []int{2, 2}, []int{2, 2})})
 54	if !paid || w != 0 {
 55		t.Fatalf("want winner 0 paid, got %d %v", w, paid)
 56	}
 57}
 58
 59func TestComputeTieLowestID(t *testing.T) {
 60	sheets := []Sheet{sheet("e1", []int{2, 0}, []int{1, 3})} // 6 vs 6
 61	w, paid := Compute(ws(LowestID, 1), 2, sheets)
 62	if !paid || w != 0 {
 63		t.Fatalf("want winner 0 paid, got %d %v", w, paid)
 64	}
 65}
 66
 67func TestComputeTieRefund(t *testing.T) {
 68	sheets := []Sheet{sheet("e1", []int{2, 0}, []int{1, 3})} // 6 vs 6
 69	_, paid := Compute(ws(Refund, 1), 2, sheets)
 70	if paid {
 71		t.Fatal("tie with Refund must not pay")
 72	}
 73}
 74
 75func TestComputeQuorum(t *testing.T) {
 76	sheets := []Sheet{
 77		sheet("e1", []int{5, 5}, []int{6, 0}),
 78		sheet("e2", []int{4, 0}, []int{6, 2}),
 79	}
 80	_, paid := Compute(ws(LowestID, 3), 2, sheets)
 81	if paid {
 82		t.Fatal("below quorum must not pay")
 83	}
 84}
 85
 86func TestComputeNoSheets(t *testing.T) {
 87	_, paid := Compute(ws(LowestID, 1), 2, nil)
 88	if paid {
 89		t.Fatal("no sheets must not pay")
 90	}
 91}
 92
 93func TestComputeNoApps(t *testing.T) {
 94	_, paid := Compute(ws(LowestID, 1), 0, []Sheet{sheet("e1")})
 95	if paid {
 96		t.Fatal("no apps must not pay")
 97	}
 98}
 99
100func TestValidateSheet(t *testing.T) {
101	r := ws(LowestID, 1)
102	if err := ValidateSheet(r, 2, sheet("e1", []int{0, 10}, []int{10, 0})); err != nil {
103		t.Fatalf("valid sheet rejected: %v", err)
104	}
105	bad := []Sheet{
106		sheet("e1", []int{11, 0}, []int{0, 0}),           // score 11
107		sheet("e1", []int{-1, 0}, []int{0, 0}),           // score -1
108		sheet("e1", []int{1, 1}),                         // too few apps
109		sheet("e1", []int{1, 1}, []int{1, 1}, []int{1, 1}), // too many apps
110		sheet("e1", []int{1}, []int{1, 1}),               // too few criteria
111		sheet("e1", []int{1, 1, 1}, []int{1, 1}),         // too many criteria
112	}
113	for i, s := range bad {
114		if err := ValidateSheet(r, 2, s); err == nil {
115			t.Errorf("bad sheet %d accepted", i)
116		}
117	}
118}