forked from msadoghi/165a-winter-2021
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathm2_tester_part2.py
55 lines (46 loc) · 1.53 KB
/
m2_tester_part2.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
from template.db import Database
from template.query import Query
from template.config import init
from random import choice, randint, sample, seed
init()
db = Database()
db.open('./ECS165')
grades_table = db.get_table('Grades')
query = Query(grades_table)
# repopulate with random data
records = {}
seed(3562901)
for i in range(0, 1000):
key = 92106429 + i
records[key] = [key, randint(0, 20), randint(0, 20), randint(0, 20), randint(0, 20)]
# Simulate updates
keys = sorted(list(records.keys()))
for _ in range(10):
for key in keys:
for j in range(1, grades_table.num_columns):
value = randint(0, 20)
records[key][j] = value
keys = sorted(list(records.keys()))
for key in keys:
print(records[key])
for key in keys:
record = query.select(key, 0, [1, 1, 1, 1, 1])[0]
error = False
for i, column in enumerate(record.columns):
if column != records[key][i]:
error = True
if error:
print('select error on', key, ':', record, ', correct:', records[key])
print("Select finished")
deleted_keys = sample(keys, 100)
for key in deleted_keys:
query.delete(key)
records.pop(key, None)
for i in range(0, 100):
r = sorted(sample(range(0, len(keys)), 2))
column_sum = sum(map(lambda x: records[x][0] if x in records else 0, keys[r[0]: r[1] + 1]))
result = query.sum(keys[r[0]], keys[r[1]], 0)
if column_sum != result:
print('sum error on [', keys[r[0]], ',', keys[r[1]], ']: ', result, ', correct: ', column_sum)
print("Aggregate finished")
db.close()