# -*- Autotest -*-

AT_BANNER([ignored problems])

AT_TESTFUN([ignored_problems_all],
[[
#include "libabrt.h"
#include "problem_data.h"
#include <assert.h>

#define SET_PATH "/tmp/ignored_problems_test"

#define ALL_CORRECT_SET "../../ignored_problems_data/ignored_problems_all_correct"
#define MISSING_DUPHAS_SET "../../ignored_problems_data/ignored_missing_duphash"
#define MISSING_UUID_AND_DUPHASH_SET "../../ignored_problems_data/ignored_missing_uuid_and_duphash"

#define FIRST_DD_ID "../../ignored_problems_data/first"
#define SECOND_DD_ID "../../ignored_problems_data/second"
#define THIRD_DD_ID "../../ignored_problems_data/third"
#define SAME_UUID_AS_THIRD_DD_ID "../../ignored_problems_data/same_uuid_as_third"
#define SAME_DUPHASH_AS_THIRD_DD_ID "../../ignored_problems_data/same_duphash_as_third"
#define MISSING_UUID_THIRD_DD_ID "../../ignored_problems_data/missing_uuid_third"
#define MISSING_DUPHASH_THIRD_DD_ID "../../ignored_problems_data/missing_duphash_third"

int main(void)
{
    {
        unlink(SET_PATH);
        ignored_problems_t *set = ignored_problems_new(xstrdup(SET_PATH));

        ignored_problems_add(set, "foo_blah");
        assert(0 == ignored_problems_contains(set, "foo_blah") || "The set contains invalid problem");
        ignored_problems_remove(set, "foo_blah");

        assert(0 == ignored_problems_contains(set, FIRST_DD_ID) || !"The set contains a problem and it wasn't added");
        ignored_problems_remove(set, FIRST_DD_ID);

        ignored_problems_add(set, FIRST_DD_ID);
        assert(0 != ignored_problems_contains(set, FIRST_DD_ID) || !"Thes set contains just added problem");

        ignored_problems_add(set, FIRST_DD_ID);
        ignored_problems_add(set, FIRST_DD_ID);

        ignored_problems_remove(set, FIRST_DD_ID);
        assert(0 == ignored_problems_contains(set, FIRST_DD_ID) || !"The set contains just removed problem");

        ignored_problems_remove(set, FIRST_DD_ID);

        ignored_problems_free(set);
        unlink(SET_PATH);
    }

    {
        unlink(SET_PATH);
        ignored_problems_t *set = ignored_problems_new(xstrdup(SET_PATH));

        struct dump_dir *dd = dd_opendir(FIRST_DD_ID, DD_OPEN_READONLY | DD_DONT_WAIT_FOR_LOCK);
        assert(NULL != dd || !"Failed to load testing directory");

        problem_data_t *pd = create_problem_data_from_dump_dir(dd);
        dd_close(dd);
        problem_data_add_text_noteditable(pd, CD_DUMPDIR, FIRST_DD_ID);

        assert(0 == ignored_problems_contains_problem_data(set, pd) || !"The set contains a problem and it wasn't added");
        ignored_problems_remove_problem_data(set, pd);

        ignored_problems_add_problem_data(set, pd);
        assert(0 != ignored_problems_contains_problem_data(set, pd) || !"Thes set contains just added problem");

        ignored_problems_add_problem_data(set, pd);
        ignored_problems_add_problem_data(set, pd);

        ignored_problems_remove_problem_data(set, pd);
        assert(0 == ignored_problems_contains_problem_data(set, pd) || !"The set contains just removed problem");

        ignored_problems_remove_problem_data(set, pd);

        problem_data_free(pd);
        ignored_problems_free(set);
        unlink(SET_PATH);
    }


    {
        ignored_problems_t *set = ignored_problems_new(xstrdup("/foo/blah"));

        ignored_problems_add(set, FIRST_DD_ID);
        ignored_problems_remove(set, FIRST_DD_ID);
        assert(0 == ignored_problems_contains(set, FIRST_DD_ID) || !"Invalid file contains a problem");

        ignored_problems_free(set);
    }

    {
        ignored_problems_t *set = ignored_problems_new(xstrdup(SET_PATH));

        ignored_problems_add(set, FIRST_DD_ID);

        ignored_problems_free(set);

        set = ignored_problems_new(xstrdup(SET_PATH));
        assert(0 != ignored_problems_contains(set, FIRST_DD_ID) || !"Thes set doesn't contain saved problem");

        ignored_problems_free(set);
    }

    {
        ignored_problems_t *set = ignored_problems_new(xstrdup(SET_PATH));

        ignored_problems_add(set, FIRST_DD_ID);
        ignored_problems_add(set, SECOND_DD_ID);
        ignored_problems_add(set, THIRD_DD_ID);

        assert(0 != ignored_problems_contains(set, FIRST_DD_ID) || !"Thes set doesn't contain added problem");
        assert(0 != ignored_problems_contains(set, SECOND_DD_ID) || !"Thes set doesn't contain added problem");
        assert(0 != ignored_problems_contains(set, THIRD_DD_ID) || !"Thes set doesn't contain added problem");

        ignored_problems_free(set);
        set = ignored_problems_new(xstrdup(SET_PATH));

        assert(0 != ignored_problems_contains(set, FIRST_DD_ID) || !"Thes set doesn't contain saved problem");
        assert(0 != ignored_problems_contains(set, SECOND_DD_ID) || !"Thes set doesn't contain saved problem");
        assert(0 != ignored_problems_contains(set, THIRD_DD_ID) || !"Thes set doesn't contain saved problem");

        ignored_problems_remove(set, SECOND_DD_ID);

        assert(0 != ignored_problems_contains(set, FIRST_DD_ID) || !"Thes set doesn't contain saved problem");
        assert(0 == ignored_problems_contains(set, SECOND_DD_ID) || !"Thes set contains removed problem");
        assert(0 != ignored_problems_contains(set, THIRD_DD_ID) || !"Thes set doesn't contain saved problem");

        ignored_problems_free(set);
        set = ignored_problems_new(xstrdup(SET_PATH));

        assert(0 != ignored_problems_contains(set, FIRST_DD_ID) || !"Thes set doesn't contain saved problem");
        assert(0 == ignored_problems_contains(set, SECOND_DD_ID) || !"Thes set contains removed problem");
        assert(0 != ignored_problems_contains(set, THIRD_DD_ID) || !"Thes set doesn't contain saved problem");

        assert(0 != ignored_problems_contains(set, SAME_UUID_AS_THIRD_DD_ID) || !"Same uuid wasn't detected");
        assert(0 != ignored_problems_contains(set, SAME_DUPHASH_AS_THIRD_DD_ID) || !"Same duphash wasn't detected");

        assert(0 != ignored_problems_contains(set, MISSING_UUID_THIRD_DD_ID) || !"Not found with missing UUID");
        assert(0 != ignored_problems_contains(set, MISSING_DUPHASH_THIRD_DD_ID) || !"Not found with missing DUPHASH");

        ignored_problems_remove(set, SAME_UUID_AS_THIRD_DD_ID);
        assert(0 == ignored_problems_contains(set, THIRD_DD_ID));

        ignored_problems_add(set, THIRD_DD_ID);
        assert(0 != ignored_problems_contains(set, THIRD_DD_ID));
        ignored_problems_remove(set, SAME_DUPHASH_AS_THIRD_DD_ID);
        assert(0 == ignored_problems_contains(set, THIRD_DD_ID));

        ignored_problems_add(set, THIRD_DD_ID);
        assert(0 != ignored_problems_contains(set, THIRD_DD_ID));
        ignored_problems_remove(set, MISSING_UUID_THIRD_DD_ID);
        assert(0 == ignored_problems_contains(set, THIRD_DD_ID));

        ignored_problems_add(set, THIRD_DD_ID);
        assert(0 != ignored_problems_contains(set, THIRD_DD_ID));
        ignored_problems_remove(set, MISSING_DUPHASH_THIRD_DD_ID);
        assert(0 == ignored_problems_contains(set, THIRD_DD_ID));

        ignored_problems_free(set);
        unlink(SET_PATH);
    }

    {
        ignored_problems_t *set = ignored_problems_new(xstrdup(ALL_CORRECT_SET));
        assert(0 != ignored_problems_contains(set, FIRST_DD_ID) || !"Thes set doesn't contain added problem");
        assert(0 != ignored_problems_contains(set, SECOND_DD_ID) || !"Thes set doesn't contain added problem");
        assert(0 != ignored_problems_contains(set, THIRD_DD_ID) || !"Thes set doesn't contain added problem");
        ignored_problems_free(set);

        set = ignored_problems_new(xstrdup(MISSING_DUPHAS_SET));
        assert(0 != ignored_problems_contains(set, FIRST_DD_ID) || !"Thes set doesn't contain added problem");
        assert(0 != ignored_problems_contains(set, SECOND_DD_ID) || !"Thes set doesn't contain added problem");
        assert(0 != ignored_problems_contains(set, THIRD_DD_ID) || !"Thes set doesn't contain added problem");
        ignored_problems_free(set);

        set = ignored_problems_new(xstrdup(MISSING_UUID_AND_DUPHASH_SET));
        assert(0 != ignored_problems_contains(set, FIRST_DD_ID) || !"Thes set doesn't contain added problem");
        assert(0 != ignored_problems_contains(set, SECOND_DD_ID) || !"Thes set doesn't contain added problem");
        assert(0 != ignored_problems_contains(set, THIRD_DD_ID) || !"Thes set doesn't contain added problem");
        ignored_problems_free(set);
    }

    return 0;
}
]])
