-
Notifications
You must be signed in to change notification settings - Fork 99
/
PartiallyOrderedSetLaws.scala
135 lines (103 loc) · 5.21 KB
/
PartiallyOrderedSetLaws.scala
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/*
* Copyright (c) 2015 Typelevel
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package cats.collections.laws
import cats.Order
import cats.collections.PartiallyOrderedSet
import cats.laws.{IsEq, IsEqArrow, UnorderedFoldableLaws}
import cats.kernel.CommutativeMonoid
import cats.implicits._
/**
* We test all default methods against this proxy so overrides can never change semantics
*/
class MinimalPartialOrderedSetProxy[F[_]](pos: PartiallyOrderedSet[F]) extends PartiallyOrderedSet[F] {
def unorderedFoldMap[A, B: CommutativeMonoid](fa: F[A])(fn: A => B): B =
pos.unorderedFoldMap(fa)(fn)
def add[A](fa: F[A], a: A)(implicit order: Order[A]): F[A] =
pos.add(fa, a)
def contains[A: Order](fa: F[A], a: A): Boolean =
pos.contains(fa, a)
def empty[A]: F[A] = pos.empty
def minimumOption[A](fa: F[A]): Option[A] =
pos.minimumOption(fa)
def removeMin[A](fa: F[A])(implicit order: Order[A]): F[A] =
pos.removeMin(fa)
def singleton[A](a: A): F[A] =
pos.singleton(a)
}
trait PartiallyOrderedSetLaws[F[_]] extends UnorderedFoldableLaws[F] {
implicit override def F: PartiallyOrderedSet[F]
private def proxy: PartiallyOrderedSet[F] = new MinimalPartialOrderedSetProxy(F)
// This should be on UnorderdFoldable but was omitted
def sizeMatchesUnorderedFoldMap[A](fa: F[A]): Boolean =
F.size(fa) == F.unorderedFoldMap(fa)(_ => 1L)
def addEmptyIsSingleton[A: Order](a: A): IsEq[F[A]] =
F.add(F.empty[A], a) <-> F.singleton(a)
def addMonoidConsistency[A: Order, B: CommutativeMonoid](fa: F[A], a: A, fn: A => B): IsEq[B] =
F.unorderedFoldMap(F.add(fa, a))(fn) <-> (CommutativeMonoid[B].combine(F.unorderedFoldMap(fa)(fn), fn(a)))
def emptyIsEmpty[A: CommutativeMonoid]: IsEq[A] =
F.unorderedFold(F.empty[A]) <-> CommutativeMonoid[A].empty
def addIncreasesSize[A: Order](fa: F[A], a: A): Boolean =
F.size(fa) == (F.size(F.add(fa, a)) - 1L)
def containsMatchesToList[A: Order](fa: F[A], a: A): Boolean =
F.contains(fa, a) == F.toSortedList(fa).exists { aa => Order[A].eqv(aa, a) }
def removeDecreasesSize[A: Order](fa: F[A]): Boolean =
F.isEmpty(fa) || (F.size(fa) == (F.size(F.removeMin(fa)) + 1L))
def minimumOptionIsTheMinimum[A: Order](fa: F[A]): Boolean = {
val mo = F.minimumOption(fa)
val listMo = F.toSortedList(fa).headOption
Order[Option[A]].eqv(mo, listMo)
}
def removeMinNeverDecreasesMin[A: Order](fa: F[A]): Boolean = {
val m1 = F.minimumOption(fa)
val fa2 = F.removeMin(fa)
val m2 = F.minimumOption(fa2)
(m1, m2) match {
case (None, None) => F.isEmpty(fa) && F.isEmpty(fa2)
case (None, Some(_)) => false // this should never happen
case (Some(_), None) => F.isEmpty(fa2)
case (Some(a), Some(b)) => Order[A].lteqv(a, b)
}
}
/*
* The rest of these test that semantics of methods didn't change from overrides
*/
def addAllListSameAsSort[A: Order](ls: List[A]): IsEq[List[A]] =
ls.sorted <-> F.toSortedList(F.build(ls))
def addAllMatchesProxy[A: Order](fa: F[A], items: Iterable[A]): IsEq[F[A]] =
F.addAll(fa, items) <-> proxy.addAll(fa, items)
def buildMatchesProxy[A: Order](items: Iterable[A]): IsEq[F[A]] =
F.build(items) <-> proxy.build(items)
def sortedFoldLeftMatchesProxy[A: Order, B](fa: F[A], init: B, fn: (B, A) => B): IsEq[B] =
F.sortedFoldLeft(fa, init)(fn) <-> proxy.sortedFoldLeft(fa, init)(fn)
def takeLargestMatchesProxy[A: Order](fa: F[A], maxSize: Long): IsEq[F[A]] =
F.takeLargest(fa, maxSize) <-> proxy.takeLargest(fa, maxSize)
def addIfLargerMatchesProxy[A: Order](fa: F[A], maxSize: Long, item: A): IsEq[F[A]] =
F.addIfLarger(fa, maxSize, item) <-> proxy.addIfLarger(fa, maxSize, item)
def toSortedListMatchesProxy[A: Order](fa: F[A]): IsEq[List[A]] =
F.toSortedList(fa) <-> proxy.toSortedList(fa)
def addAllLargestMatchesProxy[A: Order](fa: F[A], maxSize: Long, items: Iterable[A]): IsEq[F[A]] =
F.addAllLargest(fa, maxSize, items) <-> proxy.addAllLargest(fa, maxSize, items)
@deprecated("Use popMatchesProxy instead", "0.9.4")
def unaddMatchesProxy[A: Order](fa: F[A]): IsEq[Option[(A, F[A])]] =
F.pop(fa) <-> proxy.pop(fa)
def popMatchesProxy[A: Order](fa: F[A]): IsEq[Option[(A, F[A])]] =
F.pop(fa) <-> proxy.pop(fa)
}