|
| 1 | +/* |
| 2 | + Copyright (C) 2024 Albin Ahlbäck |
| 3 | +
|
| 4 | + This file is part of FLINT. |
| 5 | +
|
| 6 | + FLINT is free software: you can redistribute it and/or modify it under |
| 7 | + the terms of the GNU Lesser General Public License (LGPL) as published |
| 8 | + by the Free Software Foundation; either version 3 of the License, or |
| 9 | + (at your option) any later version. See <https://www.gnu.org/licenses/>. |
| 10 | +*/ |
| 11 | + |
| 12 | +/* NOTE: Here we check that we get the contexts we expect. */ |
| 13 | + |
| 14 | +#include "test_helpers.h" |
| 15 | +#include "fq_default.h" |
| 16 | + |
| 17 | +static const char str_fq_zech[] = "fq_zech"; |
| 18 | +static const char str_fq_nmod[] = "fq_nmod"; |
| 19 | +static const char str_fq[] = "fq"; |
| 20 | +static const char str_nmod[] = "nmod"; |
| 21 | +static const char str_fmpz_mod[] = "fmpz_mod"; |
| 22 | + |
| 23 | +static const char * get_str(int type) |
| 24 | +{ |
| 25 | + switch (type) |
| 26 | + { |
| 27 | + case FQ_DEFAULT_FQ_ZECH: return str_fq_zech; |
| 28 | + case FQ_DEFAULT_FQ_NMOD: return str_fq_nmod; |
| 29 | + case FQ_DEFAULT_FQ: return str_fq; |
| 30 | + case FQ_DEFAULT_NMOD: return str_nmod; |
| 31 | + case FQ_DEFAULT_FMPZ_MOD: return str_fmpz_mod; |
| 32 | + default: FLINT_UNREACHABLE; |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +static slong rand_deg_p_fq_zech(flint_rand_t state, fmpz_t p) |
| 37 | +{ |
| 38 | + /* d > 1 and d * bits(p) <= 16 */ |
| 39 | + /* 2 <= bits(p) <= 16 / d */ |
| 40 | + slong d = 2 + n_randint(state, 7); /* in {2, ..., 8} */ |
| 41 | + flint_bitcnt_t bits = 2 + n_randint(state, 16 / d - 1); |
| 42 | + fmpz_randprime(p, state, bits, 0); |
| 43 | + return d; |
| 44 | +} |
| 45 | + |
| 46 | +static slong rand_deg_p_fq_nmod(flint_rand_t state, fmpz_t p) |
| 47 | +{ |
| 48 | + /* d > 1 and fits_ui(p) and d * bits(p) > 16 */ |
| 49 | + slong d = 2 + n_randint(state, 20); |
| 50 | + flint_bitcnt_t bits = FLINT_MAX((16 + d - 1) / d + 1, 2) + n_randint(state, FLINT_BITS - FLINT_MAX((16 + d - 1) / d + 1, 2) + 1); |
| 51 | + fmpz_randprime(p, state, bits, 0); |
| 52 | + return d; |
| 53 | +} |
| 54 | + |
| 55 | +static slong rand_deg_p_nmod(flint_rand_t state, fmpz_t p) |
| 56 | +{ |
| 57 | + /* d == 1 and fits_ui(p) */ |
| 58 | + slong d = 1; |
| 59 | + flint_bitcnt_t bits = 2 + n_randint(state, FLINT_BITS - 1); |
| 60 | + fmpz_randprime(p, state, bits, 0); |
| 61 | + return d; |
| 62 | +} |
| 63 | + |
| 64 | +static slong rand_deg_p_fmpz_mod(flint_rand_t state, fmpz_t p) |
| 65 | +{ |
| 66 | + /* d == 1 and not fits_ui(p) */ |
| 67 | + slong d = 1; |
| 68 | + flint_bitcnt_t bits = FLINT_BITS + 1 + n_randint(state, 64); |
| 69 | + fmpz_randprime(p, state, bits, 0); |
| 70 | + return d; |
| 71 | +} |
| 72 | + |
| 73 | +static slong rand_deg_p_fq(flint_rand_t state, fmpz_t p) |
| 74 | +{ |
| 75 | + /* d > 1 and not fits_ui(p) */ |
| 76 | + slong d = 2 + n_randint(state, 20); |
| 77 | + flint_bitcnt_t bits = FLINT_BITS + 1 + n_randint(state, 64); |
| 78 | + fmpz_randprime(p, state, bits, 0); |
| 79 | + return d; |
| 80 | +} |
| 81 | + |
| 82 | +#define TYPE_MIN FQ_DEFAULT_FQ_ZECH |
| 83 | +#define TYPE_MAX FQ_DEFAULT_FMPZ_MOD |
| 84 | + |
| 85 | +TEST_FUNCTION_START(fq_default_ctx_init, state) |
| 86 | +{ |
| 87 | + slong ix; |
| 88 | + int result; |
| 89 | + fq_default_ctx_t ctx; |
| 90 | + |
| 91 | + /* fq_default_ctx_init_type */ |
| 92 | + for (ix = 0; ix < 100 * flint_test_multiplier(); ix++) |
| 93 | + { |
| 94 | + int type; |
| 95 | + fmpz_t p; |
| 96 | + slong d; |
| 97 | + char var[] = "x"; |
| 98 | + |
| 99 | + type = TYPE_MIN + n_randint(state, TYPE_MAX + TYPE_MIN - 1); |
| 100 | + fmpz_init(p); |
| 101 | + |
| 102 | + switch (type) |
| 103 | + { |
| 104 | + case FQ_DEFAULT_FQ_ZECH: d = rand_deg_p_fq_zech(state, p); break; |
| 105 | + case FQ_DEFAULT_FQ_NMOD: d = rand_deg_p_fq_nmod(state, p); break; |
| 106 | + case FQ_DEFAULT_FQ: d = rand_deg_p_fq(state, p); break; |
| 107 | + case FQ_DEFAULT_NMOD: d = rand_deg_p_nmod(state, p); break; |
| 108 | + case FQ_DEFAULT_FMPZ_MOD: d = rand_deg_p_fmpz_mod(state, p); break; |
| 109 | + default: FLINT_UNREACHABLE; |
| 110 | + } |
| 111 | + |
| 112 | + fq_default_ctx_init_type(ctx, p, d, var, 0); |
| 113 | + |
| 114 | + result = (fq_default_ctx_type(ctx) == type); |
| 115 | + if (!result) |
| 116 | + TEST_FUNCTION_FAIL( |
| 117 | + "fq_default_ctx_init_type:\n" |
| 118 | + "ix = %wd\n" |
| 119 | + "p = %{fmpz}\n" |
| 120 | + "d = %{slong}\n" |
| 121 | + "Expected type: %s\n" |
| 122 | + "Got type: %s\n", |
| 123 | + ix, p, d, get_str(type), get_str(fq_default_ctx_type(ctx))); |
| 124 | + |
| 125 | + fq_default_ctx_clear(ctx); |
| 126 | + fmpz_clear(p); |
| 127 | + } |
| 128 | + |
| 129 | + /* fq_default_ctx_init_modulus */ |
| 130 | + for (ix = 0; ix < 100 * flint_test_multiplier(); ix++) |
| 131 | + { |
| 132 | + int type; |
| 133 | + fmpz_mod_poly_t modulus; |
| 134 | + fmpz_mod_ctx_t mod_ctx; |
| 135 | + fmpz_t p; |
| 136 | + slong d; |
| 137 | + char var[] = "x"; |
| 138 | + |
| 139 | + type = TYPE_MIN + n_randint(state, TYPE_MAX + TYPE_MIN - 1); |
| 140 | + fmpz_init(p); |
| 141 | + |
| 142 | + /* Get degree and prime */ |
| 143 | + switch (type) |
| 144 | + { |
| 145 | + case FQ_DEFAULT_FQ_ZECH: d = rand_deg_p_fq_zech(state, p); break; |
| 146 | + case FQ_DEFAULT_FQ_NMOD: d = rand_deg_p_fq_nmod(state, p); break; |
| 147 | + case FQ_DEFAULT_FQ: d = rand_deg_p_fq(state, p); break; |
| 148 | + case FQ_DEFAULT_NMOD: d = rand_deg_p_nmod(state, p); break; |
| 149 | + case FQ_DEFAULT_FMPZ_MOD: d = rand_deg_p_fmpz_mod(state, p); break; |
| 150 | + default: FLINT_UNREACHABLE; |
| 151 | + } |
| 152 | + |
| 153 | + fmpz_mod_ctx_init(mod_ctx, p); |
| 154 | + fmpz_mod_poly_init(modulus, mod_ctx); |
| 155 | + |
| 156 | + if (type != FQ_DEFAULT_FQ_ZECH) |
| 157 | + fmpz_mod_poly_randtest_monic_irreducible(modulus, state, d + 1, mod_ctx); |
| 158 | + else |
| 159 | + fmpz_mod_poly_randtest_monic_primitive(modulus, state, d + 1, mod_ctx); |
| 160 | + |
| 161 | + fq_default_ctx_init_modulus(ctx, modulus, mod_ctx, var); |
| 162 | + |
| 163 | + result = (fq_default_ctx_type(ctx) == type); |
| 164 | + if (!result) |
| 165 | + TEST_FUNCTION_FAIL( |
| 166 | + "fq_default_ctx_init_modulus:\n" |
| 167 | + "ix = %wd\n" |
| 168 | + "p = %{fmpz}\n" |
| 169 | + "d = %{slong}\n" |
| 170 | + "Expected type: %s\n" |
| 171 | + "Got type: %s\n", |
| 172 | + ix, p, d, get_str(type), get_str(fq_default_ctx_type(ctx))); |
| 173 | + |
| 174 | + fq_default_ctx_clear(ctx); |
| 175 | + fmpz_mod_poly_clear(modulus, mod_ctx); |
| 176 | + fmpz_mod_ctx_clear(mod_ctx); |
| 177 | + fmpz_clear(p); |
| 178 | + } |
| 179 | + |
| 180 | + /* fq_default_ctx_init_modulus_nmod */ |
| 181 | + for (ix = 0; ix < 100 * flint_test_multiplier(); ix++) |
| 182 | + { |
| 183 | + int type; |
| 184 | + nmod_poly_t modulus; |
| 185 | + nmod_t mod_ctx; |
| 186 | + fmpz_t p; |
| 187 | + ulong px; |
| 188 | + slong d; |
| 189 | + char var[] = "x"; |
| 190 | + |
| 191 | + type = TYPE_MIN + n_randint(state, TYPE_MAX + TYPE_MIN - 1); |
| 192 | + fmpz_init(p); |
| 193 | + |
| 194 | + /* Get degree and prime */ |
| 195 | + switch (type) |
| 196 | + { |
| 197 | + case FQ_DEFAULT_FQ_ZECH: d = rand_deg_p_fq_zech(state, p); break; |
| 198 | + case FQ_DEFAULT_FQ_NMOD: d = rand_deg_p_fq_nmod(state, p); break; |
| 199 | + case FQ_DEFAULT_FQ: fmpz_clear(p); continue; |
| 200 | + case FQ_DEFAULT_NMOD: d = rand_deg_p_nmod(state, p); break; |
| 201 | + case FQ_DEFAULT_FMPZ_MOD: fmpz_clear(p); continue; |
| 202 | + default: FLINT_UNREACHABLE; |
| 203 | + } |
| 204 | + |
| 205 | + px = fmpz_get_ui(p); |
| 206 | + fmpz_clear(p); |
| 207 | + |
| 208 | + nmod_init(&mod_ctx, px); |
| 209 | + nmod_poly_init_mod(modulus, mod_ctx); |
| 210 | + |
| 211 | + if (type != FQ_DEFAULT_FQ_ZECH) |
| 212 | + nmod_poly_randtest_monic_irreducible(modulus, state, d + 1); |
| 213 | + else |
| 214 | + nmod_poly_randtest_monic_primitive(modulus, state, d + 1); |
| 215 | + |
| 216 | + fq_default_ctx_init_modulus_nmod(ctx, modulus, var); |
| 217 | + |
| 218 | + result = (fq_default_ctx_type(ctx) == type); |
| 219 | + if (!result) |
| 220 | + TEST_FUNCTION_FAIL( |
| 221 | + "fq_default_ctx_init_modulus_nmod:\n" |
| 222 | + "ix = %wd\n" |
| 223 | + "p = %{fmpz}\n" |
| 224 | + "d = %{slong}\n" |
| 225 | + "Expected type: %s\n" |
| 226 | + "Got type: %s\n", |
| 227 | + ix, p, d, get_str(type), get_str(fq_default_ctx_type(ctx))); |
| 228 | + |
| 229 | + fq_default_ctx_clear(ctx); |
| 230 | + nmod_poly_clear(modulus); |
| 231 | + } |
| 232 | + |
| 233 | + TEST_FUNCTION_END(state); |
| 234 | +} |
| 235 | + |
| 236 | +#undef TYPE_MIN |
| 237 | +#undef TYPE_MAX |
0 commit comments