Exemplo n.º 1
0
def experiment_fill(n):
    sparse_array = SparseArray(n)
    seq = [i for i in range(n)]
    start_time = time.time()
    sparse_array.fill(seq)
    end_time = time.time()
    return end_time - start_time
class TestSparseArray(unittest.TestCase):

    #def __init__(self):
    #    self.sa = SparseArray( (1,0,0,0,2,0,0,0,5) )

    def test_get(self):
        self.sa = SparseArray( (1,0,0,0,2,0,0,0,5) )
        assert self.sa[4] == 2

    def test_splice(self):
        self.sa = SparseArray( (1,0,0,0,2,0,0,0,5) )
        assert self.sa[2:5] == (0,0,2)

    def test_set(self):
        self.sa = SparseArray( (1,0,0,0,2,0,0,0,5) )
        self.sa[6] = 9
        assert self.sa[6] == 9

    def test_append(self):
        self.sa = SparseArray( (1,0,0,0,2,0,0,0,5) )
        self.sa.append(7)
        assert self.sa[:] == (1,0,0,0,2,0,0,0,5,7) 

    def test_del(self):
        self.sa = SparseArray( (1,0,0,0,2,0,0,0,5) )
        del(self.sa[0])
        assert self.sa[:] == (0,0,0,2,0,9,0,5)        
class TestSparseArray(unittest.TestCase):
    def test_get(self):
        self.sa = SparseArray((1, 0, 0, 0, 2, 0, 0, 0, 5))
        assert self.sa[4] == 2

    def test_splice(self):
        self.sa = SparseArray((1, 0, 0, 0, 2, 0, 0, 0, 5))
        assert self.sa[2:5] == [0, 0, 2]

    def test_splice_step(self):
        self.sa = SparseArray((1, 0, 0, 0, 2, 0, 0, 0, 5))
        assert self.sa[0:6:2] == [1, 0, 2]

    def test_set(self):
        self.sa = SparseArray((1, 0, 0, 0, 2, 0, 0, 0, 5))
        self.sa[6] = 9
        assert self.sa[6] == 9

    def test_append(self):
        self.sa = SparseArray((1, 0, 0, 0, 2, 0, 0, 0, 5))
        self.sa.append(7)
        assert self.sa[:] == [1, 0, 0, 0, 2, 0, 0, 0, 5, 7]

    def test_del(self):
        self.sa = SparseArray((1, 0, 0, 0, 2, 0, 0, 0, 5))
        del (self.sa[0])
        assert self.sa[:] == [0, 0, 0, 2, 0, 0, 0, 5]
Exemplo n.º 4
0
def test_eq():
    ## good to define __eq__ early, so you can use it in tests
    sa1 = SparseArray([0, 2, 4, 0, 1])
    sa2 = SparseArray([0, 2, 4, 0, 1])
    sa3 = SparseArray([0, 2, 3, 0, 1])

    assert sa1 == sa2
    assert not sa1 == sa3
Exemplo n.º 5
0
def test_set_index3():
    sa = SparseArray([0, 1, 0, 2, 0, 3])
    sa[-1] = 0

    assert sa == SparseArray([0, 1, 0, 2, 0, 0])

    with pytest.raises(IndexError):
        sa[-8] = 8
Exemplo n.º 6
0
def test_index():
    sa1 = SparseArray([1, 0, 4, 0, 0, 5, 0, 6])
    sa2 = SparseArray([0, 3, 6, 3, 0, 0, 8, 0, 9, 0, 0])
    assert sa1[1] == 0
    assert sa2[10] == 0
    assert sa1[5] == 5
    print(sa2[3], sa2[4], sa2[5])
    assert sa2[3] == 3
Exemplo n.º 7
0
def test_delete():
    sa = SparseArray([1, 2, 3, 4])
    del sa[2]
    assert sa[1] == 2
    assert sa[2] == 4
    sa2 = SparseArray([1, 0, 0, 4, 5, 3])
    del sa2[1]
    print(sa2)
    assert sa2[2] == 4
Exemplo n.º 8
0
def test_set_element_outside():
    """
    if an element is set outside the current size, the array is expanded
    to fit - makes sense for sparse, as yo know the missing values are zero
    """
    sa = SparseArray([1, 0, 2])
    sa[5] = 2
    assert len(sa) == 6
    assert sa == SparseArray([1, 0, 2, 0, 0, 2])

    # add one just on the end
    sa[6] = 10
    assert sa[6] == 10
    assert sa == SparseArray([1, 0, 2, 0, 0, 2, 10])
Exemplo n.º 9
0
def test_slice():
    sp = SparseArray(test_list)
    assert sp[1::2] == test_list[1::2]
    assert sp[::-1] == test_list[::-1]
    assert sp[::] == test_list[::]
    assert sp[0:3] == test_list[0:3]
    assert sp[6:0:-2] == test_list[6:0:-2]
Exemplo n.º 10
0
def test_initial_storage():
    sp = SparseArray(test_list)
    assert 0 not in sp.storage.values()
    assert len(sp) == len(test_list)
    
    for i in range(len(test_list)):
        assert test_list[i] == sp[i]
Exemplo n.º 11
0
def test_add():
    sa = SparseArray([0, 1, 0])

    answer = sa.size
    answer += 1

    assert answer == 4
Exemplo n.º 12
0
def experiment_setitem(n):
    sparse_array = SparseArray(n)
    for i in range(n):
        sparse_array[i - 1] = i
    start_time = time.time()
    sparse_array[n - 1] = "This one"
    end_time = time.time()
    return end_time - start_time
Exemplo n.º 13
0
def test_get_index():
    sa = SparseArray([0, 1, 2, 3, 4, 0])

    assert sa[0] == 0
    assert sa[2] == 2
    assert sa[5] == 0
    assert sa[-1] == 0
    assert sa[-2] == 4
Exemplo n.º 14
0
def test_getvalue():
    sa = SparseArray([7,29,0,0,0,0,13,0,0,72])
    test_a = sa[6]
    test_b = sa[1]
    test_c = sa[5]
    assert test_a == 13
    assert test_b == 29
    assert test_c == 0
Exemplo n.º 15
0
def test_sparsearray_del():
    """ Tests that you can delete an element from the sparse array """
    sa = SparseArray(my_array)
    del sa[4]
    print(sa)

    assert sa[4] == 0
    assert len(sa) == 8
Exemplo n.º 16
0
def experiment_getitem(n):
    sparse_array = SparseArray(n)
    for i, e in enumerate([i for i in range(n)]):
        sparse_array[i] = e
    start_time = time.time()
    x = sparse_array[n - 1]
    end_time = time.time()
    return end_time - start_time
Exemplo n.º 17
0
def test_set_item():
    sa = SparseArray([1, 0, 4, 0, 0, 5, 0, 6])
    assert sa[1] == 0
    sa[1] = 2
    assert sa[1] == 2
    print("pre addition: " + str(len(sa)))
    sa[10] = 0
    print(len(sa))
    print(sa[10])
    assert sa[8] == 0
    assert sa[10] == 0
Exemplo n.º 18
0
def test_construction():
    sa = SparseArray((1, 0, 0, 0, 2, 0, 0, 0, 5))

    assert len(sa) == 9
    assert sa[0] == 1
    assert sa[1] == 0
    assert sa[2] == 0
    assert sa[3] == 0
    assert sa[4] == 2
    assert sa[5] == 0
    assert sa[6] == 0
    assert sa[7] == 0
    assert sa[8] == 5
Exemplo n.º 19
0
def test_append():
    sa = SparseArray([0, 1, 0, 3, 4, 0])
    assert len(sa) == 6

    sa.append(8)
    assert len(sa) == 7
    assert sa == SparseArray([0, 1, 0, 3, 4, 0, 8])
    assert sa != SparseArray([0, 1, 0, 3, 4, 0])
Exemplo n.º 20
0
def test_append():
    sp = SparseArray(test_list)
    sp.append(20)
    assert sp[len(sp) - 1] == 20

    sp.append(0)
    assert sp[len(sp) - 1] == 0

    assert 0 not in sp.storage.values()
Exemplo n.º 21
0
def test_set_item():
    sp = SparseArray(test_list)
    assert test_list[3] == sp[3]
    
    sp[3] = 9
    assert test_list[3] != sp[3]
    assert sp[3] == 9
    
    sp[2] = 9
    assert test_list[2] != sp[2]
    assert sp[2] == 9

    sp[0] = 0
    assert sp[0] == 0
    assert 0 not in sp.storage.values()
Exemplo n.º 22
0
def test_set_item_out_of_range():
    sa = SparseArray((1, 0, 0, 0, 2, 0, 0, 0, 5))

    try:
        sa[9] = 1
    except IndexError:
        pass
    else:
        assert False

    try:
        sa[10] = 0
    except IndexError:
        pass
    else:
        assert False
Exemplo n.º 23
0
def test_set_item():
    sa = SparseArray((1, 0, 0, 0, 2, 0, 0, 0, 5))
    sa[1] = 2
    sa[4] = 0
    sa[7] = 10
    sa[8] = 6

    assert len(sa) == 9
    assert sa[0] == 1
    assert sa[1] == 2
    assert sa[2] == 0
    assert sa[3] == 0
    assert sa[4] == 0
    assert sa[5] == 0
    assert sa[6] == 0
    assert sa[7] == 10
    assert sa[8] == 6
Exemplo n.º 24
0
def test_exceptions():
    sp = SparseArray(test_list)

    try:
        sp[300]
    except IndexError as ie:
        assert str(ie) == "SparseArray index out of range."

    try:
        sp[900] = 2353
    except IndexError as ie:
        assert str(ie) == "SparseArray index out of range."        
    
    try:
        sp[-80] = 2
    except IndexError as ie:
        assert str(ie) == "SparseArray index out of range."        

    try:
        sp["Cresenta"]
    except TypeError as te:
        assert str(te) == "SparseArray indicies must be integers or slices, not str"
Exemplo n.º 25
0
def test_append():
    a = SA([1,0,0,0,0,3,0,0,0,4,0,5])
    a.append(8)
    assert a[12] == 8
 def test_del(self):
     self.sa = SparseArray( (1,0,0,0,2,0,0,0,5) )
     del(self.sa[0])
     assert self.sa[:] == (0,0,0,2,0,9,0,5)        
 def test_append(self):
     self.sa = SparseArray( (1,0,0,0,2,0,0,0,5) )
     self.sa.append(7)
     assert self.sa[:] == (1,0,0,0,2,0,0,0,5,7) 
 def test_set(self):
     self.sa = SparseArray( (1,0,0,0,2,0,0,0,5) )
     self.sa[6] = 9
     assert self.sa[6] == 9
 def test_splice(self):
     self.sa = SparseArray( (1,0,0,0,2,0,0,0,5) )
     assert self.sa[2:5] == (0,0,2)
 def test_get(self):
     self.sa = SparseArray( (1,0,0,0,2,0,0,0,5) )
     assert self.sa[4] == 2
Exemplo n.º 31
0
def test_append_array():
    a = SA([1,0,0,0,0,3,0,0,0,4,0,5])
    a.append([0,9])
    assert a[12] == 0
    assert a[13] == 9
    assert a.length == 14
Exemplo n.º 32
0
app = Flask(__name__)
api = Api(app)

one_result = api.model('Occurrence', {
    'word': fields.String,
    'occurrence': fields.String,
})

results_fields = api.model('Result', {
    'result': fields.List(fields.Nested(one_result)),
})

parser = reqparse.RequestParser()
parser.add_argument('query', type=str, help='words separated by comma, ex: x,y,z')

sparse_array = SparseArray(os.environ['INPUT'])


@api.route('/api')
class HelloWorld(Resource):
    @api.marshal_with(results_fields)
    @api.expect(parser)
    def get(self):
        args = parser.parse_args()
        result = sparse_array.compute(args['query'].split(","))
        output = []
        for word, occurrence in result.items():
            output.append({'word': word, 'occurrence': occurrence})
        return {'result': output}

Exemplo n.º 33
0
def test_append0():
    a = SA([1,0,0,0,0,3,0,0,0,4,0,5])
    a.append(0)
    assert a[12] == 0
    assert a.length == 13