diff --git a/src/fq_default_poly/test/main.c b/src/fq_default_poly/test/main.c index 1d711b383c..858e992921 100644 --- a/src/fq_default_poly/test/main.c +++ b/src/fq_default_poly/test/main.c @@ -14,6 +14,7 @@ #include "t-init.c" #include "t-inlines.c" #include "t-set_fmpz_poly.c" +#include "t-evaluate_fq_default.c" /* Array of test functions ***************************************************/ @@ -21,7 +22,8 @@ test_struct tests[] = { TEST_FUNCTION(fq_default_poly_init), TEST_FUNCTION(fq_default_poly_inlines), - TEST_FUNCTION(fq_default_poly_set_fmpz_poly) + TEST_FUNCTION(fq_default_poly_set_fmpz_poly), + TEST_FUNCTION(fq_default_poly_evaluate) }; /* main function *************************************************************/ diff --git a/src/fq_default_poly/test/t-evaluate_fq_default.c b/src/fq_default_poly/test/t-evaluate_fq_default.c new file mode 100644 index 0000000000..5d64762ddc --- /dev/null +++ b/src/fq_default_poly/test/t-evaluate_fq_default.c @@ -0,0 +1,98 @@ +/* + Copyright (C) 2021 William Hart + + 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 . +*/ + +#include "test_helpers.h" +#include "fq_default_poly.h" + +// Given a context, compute a random polynomial and evaluate it twice on a random fq_element +void +test_eval_with_ctx(fq_default_ctx_t ctx, + flint_rand_t state) +{ + int i, result; + for (i = 0; i < 100 * flint_test_multiplier(); i++) + { + fq_default_t a, b; + fq_default_poly_t fq_poly; + + // Random polynomial + fq_default_poly_init(fq_poly, ctx); + fq_default_poly_randtest(fq_poly, state, n_randint(state, 10), ctx); + + // Evaluate the polynomial twice and ensure results match + fq_default_randtest(a, state, ctx); + fq_default_poly_evaluate_fq_default(b, fq_poly, a, ctx); + fq_default_poly_evaluate_fq_default(a, fq_poly, a, ctx); + + result = (fq_default_equal(a, b, ctx)); + if (!result) + { + flint_printf("FAIL:\n"); + fq_default_poly_print(fq_poly, ctx), flint_printf("\n\n"); + fq_default_print(a, ctx), flint_printf("\n\n"); + fq_default_print(b, ctx), flint_printf("\n\n"); + fflush(stdout); + flint_abort(); + } + + // Clear everything + fq_default_poly_clear(fq_poly, ctx); + fq_default_clear(a, ctx); + fq_default_clear(b, ctx); + } +} + +TEST_FUNCTION_START(fq_default_poly_evaluate, state) +{ + fmpz_t p; + fq_default_ctx_t ctx; + + fmpz_init(p); + + // Test FQ_ZECH type with GF(5^5) + fmpz_set_ui(p, 5); + fq_default_ctx_init_type(ctx, p, 5, "x", 1); + test_eval_with_ctx(ctx, state); + fq_default_ctx_clear(ctx); + + // Test FQ_NMOD type with GF(163^3) + // TODO: crashes with segfault + // make: *** [build/fq_default_poly/test/main_TEST_RUN] Segmentation fault: 11 + // fmpz_set_ui(p, 163); + // fq_default_ctx_init_type(ctx, p, 3, "x", 2); + // test_eval_with_ctx(ctx, state); + // fq_default_ctx_clear(ctx); + + // Test FQ type with GF((2^127 - 1)^2) + // TODO: crashes with bus error + // make: *** [build/fq_default_poly/test/main_TEST_RUN] Bus error: 10 + // fmpz_set_str(p, "170141183460469231731687303715884105727", 10); + // fq_default_ctx_init_type(ctx, p, 2, "x", 3); + // test_eval_with_ctx(ctx, state); + // fq_default_ctx_clear(ctx); + + // Test FMPZ_MOD type with GF(65537) + fmpz_set_ui(p, 65537); + fq_default_ctx_init_type(ctx, p, 1, "x", 4); + test_eval_with_ctx(ctx, state); + fq_default_ctx_clear(ctx); + + // Test FMPZ_MOD type with GF(2^127 - 1) + // TODO: fails + // fmpz_set_str(p, "170141183460469231731687303715884105727", 10); + // fq_default_ctx_init_type(ctx, p, 1, "x", 5); + // test_eval_with_ctx(ctx, state); + // fq_default_ctx_clear(ctx); + + fmpz_clear(p); + + TEST_FUNCTION_END(state); +}