/*
    Copyright (C) 2023 Fredrik Johansson

    This file is part of FLINT.

    FLINT is free software: you can redistribute it and/or modify it under
    the terms of the GNU Lesser General Public License (LGPL) as published
    by the Free Software Foundation; either version 3 of the License, or
    (at your option) any later version.  See <https://www.gnu.org/licenses/>.
*/

#include "test_helpers.h"
#include "ulong_extras.h"
#include "gr_poly.h"

TEST_FUNCTION_START(gr_poly_mullow_complex_reorder, state)
{
    slong iter;

    for (iter = 0; iter < 1000; iter++)
    {
        gr_ctx_t ctx, real_ctx;
        slong n;
        gr_poly_t A, B, C, D;
        int status = GR_SUCCESS;
        int aliasing = n_randint(state, 5);
        int karatsuba = n_randint(state, 2);

        if (n_randint(state, 2))
        {
            gr_ctx_init_fmpzi(ctx);
            gr_ctx_init_fmpz(real_ctx);
        }
        else
        {
            slong prec = 2 + n_randint(state, 200);
            gr_ctx_init_complex_acb(ctx, prec);
            gr_ctx_init_real_arb(real_ctx, prec);
        }

        gr_poly_init(A, ctx);
        gr_poly_init(B, ctx);
        gr_poly_init(C, ctx);
        gr_poly_init(D, ctx);

        n = n_randint(state, 30);

        GR_MUST_SUCCEED(gr_poly_randtest(A, state, 1 + n_randint(state, 30), ctx));
        GR_MUST_SUCCEED(gr_poly_randtest(B, state, 1 + n_randint(state, 30), ctx));
        GR_MUST_SUCCEED(gr_poly_randtest(C, state, 1 + n_randint(state, 30), ctx));
        GR_MUST_SUCCEED(gr_poly_randtest(D, state, 1 + n_randint(state, 30), ctx));

        switch (aliasing)
        {
            case 0:
                status |= gr_poly_mullow_complex_reorder(C, A, B, n, karatsuba, ctx, real_ctx);
                break;
            case 1:
                status |= gr_poly_set(C, A, ctx);
                status |= gr_poly_mullow_complex_reorder(C, C, B, n, karatsuba, ctx, real_ctx);
                break;
            case 2:
                status |= gr_poly_set(C, B, ctx);
                status |= gr_poly_mullow_complex_reorder(C, A, C, n, karatsuba, ctx, real_ctx);
                break;
            case 3:
                status |= gr_poly_set(B, A, ctx);
                status |= gr_poly_mullow_complex_reorder(C, A, A, n, karatsuba, ctx, real_ctx);
                break;
            case 4:
                status |= gr_poly_set(B, A, ctx);
                status |= gr_poly_set(C, A, ctx);
                status |= gr_poly_mullow_complex_reorder(C, C, C, n, karatsuba, ctx, real_ctx);
                break;

            default:
                flint_abort();
        }

        status |= gr_poly_mullow_classical(D, A, B, n, ctx);

        if (status == GR_SUCCESS && gr_poly_equal(C, D, ctx) == T_FALSE)
        {
            flint_printf("FAIL\n\n");
            flint_printf("aliasing = %d, n = %wd, karatsuba = %d\n\n", aliasing, n, karatsuba);
            gr_ctx_println(ctx);
            flint_printf("A = "); gr_poly_print(A, ctx); flint_printf("\n\n");
            flint_printf("B = "); gr_poly_print(B, ctx); flint_printf("\n\n");
            flint_printf("C = "); gr_poly_print(C, ctx); flint_printf("\n\n");
            flint_printf("D = "); gr_poly_print(D, ctx); flint_printf("\n\n");
            flint_abort();
        }

        gr_poly_clear(A, ctx);
        gr_poly_clear(B, ctx);
        gr_poly_clear(C, ctx);
        gr_poly_clear(D, ctx);

        gr_ctx_clear(ctx);
    }

    TEST_FUNCTION_END(state);
}

