libguf/dbuf_tests.py
2025-01-03 10:38:57 +01:00

190 lines
4.7 KiB
Python

from functools import partial
from testgen import gen_test_struct, gen_res_str
DEFAULT_N = 4
test_funs = list()
def test_push(is_str = False, n = DEFAULT_N):
buf = list()
for i in range(n):
if is_str:
buf.append("element at index " + str(i))
else:
buf.append(i)
name = ""
if is_str:
name = "str"
return gen_test_struct(f"tst_push{name}", f"guf_dbuf_test: push {name}", gen_res_str(buf))
test_funs.append(partial(test_push, False))
test_funs.append(partial(test_push, True))
def test_insert_empty_front():
buf = list()
buf.insert(0, 3141)
return gen_test_struct("tst_insert_empty_front", "guf_dbuf_test: insert empty front", gen_res_str(buf))
test_funs.append(test_insert_empty_front)
def test_insert_empty_back():
buf = list()
buf.insert(1, 3141)
return gen_test_struct("tst_insert_empty_back", "guf_dbuf_test: insert empty back", gen_res_str(buf))
test_funs.append(test_insert_empty_back)
def test_insert(is_str = False, n = DEFAULT_N):
buf = list()
for i in range(n):
if i % 7 == 0:
idx = len(buf)
elif i % 2 == 0:
idx = 1
else:
assert(len(buf) > 0)
idx = len(buf) - 1
if is_str:
buf.insert(idx, f"element at index {idx}")
else:
buf.insert(idx, i)
if is_str:
start = "pi" * 64
end = "euler" * 64
else:
start = 3141
end = 2718
buf.insert(0, start)
buf.insert(len(buf), end)
buf.insert(1, start * 2)
buf.insert(len(buf) - 1, end * 2)
name = "int"
if is_str:
name = "str"
return gen_test_struct(f"tst_insert{name}", f"guf_dbuf_test: insert {name}", gen_res_str(buf))
test_funs.append(partial(test_insert, False))
test_funs.append(partial(test_insert, True))
def test_erase(is_str = False, n = DEFAULT_N):
buf = list()
for i in range(n):
if is_str:
buf.append("element at index " + str(i))
else:
buf.append(i)
for i, elem in enumerate(buf):
if i % 2 == 0:
del elem
name = "int"
if is_str:
name = "str"
return gen_test_struct(f"tst_erase{name}", f"guf_dbuf_test: erase {name}", gen_res_str(buf))
test_funs.append(partial(test_erase, False))
test_funs.append(partial(test_erase, True))
def test_erase_all(is_str = False, n = DEFAULT_N):
buf = list()
for i in range(n):
if is_str:
buf.append("element at index " + str(i))
else:
buf.append(i)
for i, elem in enumerate(buf):
del elem
name = "int"
if is_str:
name = "str"
return gen_test_struct(f"tst_remove{name}", f"guf_dbuf_test: erase {name} all", gen_res_str(buf))
test_funs.append(partial(test_erase_all, False))
test_funs.append(partial(test_erase_all, True))
def test_pop(is_str = False, n = DEFAULT_N):
buf = list()
for i in range(n):
if is_str:
buf.append("element at index " + str(i))
else:
buf.append(i)
new_buf = list()
for i in range(len(buf)):
new_buf.append(buf.pop())
new_buf.append(len(buf))
name = "int"
if is_str:
name = "str"
return gen_test_struct(f"tst_pop{name}", f"guf_dbuf_test: pop {name}", gen_res_str(buf))
test_funs.append(partial(test_pop, False))
test_funs.append(partial(test_pop, True))
def test_front_back(is_str = False, n = DEFAULT_N):
buf = list()
new_buf = list()
for i in range(n):
if is_str:
buf.append("element at index " + str(i))
else:
buf.append(i)
if i % 2:
new_buf.append(buf[0]) # front
else:
new_buf.append(buf[-1]) # back
if is_str:
new_buf[0] = "first elem"
new_buf[-1] = "last elem"
else:
new_buf[0] = 12345
new_buf[-1] = 54321
new_buf.append(len(new_buf))
name = "int"
if is_str:
name = "str"
return gen_test_struct(f"tst_front_back{name}", f"guf_dbuf_test: front() back() {name}", gen_res_str(new_buf))
test_funs.append(partial(test_front_back, False))
test_funs.append(partial(test_front_back, True))
def test_at(is_str = False, n = DEFAULT_N):
buf = list()
for i in range(n):
if is_str:
buf.append("element at index " + str(i))
else:
buf.append(i)
new_buf = list()
for elem in reversed(buf):
new_buf.append(elem * 2)
name = "int"
if is_str:
name = "str"
return gen_test_struct(f"tst_at{name}", f"guf_dbuf_test: at() {name}", gen_res_str(new_buf))
test_funs.append(partial(test_at, False))
test_funs.append(partial(test_at, True))
def all_tests():
return test_funs