Esempio n. 1
0
def test_dict_delitem():
    '''Test dictionary-like operation: __delitem__'''

    all_good = True

    dict_ = red_black_dict_mod.RedBlackTree()

    for number in range(10):
        dict_[number] = 2**number

    for number in range(0, 10, 2):
        del dict_[number]

    if len(dict_) != 5:
        sys.stderr.write('%s: test_dict_delitem: len(dict_) is not 5\n' %
                         (sys.argv[0], ))
        all_good = False

    if dict_[1] != 2:
        sys.stderr.write(
            '%s: test_dict_delitem: 1th element of dict_ is not 2\n' %
            (sys.argv[0], ))
        all_good = False

    if dict_[9] != 512:
        sys.stderr.write(
            '%s: test_dict_delitem: 9th element of dict_ is not 512\n' %
            (sys.argv[0], ))
        all_good = False

    return all_good
Esempio n. 2
0
def test_dict_getitem():
    '''Test dictionary-like operation: __getitem__'''

    all_good = True

    dict_ = red_black_dict_mod.RedBlackTree()

    for number in range(10):
        dict_[number] = 2**number

    value = dict_[0]
    if value != 1:
        sys.stderr.write(
            '%s: test_dict_getitem: 0th element of dict_ is not 1, instead got %s\n'
            % (sys.argv[0], value))
        all_good = False

    value = dict_[2]
    if value != 4:
        sys.stderr.write(
            '%s: test_dict_getitem: 2nd element of dict_ is not 4, instead got %s\n'
            % (sys.argv[0], value))
        all_good = False

    value = dict_[9]
    if value != 512:
        sys.stderr.write(
            '%s: test_dict_getitem: 9th element of dict_ is not 512, instead got %s\n'
            % (sys.argv[0], value))
        all_good = False

    return all_good
Esempio n. 3
0
def test_dict_add():
    '''Test adding to a dictionary'''

    all_good = True

    dict_ = red_black_dict_mod.RedBlackTree()
    for integer in range(5):
        dict_.add(key=integer, value=2**integer)

    return all_good
Esempio n. 4
0
def test_successor():
    '''Test the successor property'''

    all_good = True

    # create a "dictionary" without duplicates
    dict_ = red_black_dict_mod.RedBlackTree()
    expected_keys = []
    expected_values = []
    for i in range(10):
        dict_[i] = 2**i
        expected_keys.append(i)
        expected_values.append(2**i)


#    for i in range(10):
#        dict_[i] = 2 ** i

    expected_keys.sort()
    expected_values.sort()

    keys_list = []
    values_list = []

    node = dict_.find(0)
    while node is not None:
        keys_list.append(node.key)
        values_list.append(node.value)
        node = node.successor

    if keys_list != expected_keys:
        sys.stderr.write('%s: test_successor: keys_list != expected_keys\n' %
                         (sys.argv[0], ))
        sys.stderr.write('keys_list:\n%s\n' % (pprint.pformat(keys_list)))
        sys.stderr.write('exected_keys:\n%s\n' %
                         (pprint.pformat(expected_keys)))
        all_good = False

    if values_list != expected_values:
        sys.stderr.write(
            '%s: test_successor: values_list != expected_values\n' %
            (sys.argv[0], ))
        sys.stderr.write('values_list:\n%s\n' % (pprint.pformat(values_list)))
        sys.stderr.write('exected_values:\n%s\n' %
                         (pprint.pformat(expected_values)))
        all_good = False

    return all_good
Esempio n. 5
0
def test_dict_setitem():
    '''Test dictionary-like operation: __setitem__'''

    all_good = True

    dict_ = red_black_dict_mod.RedBlackTree()

    for number in range(10):
        dict_[number] = 2**number

    if len(dict_) != 10:
        sys.stderr.write('%s: test_dict_setitem: len(dict_) is not 10\n' %
                         (sys.argv[0], ))
        all_good = False

    return all_good
Esempio n. 6
0
def test_dict_find():
    '''Test adding to a dictionary'''

    all_good = True

    dict_ = red_black_dict_mod.RedBlackTree()
    for integer in range(5):
        dict_.add(key=integer, value=2**integer)

    found_key = dict_.find(3).key

    if found_key != 3:
        sys.stderr.write(
            '%s: test_dict_find: 3th element is not 3, instead got %s\n' %
            (sys.argv[0], found_key))
        all_good = False

    return all_good
Esempio n. 7
0
def test_dict_itervalues():
    '''Test dictionary-like operation: __itervalues__'''

    all_good = True

    dict_ = red_black_dict_mod.RedBlackTree()
    list_ = []

    for number in range(10):
        dict_[number] = 2**number
        list_.append(2**number)

    if list(dict_.itervalues()) != list_:
        sys.stderr.write(
            '%s: test_dict_itervalues: dict_.itervalues() != list_\n' %
            (sys.argv[0], ))
        all_good = False

    return all_good
Esempio n. 8
0
def test():
    '''Run the red black tree tests'''
    all_good = True

    rbset = red_black_set_mod.RedBlackTree()
    rbset_items = [6, 15, 16, 17, 5, 8, 0, 4, 10]
    for item in rbset_items:
        rbset.add(item)
    acopy = rbset.copy()

    dict_items = list(range(10))
    dict_ = red_black_dict_mod.RedBlackTree()
    for element in dict_items:
        dict_[element] = 2**element

    all_good &= test_entire_tree_ops(rbset, acopy)
    all_good &= test_adding(rbset, acopy)
    all_good &= test_tree_list_and_del_node(rbset, acopy)
    all_good &= test_min_and_max(rbset)
    all_good &= test_contains_and_find(rbset)
    all_good &= test_in_order(rbset, rbset_items)
    all_good &= test_pred_and_succ(dict_items, dict_)
    all_good &= test_check(rbset)
    all_good &= test_uniquing()
    all_good &= test_similar()
    all_good &= test_dict_add()
    all_good &= test_dict_find()
    all_good &= test_dict_setitem()
    all_good &= test_dict_getitem()
    all_good &= test_dict_delitem()
    all_good &= test_dict_iteritems()
    all_good &= test_dict_itervalues()
    all_good &= test_successor()

    if not all_good:
        sys.stderr.write('%s: One or more tests failed\n' % (sys.argv[0], ))
        sys.exit(1)