示例#1
0
def test_Fibonacci_get_number_from_cache(cache_mgr):
  """ Test the get_number function with valid parameter """
  cache_mgr.set(str(0), '0')
  cache_mgr.set(str(1), '1')
  cache_mgr.set(str(2), '1')
  cache_mgr.set(str(3), '2')
  cache_mgr.set(str(4), '3')
  cache_mgr.set(str(5), '5')
  cache_mgr.set(str(6), '8')

  fibo = Fibonacci(cache_mgr, 6)
  # generate the fibonacci number for the first time
  y = fibo.get_number(4)
  assert y == 3

  # get the fibonacci number from cache
  y = fibo.get_number(7)
  assert y == 13
示例#2
0
def fibonacci(n):
    offset = 0
    limit = 100
    # Validate parameters
    offset_str = request.args.get('offset')
    limit_str = request.args.get('limit')
    # validate offset parameter
    if offset_str:
        msg = "'offset' should be an integer and be in the range of [0, %d]." % (
            n)
        try:
            offset = int(offset_str)
            if (offset < 0) or (offset > n):
                return gen_response(msg, 403)
        except ValueError:
            return gen_response(msg, 403)

    # validate offset parameter
    if limit_str:
        msg = "'limit' should be an integer and be in the range of (0, %d]." % (
            MAX_LIMIT)
        try:
            limit = int(limit_str)
            if (limit <= 0) or (limit > MAX_LIMIT):
                return gen_response(msg, 403)
        except ValueError:
            return gen_response(msg, 403)

    # Get fibonacci number sequence
    try:
        fibo = Fibonacci()
        seq = fibo.get_sequence(n, offset, limit)
        resp_data = RespData(n, offset, limit, seq, '/api/fibonacci')
        response = jsonify(results=resp_data.data())
        response.status_code = 200
    except Exception as e:
        app.logger.exception(str(e))
        response = jsonify(message=str(e))
        response.status_code = 403
    return response
示例#3
0
def test_get_number():
    """ Test the get_number function with valid parameter """
    fibo = Fibonacci()
    y = fibo.get_number(0)
    assert y == 0
    y = fibo.get_number(1)
    assert y == 1
    y = fibo.get_number(2)
    assert y == 1
    y = fibo.get_number(3)
    assert y == 2
    y = fibo.get_number(4)
    assert y == 3
    y = fibo.get_number(10)
    assert y == 55
示例#4
0
def test_get_sequence_default():
    """ Test the get_sequence function with default 'offset' and 'limit' """
    fibo = Fibonacci()
    seq = fibo.get_sequence(0)
    assert seq == (0, )
    seq = fibo.get_sequence(1)
    assert seq == (0, 1)
    seq = fibo.get_sequence(2)
    assert seq == (0, 1, 1)
    seq = fibo.get_sequence(3)
    assert seq == (0, 1, 1, 2)
    seq = fibo.get_sequence(7)
    assert seq == (0, 1, 1, 2, 3, 5, 8, 13)
示例#5
0
def test_get_number_too_large():
    """ Test the get_number function with parameter larger than the max limit """
    # the max fibonacci number that we support is 8
    fibo = Fibonacci(max_fibo_layer=3)
    y = fibo.get_number(8)
    assert y == 21

    with pytest.raises(ParamsError) as excinfo:
        y = fibo.get_number(9)
    assert 'The Fibonacci parameter is too larger' in str(excinfo.value)

    # the max fibonacci number that we support is (math.pow(2, 20)) = 1048576
    fibo1 = Fibonacci()
    y = fibo1.get_number(1048576)
    assert y > 0

    with pytest.raises(ParamsError) as excinfo:
        y = fibo1.get_number(1048577)
    assert 'The Fibonacci parameter is too larger' in str(excinfo.value)
示例#6
0
def test_get_sequence_with_offset():
    """ Test the get_sequence function with different 'offset' parameter """
    fibo = Fibonacci()
    seq = fibo.get_sequence(3, offset=0)
    assert seq == (0, 1, 1, 2)

    seq = fibo.get_sequence(3, offset=1)
    assert seq == (1, 1, 2)

    seq = fibo.get_sequence(3, offset=2)
    assert seq == (1, 2)

    seq = fibo.get_sequence(3, offset=3)
    assert seq == (2, )

    with pytest.raises(ParamsError):
        seq = fibo.get_sequence(3, offset=4)

    with pytest.raises(ParamsError):
        seq = fibo.get_sequence(3, offset=-1)
示例#7
0
def test_get_sequence_with_limit():
    """ Test the get_sequence umber function with different 'limit' parameter """
    fibo = Fibonacci()

    seq = fibo.get_sequence(3, limit=1)
    assert seq == (0, )

    seq = fibo.get_sequence(3, limit=2)
    assert seq == (0, 1)

    seq = fibo.get_sequence(3, limit=3)
    assert seq == (0, 1, 1)

    seq = fibo.get_sequence(3, limit=4)
    assert seq == (0, 1, 1, 2)

    seq = fibo.get_sequence(3, limit=5)
    assert seq == (0, 1, 1, 2)

    with pytest.raises(ParamsError):
        seq = fibo.get_sequence(3, limit=0)

    with pytest.raises(ParamsError):
        seq = fibo.get_sequence(3, limit=-1)
示例#8
0
def test_get_sequence_with_offset_and_limit():
    """ Test the get_sequence umber function with different 'offset' and 'limit' parameter """
    fibo = Fibonacci()

    seq = fibo.get_sequence(3, offset=0, limit=2)
    assert seq == (0, 1)

    seq = fibo.get_sequence(3, offset=1, limit=2)
    assert seq == (1, 1)

    seq = fibo.get_sequence(3, offset=2, limit=2)
    assert seq == (1, 2)

    seq = fibo.get_sequence(3, offset=3, limit=2)
    assert seq == (2, )

    with pytest.raises(ParamsError):
        seq = fibo.get_sequence(3, offset=4, limit=2)
示例#9
0
def test_get_number_invalid():
    """ Test the get_number function with invalid parameter """
    fibo = Fibonacci()
    with pytest.raises(ParamsError):
        y = fibo.get_number(-4)