import pytest
from hypothesis import given, settings, assume
from hypothesis.strategies import text

import smaz


def test_ok():
    """Simple test case."""
    raw = "The quick brown fox jumps over the lazy dog."
    compressed = smaz.compress(raw)
    decompressed = smaz.decompress(compressed)
    assert decompressed == raw


@given(text())
@settings(max_examples=10000)
def test_compress_decompress_unicode(string):
    """Verify all sorts of strings, generated by hypothesis.

    This includes empty strings, very long strings, various kinds of unicode
    characters etc.
    """
    # Exclude strings with NULL characters in them. I am not sure this is smart. Lets
    # see how people use this and if I ever get a bug report about not supporting null
    # characters.
    assume("\x00" not in string)
    compressed = smaz.compress(string)
    decompressed = smaz.decompress(compressed)
    assert string == decompressed


def test_compress_non_str():
    """Verify only str being accepted by compress."""
    raw = "The quick brown fox jumps over the lazy dog."
    with pytest.raises(TypeError):
        smaz.compress([raw])
    with pytest.raises(TypeError):
        smaz.compress((raw,))
    with pytest.raises(TypeError):
        smaz.compress({"raw": raw})
    with pytest.raises(TypeError):
        smaz.compress(1)
    with pytest.raises(TypeError):
        smaz.compress(1.5)
    with pytest.raises(TypeError):
        smaz.compress(raw.encode("utf-8"))


def test_compress_non_bytes():
    """Verify only bytes being accepted by decompress."""
    raw = "The quick brown fox jumps over the lazy dog."
    with pytest.raises(TypeError):
        smaz.decompress(raw)
    with pytest.raises(TypeError):
        smaz.decompress([raw])
    with pytest.raises(TypeError):
        smaz.decompress((raw,))
    with pytest.raises(TypeError):
        smaz.decompress({"raw": raw})
    with pytest.raises(TypeError):
        smaz.decompress(1)
    with pytest.raises(TypeError):
        smaz.decompress(1.5)


@pytest.mark.parametrize(
    "raw",
    [
        "This is a small string",
        "foobar",
        "the end",
        "not-a-g00d-Exampl333",
        "Smaz is a simple compression library",
        "Nothing is more difficult, and therefore more precious, than to be able to decide",
        "this is an example of what works very well with smaz",
        "1000 numbers 2000 will 10 20 30 compress very little",
        "and now a few italian sentences:",
        "Nel mezzo del cammin di nostra vita, mi ritrovai in una selva oscura",
        "Mi illumino di immenso",
        "L'autore di questa libreria vive in Sicilia",
        "try it against urls",
        "http://google.com",
        "http://programming.reddit.com",
        "http://github.com/antirez/smaz/tree/master",
        "/media/hdb1/music/Alben/The Bla",
    ],
)
def test_original_smaz(raw):
    """Verify functionality based on the original smaz_test.c

    https://github.com/antirez/smaz/blob/master/smaz_test.c
    """
    compressed = smaz.compress(raw)
    decompressed = smaz.decompress(compressed)
    assert decompressed == raw
