diff options
Diffstat (limited to '')
-rw-r--r-- | stdlib/source/test/aedifex.lux | 2 | ||||
-rw-r--r-- | stdlib/source/test/aedifex/hash.lux | 95 |
2 files changed, 97 insertions, 0 deletions
diff --git a/stdlib/source/test/aedifex.lux b/stdlib/source/test/aedifex.lux index 48ecc9189..c496eb88b 100644 --- a/stdlib/source/test/aedifex.lux +++ b/stdlib/source/test/aedifex.lux @@ -10,6 +10,7 @@ ["#." profile] ["#." project] ["#." cli] + ["#." hash] ["#." parser]]) (def: test @@ -19,6 +20,7 @@ /profile.test /project.test /cli.test + /hash.test /parser.test )) diff --git a/stdlib/source/test/aedifex/hash.lux b/stdlib/source/test/aedifex/hash.lux new file mode 100644 index 000000000..21e318be6 --- /dev/null +++ b/stdlib/source/test/aedifex/hash.lux @@ -0,0 +1,95 @@ +(.module: + [lux #* + ["_" test (#+ Test)] + [abstract + [monad (#+ do)] + {[0 #spec] + [/ + ["$." equivalence] + ["$." codec]]}] + [control + ["." try] + ["." exception]] + [data + ["." binary (#+ Binary)] + [number + ["n" nat]] + [text + ["%" format (#+ format)]]] + [math + ["." random (#+ Random)]]] + {#program + ["." /]} + [test + [lux + [data + ["_." binary]]]]) + +(def: (random hash) + (All [h] + (-> (-> Binary (/.Hash h)) + (Random (/.Hash h)))) + (do {@ random.monad} + [size (:: @ map (n.% 100) random.nat)] + (:: @ map hash (_binary.random size)))) + +(def: #export test + Test + (<| (_.covering /._) + (_.with-cover [/.Hash /.SHA-1 /.MD5]) + (`` ($_ _.and + (_.with-cover [/.equivalence] + ($_ _.and + ($equivalence.spec /.equivalence (..random /.sha1)) + ($equivalence.spec /.equivalence (..random /.md5)) + )) + (_.with-cover [/.data] + ($_ _.and + (~~ (template [<hash> <constructor> <exception>] + [(do random.monad + [expected (..random <hash>)] + (_.cover [<hash> <constructor> <exception>] + (and (case (<constructor> (/.data expected)) + (#try.Success actual) + (:: /.equivalence = expected actual) + + (#try.Failure error) + false) + (case (<constructor> (:: binary.monoid compose + (/.data expected) + (/.data expected))) + (#try.Success actual) + false + + (#try.Failure error) + (exception.match? <exception> error)))))] + + [/.sha1 /.as-sha1 /.not-a-sha1] + [/.md5 /.as-md5 /.not-a-md5] + )))) + (~~ (template [<codec> <hash>] + [(_.with-cover [<codec>] + ($codec.spec /.equivalence <codec> (..random <hash>)))] + + [/.sha1-codec /.sha1] + [/.md5-codec /.md5] + )) + (_.with-cover [/.not-a-hash] + ($_ _.and + (~~ (template [<codec> <hash>] + [(do random.monad + [expected (..random <hash>)] + (_.cover [<codec>] + (case (:: <codec> decode + (format (:: <codec> encode expected) + "AABBCC")) + (#try.Success actual) + false + + (#try.Failure error) + (exception.match? /.not-a-hash error))))] + + [/.sha1-codec /.sha1] + [/.md5-codec /.md5] + )))) + )))) |