diff options
author | Matthew Sotoudeh <masotoudeh@ucdavis.edu> | 2020-11-10 14:16:20 -0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-11-10 14:16:20 -0800 |
commit | de88829eccd369aa1872413d6068d5179468c47e (patch) | |
tree | 1947f69081b4f647c643b0f9d5ce8c246a9b097c /runtime/tests/test_utils.py | |
parent | db99b3af36fa4687c734e1c74d83157d2f10c9ed (diff) | |
parent | be64046354a0451869d475e7f0d35f4eb2344c93 (diff) |
Contains code for reproducing the demos from our Onward '20 paper about Sifter.
Diffstat (limited to 'runtime/tests/test_utils.py')
-rw-r--r-- | runtime/tests/test_utils.py | 71 |
1 files changed, 71 insertions, 0 deletions
diff --git a/runtime/tests/test_utils.py b/runtime/tests/test_utils.py new file mode 100644 index 0000000..fcdc02d --- /dev/null +++ b/runtime/tests/test_utils.py @@ -0,0 +1,71 @@ +"""Tests for utils.py""" +from external.bazel_python.pytest_helper import main +import runtime.utils as utils + +def test_freeze_thaw_dict(): + """Tests freezedict/thawdict.""" + x = dict({"hello": "there"}) + assert utils.thawdict(utils.freezedict(x)) == x + assert isinstance(utils.freezedict(x), tuple) + +def test_is_empty(): + """Tests is_empty(...).""" + assert utils.is_empty(x for x in range(0)) + assert not utils.is_empty(x for x in range(5)) + +def test_translator(): + """Tests the Translator class.""" + translator = utils.Translator(dict({ + "hello": "bonjour", + "why": "porquoi", + "what": "quoi", + })) + assert translator.translate("hello") == "bonjour" + assert translator.translate("Matthew") == "Matthew" + + assert (translator.translate_tuple(("hello", "Matthew")) + == ("bonjour", "Matthew")) + + assert (translator.translate_tuples([("hello", "Matthew"), + ("why", "what")]) + == [("bonjour", "Matthew"), ("porquoi", "quoi")]) + + assert (translator.translate_list(["hello", "Matthew"]) + == ["bonjour", "Matthew"]) + + composed = translator.compose(dict({ + "bonjour": "salam", + "porquoi": "chera", + "merci": "merci", + })) + assert composed == dict({"hello": "salam", "why": "chera"}) + composed = translator.compose(dict({ + "bonjour": "salam", + "porquoi": "chera", + "merci": "merci", + }), default_identity=True) + assert composed == dict({"hello": "salam", "why": "chera", "what": "quoi"}) + + concatenated = translator.concatenated_with(dict({ + "thanks": "merci", + })) + assert concatenated == dict({ + "hello": "bonjour", + "why": "porquoi", + "what": "quoi", + "thanks": "merci", + }) + +def test_real_hash(): + """Regression test for the real_hash method.""" + truth = "ff42aa4718d9da1c7d491e8be8116f0c62db8d910de16e8ee0648147" + assert utils.real_hash(dict({"hello": "there"})) == truth + try: + # We don't yet support tuples, it should throw a NIE. + utils.real_hash(("hi", "hello")) + except NotImplementedError: + pass + else: + assert False + +main(__name__, __file__) |