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

coinsort_test.gno

1.64 Kb · 75 lines
 1package coinsort
 2
 3import (
 4	"chain"
 5	"testing"
 6)
 7
 8func TestSortByBalance(t *testing.T) {
 9	coins := chain.Coins{
10		chain.Coin{Denom: "b", Amount: 50},
11		chain.Coin{Denom: "c", Amount: 10},
12		chain.Coin{Denom: "a", Amount: 100},
13	}
14
15	expected := chain.Coins{
16		chain.Coin{Denom: "c", Amount: 10},
17		chain.Coin{Denom: "b", Amount: 50},
18		chain.Coin{Denom: "a", Amount: 100},
19	}
20
21	SortByBalance(coins)
22
23	for i := range coins {
24		if coins[i] != expected[i] {
25			t.Errorf("SortByBalance failed at index %d: got %+v, want %+v", i, coins[i], expected[i])
26		}
27	}
28}
29
30func TestSortByCustomDescendingAmount(t *testing.T) {
31	coins := chain.Coins{
32		chain.Coin{Denom: "a", Amount: 2},
33		chain.Coin{Denom: "b", Amount: 3},
34		chain.Coin{Denom: "c", Amount: 1},
35	}
36
37	expected := chain.Coins{
38		chain.Coin{Denom: "b", Amount: 3},
39		chain.Coin{Denom: "a", Amount: 2},
40		chain.Coin{Denom: "c", Amount: 1},
41	}
42
43	SortBy(coins, func(a, b chain.Coin) bool {
44		return a.Amount > b.Amount // descending
45	})
46
47	for i := range coins {
48		if coins[i] != expected[i] {
49			t.Errorf("SortBy custom descending failed at index %d: got %+v, want %+v", i, coins[i], expected[i])
50		}
51	}
52}
53
54func TestSortByNilFunc(t *testing.T) {
55	coins := chain.Coins{
56		chain.Coin{Denom: "x", Amount: 5},
57		chain.Coin{Denom: "z", Amount: 20},
58		chain.Coin{Denom: "y", Amount: 10},
59	}
60
61	expected := chain.Coins{
62		chain.Coin{Denom: "x", Amount: 5},
63		chain.Coin{Denom: "z", Amount: 20},
64		chain.Coin{Denom: "y", Amount: 10},
65	}
66
67	SortBy(coins, nil)
68
69	// should stay the same
70	for i := range coins {
71		if coins[i] != expected[i] {
72			t.Errorf("SortBy nil func failed at index %d: got %+v, want %+v", i, coins[i], expected[i])
73		}
74	}
75}