1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
|
package economy_test
import (
"nonsense-time/economy"
"testing"
)
func assert[T comparable](t *testing.T, expected T, actual T) bool {
if expected != actual {
t.Logf("expected %v != actual %v\n", expected, actual)
t.Fail()
return false
}
return true
}
func TestMoneyAdd(t *testing.T) {
a := economy.Money{0, -1, -2, -3, -4}
b := economy.Money{1, 2, 3, 4, 5}
a.Add(b)
pass := true
pass = pass && assert(t, 1, a.Copper)
pass = pass && assert(t, 1, a.Silver)
pass = pass && assert(t, 1, a.Electrum)
pass = pass && assert(t, 1, a.Gold)
pass = pass && assert(t, 1, a.Platinum)
if !pass {
t.Logf("%+v\n", a)
}
}
func TestMoneySubtract(t *testing.T) {
a := economy.Money{1, 2, 3, 4, 5}
b := economy.Money{0, 1, 2, 3, 4}
a.Subtract(b)
pass := true
pass = pass && assert(t, 1, a.Copper)
pass = pass && assert(t, 1, a.Silver)
pass = pass && assert(t, 1, a.Electrum)
pass = pass && assert(t, 1, a.Gold)
pass = pass && assert(t, 1, a.Platinum)
if !pass {
t.Logf("%+v\n", a)
}
}
func TestMoneyMult(t *testing.T) {
a := economy.Money{1, 2, 3, 5, 7}
b := economy.Money{11, 13, 17, 19, 23}
t.Log("Testing positive multiplication")
a.Multiply(b)
pass := true
pass = pass && assert(t, 11, a.Copper)
pass = pass && assert(t, 26, a.Silver)
pass = pass && assert(t, 51, a.Electrum)
pass = pass && assert(t, 95, a.Gold)
pass = pass && assert(t, 161, a.Platinum)
if !pass {
t.Logf("%+v\n", a)
}
}
func TestMoneyValue(t *testing.T) {
a := economy.Money{1, 1, 1, 1, 1}
t.Log("Testing all positive coins")
if !assert(t, 1161, a.Value()) {
t.Log(a)
}
t.Log("Testing all negative coins")
a = economy.Money{-1, -1, -1, -1, -1}
if !assert(t, -1161, a.Value()) {
t.Log(a)
}
t.Log("Testing mixed coins")
a.Copper = 10
a.Electrum = 2
if !assert(t, -1000, a.Value()) {
t.Log(a)
}
}
|