This is a playground for property-based testing. The exercises are meant to help you get to know a PBT framework and can be repeated with any language / framework combination. This repository currently contains solutions in:
To kick things off, let's begin with a simple integer comparison test. Implement the two functions (or use the one provided in a library):
min(Int, Int)which returns the smaller of two integers andmax(Int, Int)which returns the larger of two integers.
Verify the following properties:
- Given any integer
a,min(a, a)should returna. - Given any two integers
aandb,max(a, b)should equalmax(b, a). - Given any two integers
aandb, ifmin(a, b)returnsa, thenmax(a, b)should returnb.
Next, implement the holy grail of property-based testing examples: reversing a list. You can either implement your own reverse method, or use a library one.
Verify the following properties:
- Reversing a list with one element results in the same list.
- Reversing a list twice results in the original list.
- The length of a reversed list is the same as the length of the original list.
Try populating the generated lists with different types of elements for each property.
Choose two types of sort algorithms (e.g., merge sort and quick sort), and verify that they always produce the same output. You can implement one or both on your own, or use library implementations.
inspired by: https://johanneslink.net/property-based-testing-in-kotlin/#the-poker-domain
First, define a data structure for a deck of cards. A deck consists of 52 cards, each card having a suit (Spades, Diamonds, Clubs, Hearts) and a rank (2, 3, 4, 5, 6, 7, 8, 9, 10, Jack, Queen, King, Ace).
Next, define a deal(players: Int): [[Card]] function on this data structure which
- takes the number of players to whom cards are being dealt and
- returns a list of lists of cards, where each list of cards represents the 5 cards dealt to a player.
Verify the following properties:
- A deck has 52 cards.
- A deck has 52 unique cards.
- All 52 cards are in the deck (this verifies the same as above but with different semantics).
- Dealing to
Xplayers where1 <= X <= 10results inXlists of 5 cards. - Dealing to
Xplayers whereX < 1results in an error. - Dealing to
Xplayers whereX > 10results in an error. - Dealing to
Xplayers leaves52 - 5Xcards in the deck. - Each hand has 5 cards.
- Each hand has 5 unique cards.
- No hands share cards.