#include "api/z3.h"
#include "util/trace.h"
#include "util/rational.h"
void tst_api_algebraic() {
Z3_config cfg = Z3_mk_config();
Z3_set_param_value(cfg, "model", "true");
Z3_context ctx = Z3_mk_context(cfg);
Z3_del_config(cfg);
{
Z3_ast zero = Z3_mk_real(ctx, 0, 1);
Z3_ast one = Z3_mk_real(ctx, 1, 1);
Z3_ast negative_one = Z3_mk_real(ctx, -1, 1);
Z3_ast half = Z3_mk_real(ctx, 1, 2);
ENSURE(Z3_algebraic_is_value(ctx, zero));
ENSURE(Z3_algebraic_is_value(ctx, one));
ENSURE(Z3_algebraic_is_value(ctx, negative_one));
ENSURE(Z3_algebraic_is_value(ctx, half));
}
{
Z3_symbol x_sym = Z3_mk_string_symbol(ctx, "x");
Z3_sort real_sort = Z3_mk_real_sort(ctx);
Z3_ast x = Z3_mk_const(ctx, x_sym, real_sort);
ENSURE(!Z3_algebraic_is_value(ctx, x));
}
{
Z3_ast zero = Z3_mk_real(ctx, 0, 1);
Z3_ast positive = Z3_mk_real(ctx, 5, 1);
Z3_ast negative = Z3_mk_real(ctx, -3, 1);
ENSURE(Z3_algebraic_is_zero(ctx, zero));
ENSURE(!Z3_algebraic_is_pos(ctx, zero));
ENSURE(!Z3_algebraic_is_neg(ctx, zero));
ENSURE(!Z3_algebraic_is_zero(ctx, positive));
ENSURE(Z3_algebraic_is_pos(ctx, positive));
ENSURE(!Z3_algebraic_is_neg(ctx, positive));
ENSURE(!Z3_algebraic_is_zero(ctx, negative));
ENSURE(!Z3_algebraic_is_pos(ctx, negative));
ENSURE(Z3_algebraic_is_neg(ctx, negative));
}
{
Z3_ast zero = Z3_mk_real(ctx, 0, 1);
Z3_ast positive = Z3_mk_real(ctx, 7, 1);
Z3_ast negative = Z3_mk_real(ctx, -4, 1);
ENSURE(Z3_algebraic_sign(ctx, zero) == 0);
ENSURE(Z3_algebraic_sign(ctx, positive) > 0);
ENSURE(Z3_algebraic_sign(ctx, negative) < 0);
}
{
Z3_ast two = Z3_mk_real(ctx, 2, 1);
Z3_ast three = Z3_mk_real(ctx, 3, 1);
Z3_ast five = Z3_mk_real(ctx, 5, 1);
Z3_ast result = Z3_algebraic_add(ctx, two, three);
ENSURE(Z3_algebraic_eq(ctx, result, five));
}
{
Z3_ast seven = Z3_mk_real(ctx, 7, 1);
Z3_ast three = Z3_mk_real(ctx, 3, 1);
Z3_ast four = Z3_mk_real(ctx, 4, 1);
Z3_ast result = Z3_algebraic_sub(ctx, seven, three);
ENSURE(Z3_algebraic_eq(ctx, result, four));
}
{
Z3_ast three = Z3_mk_real(ctx, 3, 1);
Z3_ast four = Z3_mk_real(ctx, 4, 1);
Z3_ast twelve = Z3_mk_real(ctx, 12, 1);
Z3_ast result = Z3_algebraic_mul(ctx, three, four);
ENSURE(Z3_algebraic_eq(ctx, result, twelve));
}
{
Z3_ast twelve = Z3_mk_real(ctx, 12, 1);
Z3_ast three = Z3_mk_real(ctx, 3, 1);
Z3_ast four = Z3_mk_real(ctx, 4, 1);
Z3_ast result = Z3_algebraic_div(ctx, twelve, three);
ENSURE(Z3_algebraic_eq(ctx, result, four));
}
{
Z3_ast two = Z3_mk_real(ctx, 2, 1);
Z3_ast eight = Z3_mk_real(ctx, 8, 1);
Z3_ast result = Z3_algebraic_power(ctx, two, 3);
ENSURE(Z3_algebraic_eq(ctx, result, eight));
}
{
Z3_ast two = Z3_mk_real(ctx, 2, 1);
Z3_ast three = Z3_mk_real(ctx, 3, 1);
Z3_ast also_three = Z3_mk_real(ctx, 3, 1);
ENSURE(Z3_algebraic_lt(ctx, two, three));
ENSURE(!Z3_algebraic_lt(ctx, three, two));
ENSURE(!Z3_algebraic_lt(ctx, three, also_three));
ENSURE(Z3_algebraic_le(ctx, two, three));
ENSURE(!Z3_algebraic_le(ctx, three, two));
ENSURE(Z3_algebraic_le(ctx, three, also_three));
ENSURE(!Z3_algebraic_gt(ctx, two, three));
ENSURE(Z3_algebraic_gt(ctx, three, two));
ENSURE(!Z3_algebraic_gt(ctx, three, also_three));
ENSURE(!Z3_algebraic_ge(ctx, two, three));
ENSURE(Z3_algebraic_ge(ctx, three, two));
ENSURE(Z3_algebraic_ge(ctx, three, also_three));
}
{
Z3_ast two = Z3_mk_real(ctx, 2, 1);
Z3_ast three = Z3_mk_real(ctx, 3, 1);
Z3_ast also_two = Z3_mk_real(ctx, 2, 1);
ENSURE(Z3_algebraic_eq(ctx, two, also_two));
ENSURE(!Z3_algebraic_eq(ctx, two, three));
ENSURE(!Z3_algebraic_neq(ctx, two, also_two));
ENSURE(Z3_algebraic_neq(ctx, two, three));
}
{
Z3_ast four = Z3_mk_real(ctx, 4, 1);
Z3_ast two = Z3_mk_real(ctx, 2, 1);
Z3_ast result = Z3_algebraic_root(ctx, four, 2); ENSURE(Z3_algebraic_eq(ctx, result, two));
}
{
Z3_ast neg_half = Z3_mk_real(ctx, -1, 2);
Z3_ast quarter = Z3_mk_real(ctx, 1, 4);
Z3_ast neg_quarter = Z3_mk_real(ctx, -1, 4);
ENSURE(Z3_algebraic_is_value(ctx, neg_half));
ENSURE(Z3_algebraic_is_neg(ctx, neg_half));
ENSURE(Z3_algebraic_is_pos(ctx, quarter));
Z3_ast result = Z3_algebraic_add(ctx, neg_half, quarter);
ENSURE(Z3_algebraic_eq(ctx, result, neg_quarter));
}
Z3_del_context(ctx);
}