lama_byterun/stdlib/regression/test30.lama

124 lines
2.8 KiB
Text
Raw Normal View History

2020-08-02 23:56:21 +03:00
import Collection;
import Ref;
import Random;
import Array;
import Fun;
2020-08-06 14:56:41 +03:00
import Data;
2020-08-02 23:56:21 +03:00
2020-08-04 15:11:14 +03:00
fun genCyclicArrays (n, eq, cross) {
var f = ref (true);
2020-08-04 15:11:14 +03:00
2020-08-02 23:56:21 +03:00
fun genrec (n, stacka, stackb, depth) {
fun peek (k, stack) {
case stack of
[x, prev] -> if k == 0 then x else peek (k-1, prev) fi
esac
}
if n == 1
then
case if random (2)
then randomString (16)
else randomInt ()
fi of
x -> [x, clone (x)]
esac
else
var a = split (n, random (n+1)),
2020-08-02 23:56:21 +03:00
b = mapArray (id, a),
index = initArray (random (a.length + 1), fun (_) {random (a.length)});
fun shared (i) {
var found = false;
2020-08-02 23:56:21 +03:00
for var j=0;, j < index.length && 1 - found, j := j + 1
2020-08-02 23:56:21 +03:00
do
found := i == index[j]
od;
found
}
for var i=0;, i < a.length, i := i + 1
2020-08-02 23:56:21 +03:00
do
if shared (i)
then
if depth == 0
then
a[i] := a;
2020-08-04 15:11:14 +03:00
b[i] := if cross then a else b fi
2020-08-02 23:56:21 +03:00
else
case random (depth) of
r -> a [i] := peek (r, stacka);
2020-08-04 15:11:14 +03:00
b [i] := if cross then a[i] else peek (r, stackb) fi
2020-08-02 23:56:21 +03:00
esac
2020-08-04 15:11:14 +03:00
fi;
if 1 - eq && deref (f) then b[i] := 0; f ::= true fi
2020-08-02 23:56:21 +03:00
else
case genrec (a[i], [a, stacka], [b, stackb], depth + 1) of
[ai, bi] -> a [i] := ai;
2020-08-04 15:11:14 +03:00
b [i] := bi;
if 1 - eq && deref (f) then
case b[i] of
2021-01-31 21:07:17 +03:00
#val -> b[i] := b[i] + 1
| _ -> b[i] := 0
2020-08-04 15:11:14 +03:00
esac;
f ::= true
fi
2020-08-02 23:56:21 +03:00
esac
fi
od;
[a, b]
fi
}
genrec (n, [], [], 0)
}
2020-08-06 14:56:41 +03:00
fun normalize (x) {
if x < 0 then -1
elif x > 0 then 1
else 0
fi
}
fun not (x) {0 - x}
for var i=0;, i<25, i:=i+1
2020-08-04 15:11:14 +03:00
do
case genCyclicArrays (1000, true, false) of
2020-08-06 14:56:41 +03:00
[a, b] ->
printf ("%d\n", (a =?= a) == 0);
printf ("%d\n", (a =?= b) == 0)
2020-08-04 15:11:14 +03:00
esac
od;
for var i=0;, i<25, i:=i+1
2020-08-04 15:11:14 +03:00
do
case genCyclicArrays (1000, true, true) of
2020-08-06 14:56:41 +03:00
[a, b] ->
printf ("%d\n", (a =?= a) == 0);
printf ("%d\n", (a =?= b) == 0)
2020-08-04 15:11:14 +03:00
esac
od;
for var i=0;, i<25, i:=i+1
2020-08-04 15:11:14 +03:00
do
case genCyclicArrays (1000, false, false) of
[a, b] -> var x = normalize (a =?= b);
2020-08-06 14:56:41 +03:00
printf ("%d\n", x != 0);
printf ("%d\n", not (x) == normalize (b =?= a))
2020-08-04 15:11:14 +03:00
esac
od;
for var i=0;, i<25, i:=i+1
2020-08-02 23:56:21 +03:00
do
2020-08-04 15:11:14 +03:00
case genCyclicArrays (1000, false, true) of
[a, b] -> var x = normalize (a =?= b);
2020-08-06 14:56:41 +03:00
printf ("%d\n", x != 0);
printf ("%d\n", not (x) == normalize (b =?= a))
2020-08-02 23:56:21 +03:00
esac
od