module Test.Ouroboros.Network.PeerSelection.KnownPeers (tests) where
import Data.Map (Map)
import Ouroboros.Network.PeerSelection.LedgerPeers (RelayAccessPoint)
import Ouroboros.Network.PeerSelection.PeerAdvertise (PeerAdvertise)
import Ouroboros.Network.PeerSelection.PeerSharing (PeerSharing)
import Ouroboros.Network.PeerSelection.State.KnownPeers qualified as KnownPeers
import Test.Ouroboros.Network.PeerSelection.Instances ()
import Test.QuickCheck (Property, counterexample)
import Test.Tasty (TestTree, testGroup)
import Test.Tasty.QuickCheck (testProperty)

tests :: TestTree
tests :: TestTree
tests =
  TestName -> [TestTree] -> TestTree
testGroup TestName
"Ouroboros.Network.PeerSelection"
  [ TestName -> [TestTree] -> TestTree
testGroup TestName
"KnownPeers"
    [ TestName
-> (Map RelayAccessPoint (Maybe PeerSharing, Maybe PeerAdvertise)
    -> Property)
-> TestTree
forall a. Testable a => TestName -> a -> TestTree
testProperty TestName
"insert is idempotent" Map RelayAccessPoint (Maybe PeerSharing, Maybe PeerAdvertise)
-> Property
prop_insert_idempotent
    ]
  ]

prop_insert_idempotent
  :: Map RelayAccessPoint (Maybe PeerSharing, Maybe PeerAdvertise)
  -> Property
prop_insert_idempotent :: Map RelayAccessPoint (Maybe PeerSharing, Maybe PeerAdvertise)
-> Property
prop_insert_idempotent Map RelayAccessPoint (Maybe PeerSharing, Maybe PeerAdvertise)
m =
  let knownPeers :: KnownPeers RelayAccessPoint
knownPeers = Map RelayAccessPoint (Maybe PeerSharing, Maybe PeerAdvertise)
-> KnownPeers RelayAccessPoint -> KnownPeers RelayAccessPoint
forall peeraddr.
Ord peeraddr =>
Map peeraddr (Maybe PeerSharing, Maybe PeerAdvertise)
-> KnownPeers peeraddr -> KnownPeers peeraddr
KnownPeers.insert Map RelayAccessPoint (Maybe PeerSharing, Maybe PeerAdvertise)
m KnownPeers RelayAccessPoint
forall peeraddr. KnownPeers peeraddr
KnownPeers.empty
      knownPeers' :: KnownPeers RelayAccessPoint
knownPeers' = Map RelayAccessPoint (Maybe PeerSharing, Maybe PeerAdvertise)
-> KnownPeers RelayAccessPoint -> KnownPeers RelayAccessPoint
forall peeraddr.
Ord peeraddr =>
Map peeraddr (Maybe PeerSharing, Maybe PeerAdvertise)
-> KnownPeers peeraddr -> KnownPeers peeraddr
KnownPeers.insert Map RelayAccessPoint (Maybe PeerSharing, Maybe PeerAdvertise)
m KnownPeers RelayAccessPoint
knownPeers
   in TestName -> Bool -> Property
forall prop. Testable prop => TestName -> prop -> Property
counterexample (TestName
"KnownPeers 1: " TestName -> TestName -> TestName
forall a. [a] -> [a] -> [a]
++ KnownPeers RelayAccessPoint -> TestName
forall a. Show a => a -> TestName
show KnownPeers RelayAccessPoint
knownPeers TestName -> TestName -> TestName
forall a. [a] -> [a] -> [a]
++ TestName
"\n"
                    TestName -> TestName -> TestName
forall a. [a] -> [a] -> [a]
++ TestName
"KnownPeers 2: " TestName -> TestName -> TestName
forall a. [a] -> [a] -> [a]
++ KnownPeers RelayAccessPoint -> TestName
forall a. Show a => a -> TestName
show KnownPeers RelayAccessPoint
knownPeers') (Bool -> Property) -> Bool -> Property
forall a b. (a -> b) -> a -> b
$
      KnownPeers RelayAccessPoint
knownPeers KnownPeers RelayAccessPoint -> KnownPeers RelayAccessPoint -> Bool
forall a. Eq a => a -> a -> Bool
== KnownPeers RelayAccessPoint
knownPeers'