import unittest
from util import eval_code

class test_opcodes_slices(unittest.TestCase):
    def test_slicing(self):
        self.failUnless(eval_code("a=[1,2,3];a[:]","[1, 2, 3]\n",title='test_opcodes_slices: 1'))
        self.failUnless(eval_code("a=[1,2,3,4,5];a[3:]","[4, 5]\n",title='test_opcodes_slices: 2'))
        self.failUnless(eval_code("a=[1,2,3,4,5];a[2:3]","[3]\n",title='test_opcodes_slices: 3'))
        self.failUnless(eval_code("a=[1,2,3,4,5];a[:3]","[1, 2, 3]\n",title='test_opcodes_slices: 4'))
        self.failUnless(eval_code("a=[1,2,3,4,5];a[3L:]","[4, 5]\n",title='test_opcodes_slices: 5'))
        self.failUnless(eval_code("a=[1,2,3,4,5];a[2L:3L]","[3]\n",title='test_opcodes_slices: 6'))
        self.failUnless(eval_code("a=[1,2,3,4,5];a[:3L]","[1, 2, 3]\n",title='test_opcodes_slices: 7'))

    def test_slice_assignment(self):
        self.failUnless(eval_code("a=[1,2,3,4,5,6];a[:]=[2,3];a","[2, 3]\n",title='test_opcodes_slices: 8'))
        self.failUnless(eval_code("a=[1,2,3,4,5,6];a[2:]=[2,3];a","[1, 2, 2, 3]\n",title='test_opcodes_slices: 9'))
        self.failUnless(eval_code("a=[1,2,3,4,5,6];a[:4]=[2,3];a","[2, 3, 5, 6]\n",title='test_opcodes_slices: 10'))
        self.failUnless(eval_code("a=[1,2,3,4,5,6];a[3:5]=[2,3];a","[1, 2, 3, 2, 3, 6]\n",title='test_opcodes_slices: 11'))

    def test_slice_delete(self):
        self.failUnless(eval_code("a=[1,2,3,4,5,6];del a[:];a","[]\n",title='test_opcodes_slices: 12'))
        self.failUnless(eval_code("a=[1,2,3,4,5,6];del a[2:];a","[1, 2]\n",title='test_opcodes_slices: 13'))
        self.failUnless(eval_code("a=[1,2,3,4,5,6];del a[:4];a","[5, 6]\n",title='test_opcodes_slices: 14'))
        self.failUnless(eval_code("z=[1,2,3,4,5,6];del z[3:4];z","[1, 2, 3, 5, 6]\n",title='test_opcodes_slices: 14.5'))

    def test_delete_subscr(self):
        self.failUnless(eval_code("a=[1,2,3,4,5,6];del a[3];a","[1, 2, 3, 5, 6]\n",title='test_opcodes_slices: 15'))

    def test_slice_build(self):
        self.failUnless(eval_code("x=[1,2,3,4];x[::]","[1, 2, 3, 4]\n",title='test_opcodes_slices: 16'))
        
if __name__ == "__main__":
    unittest.main()

