(.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 [ ] [(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)))))] [/.sha1 /.as-sha1 /.not-a-sha1] [/.md5 /.as-md5 /.not-a-md5] )))) (~~ (template [ ] [(_.with-cover [] ($codec.spec /.equivalence (..random )))] [/.sha1-codec /.sha1] [/.md5-codec /.md5] )) (_.with-cover [/.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))))] [/.sha1-codec /.sha1] [/.md5-codec /.md5] )))) ))))