aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/test/aedifex/hash.lux
blob: a620a0f4875119aa1cacc307d435a3ad78628ca0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
... This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
... If a copy of the MPL was not distributed with this file, You can obtain one at https://mozilla.org/MPL/2.0/.

(.require
 [library
  [lux (.except)
   [abstract
    [monad (.only do)]
    ["[0]" equivalence
     ["[1]T" \\test]]
    ["[0]" codec
     ["[1]T" \\test]]]
   [control
    ["[0]" try]
    ["[0]" exception]]
   [data
    ["[0]" binary (.only Binary)]
    [text
     ["%" \\format (.only format)]]]
   [math
    ["[0]" random (.only Random)]
    [number
     ["n" nat]]]
   [test
    ["_" property (.only Test)]]]]
 [\\program
  ["[0]" /]]
 [test
  [lux
   [data
    ["_[0]" binary]]]])

(def .public (random hash)
  (All (_ h)
    (-> (-> Binary (/.Hash h))
        (Random (/.Hash h))))
  (do [! random.monad]
    [size (of ! each (n.% 100) random.nat)]
    (of ! each hash (_binary.random size))))

(def .public test
  Test
  (<| (_.covering /._)
      (_.for [/.Hash /.SHA1 /.MD5])
      (`` (all _.and
               (_.for [/.equivalence]
                      (all _.and
                           (equivalenceT.spec /.equivalence (..random /.sha1))
                           (equivalenceT.spec /.equivalence (..random /.md5))
                           ))
               (_.for [/.data]
                      (all _.and
                           (,, (with_template [<hash> <constructor> <exception>]
                                 [(do random.monad
                                    [expected (..random <hash>)]
                                    (_.coverage [<hash> <constructor> <exception>]
                                      (and (when (<constructor> (/.data expected))
                                             {try.#Success actual}
                                             (of /.equivalence = expected actual)

                                             {try.#Failure error}
                                             false)
                                           (when (<constructor> (of binary.monoid composite
                                                                    (/.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]
                                 ))))
               (,, (with_template [<codec> <hash>]
                     [(_.for [<codec>]
                             (codecT.spec /.equivalence <codec> (..random <hash>)))]
                     
                     [/.sha1_codec /.sha1]
                     [/.md5_codec /.md5]
                     ))
               (_.for [/.not_a_hash]
                      (all _.and
                           (,, (with_template [<codec> <hash>]
                                 [(do random.monad
                                    [expected (..random <hash>)]
                                    (_.coverage [<codec>]
                                      (when (of <codec> decoded
                                                (format (of <codec> encoded expected)
                                                        "AABBCC"))
                                        {try.#Success actual}
                                        false

                                        {try.#Failure error}
                                        (exception.match? /.not_a_hash error))))]

                                 [/.sha1_codec /.sha1]
                                 [/.md5_codec /.md5]
                                 ))))
               ))))