aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/test/lux/data/collection/row.lux
blob: 2eb342e6eaf9bd8a6860c77847d1f81cc04ea39d (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
(.module:
  [lux #*
   [control
    [monad (#+ Monad do)]]
   [data
    ["." number]
    ["." maybe]
    [collection
     ["&" row]
     [list ("list/." fold)]]]
   [math
    ["r" random]]]
  lux/test)

(context: "Rows"
  (<| (times 100)
      (do @
        [size (|> r.nat (:: @ map (|>> (n/% 100) (n/max 1))))
         idx (|> r.nat (:: @ map (n/% size)))
         sample (r.row size r.nat)
         other-sample (r.row size r.nat)
         non-member (|> r.nat (r.filter (|>> (&.member? number.equivalence sample) not)))
         #let [(^open "&/.") (&.equivalence number.equivalence)
               (^open "&/.") &.apply
               (^open "&/.") &.monad
               (^open "&/.") &.fold
               (^open "&/.") &.monoid]]
        ($_ seq
            (test "Can query size of row."
                  (if (&.empty? sample)
                    (and (n/= 0 size)
                         (n/= 0 (&.size sample)))
                    (n/= size (&.size sample))))

            (test "Can add and remove elements to rows."
                  (and (n/= (inc size) (&.size (&.add non-member sample)))
                       (n/= (dec size) (&.size (&.pop sample)))))

            (test "Can put and get elements into rows."
                  (|> sample
                      (&.put idx non-member)
                      (&.nth idx)
                      maybe.assume
                      (is? non-member)))

            (test "Can update elements of rows."
                  (|> sample
                      (&.put idx non-member) (&.update idx inc)
                      (&.nth idx) maybe.assume
                      (n/= (inc non-member))))

            (test "Can safely transform to/from lists."
                  (|> sample &.to-list &.from-list (&/= sample)))

            (test "Can identify members of a row."
                  (and (not (&.member? number.equivalence sample non-member))
                       (&.member? number.equivalence (&.add non-member sample) non-member)))

            (test "Can fold over elements of row."
                  (n/= (list/fold n/+ 0 (&.to-list sample))
                       (&/fold n/+ 0 sample)))
            
            (test "Functor goes over every element."
                  (let [there (&/map inc sample)
                        back-again (&/map dec there)]
                    (and (not (&/= sample there))
                         (&/= sample back-again))))

            (test "Apply allows you to create singleton rows, and apply rows of functions to rows of values."
                  (and (&/= (&.row non-member) (&/wrap non-member))
                       (&/= (&/map inc sample)  (&/apply (&/wrap inc) sample))))

            (test "Row concatenation is a monad."
                  (&/= (&/compose sample other-sample)
                       (&/join (&.row sample other-sample))))

            (test "Can reverse."
                  (and (not (&/= sample
                                 (&.reverse sample)))
                       (not (&/= sample
                                 (&.reverse (&.reverse sample))))))
            ))))