예제 #1
0
def unhashable_memoize_test():
    ''' Unhashable arg memoize check '''
    def sort(thing):
        return sorted(thing)

    counter = CallCounter(sort)
    sort = memoize(counter)

    values = [4, 2, 3, 1]
    for dummy in xrange(5):
        eq_(sort(values), sorted(values))
예제 #2
0
def arg_memoize_test():
    ''' Single arg memoize test '''
    def double(val):
        return 2 * val

    counter = CallCounter(double)
    double = memoize(counter)

    values = [1, 2, 3, 4]
    for val in values:
        for dummy in xrange(5):
            eq_(double(val), 2 * val)
    eq_(counter.call_count, len(values))
예제 #3
0
def kwarg_memoize_test():
    ''' kwarg memoize test '''
    def multiply(vala, valb):
        return vala * valb

    counter = CallCounter(multiply)
    multiply = memoize(counter)

    values = [1, 2, 3, 4]
    for vala, valb in itertools.product(values, values):
        for dummy in xrange(5):
            eq_(multiply(vala=vala, valb=valb), vala * valb)
            eq_(multiply(valb=valb, vala=vala), vala * valb)
    eq_(counter.call_count, len(values) ** 2)
예제 #4
0
def noarg_memoize_test():
    ''' Tests memoize on a 0-ary function '''
    value = [1, 2, 3]

    def func():
        return value

    counter = CallCounter(func)
    func = memoize(counter)

    for dummy in xrange(5):
        eq_(func(), value)

    eq_(counter.call_count, 1)