module qoi_test @test;

import std::io::file;
import std::compression::qoi;


const char[*] TEST_QOI_DATA = b64"[encoded data, 5820 characters]";


fn void test_qoi_all()
{
    @pool()
    {
        // create a test descriptor
        QOIDesc test_desc;

        // decode the test data
        char[] decoded = qoi::decode(mem, TEST_QOI_DATA[..], &test_desc)!!;
        defer free(decoded);

        assert(test_desc.width == 340 && test_desc.height == 169, "Expected resolution of 340x169");

        // encode the decoded data
        char[] encoded = qoi::encode(mem, decoded, &test_desc)!!;
        assert(encoded == TEST_QOI_DATA[..], "Encoder output should match the test data");
		defer free(encoded);
        // encode and write the decoded data to a file
        usz written = qoi::write("unittest.qoi", decoded, &test_desc)!!;

        // read and decode the written data
        char[] read = qoi::read(mem, "unittest.qoi", &test_desc)!!;
        assert(read == decoded, "Read data should match the decoded data");

        // cleanup
        file::delete("unittest.qoi")!!;
        free(read);
    };
}
