unison/unison-src/transcripts-using-base/random-deserial.md

63 lines
1.6 KiB
Markdown
Raw Normal View History

```ucm:hide
.> builtins.mergeio
```
```unison
directory = "unison-src/transcripts-using-base/serialized-cases/"
availableCases : '{IO,Exception} [Text]
availableCases _ =
l = filter (contains ".ser") (directoryContents directory)
map (t -> Text.take (drop (Text.size t) 4) t) l
gen : Nat -> Nat -> (Nat, Nat)
gen seed k =
c = 1442695040888963407
a = 6364136223846793005
(mod seed k, a * seed + c)
shuffle : Nat -> [a] -> [a]
shuffle =
pick acc seed = cases
l | lteq (List.size l) 1 -> acc ++ l
| otherwise -> match gen seed (size l) with
(k, seed) -> match (take k l, drop k l) with
(pre, x +: post) -> pick (acc :+ x) seed (pre ++ post)
2023-01-13 18:16:07 +03:00
(pre, []) -> pick acc seed pre
pick []
runTestCase : Text ->{Exception,IO} (Text, Test.Result)
runTestCase name =
sfile = directory ++ name ++ ".ser"
lsfile = directory ++ name ++ ".lser"
ofile = directory ++ name ++ ".out"
hfile = directory ++ name ++ ".hash"
p@(f, i) = loadSelfContained sfile
pl@(fl, il) = loadSelfContained lsfile
o = fromUtf8 (readFile ofile)
h = readFile hfile
result =
if not (f i == o)
then Fail (name ++ " output mismatch")
else if not (toBase32 (crypto.hash Sha3_512 p) == h)
then Fail (name ++ " hash mismatch")
else if not (fl il == f i)
then Fail (name ++ " legacy mismatch")
else Ok name
(name, result)
serialTests : '{IO,Exception} [Test.Result]
serialTests = do
l = !availableCases
cs = shuffle (toRepresentation !systemTimeMicroseconds) l
List.map snd (bSort (List.map runTestCase cs))
```
```ucm
.> add
.> io.test serialTests
```