1
0
Fork 0
forked from itec/banana
banana/tests/test_banana.py

221 lines
5.3 KiB
Python
Raw Normal View History

2020-09-25 12:55:05 +02:00
#!/usr/bin/env python
"""Tests for `banana` package."""
2020-10-03 16:43:27 +02:00
import random
2020-10-01 20:21:38 +02:00
import pytest
2020-09-25 12:55:05 +02:00
2020-10-02 11:10:33 +02:00
from banana import AnanasCodec, AvocadoCodec, BananaCodec, RibesCodec
2020-09-25 12:55:05 +02:00
2020-10-01 20:29:27 +02:00
banana_conversions = {"be": 1, "beba": 70, "zu": 69, "bezu": 139, "nana": 2485}
@pytest.fixture(params=banana_conversions.items())
def banana_known(request):
yield request.param
2020-09-25 12:55:05 +02:00
2020-10-01 20:21:38 +02:00
avocado_conversions = {
2020-10-01 20:06:03 +02:00
"a": 0,
"aca": 5,
"ada": 10,
"afa": 15,
"aga": 20,
"ala": 25,
"ama": 30,
"ana": 35,
"apa": 40,
"ara": 45,
"asa": 50,
"ata": 55,
"ava": 60,
"aza": 65,
"eba": 70,
"eca": 75,
"eda": 80,
"efa": 85,
"ega": 90,
}
2020-10-01 20:29:27 +02:00
@pytest.fixture(params=avocado_conversions.items())
def avocado_known(request):
yield request.param
2020-10-01 20:21:38 +02:00
ribes_conversions = {"b": 0, "c": 1, "z": 13, "beb": 14, "bec": 15}
@pytest.fixture(params=ribes_conversions.items())
def ribes_known(request):
yield request.param
2020-10-01 19:53:19 +02:00
2020-10-01 20:32:26 +02:00
ananas_conversions = {
"ac": 1,
"al": 5,
"as": 10,
"ec": 15,
"em": 20,
"et": 25,
"id": 30,
"in": 35,
"iv": 40,
"of": 45,
"op": 50,
"oz": 55,
"ug": 60,
"ur": 65,
"acab": 70,
"acal": 75,
"acas": 80,
"acec": 85,
"acem": 90,
}
2020-10-02 11:10:33 +02:00
ananas_codec = AnanasCodec()
avocado_codec = AvocadoCodec()
banana_codec = BananaCodec()
ribes_codec = RibesCodec()
2020-10-01 20:32:26 +02:00
@pytest.fixture(params=ananas_conversions.items())
def ananas_known(request):
yield request.param
2020-10-01 20:29:27 +02:00
def test_banana_to_dec_known(banana_known):
word, value = banana_known
2020-10-02 11:10:33 +02:00
assert banana_codec.decode(word) == value
2020-10-01 19:53:19 +02:00
2020-10-01 20:29:27 +02:00
def test_dec_to_banana_known(banana_known):
word, value = banana_known
2020-10-02 11:10:33 +02:00
assert banana_codec.encode(value) == word
2020-10-01 19:53:19 +02:00
2020-10-02 10:46:51 +02:00
def test_banana_is_banana(banana_known):
2020-10-02 11:10:33 +02:00
assert banana_codec.is_valid(banana_known[0])
2020-10-02 10:46:51 +02:00
def test_banana_is_only_banana(banana_known):
2020-10-02 11:10:33 +02:00
assert not ribes_codec.is_valid(banana_known[0])
assert not ananas_codec.is_valid(banana_known[0])
assert not avocado_codec.is_valid(banana_known[0])
2020-10-02 10:46:51 +02:00
2020-10-01 20:29:27 +02:00
def test_banana2dec_prefix_ba(banana_known):
"""un ba all'inizio non cambia nulla!"""
word, value = banana_known
for prefix in ("ba", "baba", "bababa"):
2020-10-02 11:10:33 +02:00
assert banana_codec.decode(prefix + word) == value
2020-09-25 12:55:05 +02:00
2020-10-01 20:21:38 +02:00
def test_ribes_to_dec_known(ribes_known):
word, value = ribes_known
2020-10-02 11:10:33 +02:00
assert ribes_codec.decode(word) == value
2020-10-01 20:06:03 +02:00
2020-10-01 20:29:27 +02:00
def test_dec_to_ribes_known(ribes_known):
word, value = ribes_known
2020-10-02 11:10:33 +02:00
assert ribes_codec.encode(value) == word
2020-10-01 20:06:03 +02:00
2020-10-02 10:46:51 +02:00
def test_ribes_is_ribes(ribes_known):
2020-10-02 11:10:33 +02:00
assert ribes_codec.is_valid(ribes_known[0])
2020-10-02 10:46:51 +02:00
def test_ribes_is_only_ribes(ribes_known):
2020-10-02 11:10:33 +02:00
assert not banana_codec.is_valid(ribes_known[0])
assert not ananas_codec.is_valid(ribes_known[0])
assert not avocado_codec.is_valid(ribes_known[0])
2020-10-02 10:46:51 +02:00
2020-10-01 20:29:27 +02:00
def test_avocado_to_dec_known(avocado_known):
word, value = avocado_known
2020-10-02 11:10:33 +02:00
assert avocado_codec.decode(word) == value
2020-10-01 20:21:38 +02:00
2020-10-01 20:29:27 +02:00
def test_dec_to_avocado_known(avocado_known):
word, value = avocado_known
2020-10-02 11:10:33 +02:00
assert avocado_codec.encode(value) == word
2020-10-01 20:21:38 +02:00
2020-10-02 10:46:51 +02:00
def test_avocado_is_avocado(avocado_known):
2020-10-02 11:10:33 +02:00
assert avocado_codec.is_valid(avocado_known[0])
2020-10-02 10:46:51 +02:00
def test_avocado_is_only_avocado(avocado_known):
2020-10-02 11:10:33 +02:00
assert not ribes_codec.is_valid(avocado_known[0])
assert not ananas_codec.is_valid(avocado_known[0])
assert not banana_codec.is_valid(avocado_known[0])
2020-10-02 10:46:51 +02:00
2020-10-01 20:32:26 +02:00
def test_ananas_to_dec_known(ananas_known):
word, value = ananas_known
2020-10-02 11:10:33 +02:00
assert ananas_codec.decode(word) == value
2020-10-01 20:32:26 +02:00
def test_dec_to_ananas_known(ananas_known):
word, value = ananas_known
2020-10-02 11:10:33 +02:00
assert ananas_codec.encode(value) == word
2020-10-01 20:32:26 +02:00
2020-10-02 10:46:51 +02:00
def test_ananas_is_ananas(ananas_known):
2020-10-02 11:10:33 +02:00
assert ananas_codec.is_valid(ananas_known[0])
2020-10-02 10:46:51 +02:00
def test_ananas_is_only_ananas(ananas_known):
2020-10-02 11:10:33 +02:00
assert not ribes_codec.is_valid(ananas_known[0])
assert not banana_codec.is_valid(ananas_known[0])
assert not banana_codec.is_valid(ananas_known[0])
2020-10-02 10:46:51 +02:00
2020-09-25 12:55:05 +02:00
def test_answer_to_life_the_universe_and_everything():
2020-10-02 11:10:33 +02:00
banana = banana_codec.decode("banana")
2020-09-25 12:55:05 +02:00
assert banana != 42
assert banana == 2485
2020-10-03 16:43:27 +02:00
def test_random_len_0():
assert banana_codec.random(minlength=0) == ""
assert ananas_codec.random(minlength=0) == ""
assert avocado_codec.random(minlength=0) == ""
assert ribes_codec.random(minlength=0) == ""
def test_banana_random_minlength_even():
for l in (0, 2, 4, 6, 8, 10, 12):
assert len(banana_codec.random(minlength=l)) == l
assert len(ananas_codec.random(minlength=l)) == l
def test_banana_random_minlength_odd():
for l in (1, 3, 5, 7, 9):
assert len(banana_codec.random(minlength=l)) == l + 1
assert len(ananas_codec.random(minlength=l)) == l + 1
def test_ribes_random_minlength_even():
for l in (2, 4, 6, 8, 10, 12):
assert len(ribes_codec.random(minlength=l)) == l + 1
assert len(avocado_codec.random(minlength=l)) == l + 1
def test_ribes_random_minlength_odd():
for l in (1, 3, 5, 7, 9):
assert len(ribes_codec.random(minlength=l)) == l
assert len(avocado_codec.random(minlength=l)) == l
def test_random_coherence_please():
for codec in (ribes_codec, ananas_codec, avocado_codec, banana_codec):
for seed in range(30):
word = codec.random(prng=random.Random(seed))
assert codec.is_valid(word), "%s (seed %d) non valido per %s" % (
word,
seed,
codec.__class__.__name__,
)