Пример #1
0
 def test_erase(self):
     """make sure we can erase"""
     x = PySparseMatInt(2,3)
     x.insert(1,2,10)
     self.assertEqual(x.get(1,2), 10)
     x.erase(1,2)
     self.assertEqual(x.get(1,2), 0)
     self.assertEqual(x.contains(1,2), 0)
Пример #2
0
class PySparseMatIntTests(TestCase):
    def setUp(self):
        self.obj = PySparseMatInt(20,30)

    def test_get(self):
        """make sure we can get shibby"""
        self.obj.insert(10,20,30)
        self.assertEqual(self.obj.get(10,20), 30) # expect cast
        self.assertTrue(type(self.obj.get(10,20)), type(30))
        self.assertEqual(self.obj.get(10,19), 0)
        self.assertRaises(KeyError, self.obj.get, -1, 10)
        self.assertRaises(KeyError, self.obj.get, 1, -10)

    def test_insert(self):
        """make sure we can insert"""
        self.obj.insert(10,20,30)
        self.assertEqual(self.obj.get(10,20), 30) # expect cast
        self.assertTrue(type(self.obj.get(10,20)), type(30))
        self.obj.insert(10,20,-10)
        self.assertEqual(self.obj.get(10,20), -10)
        self.assertRaises(KeyError, self.obj.insert, -1, 10, 2)
        self.assertRaises(KeyError, self.obj.insert, 1, -10, 3)

    def test_contains(self):
        """Make sure we can check if things are present"""
        x = PySparseMatInt(2,3)
        self.assertEqual(x.contains(1,2), 0)
        x.insert(1,2,20)
        self.assertEqual(x.contains(1,2), 1)
        x.insert(1,2,10)
        self.assertEqual(x.contains(1,2), 1)
        x.erase(1,2)
        self.assertEqual(x.contains(1,2), 0)
        x.insert(1,2,0.0)
        self.assertEqual(x.contains(1,2), 0)
        
    def test_length(self):
        """make sure we can test length"""
        x = PySparseMatInt(3,4)
        self.assertEqual(x.length(), 0)
        x.insert(1,2,10)
        self.assertEqual(x.length(), 1)
        x.insert(2,3,4)
        x.insert(2,3,4)
        x.insert(2,3,4)
        self.assertEqual(x.length(), 2)
        x.erase(2,3)
        self.assertEqual(x.length(), 1)
        
    def test_erase(self):
        """make sure we can erase"""
        x = PySparseMatInt(2,3)
        x.insert(1,2,10)
        self.assertEqual(x.get(1,2), 10)
        x.erase(1,2)
        self.assertEqual(x.get(1,2), 0)
        self.assertEqual(x.contains(1,2), 0)
    
    def test_keys(self):
        """make sure we can get keys"""
        x = PySparseMatInt(3,4)
        self.assertEqual(x.keys(), [])
        x.insert(1,2,10)
        x.insert(2,3,4)
        x.insert(2,3,4)
        x.insert(2,3,4)
        self.assertEqual(sorted(x.keys()), [(1,2),(2,3)])
        x.erase(2,3)
        self.assertEqual(x.keys(), [(1,2)])
            
    def test_items(self):
        """make sure we can get items"""
        x = PySparseMatInt(3,4)
        self.assertEqual(x.items(), [])
        x.insert(1,2,10)
        x.insert(2,3,4)
        x.insert(2,3,4)
        x.insert(2,3,4)
        self.assertEqual(sorted(x.items()), [((1,2),10),((2,3),4)])
        x.erase(2,3)
        self.assertEqual(x.items(), [((1,2),10)])