From 51723cfd9a7d31643fa7c14cc2df9d8e9e2bd33d Mon Sep 17 00:00:00 2001 From: JP Appel Date: Fri, 13 Sep 2024 14:32:15 -0400 Subject: Add money and head handling functions Created types to manage the cannonical coinage of DnD. First pass at computing computing values for heads. --- economy/coins_test.go | 90 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 90 insertions(+) create mode 100644 economy/coins_test.go (limited to 'economy/coins_test.go') diff --git a/economy/coins_test.go b/economy/coins_test.go new file mode 100644 index 0000000..d318fbe --- /dev/null +++ b/economy/coins_test.go @@ -0,0 +1,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) + } +} -- cgit v1.2.3