(.module: [library [lux #* ["_" test (#+ Test)] [abstract [monad (#+ do)] [\\specification ["$." equivalence] ["$." codec]]] [control ["." try] ["." exception]] [data ["." binary (#+ Binary)] [text ["%" format (#+ format)]]] [math ["." random (#+ Random)] [number ["n" nat]]]]] [\\program ["." /]] [test [lux [data ["_." binary]]]]) (def: .public (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: .public test Test (<| (_.covering /._) (_.for [/.Hash /.SHA-1 /.MD5]) (`` ($_ _.and (_.for [/.equivalence] ($_ _.and ($equivalence.spec /.equivalence (..random /.sha-1)) ($equivalence.spec /.equivalence (..random /.md5)) )) (_.for [/.data] ($_ _.and (~~ (template [ ] [(do random.monad [expected (..random )] (_.cover [ ] (and (case ( (/.data expected)) (#try.Success actual) (\ /.equivalence = expected actual) (#try.Failure error) false) (case ( (\ binary.monoid compose (/.data expected) (/.data expected))) (#try.Success actual) false (#try.Failure error) (exception.match? error)))))] [/.sha-1 /.as_sha-1 /.not_a_sha-1] [/.md5 /.as_md5 /.not_a_md5] )))) (~~ (template [ ] [(_.for [] ($codec.spec /.equivalence (..random )))] [/.sha-1_codec /.sha-1] [/.md5_codec /.md5] )) (_.for [/.not_a_hash] ($_ _.and (~~ (template [ ] [(do random.monad [expected (..random )] (_.cover [] (case (\ decode (format (\ encode expected) "AABBCC")) (#try.Success actual) false (#try.Failure error) (exception.match? /.not_a_hash error))))] [/.sha-1_codec /.sha-1] [/.md5_codec /.md5] )))) ))))