
function test_vring(K, p, x, y)

    if IsZero(x) or IsZero(y) then
	return false;
    end if;

    if IsZero(p) then
	V := ValuationRing(K);
    else 
	V := ValuationRing(K, p);
    end if;
    assert FieldOfFractions(V) eq K;
    assert Characteristic(V) eq Characteristic(K);

    V, x, y;
    assert K!x in V;

    x := V!x;
    y := V!y;

    Valuation(x), Valuation(y);
    
    assert IsZero(x + (-x));
    assert IsZero(y + (-y));

    assert IsOne(V!1);
    assert not IsOne(V!2);
    assert IsMinusOne(V!-1);

    assert x * y eq y * x;
    assert x*x*x eq x^3;

    assert IsOne(x/x);
    assert IsOne(x * x^-1);

    assert IsOne(y div y);
    assert IsOne(y * y^-1);

    assert 2*x - x eq x;
    assert 3*y - y ne y;

    assert y in V;

    IsNilpotent(x);
    IsIdempotent(y);

    assert IsUnit(x) or Valuation(x) gt 0;
    IsZeroDivisor(x);
    IsRegular(y);

    EuclideanNorm(x);
    q, r := Quotrem(x, y);
    assert x eq q*y + r;

    g, u, v := XGCD(x, y);
    assert g eq x*u + y*v;

    return true;
end function;

i := 0;
for p in [3, 5, 7, 11, 19] do 
while not test_vring(Rationals(), p, Random(100), Random(100)) do i +:= 1; end while;
while not test_vring(Rationals(), p, -Random(100), -Random(100)) do i +:= 1; end while;
while not test_vring(Rationals(), p, -Random(100), Random(100)) do i +:= 1; end while;
while not test_vring(Rationals(), p, Random(100), -Random(100)) do i +:= 1; end while;
end for;

F<t> := FunctionField(GF(101));
for j in [1 .. 10] do
while not test_vring(F, 0, RandomPrimePolynomial(Integers(F), 2)*RandomPrimePolynomial(Integers(F), 3)/RandomPrimePolynomial(Integers(F), 5), RandomPrimePolynomial(Integers(F), 2)*RandomPrimePolynomial(Integers(F), 3)/RandomPrimePolynomial(Integers(F), 6)) do i +:= 1; end while;
while not test_vring(F, RandomPrimePolynomial(Integers(F), 2), RandomPrimePolynomial(Integers(F), 2)*RandomPrimePolynomial(Integers(F), 3)/RandomPrimePolynomial(Integers(F), 5), RandomPrimePolynomial(Integers(F), 2)*RandomPrimePolynomial(Integers(F), 3)/RandomPrimePolynomial(Integers(F), 2)) do i +:= 1; end while;
while not test_vring(F, RandomPrimePolynomial(Integers(F), 3), RandomPrimePolynomial(Integers(F), 2)*RandomPrimePolynomial(Integers(F), 3)/RandomPrimePolynomial(Integers(F), 5), RandomPrimePolynomial(Integers(F), 2)*RandomPrimePolynomial(Integers(F), 3)/RandomPrimePolynomial(Integers(F), 3)) do i +:= 1; end while;
while not test_vring(F, RandomPrimePolynomial(Integers(F), 4), RandomPrimePolynomial(Integers(F), 2)*RandomPrimePolynomial(Integers(F), 3)/RandomPrimePolynomial(Integers(F), 5), RandomPrimePolynomial(Integers(F), 2)*RandomPrimePolynomial(Integers(F), 3)/RandomPrimePolynomial(Integers(F), 4)) do i +:= 1; end while;
while not test_vring(F, RandomPrimePolynomial(Integers(F), 5), RandomPrimePolynomial(Integers(F), 2)*RandomPrimePolynomial(Integers(F), 3)/RandomPrimePolynomial(Integers(F), 4), RandomPrimePolynomial(Integers(F), 2)*RandomPrimePolynomial(Integers(F), 3)/RandomPrimePolynomial(Integers(F), 5)) do i +:= 1; end while;
end for;

_<x> := PolynomialRing(Integers());
K := NumberField(x^2 - 3);
O := MaximalOrder(K);
KK := ext<K | x^3-2>;
OO := MaximalOrder(KK);
for p in [2, 3, 5, 7, 11, 23] do
I := Decomposition(O, p)[1][1];
while not test_vring(K, I, Random(O, 5), Random(O, 5)) do i +:= 1; end while;
while not test_vring(KK, Decomposition(OO, I)[1][1], Random(OO, 5), Random(OO, 5)) do i +:= 1; end while;
end for;


_<y> := PolynomialRing(F);
K := FunctionField(y^3 - t);
OF :=  MaximalOrderFinite(K);
OI := MaximalOrderInfinite(K);
KK := ext<K | y^2 - t + 1>;
OOF := MaximalOrderFinite(KK);
OOI := MaximalOrderInfinite(KK);
I := Decomposition(OF, Numerator(t))[1];
while not test_vring(K, I, Random(OF, 3), Random(OF, 3)) do i +:= 1; end while;
while not test_vring(KK, Decomposition(OOF, I)[1], Random(OOF, 3), Random(OOF, 3)) do i +:= 1; end while;
I := Decomposition(OF, Numerator(t+1))[1];
while not test_vring(K, I, Random(OF, 3), Random(OF, 3)) do i +:= 1; end while;
while not test_vring(KK, Decomposition(OOF, I)[1], Random(OOF, 3), Random(OOF, 3)) do i +:= 1; end while;
I := Decomposition(OF, Numerator(t^2+t+1))[1];
while not test_vring(K, I, Random(OF, 3), Random(OF, 3)) do i +:= 1; end while;
while not test_vring(KK, Decomposition(OOF, I)[1], Random(OOF, 3), Random(OOF, 3)) do i +:= 1; end while;
I := Decomposition(OF, Numerator(t^3+t+1))[1];
while not test_vring(K, I, Random(OF, 3), Random(OF, 3)) do i +:= 1; end while;
while not test_vring(KK, Decomposition(OOF, I)[1], Random(OOF, 3), Random(OOF, 3)) do i +:= 1; end while;
I := Decomposition(OI, CoefficientRing(OI)!(1/t))[1];
while not test_vring(K, I, Random(OI, 3), Random(OI, 3)) do i +:= 1; end while;
while not test_vring(KK, Decomposition(OOI, I)[1], Random(OOI, 3), Random(OOI, 3)) do i +:= 1; end while;
for j in [1 .. 10] do
I := Decomposition(OF, RandomPrimePolynomial(Integers(F), j))[1];
while not test_vring(K, I, Random(OF, 3), Random(OF, 3)) do i +:= 1; end while;
while not test_vring(KK, Decomposition(OOF, I)[1], Random(OOF, 3), Random(OOF, 3)) do i +:= 1; end while;
end for;
