#####################################################################################
#
#  Copyright (c) Microsoft Corporation. All rights reserved.
#
#  This source code is subject to terms and conditions of the Shared Source License
#  for IronPython. A copy of the license can be found in the License.html file
#  at the root of this distribution. If you can not locate the Shared Source License
#  for IronPython, please send an email to ironpy@microsoft.com.
#  By using this source code in any fashion, you are agreeing to be bound by
#  the terms of the Shared Source License for IronPython.
#
#  You must not remove this notice, or any other, from this software.
#
######################################################################################

from common import *
import testdata

import sys

class test_builtin: 
    ''' test built-in type, etc '''
    
    def test_slice(self):
        ''' currently mainly test 
                del list[slice]
        '''
        test_str = testdata.long_string
        str_len  = len(test_str)
        
        choices = ['', 0]
        numbers = [1, 2, 3, str_len/2-1, str_len/2, str_len/2+1, str_len-3, str_len-2, str_len-1, str_len, str_len+1, str_len+2, str_len+3, str_len*2]
        numbers = numbers[::3] # Temporary approach to speed things up...
        choices.extend(numbers)
        choices.extend([-1 * x for x in numbers])
        
        for x in choices:
            for y in choices:
                for z in choices:
                    if z == 0:  continue

                    line = "l = list(test_str); del l[%s:%s:%s]" % (str(x), str(y), str(z))
                    exec line                
                    printwith("case", "del l[%s:%s:%s]" % (str(x), str(y), str(z)))
                    printwith("same", eval("l"), eval("len(l)"))

    def test_xrange(self):
        ''' test xrange with corner cases'''
        import sys
        maxint = sys.maxint
        numbers = [1, 2, maxint/2, maxint-1, maxint, maxint+1, maxint+2]
        choices = [0]
        choices.extend(numbers)
        choices.extend([-1 * x for x in numbers])
        
        for x in choices:
            for y in choices:
                for z in choices:
                    line = "xrange(%s, %s, %s)" % (str(x), str(y), str(z))
                    printwith("case", line)
                    try: 
                        xr = eval(line)
                        xl = len(xr)
                        cnt = 0
                        first = last = first2 = last2 = "n/a"
                        # testing XRangeIterator
                        if xl < 10:
                            for x in xr: 
                                if cnt == 0: first = x
                                if cnt == xl -1 : last = x
                                cnt += 1
                        # testing this[index]
                        if xl == 0: first2 = xr[0]
                        if xl > 1 : first2, last2 = xr[0], xr[xl - 1]
                        
                        printwith("same", xr, xl, first, last, first2, last2)
                    except: 
                        printwith("same", sys.exc_type)


runtests(test_builtin)