<commit_before>#!/usr/bin/env python
# -*- coding: utf-8 -*-

'''
testdict.py
(c) <NAME>  25 February, 2015

Unit tests for the redblack.rbdict class.
'''

import random
import unittest
from .. import redblack

class TestDict(unittest.TestCase):

    @unittest.expectedFailure
    def test_ctor_none(self):
        # dict(None) raises TypeError
        # redblack.rbdict does not
        self.assertRaises(TypeError, redblack.rbdict, None)

    def test_ctor1(self):
        # dict(2) raises TypeError
        self.assertRaises(TypeError, redblack.rbdict, 2)

    def test_ctor2(self):
        # dict([1,2,3]) raises TypeError
        self.assertRaises(TypeError, redblack.rbdict, [1,2,3],)

    def test_ctor3(self):
        # dict('abc') raises ValueError
        self.assertRaises(ValueError, redblack.rbdict, 'abc')

    def test_ctor4(self):
        # dict([(1,2), (2,3), (3,3)]) raises no error
        d = redblack.rbdict([(1,2), (2,3), (3,3)])
        self.assertEqual(len(d), 3)
        self.assertEqual(sorted(d.keys()), [1, 2, 3])
        self.assertEqual(sorted(d.values()), [2, 3, 3])
        self.assertEqual(sorted(d.items()), [(1, 2), (2, 3), (3, 3)])

    def test_ctor5(self):
        # dict([(1,2,1), (2,3,2), (3,3,2)]) raises ValueError
        self.assertRaises(ValueError, redblack.rbdict,
                          [(1,2,1), (2,3,2), (3,3,2)])

    def test_ctor6(self):
        # dict(one=3,two=2,three=1) raises no error
        d = redblack.rbdict(one=3,two=2,three=1)
        self.assertEqual(len(d), 3)
        self.assertEqual(sorted(d.keys()), ['one', 'three', 'two'])
        self.assertEqual(sorted(d.values()), [1, 2, 3])
        self.assertEqual(sorted(d.items()),
                         [('one', 3), ('three', 1), ('two', 2)])

    def test_bool(self):
        d = redblack.rbdict()
        self.assertFalse(bool(d))
        d[2] = 'something'
        self.assertTrue(bool(d))

    def test_len(self):
        d = redblack.rbdict()
        self.assertEqual(len(d), 0)
        d[2] = 'something'
        self.assertEqual(len(d), 1)
        del d[2]
        self.assertEqual(len(d), 0)

    def test_missingkey(self):
        d = redblack.rbdict()
        self.assertRaises(KeyError, d.__getitem__, 2,)
        self.assertRaises(KeyError, d.__delitem__, 2,)

    def test_set(self):
        for _try in range(15):
            d1 = dict()
            d2 = redblack.rbdict()
            for _iter in range(5000):
                num = random.randint(0, 1000)
                key = 'big string key value {}'.format(num)
                val = 'big string value value {}'.format(num)
                d1[key] = val
                d2[key] = val
            self.assertEqual(len(d1), len(d2))
            self.assertEqual(sorted(d1.keys()), sorted(d2.keys()))
            self.assertEqual(sorted(d1.values()), sorted(d2.values()))
            self.assertEqual(sorted(d1.items()), sorted(d2.items()))

    def test_ops(self):
        for _try in range(15):
            d1 = dict()
            d2 = redblack.rbdict()
            for _iter in range(5000):
                num = random.randint(0, 1000)
                key = 'big string key value {}'.format(num)
                val = 'big string value value {}'.format(num)
                op = random.randint(0,1)
                if op == 0:
                    d1[key] = val
                    d2[key] = val
                elif op == 1:
                    d1.pop(key, None)
                    d2.pop(key, None)
            self.assertEqual(len(d1), len(d2))
            self.assertEqual(sorted(d1.keys()), sorted(d2.keys()))
            self.assertEqual(sorted(d1.values()), sorted(d2.values()))
            self.assertEqual(sorted(d1.items()), sorted(d2.items()))

    def test_popitem(self):
        d = redblack.rbdict()
        for _iter in range(5000):
            num = random.randint(0, 1000)
            key = 'big string key value {}'.format(num)
            val = 'big string value value {}'.format(num)
            d[key] = val
        items = list(d.items())
        items2 = []
        while d:
            items2.append(d.popitem())
        self.assertEqual(items, items2)

    def test_hashable(self):
        d = redblack.rbdict()
        # dict[[1,2]] raises TypeError
        self.assertRaises(TypeError, d.__setitem__, [1,2], 'stringval')<commit_msg>testdict: test case to reproduce segfault<commit_after>#!/usr/bin/env python
# -*- coding: utf-8 -*-

'''
testdict.py
(c) <NAME>  25 February, 2015

Unit tests for the redblack.rbdict class.
'''

import random
import unittest
from .. import redblack

class TestDict(unittest.TestCase):

    @unittest.expectedFailure
    def test_ctor_none(self):
        # dict(None) raises TypeError
        # redblack.rbdict does not
        self.assertRaises(TypeError, redblack.rbdict, None)

    def test_ctor1(self):
        # dict(2) raises TypeError
        self.assertRaises(TypeError, redblack.rbdict, 2)

    def test_ctor2(self):
        # dict([1,2,3]) raises TypeError
        self.assertRaises(TypeError, redblack.rbdict, [1,2,3],)

    def test_ctor3(self):
        # dict('abc') raises ValueError
        self.assertRaises(ValueError, redblack.rbdict, 'abc')

    def test_ctor4(self):
        # dict([(1,2), (2,3), (3,3)]) raises no error
        d = redblack.rbdict([(1,2), (2,3), (3,3)])
        self.assertEqual(len(d), 3)
        self.assertEqual(sorted(d.keys()), [1, 2, 3])
        self.assertEqual(sorted(d.values()), [2, 3, 3])
        self.assertEqual(sorted(d.items()), [(1, 2), (2, 3), (3, 3)])

    def test_ctor5(self):
        # dict([(1,2,1), (2,3,2), (3,3,2)]) raises ValueError
        self.assertRaises(ValueError, redblack.rbdict,
                          [(1,2,1), (2,3,2), (3,3,2)])

    def test_ctor6(self):
        # dict(one=3,two=2,three=1) raises no error
        d = redblack.rbdict(one=3,two=2,three=1)
        self.assertEqual(len(d), 3)
        self.assertEqual(sorted(d.keys()), ['one', 'three', 'two'])
        self.assertEqual(sorted(d.values()), [1, 2, 3])
        self.assertEqual(sorted(d.items()),
                         [('one', 3), ('three', 1), ('two', 2)])

    def test_bool(self):
        d = redblack.rbdict()
        self.assertFalse(bool(d))
        d[2] = 'something'
        self.assertTrue(bool(d))

    def test_len(self):
        d = redblack.rbdict()
        self.assertEqual(len(d), 0)
        d[2] = 'something'
        self.assertEqual(len(d), 1)
        del d[2]
        self.assertEqual(len(d), 0)

    def test_missingkey(self):
        d = redblack.rbdict()
        self.assertRaises(KeyError, d.__getitem__, 2,)
        self.assertRaises(KeyError, d.__delitem__, 2,)

    def test_set(self):
        for _try in range(15):
            d1 = dict()
            d2 = redblack.rbdict()
            for _iter in range(5000):
                num = random.randint(0, 1000)
                key = 'big string key value {}'.format(num)
                val = 'big string value value {}'.format(num)
                d1[key] = val
                d2[key] = val
            self.assertEqual(len(d1), len(d2))
            self.assertEqual(sorted(d1.keys()), sorted(d2.keys()))
            self.assertEqual(sorted(d1.values()), sorted(d2.values()))
            self.assertEqual(sorted(d1.items()), sorted(d2.items()))

    def test_ops(self):
        for _try in range(15):
            d1 = dict()
            d2 = redblack.rbdict()
            for _iter in range(5000):
                num = random.randint(0, 1000)
                key = 'big string key value {}'.format(num)
                val = 'big string value value {}'.format(num)
                op = random.randint(0,1)
                if op == 0:
                    d1[key] = val
                    d2[key] = val
                elif op == 1:
                    d1.pop(key, None)
                    d2.pop(key, None)
            self.assertEqual(len(d1), len(d2))
            self.assertEqual(sorted(d1.keys()), sorted(d2.keys()))
            self.assertEqual(sorted(d1.values()), sorted(d2.values()))
            self.assertEqual(sorted(d1.items()), sorted(d2.items()))

    def test_popitem(self):
        d = redblack.rbdict()
        for _iter in range(5000):
            num = random.randint(0, 1000)
            key = 'big string key value {}'.format(num)
            val = 'big string value value {}'.format(num)
            d[key] = val
        items = list(d.items())
        items2 = []
        while d:
            items2.append(d.popitem())
        self.assertEqual(items, items2)

    def test_hashable(self):
        d = redblack.rbdict()
        # dict[[1,2]] raises TypeError
        self.assertRaises(TypeError, d.__setitem__, [1,2], 'stringval')

    def test_popitem(self):
        d = redblack.rbdict(Germany = 'Berlin',
                            Hungary = 'Budapest',
                            Ireland = 'Dublin',
                            Portugal = 'Lisbon',
                            Cyprus = 'Nicosia',
                            Greenland = 'Nuuk',
                            Iceland = 'Reykjavik',
                            Macedonia = 'Skopje',
                            Bulgaria = 'Sofia',
                            Sweden = 'Stockholm')
        for _try in range(5):
            self.assertEqual(d['Ireland'], 'Dublin')
        (k, v) = d.popitem()
        self.assertEqual(k, 'Bulgaria')
        self.assertEqual(v, 'Sofia')
{'fraction_non_alphanumeric': 0.15271301398897838, 'fraction_numerical': 0.032534972445951674, 'mean_word_length': 1.9234820322180917, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5421985', 'n_tokens_mistral': 3202, 'n_tokens_neox': 3028, 'n_words': 634}