aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/test/aedifex
diff options
context:
space:
mode:
authorEduardo Julian2020-10-15 01:01:21 -0400
committerEduardo Julian2020-10-15 01:01:21 -0400
commitc006a5fe8e82f6fc7c8cdb9db0f44c06d229f34e (patch)
treef00af06ceb7cd77ab53aa214abb2e7383dc87500 /stdlib/source/test/aedifex
parent440608bc32916329c9f3c0f2bd9a8d1152ed5da8 (diff)
Re-named "to-text" functions to "format".
Diffstat (limited to 'stdlib/source/test/aedifex')
-rw-r--r--stdlib/source/test/aedifex/hash.lux95
1 files changed, 95 insertions, 0 deletions
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]
+ ))))
+ ))))