Commit 9688a5df authored by Jason Rhinelander's avatar Jason Rhinelander

Fix test script for Negative -> Signed renaming

parent 95932968
# Test script for testing various Bundle and BundleNegative behaviours
# Test script for testing various Bundle and BundleSigned behaviours
#
# NB: Take care to use numbers that are exactly representable in doubles when doing arithmetic
# manipulations: otherwise you get numerical imprecision resulting in things not being equal. e.g.
......@@ -7,20 +7,20 @@
#
# Alternatively, use EXPECT_DOUBLE_EQ when appropriate instead of EXPECT_EQ
from eris import Bundle, BundleNegative, BundleNegativityError, BundleTransactionError
from eris import Bundle, BundleSigned, BundleNegativityError, BundleTransactionError
import unittest
import math
import sys
class Base(unittest.TestCase):
def setUp(self):
self.a = BundleNegative({ 23: -4.5, 45: 100, 678: 0, 2: -483.125 })
self.a = BundleSigned({ 23: -4.5, 45: 100, 678: 0, 2: -483.125 })
self.b = Bundle({ 44: 0, 55: 12, 100000000000: 1e-10 })
self.c = Bundle()
self.d = BundleNegative({ 1: 0, 2: 0, 3: 1 })
self.e = BundleNegative({ 1: 0, 2: 0, 3: 0 })
self.d = BundleSigned({ 1: 0, 2: 0, 3: 1 })
self.e = BundleSigned({ 1: 0, 2: 0, 3: 0 })
self.a2 = Bundle({ 23: 4.5, 45: 100, 678: 0, 2: 483.125 })
self.b2 = BundleNegative({ 44: 0, 55: -12, 100000000000: 1e-10 })
self.b2 = BundleSigned({ 44: 0, 55: -12, 100000000000: 1e-10 })
self.b1 = Bundle({ 1: 3, 2: 4 })
......@@ -47,7 +47,7 @@ class Base(unittest.TestCase):
class Construction(Base):
def test_empty(self):
a = BundleNegative()
a = BundleSigned()
b = Bundle()
self.assertEqual(len(a), 0)
......@@ -68,7 +68,7 @@ class Construction(Base):
def test_pair(self):
real_idA, real_idB, real_idC = 2, 43, 8949
real_valA, real_valB, real_valC = -13.75, 1.25, 0
a = BundleNegative(real_idA, real_valA)
a = BundleSigned(real_idA, real_valA)
b = Bundle(real_idB, real_valB)
c = Bundle(real_idC, real_valC)
......@@ -136,7 +136,7 @@ class Construction(Base):
self.assertRaisesRegex(BundleNegativityError, 'illegal negative value', Bundle, {3: 1, 4: -71})
def test_clear(self):
a = BundleNegative(1, 2)
a = BundleSigned(1, 2)
b = Bundle(3, 5)
b[14] = 99
self.assertTrue(a)
......@@ -1059,8 +1059,8 @@ class Algebra(Base):
self.assertEqual(bb[7] + Bundle({ 3: 0.125}), bb[2])
self.assertEqual(bb[6] + bb[1], Bundle({ 1: 11, 2: 6, 3: 1}))
self.assertEqual(BundleNegative({ 2: 3, 4: 4}),
BundleNegative({ 1: -1, 2: 2, 3: 0, 4: 4}) + BundleNegative({ 1: 1, 2: 1, 3: 0, 4: 0}))
self.assertEqual(BundleSigned({ 2: 3, 4: 4}),
BundleSigned({ 1: -1, 2: 2, 3: 0, 4: 4}) + BundleSigned({ 1: 1, 2: 1, 3: 0, 4: 0}))
def test_subtraction(self):
bb = self.bb
......@@ -1079,8 +1079,8 @@ class Algebra(Base):
temp = bb[8] - Bundle({ 1: 1, 2: 3, 3: 1, 4: 0, 5: 0})
self.assertRaises(BundleNegativityError, lambda bb=bb: bb[8] - Bundle({ 1: 1, 2: 3, 3: 1.001}))
self.assertEqual(BundleNegative({ 1: -2, 2: 1, 4: 4}),
BundleNegative({ 1: -1, 2: 2, 3: 0, 4: 4}) - BundleNegative({ 1: 1, 2: 1, 3: 0, 4: 0}))
self.assertEqual(BundleSigned({ 1: -2, 2: 1, 4: 4}),
BundleSigned({ 1: -1, 2: 2, 3: 0, 4: 4}) - BundleSigned({ 1: 1, 2: 1, 3: 0, 4: 0}))
def test_unary_minus(self):
bb = self.bb
......@@ -1089,45 +1089,45 @@ class Algebra(Base):
temp = -bb[4]
self.assertEqual(bb[4], Bundle({ 1: 8, 2: 12, 3: 1}))
self.assertEqual(-bb[6], BundleNegative({ 1: -6, 2: -6}))
self.assertEqual(-bb[6], BundleSigned({ 1: -6, 2: -6}))
self.assertEqual(BundleNegative({ 1: 1, 2: -2, 4: -4}), -BundleNegative({ 1: -1, 2: 2, 3: 0, 4: 4 }))
self.assertEqual(BundleSigned({ 1: 1, 2: -2, 4: -4}), -BundleSigned({ 1: -1, 2: 2, 3: 0, 4: 4 }))
def test_bundle_times_constant(self):
bn = BundleNegative({ 1: 14, 2: -3, 3: 0})
bn = BundleSigned({ 1: 14, 2: -3, 3: 0})
bp = Bundle({ 1: 14, 2: 3, 3: 0})
# Make sure * doesn't affect argument
temp = bn * 2
self.assertEqual(bn, BundleNegative({ 1: 14, 2: -3}))
self.assertEqual(bn, BundleSigned({ 1: 14, 2: -3}))
self.assertEqual(bn*-0.5, BundleNegative({ 1: -7, 2: 1.5}))
self.assertEqual(bn*-0.5, BundleSigned({ 1: -7, 2: 1.5}))
self.assertEqual(bp*0.5, Bundle({ 1: 7, 2: 1.5}))
self.assertRaises(BundleNegativityError, lambda bp=bp: bp * -3)
def test_constant_times_bundle(self):
bn = BundleNegative({ 1: 14, 2: -3, 3: 0})
bn = BundleSigned({ 1: 14, 2: -3, 3: 0})
bp = Bundle({ 1: 14, 2: 3, 3: 0})
# Make sure * doesn't affect argument
temp = 2 * bn
self.assertEqual(bn, BundleNegative({ 1: 14, 2: -3}))
self.assertEqual(bn, BundleSigned({ 1: 14, 2: -3}))
self.assertEqual(-0.5*bn, BundleNegative({ 1: -7, 2: 1.5}))
self.assertEqual(-0.5*bn, BundleSigned({ 1: -7, 2: 1.5}))
self.assertEqual(0.5*bp, Bundle({ 1: 7, 2: 1.5}))
self.assertRaises(BundleNegativityError, lambda bp=bp: -3 * bp)
def test_bundle_div_constant(self):
bn = BundleNegative({ 1: 14, 2: -3, 3: 0})
bn = BundleSigned({ 1: 14, 2: -3, 3: 0})
bp = Bundle({ 1: 14, 2: 3, 3: 0})
# Make sure / doesn't affect argument
temp = bn / 2
self.assertEqual(bn, BundleNegative({ 1: 14, 2: -3}))
self.assertEqual(bn, BundleSigned({ 1: 14, 2: -3}))
self.assertEqual(bn/-2, BundleNegative({ 1: -7, 2: 1.5}))
self.assertEqual(bn/-2, BundleSigned({ 1: -7, 2: 1.5}))
self.assertEqual(bp/2, Bundle({ 1: 7, 2: 1.5}))
self.assertRaises(BundleNegativityError, lambda bp=bp: bp / -0.0001)
......@@ -1146,7 +1146,7 @@ class AlgebraicModifiers(Base):
def test_plusequals(self):
a, a2, b, c, d, e = self.a, self.a2, self.b, self.c, self.d, self.e
e += a
self.assertEqual(e, BundleNegative({ 23: -4.5, 45: 100, 2: -483.125}))
self.assertEqual(e, BundleSigned({ 23: -4.5, 45: 100, 2: -483.125}))
c += a2
self.assertEqual(c, Bundle({ 23: 4.5, 45: 100, 2: 483.125}))
......@@ -1158,21 +1158,21 @@ class AlgebraicModifiers(Base):
a2 += b
a2 += c # == a2, from above
a2 += a2
self.assertEqual(a2, BundleNegative({ 55: 24, 100000000000: 0.0000000002, 23: 18, 45: 400, 2: 1932.5}))
self.assertEqual(a2, BundleSigned({ 55: 24, 100000000000: 0.0000000002, 23: 18, 45: 400, 2: 1932.5}))
self.assertRaises(BundleNegativityError, self.plusmutate, c, BundleNegative({ 333: -0.125}))
self.assertRaises(BundleNegativityError, self.plusmutate, c, BundleSigned({ 333: -0.125}))
def test_minusequal(self):
a, a2, c, d, e = self.a, self.a2, self.c, self.d, self.e
e -= a
self.assertEqual(e, BundleNegative({ 23: 4.5, 45: -100, 2: 483.125}))
self.assertEqual(e, BundleSigned({ 23: 4.5, 45: -100, 2: 483.125}))
c -= -a2
self.assertEqual(c, Bundle({ 23: 4.5, 45: 100, 2: 483.125}))
a -= d
a -= a2
self.assertEqual(a, BundleNegative({ 23: -9, 45: 0, 2: -966.25, 3: -1}))
self.assertEqual(a, BundleSigned({ 23: -9, 45: 0, 2: -966.25, 3: -1}))
a2 -= Bundle({ 23: 4.4375, 45: 100, 678: 0, 88: 0})
self.assertEqual(a2, Bundle({ 23: 0.0625, 2: 483.125}))
......@@ -1186,7 +1186,7 @@ class AlgebraicModifiers(Base):
b2 *= -0.5
self.assertEqual(a2, Bundle({ 23: 9, 45: 200, 2: 966.25}))
self.assertEqual(b2, BundleNegative({ 55: 6, 100000000000: -5e-11}))
self.assertEqual(b2, BundleSigned({ 55: 6, 100000000000: -5e-11}))
self.assertRaises(BundleNegativityError, self.multmutate, b, -1)
......@@ -1197,7 +1197,7 @@ class AlgebraicModifiers(Base):
b2 /= -2
self.assertEqual(a2, Bundle({ 23: 9, 45: 200, 2: 966.25}))
self.assertEqual(b2, BundleNegative({ 55: 6, 100000000000: -5e-11}))
self.assertEqual(b2, BundleSigned({ 55: 6, 100000000000: -5e-11}))
self.assertRaises(BundleNegativityError, self.divmutate, b, -1)
......@@ -1206,7 +1206,7 @@ class AlgebraicModifiers(Base):
c = Bundle({ 1: 5000, 2: 40000})
# goods 1 and 2 should trigger the epsilon-rounding (for epsilon ~ 1e-3), 3 should not:
transfer = BundleNegative({ 1: 1000, 2: 9998, 3: 95000})
transfer = BundleSigned({ 1: 1000, 2: 9998, 3: 95000})
aa = a.copy()
cc = c.copy()
......@@ -1223,17 +1223,17 @@ class AlgebraicModifiers(Base):
# Now test out the destination epsilon-rounding (which applies when the destination ends up
# close to 0, which can only happen when the destination starts negative).
an = -BundleNegative(a)
cn = BundleNegative(c)
an = -BundleSigned(a)
cn = BundleSigned(c)
cn.transfer_approx(transfer, an, 1.5e-3)
self.assertEqual(an, BundleNegative({ 1: 0, 2: 0, 3: -5000}))
self.assertEqual(cn, BundleNegative({ 1: 4001, 2: 30001, 3: -95000}))
self.assertEqual(an, BundleSigned({ 1: 0, 2: 0, 3: -5000}))
self.assertEqual(cn, BundleSigned({ 1: 4001, 2: 30001, 3: -95000}))
# Test both at once
ab = BundleNegative({ 1: 999, 2: -9999, 3: 100000, 4: 500})
cb = BundleNegative({ 1: 5000, 2: 40000, 4: 500.5})
ab = BundleSigned({ 1: 999, 2: -9999, 3: 100000, 4: 500})
cb = BundleSigned({ 1: 5000, 2: 40000, 4: 500.5})
cb.transfer_approx(BundleNegative({ 1: -1000, 2: 10000, 3: -95001, 4: 500}), ab, 1.5e-3)
cb.transfer_approx(BundleSigned({ 1: -1000, 2: 10000, 3: -95001, 4: 500}), ab, 1.5e-3)
self.assertEqual(ab, Bundle({ 1: 0, 2: 0, 3: 4999, 4: 1000.5}))
self.assertEqual(cb, Bundle({ 1: 5999, 2: 30001, 3: 95001, 4: 0}))
......@@ -1241,7 +1241,7 @@ class AlgebraicModifiers(Base):
# Test out hasApprox
ha = Bundle({ 1: 1001})
hb = Bundle({ 2: 99})
ht = BundleNegative({ 1: 1000, 2: -99.3})
ht = BundleSigned({ 1: 1000, 2: -99.3})
self.assertTrue(ha.has_approx(ht, hb, .01))
self.assertFalse(ha.has_approx(ht, hb, .00001))
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment