Best Python code snippet using autotest_python
test_array.py
Source:test_array.py  
...49    # smallerexample: the same length as example, but smaller50    # biggerexample: the same length as example, but bigger51    # outside: An entry that is not in example52    # minitemsize: the minimum guaranteed itemsize53    def assertEntryEqual(self, entry1, entry2):54        self.assertEqual(entry1, entry2)55    def badtypecode(self):56        # Return a typecode that is different from our own57        return typecodes[(typecodes.index(self.typecode)+1) % len(typecodes)]58    def test_constructor(self):59        a = array.array(self.typecode)60        self.assertEqual(a.typecode, self.typecode)61        self.assertGreaterEqual(a.itemsize, self.minitemsize)62        self.assertRaises(TypeError, array.array, self.typecode, None)63    def test_len(self):64        a = array.array(self.typecode)65        a.append(self.example[0])66        self.assertEqual(len(a), 1)67        a = array.array(self.typecode, self.example)68        self.assertEqual(len(a), len(self.example))69    def test_buffer_info(self):70        a = array.array(self.typecode, self.example)71        self.assertRaises(TypeError, a.buffer_info, 42)72        bi = a.buffer_info()73        self.assertIsInstance(bi, tuple)74        self.assertEqual(len(bi), 2)75        self.assertIsInstance(bi[0], (int, long))76        self.assertIsInstance(bi[1], int)77        self.assertEqual(bi[1], len(a))78    def test_byteswap(self):79        a = array.array(self.typecode, self.example)80        self.assertRaises(TypeError, a.byteswap, 42)81        if a.itemsize in (1, 2, 4, 8):82            b = array.array(self.typecode, self.example)83            b.byteswap()84            if a.itemsize==1:85                self.assertEqual(a, b)86            else:87                self.assertNotEqual(a, b)88            b.byteswap()89            self.assertEqual(a, b)90    def test_copy(self):91        import copy92        a = array.array(self.typecode, self.example)93        b = copy.copy(a)94        self.assertNotEqual(id(a), id(b))95        self.assertEqual(a, b)96    def test_deepcopy(self):97        import copy98        a = array.array(self.typecode, self.example)99        b = copy.deepcopy(a)100        self.assertNotEqual(id(a), id(b))101        self.assertEqual(a, b)102    def test_pickle(self):103        for protocol in range(HIGHEST_PROTOCOL + 1):104            a = array.array(self.typecode, self.example)105            b = loads(dumps(a, protocol))106            self.assertNotEqual(id(a), id(b))107            self.assertEqual(a, b)108            a = ArraySubclass(self.typecode, self.example)109            a.x = 10110            b = loads(dumps(a, protocol))111            self.assertNotEqual(id(a), id(b))112            self.assertEqual(a, b)113            self.assertEqual(a.x, b.x)114            self.assertEqual(type(a), type(b))115    def test_pickle_for_empty_array(self):116        for protocol in range(HIGHEST_PROTOCOL + 1):117            a = array.array(self.typecode)118            b = loads(dumps(a, protocol))119            self.assertNotEqual(id(a), id(b))120            self.assertEqual(a, b)121            a = ArraySubclass(self.typecode)122            a.x = 10123            b = loads(dumps(a, protocol))124            self.assertNotEqual(id(a), id(b))125            self.assertEqual(a, b)126            self.assertEqual(a.x, b.x)127            self.assertEqual(type(a), type(b))128    def test_insert(self):129        a = array.array(self.typecode, self.example)130        a.insert(0, self.example[0])131        self.assertEqual(len(a), 1+len(self.example))132        self.assertEqual(a[0], a[1])133        self.assertRaises(TypeError, a.insert)134        self.assertRaises(TypeError, a.insert, None)135        self.assertRaises(TypeError, a.insert, 0, None)136        a = array.array(self.typecode, self.example)137        a.insert(-1, self.example[0])138        self.assertEqual(139            a,140            array.array(141                self.typecode,142                self.example[:-1] + self.example[:1] + self.example[-1:]143            )144        )145        a = array.array(self.typecode, self.example)146        a.insert(-1000, self.example[0])147        self.assertEqual(148            a,149            array.array(self.typecode, self.example[:1] + self.example)150        )151        a = array.array(self.typecode, self.example)152        a.insert(1000, self.example[0])153        self.assertEqual(154            a,155            array.array(self.typecode, self.example + self.example[:1])156        )157    def test_tofromfile(self):158        a = array.array(self.typecode, 2*self.example)159        self.assertRaises(TypeError, a.tofile)160        self.assertRaises(TypeError, a.tofile, cStringIO.StringIO())161        test_support.unlink(test_support.TESTFN)162        f = open(test_support.TESTFN, 'wb')163        try:164            a.tofile(f)165            f.close()166            b = array.array(self.typecode)167            f = open(test_support.TESTFN, 'rb')168            self.assertRaises(TypeError, b.fromfile)169            self.assertRaises(170                TypeError,171                b.fromfile,172                cStringIO.StringIO(), len(self.example)173            )174            b.fromfile(f, len(self.example))175            self.assertEqual(b, array.array(self.typecode, self.example))176            self.assertNotEqual(a, b)177            b.fromfile(f, len(self.example))178            self.assertEqual(a, b)179            self.assertRaises(EOFError, b.fromfile, f, 1)180            f.close()181        finally:182            if not f.closed:183                f.close()184            test_support.unlink(test_support.TESTFN)185    def test_fromfile_ioerror(self):186        # Issue #5395: Check if fromfile raises a proper IOError187        # instead of EOFError.188        a = array.array(self.typecode)189        f = open(test_support.TESTFN, 'wb')190        try:191            self.assertRaises(IOError, a.fromfile, f, len(self.example))192        finally:193            f.close()194            test_support.unlink(test_support.TESTFN)195    def test_filewrite(self):196        a = array.array(self.typecode, 2*self.example)197        f = open(test_support.TESTFN, 'wb')198        try:199            f.write(a)200            f.close()201            b = array.array(self.typecode)202            f = open(test_support.TESTFN, 'rb')203            b.fromfile(f, len(self.example))204            self.assertEqual(b, array.array(self.typecode, self.example))205            self.assertNotEqual(a, b)206            b.fromfile(f, len(self.example))207            self.assertEqual(a, b)208            f.close()209        finally:210            if not f.closed:211                f.close()212            test_support.unlink(test_support.TESTFN)213    def test_tofromlist(self):214        a = array.array(self.typecode, 2*self.example)215        b = array.array(self.typecode)216        self.assertRaises(TypeError, a.tolist, 42)217        self.assertRaises(TypeError, b.fromlist)218        self.assertRaises(TypeError, b.fromlist, 42)219        self.assertRaises(TypeError, b.fromlist, [None])220        b.fromlist(a.tolist())221        self.assertEqual(a, b)222    def test_tofromstring(self):223        a = array.array(self.typecode, 2*self.example)224        b = array.array(self.typecode)225        self.assertRaises(TypeError, a.tostring, 42)226        self.assertRaises(TypeError, b.fromstring)227        self.assertRaises(TypeError, b.fromstring, 42)228        self.assertRaises(ValueError, a.fromstring, a)229        b.fromstring(a.tostring())230        self.assertEqual(a, b)231        if a.itemsize>1:232            self.assertRaises(ValueError, b.fromstring, "x")233    def test_repr(self):234        a = array.array(self.typecode, 2*self.example)235        self.assertEqual(a, eval(repr(a), {"array": array.array}))236        a = array.array(self.typecode)237        self.assertEqual(repr(a), "array('%s')" % self.typecode)238    def test_str(self):239        a = array.array(self.typecode, 2*self.example)240        str(a)241    def test_cmp(self):242        a = array.array(self.typecode, self.example)243        self.assertIs(a == 42, False)244        self.assertIs(a != 42, True)245        self.assertIs(a == a, True)246        self.assertIs(a != a, False)247        self.assertIs(a < a, False)248        self.assertIs(a <= a, True)249        self.assertIs(a > a, False)250        self.assertIs(a >= a, True)251        al = array.array(self.typecode, self.smallerexample)252        ab = array.array(self.typecode, self.biggerexample)253        self.assertIs(a == 2*a, False)254        self.assertIs(a != 2*a, True)255        self.assertIs(a < 2*a, True)256        self.assertIs(a <= 2*a, True)257        self.assertIs(a > 2*a, False)258        self.assertIs(a >= 2*a, False)259        self.assertIs(a == al, False)260        self.assertIs(a != al, True)261        self.assertIs(a < al, False)262        self.assertIs(a <= al, False)263        self.assertIs(a > al, True)264        self.assertIs(a >= al, True)265        self.assertIs(a == ab, False)266        self.assertIs(a != ab, True)267        self.assertIs(a < ab, True)268        self.assertIs(a <= ab, True)269        self.assertIs(a > ab, False)270        self.assertIs(a >= ab, False)271    def test_add(self):272        a = array.array(self.typecode, self.example) \273            + array.array(self.typecode, self.example[::-1])274        self.assertEqual(275            a,276            array.array(self.typecode, self.example + self.example[::-1])277        )278        b = array.array(self.badtypecode())279        self.assertRaises(TypeError, a.__add__, b)280        self.assertRaises(TypeError, a.__add__, "bad")281    def test_iadd(self):282        a = array.array(self.typecode, self.example[::-1])283        b = a284        a += array.array(self.typecode, 2*self.example)285        self.assertIs(a, b)286        self.assertEqual(287            a,288            array.array(self.typecode, self.example[::-1]+2*self.example)289        )290        a = array.array(self.typecode, self.example)291        a += a292        self.assertEqual(293            a,294            array.array(self.typecode, self.example + self.example)295        )296        b = array.array(self.badtypecode())297        self.assertRaises(TypeError, a.__add__, b)298        self.assertRaises(TypeError, a.__iadd__, "bad")299    def test_mul(self):300        a = 5*array.array(self.typecode, self.example)301        self.assertEqual(302            a,303            array.array(self.typecode, 5*self.example)304        )305        a = array.array(self.typecode, self.example)*5306        self.assertEqual(307            a,308            array.array(self.typecode, self.example*5)309        )310        a = 0*array.array(self.typecode, self.example)311        self.assertEqual(312            a,313            array.array(self.typecode)314        )315        a = (-1)*array.array(self.typecode, self.example)316        self.assertEqual(317            a,318            array.array(self.typecode)319        )320        self.assertRaises(TypeError, a.__mul__, "bad")321    def test_imul(self):322        a = array.array(self.typecode, self.example)323        b = a324        a *= 5325        self.assertIs(a, b)326        self.assertEqual(327            a,328            array.array(self.typecode, 5*self.example)329        )330        a *= 0331        self.assertIs(a, b)332        self.assertEqual(a, array.array(self.typecode))333        a *= 1000334        self.assertIs(a, b)335        self.assertEqual(a, array.array(self.typecode))336        a *= -1337        self.assertIs(a, b)338        self.assertEqual(a, array.array(self.typecode))339        a = array.array(self.typecode, self.example)340        a *= -1341        self.assertEqual(a, array.array(self.typecode))342        self.assertRaises(TypeError, a.__imul__, "bad")343    def test_getitem(self):344        a = array.array(self.typecode, self.example)345        self.assertEntryEqual(a[0], self.example[0])346        self.assertEntryEqual(a[0L], self.example[0])347        self.assertEntryEqual(a[-1], self.example[-1])348        self.assertEntryEqual(a[-1L], self.example[-1])349        self.assertEntryEqual(a[len(self.example)-1], self.example[-1])350        self.assertEntryEqual(a[-len(self.example)], self.example[0])351        self.assertRaises(TypeError, a.__getitem__)352        self.assertRaises(IndexError, a.__getitem__, len(self.example))353        self.assertRaises(IndexError, a.__getitem__, -len(self.example)-1)354    def test_setitem(self):355        a = array.array(self.typecode, self.example)356        a[0] = a[-1]357        self.assertEntryEqual(a[0], a[-1])358        a = array.array(self.typecode, self.example)359        a[0L] = a[-1]360        self.assertEntryEqual(a[0], a[-1])361        a = array.array(self.typecode, self.example)362        a[-1] = a[0]363        self.assertEntryEqual(a[0], a[-1])364        a = array.array(self.typecode, self.example)365        a[-1L] = a[0]366        self.assertEntryEqual(a[0], a[-1])367        a = array.array(self.typecode, self.example)368        a[len(self.example)-1] = a[0]369        self.assertEntryEqual(a[0], a[-1])370        a = array.array(self.typecode, self.example)371        a[-len(self.example)] = a[-1]372        self.assertEntryEqual(a[0], a[-1])373        self.assertRaises(TypeError, a.__setitem__)374        self.assertRaises(TypeError, a.__setitem__, None)375        self.assertRaises(TypeError, a.__setitem__, 0, None)376        self.assertRaises(377            IndexError,378            a.__setitem__,379            len(self.example), self.example[0]380        )381        self.assertRaises(382            IndexError,383            a.__setitem__,384            -len(self.example)-1, self.example[0]385        )386    def test_delitem(self):387        a = array.array(self.typecode, self.example)388        del a[0]389        self.assertEqual(390            a,391            array.array(self.typecode, self.example[1:])392        )393        a = array.array(self.typecode, self.example)394        del a[-1]395        self.assertEqual(396            a,397            array.array(self.typecode, self.example[:-1])398        )399        a = array.array(self.typecode, self.example)400        del a[len(self.example)-1]401        self.assertEqual(402            a,403            array.array(self.typecode, self.example[:-1])404        )405        a = array.array(self.typecode, self.example)406        del a[-len(self.example)]407        self.assertEqual(408            a,409            array.array(self.typecode, self.example[1:])410        )411        self.assertRaises(TypeError, a.__delitem__)412        self.assertRaises(TypeError, a.__delitem__, None)413        self.assertRaises(IndexError, a.__delitem__, len(self.example))414        self.assertRaises(IndexError, a.__delitem__, -len(self.example)-1)415    def test_getslice(self):416        a = array.array(self.typecode, self.example)417        self.assertEqual(a[:], a)418        self.assertEqual(419            a[1:],420            array.array(self.typecode, self.example[1:])421        )422        self.assertEqual(423            a[:1],424            array.array(self.typecode, self.example[:1])425        )426        self.assertEqual(427            a[:-1],428            array.array(self.typecode, self.example[:-1])429        )430        self.assertEqual(431            a[-1:],432            array.array(self.typecode, self.example[-1:])433        )434        self.assertEqual(435            a[-1:-1],436            array.array(self.typecode)437        )438        self.assertEqual(439            a[2:1],440            array.array(self.typecode)441        )442        self.assertEqual(443            a[1000:],444            array.array(self.typecode)445        )446        self.assertEqual(a[-1000:], a)447        self.assertEqual(a[:1000], a)448        self.assertEqual(449            a[:-1000],450            array.array(self.typecode)451        )452        self.assertEqual(a[-1000:1000], a)453        self.assertEqual(454            a[2000:1000],455            array.array(self.typecode)456        )457    def test_extended_getslice(self):458        # Test extended slicing by comparing with list slicing459        # (Assumes list conversion works correctly, too)460        a = array.array(self.typecode, self.example)461        indices = (0, None, 1, 3, 19, 100, -1, -2, -31, -100)462        for start in indices:463            for stop in indices:464                # Everything except the initial 0 (invalid step)465                for step in indices[1:]:466                    self.assertEqual(list(a[start:stop:step]),467                                     list(a)[start:stop:step])468    def test_setslice(self):469        a = array.array(self.typecode, self.example)470        a[:1] = a471        self.assertEqual(472            a,473            array.array(self.typecode, self.example + self.example[1:])474        )475        a = array.array(self.typecode, self.example)476        a[:-1] = a477        self.assertEqual(478            a,479            array.array(self.typecode, self.example + self.example[-1:])480        )481        a = array.array(self.typecode, self.example)482        a[-1:] = a483        self.assertEqual(484            a,485            array.array(self.typecode, self.example[:-1] + self.example)486        )487        a = array.array(self.typecode, self.example)488        a[1:] = a489        self.assertEqual(490            a,491            array.array(self.typecode, self.example[:1] + self.example)492        )493        a = array.array(self.typecode, self.example)494        a[1:-1] = a495        self.assertEqual(496            a,497            array.array(498                self.typecode,499                self.example[:1] + self.example + self.example[-1:]500            )501        )502        a = array.array(self.typecode, self.example)503        a[1000:] = a504        self.assertEqual(505            a,506            array.array(self.typecode, 2*self.example)507        )508        a = array.array(self.typecode, self.example)509        a[-1000:] = a510        self.assertEqual(511            a,512            array.array(self.typecode, self.example)513        )514        a = array.array(self.typecode, self.example)515        a[:1000] = a516        self.assertEqual(517            a,518            array.array(self.typecode, self.example)519        )520        a = array.array(self.typecode, self.example)521        a[:-1000] = a522        self.assertEqual(523            a,524            array.array(self.typecode, 2*self.example)525        )526        a = array.array(self.typecode, self.example)527        a[1:0] = a528        self.assertEqual(529            a,530            array.array(self.typecode, self.example[:1] + self.example + self.example[1:])531        )532        a = array.array(self.typecode, self.example)533        a[2000:1000] = a534        self.assertEqual(535            a,536            array.array(self.typecode, 2*self.example)537        )538        a = array.array(self.typecode, self.example)539        self.assertRaises(TypeError, a.__setslice__, 0, 0, None)540        self.assertRaises(TypeError, a.__setitem__, slice(0, 0), None)541        self.assertRaises(TypeError, a.__setitem__, slice(0, 1), None)542        b = array.array(self.badtypecode())543        self.assertRaises(TypeError, a.__setslice__, 0, 0, b)544        self.assertRaises(TypeError, a.__setitem__, slice(0, 0), b)545        self.assertRaises(TypeError, a.__setitem__, slice(0, 1), b)546    def test_extended_set_del_slice(self):547        indices = (0, None, 1, 3, 19, 100, -1, -2, -31, -100)548        for start in indices:549            for stop in indices:550                # Everything except the initial 0 (invalid step)551                for step in indices[1:]:552                    a = array.array(self.typecode, self.example)553                    L = list(a)554                    # Make sure we have a slice of exactly the right length,555                    # but with (hopefully) different data.556                    data = L[start:stop:step]557                    data.reverse()558                    L[start:stop:step] = data559                    a[start:stop:step] = array.array(self.typecode, data)560                    self.assertEqual(a, array.array(self.typecode, L))561                    del L[start:stop:step]562                    del a[start:stop:step]563                    self.assertEqual(a, array.array(self.typecode, L))564    def test_index(self):565        example = 2*self.example566        a = array.array(self.typecode, example)567        self.assertRaises(TypeError, a.index)568        for x in example:569            self.assertEqual(a.index(x), example.index(x))570        self.assertRaises(ValueError, a.index, None)571        self.assertRaises(ValueError, a.index, self.outside)572    def test_count(self):573        example = 2*self.example574        a = array.array(self.typecode, example)575        self.assertRaises(TypeError, a.count)576        for x in example:577            self.assertEqual(a.count(x), example.count(x))578        self.assertEqual(a.count(self.outside), 0)579        self.assertEqual(a.count(None), 0)580    def test_remove(self):581        for x in self.example:582            example = 2*self.example583            a = array.array(self.typecode, example)584            pos = example.index(x)585            example2 = example[:pos] + example[pos+1:]586            a.remove(x)587            self.assertEqual(a, array.array(self.typecode, example2))588        a = array.array(self.typecode, self.example)589        self.assertRaises(ValueError, a.remove, self.outside)590        self.assertRaises(ValueError, a.remove, None)591    def test_pop(self):592        a = array.array(self.typecode)593        self.assertRaises(IndexError, a.pop)594        a = array.array(self.typecode, 2*self.example)595        self.assertRaises(TypeError, a.pop, 42, 42)596        self.assertRaises(TypeError, a.pop, None)597        self.assertRaises(IndexError, a.pop, len(a))598        self.assertRaises(IndexError, a.pop, -len(a)-1)599        self.assertEntryEqual(a.pop(0), self.example[0])600        self.assertEqual(601            a,602            array.array(self.typecode, self.example[1:]+self.example)603        )604        self.assertEntryEqual(a.pop(1), self.example[2])605        self.assertEqual(606            a,607            array.array(self.typecode, self.example[1:2]+self.example[3:]+self.example)608        )609        self.assertEntryEqual(a.pop(0), self.example[1])610        self.assertEntryEqual(a.pop(), self.example[-1])611        self.assertEqual(612            a,613            array.array(self.typecode, self.example[3:]+self.example[:-1])614        )615    def test_reverse(self):616        a = array.array(self.typecode, self.example)617        self.assertRaises(TypeError, a.reverse, 42)618        a.reverse()619        self.assertEqual(620            a,621            array.array(self.typecode, self.example[::-1])622        )623    def test_extend(self):624        a = array.array(self.typecode, self.example)625        self.assertRaises(TypeError, a.extend)626        a.extend(array.array(self.typecode, self.example[::-1]))627        self.assertEqual(628            a,629            array.array(self.typecode, self.example+self.example[::-1])630        )631        a = array.array(self.typecode, self.example)632        a.extend(a)633        self.assertEqual(634            a,635            array.array(self.typecode, self.example+self.example)636        )637        b = array.array(self.badtypecode())638        self.assertRaises(TypeError, a.extend, b)639        a = array.array(self.typecode, self.example)640        a.extend(self.example[::-1])641        self.assertEqual(642            a,643            array.array(self.typecode, self.example+self.example[::-1])644        )645    def test_constructor_with_iterable_argument(self):646        a = array.array(self.typecode, iter(self.example))647        b = array.array(self.typecode, self.example)648        self.assertEqual(a, b)649        # non-iterable argument650        self.assertRaises(TypeError, array.array, self.typecode, 10)651        # pass through errors raised in __iter__652        class A:653            def __iter__(self):654                raise UnicodeError655        self.assertRaises(UnicodeError, array.array, self.typecode, A())656        # pass through errors raised in next()657        def B():658            raise UnicodeError659            yield None660        self.assertRaises(UnicodeError, array.array, self.typecode, B())661    def test_coveritertraverse(self):662        try:663            import gc664        except ImportError:665            self.skipTest('gc module not available')666        a = array.array(self.typecode)667        l = [iter(a)]668        l.append(l)669        gc.collect()670    def test_buffer(self):671        a = array.array(self.typecode, self.example)672        with test_support.check_py3k_warnings():673            b = buffer(a)674        self.assertEqual(b[0], a.tostring()[0])675    def test_weakref(self):676        s = array.array(self.typecode, self.example)677        p = proxy(s)678        self.assertEqual(p.tostring(), s.tostring())679        s = None680        self.assertRaises(ReferenceError, len, p)681    @unittest.skipUnless(hasattr(sys, 'getrefcount'),682                         'test needs sys.getrefcount()')683    def test_bug_782369(self):684        for i in range(10):685            b = array.array('B', range(64))686        rc = sys.getrefcount(10)687        for i in range(10):688            b = array.array('B', range(64))689        self.assertEqual(rc, sys.getrefcount(10))690    def test_subclass_with_kwargs(self):691        # SF bug #1486663 -- this used to erroneously raise a TypeError692        with warnings.catch_warnings():693            warnings.filterwarnings("ignore", '', DeprecationWarning)694            ArraySubclassWithKwargs('b', newarg=1)695class StringTest(BaseTest):696    def test_setitem(self):697        super(StringTest, self).test_setitem()698        a = array.array(self.typecode, self.example)699        self.assertRaises(TypeError, a.__setitem__, 0, self.example[:2])700class CharacterTest(StringTest):701    typecode = 'c'702    example = '\x01azAZ\x00\xfe'703    smallerexample = '\x01azAY\x00\xfe'704    biggerexample = '\x01azAZ\x00\xff'705    outside = '\x33'706    minitemsize = 1707    def test_subbclassing(self):708        class EditableString(array.array):709            def __new__(cls, s, *args, **kwargs):710                return array.array.__new__(cls, 'c', s)711            def __init__(self, s, color='blue'):712                self.color = color713            def strip(self):714                self[:] = array.array('c', self.tostring().strip())715            def __repr__(self):716                return 'EditableString(%r)' % self.tostring()717        s = EditableString("\ttest\r\n")718        s.strip()719        self.assertEqual(s.tostring(), "test")720        self.assertEqual(s.color, "blue")721        s.color = "red"722        self.assertEqual(s.color, "red")723        self.assertEqual(s.__dict__.keys(), ["color"])724    @test_support.requires_unicode725    def test_nounicode(self):726        a = array.array(self.typecode, self.example)727        self.assertRaises(ValueError, a.fromunicode, unicode(''))728        self.assertRaises(ValueError, a.tounicode)729tests.append(CharacterTest)730if test_support.have_unicode:731    class UnicodeTest(StringTest):732        typecode = 'u'733        example = unicode(r'\x01\u263a\x00\ufeff', 'unicode-escape')734        smallerexample = unicode(r'\x01\u263a\x00\ufefe', 'unicode-escape')735        biggerexample = unicode(r'\x01\u263a\x01\ufeff', 'unicode-escape')736        outside = unicode('\x33')737        minitemsize = 2738        def test_unicode(self):739            self.assertRaises(TypeError, array.array, 'b', unicode('foo', 'ascii'))740            a = array.array('u', unicode(r'\xa0\xc2\u1234', 'unicode-escape'))741            a.fromunicode(unicode(' ', 'ascii'))742            a.fromunicode(unicode('', 'ascii'))743            a.fromunicode(unicode('', 'ascii'))744            a.fromunicode(unicode(r'\x11abc\xff\u1234', 'unicode-escape'))745            s = a.tounicode()746            self.assertEqual(747                s,748                unicode(r'\xa0\xc2\u1234 \x11abc\xff\u1234', 'unicode-escape')749            )750            s = unicode(r'\x00="\'a\\b\x80\xff\u0000\u0001\u1234', 'unicode-escape')751            a = array.array('u', s)752            self.assertEqual(753                repr(a),754                r"""array('u', u'\x00="\'a\\b\x80\xff\x00\x01\u1234')"""755            )756            self.assertRaises(TypeError, a.fromunicode)757    tests.append(UnicodeTest)758class NumberTest(BaseTest):759    def test_extslice(self):760        a = array.array(self.typecode, range(5))761        self.assertEqual(a[::], a)762        self.assertEqual(a[::2], array.array(self.typecode, [0,2,4]))763        self.assertEqual(a[1::2], array.array(self.typecode, [1,3]))764        self.assertEqual(a[::-1], array.array(self.typecode, [4,3,2,1,0]))765        self.assertEqual(a[::-2], array.array(self.typecode, [4,2,0]))766        self.assertEqual(a[3::-2], array.array(self.typecode, [3,1]))767        self.assertEqual(a[-100:100:], a)768        self.assertEqual(a[100:-100:-1], a[::-1])769        self.assertEqual(a[-100L:100L:2L], array.array(self.typecode, [0,2,4]))770        self.assertEqual(a[1000:2000:2], array.array(self.typecode, []))771        self.assertEqual(a[-1000:-2000:-2], array.array(self.typecode, []))772    def test_delslice(self):773        a = array.array(self.typecode, range(5))774        del a[::2]775        self.assertEqual(a, array.array(self.typecode, [1,3]))776        a = array.array(self.typecode, range(5))777        del a[1::2]778        self.assertEqual(a, array.array(self.typecode, [0,2,4]))779        a = array.array(self.typecode, range(5))780        del a[1::-2]781        self.assertEqual(a, array.array(self.typecode, [0,2,3,4]))782        a = array.array(self.typecode, range(10))783        del a[::1000]784        self.assertEqual(a, array.array(self.typecode, [1,2,3,4,5,6,7,8,9]))785        # test issue7788786        a = array.array(self.typecode, range(10))787        del a[9::1<<333]788    def test_assignment(self):789        a = array.array(self.typecode, range(10))790        a[::2] = array.array(self.typecode, [42]*5)791        self.assertEqual(a, array.array(self.typecode, [42, 1, 42, 3, 42, 5, 42, 7, 42, 9]))792        a = array.array(self.typecode, range(10))793        a[::-4] = array.array(self.typecode, [10]*3)794        self.assertEqual(a, array.array(self.typecode, [0, 10, 2, 3, 4, 10, 6, 7, 8 ,10]))795        a = array.array(self.typecode, range(4))796        a[::-1] = a797        self.assertEqual(a, array.array(self.typecode, [3, 2, 1, 0]))798        a = array.array(self.typecode, range(10))799        b = a[:]800        c = a[:]801        ins = array.array(self.typecode, range(2))802        a[2:3] = ins803        b[slice(2,3)] = ins804        c[2:3:] = ins805    def test_iterationcontains(self):806        a = array.array(self.typecode, range(10))807        self.assertEqual(list(a), range(10))808        b = array.array(self.typecode, [20])809        self.assertEqual(a[-1] in a, True)810        self.assertEqual(b[0] not in a, True)811    def check_overflow(self, lower, upper):812        # method to be used by subclasses813        # should not overflow assigning lower limit814        a = array.array(self.typecode, [lower])815        a[0] = lower816        # should overflow assigning less than lower limit817        self.assertRaises(OverflowError, array.array, self.typecode, [lower-1])818        self.assertRaises(OverflowError, a.__setitem__, 0, lower-1)819        # should not overflow assigning upper limit820        a = array.array(self.typecode, [upper])821        a[0] = upper822        # should overflow assigning more than upper limit823        self.assertRaises(OverflowError, array.array, self.typecode, [upper+1])824        self.assertRaises(OverflowError, a.__setitem__, 0, upper+1)825    def test_subclassing(self):826        typecode = self.typecode827        class ExaggeratingArray(array.array):828            __slots__ = ['offset']829            def __new__(cls, typecode, data, offset):830                return array.array.__new__(cls, typecode, data)831            def __init__(self, typecode, data, offset):832                self.offset = offset833            def __getitem__(self, i):834                return array.array.__getitem__(self, i) + self.offset835        a = ExaggeratingArray(self.typecode, [3, 6, 7, 11], 4)836        self.assertEntryEqual(a[0], 7)837        self.assertRaises(AttributeError, setattr, a, "color", "blue")838class SignedNumberTest(NumberTest):839    example = [-1, 0, 1, 42, 0x7f]840    smallerexample = [-1, 0, 1, 42, 0x7e]841    biggerexample = [-1, 0, 1, 43, 0x7f]842    outside = 23843    def test_overflow(self):844        a = array.array(self.typecode)845        lower = -1 * long(pow(2, a.itemsize * 8 - 1))846        upper = long(pow(2, a.itemsize * 8 - 1)) - 1L847        self.check_overflow(lower, upper)848class UnsignedNumberTest(NumberTest):849    example = [0, 1, 17, 23, 42, 0xff]850    smallerexample = [0, 1, 17, 23, 42, 0xfe]851    biggerexample = [0, 1, 17, 23, 43, 0xff]852    outside = 0xaa853    def test_overflow(self):854        a = array.array(self.typecode)855        lower = 0856        upper = long(pow(2, a.itemsize * 8)) - 1L857        self.check_overflow(lower, upper)858    @test_support.cpython_only859    def test_sizeof_with_buffer(self):860        a = array.array(self.typecode, self.example)861        basesize = test_support.calcvobjsize('4P')862        buffer_size = a.buffer_info()[1] * a.itemsize863        test_support.check_sizeof(self, a, basesize + buffer_size)864    @test_support.cpython_only865    def test_sizeof_without_buffer(self):866        a = array.array(self.typecode)867        basesize = test_support.calcvobjsize('4P')868        test_support.check_sizeof(self, a, basesize)869class ByteTest(SignedNumberTest):870    typecode = 'b'871    minitemsize = 1872tests.append(ByteTest)873class UnsignedByteTest(UnsignedNumberTest):874    typecode = 'B'875    minitemsize = 1876tests.append(UnsignedByteTest)877class ShortTest(SignedNumberTest):878    typecode = 'h'879    minitemsize = 2880tests.append(ShortTest)881class UnsignedShortTest(UnsignedNumberTest):882    typecode = 'H'883    minitemsize = 2884tests.append(UnsignedShortTest)885class IntTest(SignedNumberTest):886    typecode = 'i'887    minitemsize = 2888tests.append(IntTest)889class UnsignedIntTest(UnsignedNumberTest):890    typecode = 'I'891    minitemsize = 2892tests.append(UnsignedIntTest)893class LongTest(SignedNumberTest):894    typecode = 'l'895    minitemsize = 4896tests.append(LongTest)897class UnsignedLongTest(UnsignedNumberTest):898    typecode = 'L'899    minitemsize = 4900tests.append(UnsignedLongTest)901@test_support.requires_unicode902class UnicodeTypecodeTest(unittest.TestCase):903    def test_unicode_typecode(self):904        for typecode in typecodes:905            a = array.array(unicode(typecode))906            self.assertEqual(a.typecode, typecode)907            self.assertIs(type(a.typecode), str)908tests.append(UnicodeTypecodeTest)909class FPTest(NumberTest):910    example = [-42.0, 0, 42, 1e5, -1e10]911    smallerexample = [-42.0, 0, 42, 1e5, -2e10]912    biggerexample = [-42.0, 0, 42, 1e5, 1e10]913    outside = 23914    def assertEntryEqual(self, entry1, entry2):915        self.assertAlmostEqual(entry1, entry2)916    def test_byteswap(self):917        a = array.array(self.typecode, self.example)918        self.assertRaises(TypeError, a.byteswap, 42)919        if a.itemsize in (1, 2, 4, 8):920            b = array.array(self.typecode, self.example)921            b.byteswap()922            if a.itemsize==1:923                self.assertEqual(a, b)924            else:925                # On alphas treating the byte swapped bit patters as926                # floats/doubles results in floating point exceptions927                # => compare the 8bit string values instead928                self.assertNotEqual(a.tostring(), b.tostring())...Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!
