def testDouble(self): sv1 = sparse(self.example) sv2 = sparse(self.example) sv = sv1 + sv2 d = dict(self.example) for k in range(20): self.failUnlessEqual(sv[k], 2 * d.get(k, 0))
def testSumSparse(self): for m in range(5): sv = sparse(self.example) sv = sumSparse(m*[sv]) d = dict(self.example) for k in range(20): self.failUnlessEqual(sv[k], m * d.get(k, 0))
def testIter(self): sv = sparse(self.example) ref = sorted(self.example) for i, item in enumerate(sv): self.failUnlessEqual(item, ref[i]) sl = list(sv) self.failUnlessEqual(sl, ref)
def testSeven(self): sv = sparse(self.example) self.failUnlessEqual(sv.size, len(self.example)) d = dict(self.example) for k in range(20): self.failUnlessEqual(sv[k], d.get(k, 0))
def testSum(self): sv = sparse(self.example) self.failUnlessEqual(sv.sum(), sum(dict(self.example).values()))
def testBool(self): self.failUnlessEqual(bool(sparse([])), False) self.failUnlessEqual(bool(sparse(self.example)), True)
def testEmpty(self): sv = sparse([]) self.failUnlessEqual(sv.size, 0) for i in range(10): self.failUnlessEqual(sv[i], 0)
def testCopyByConstructor(self): sv1 = sparse(self.example) sv2 = sparse(sv1) self.assertEqual(sv1, sv2)
def testThreshold(self): for t in [0.5, 1.0, 1.5, 2.0, 2.5]: sv = sparse(self.example).threshold(t) self.failUnlessEqual(list(sv), sorted(filter(lambda x: x[1]>=t, self.example)))
def testSum(self): sv = sparse(self.example) self.assertEqual(sv.sum(), sum(dict(self.example).values()))
def testCopyByCopy(self): from copy import copy sv1 = sparse(self.example) sv2 = copy(sv1) self.failUnlessEqual(sv1, sv2)
def testDiv(self): sv = sparse(self.example) sv3 = sv / 3.0 ref = sparse([ (k, v / 3) for k, v in self.example ]) self.failUnlessEqual(sv3, ref)
def testContains(self): sv = sparse(self.example) d = dict(self.example) for k in range(20): self.failUnlessEqual(k in sv, k in d)
def testAdd(self): sv = sparse(self.example) for x in [ [ (-100, 42) ], [], [(100, 42)], [(-100, 42), (100, 42)], [(0, 32)] ]: self.failUnlessEqual(list(sv + sparse(x)), self.dictAdd(self.example, x)) self.failUnlessEqual(list(sparse(x) + sv), self.dictAdd(self.example, x))
def testBool(self): self.assertEqual(bool(sparse([])), False) self.assertEqual(bool(sparse(self.example)), True)
def testEmpty(self): sv = sparse([]) self.assertEqual(sv.size, 0) for i in range(10): self.assertEqual(sv[i], 0)
def testThreshold(self): for t in [0.5, 1.0, 1.5, 2.0, 2.5]: sv = sparse(self.example).threshold(t) self.assertEqual(list(sv), sorted([x for x in self.example if x[1] >= t]))
def testCopyByCopy(self): from copy import copy sv1 = sparse(self.example) sv2 = copy(sv1) self.assertEqual(sv1, sv2)
def testCopyByConstructor(self): sv1 = sparse(self.example) sv2 = sparse(sv1) self.failUnlessEqual(sv1, sv2)
def testDiv(self): sv = sparse(self.example) sv3 = sv / 3.0 ref = sparse([(k, v / 3) for k, v in self.example]) self.assertEqual(sv3, ref)