Skip to content
This repository was archived by the owner on Aug 18, 2020. It is now read-only.

Commit 2b5478b

Browse files
committed
[CO-347] Improvements on UtxoStatistics arbitrary instance
1 parent a05924a commit 2b5478b

File tree

1 file changed

+15
-16
lines changed

1 file changed

+15
-16
lines changed

wallet-new/src/Cardano/Wallet/Types/UtxoStatistics.hs

+15-16
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ import Data.Word (Word64)
2626
import Formatting (bprint, build, formatToString, (%))
2727
import qualified Formatting.Buildable
2828
import Serokell.Util (listJson)
29-
import Test.QuickCheck (Arbitrary (..), arbitrary, elements, suchThat)
29+
import Test.QuickCheck (Arbitrary (..), arbitrary, choose, elements,
30+
infiniteListOf, shuffle)
3031

3132
import Cardano.Wallet.API.V1.Swagger.Example (Example)
3233
import Pos.Infra.Util.LogSafe (BuildableSafeGen (..),
@@ -59,9 +60,9 @@ generateBounds bType =
5960

6061
instance Arbitrary HistogramBar where
6162
arbitrary = do
62-
possiblenames <- elements (NL.toList $ generateBounds Log10)
63-
bound <- arbitrary `suchThat` (>= 0)
64-
pure (HistogramBarCount possiblenames bound)
63+
upperBound <- elements (NL.toList $ generateBounds Log10)
64+
count <- arbitrary
65+
pure (HistogramBarCount upperBound count)
6566

6667

6768
deriveSafeBuildable ''HistogramBar
@@ -112,11 +113,11 @@ eitherToParser :: Buildable a => Either a b -> Parser b
112113
eitherToParser =
113114
either (fail . formatToString build) pure
114115

115-
toMap :: [HistogramBar] -> Map Word64 Word64
116-
toMap = Map.fromList . map (\(HistogramBarCount key val) -> (key,val))
116+
sorted :: [HistogramBar] -> [HistogramBar]
117+
sorted = sortOn (\(HistogramBarCount key _) -> key)
117118

118119
instance Eq UtxoStatistics where
119-
(UtxoStatistics h s) == (UtxoStatistics h' s') = s == s' && toMap h == toMap h'
120+
(UtxoStatistics h s) == (UtxoStatistics h' s') = s == s' && sorted h == sorted h'
120121

121122
instance ToJSON UtxoStatistics where
122123
toJSON (UtxoStatistics bars allStakes) =
@@ -206,16 +207,14 @@ instance ToSchema UtxoStatistics where
206207
)
207208

208209
instance Arbitrary UtxoStatistics where
209-
arbitrary = UtxoStatistics <$> arbitrary
210-
<*> arbitrary
211-
-- This code goes into nonstoping computation when checking swagger integration of WalletResponse UtxoStatistics
212-
{-- do
213-
histogram <- arbitrary
214-
let (minPossibleValue, maxPossibleValue) = getPossibleBounds histogram
215-
let histoBars = map (uncurry HistogramBarCount) $ Map.toList histogram
216-
allStakes <- arbitrary `suchThat` (\s -> s >= minPossibleValue && s <= maxPossibleValue)
210+
arbitrary = do
211+
upperBounds <- shuffle (NL.toList $ generateBounds Log10)
212+
counts <- infiniteListOf arbitrary
213+
let histogram = zip upperBounds counts
214+
let histoBars = map (uncurry HistogramBarCount) histogram
215+
allStakes <- choose (getPossibleBounds $ Map.fromList histogram)
217216
return $ UtxoStatistics histoBars allStakes
218-
--}
217+
219218
instance Buildable [HistogramBar] where
220219
build =
221220
bprint listJson

0 commit comments

Comments
 (0)