Skip to content

Homework 12: PBT in a language of your choosing

Due: Monday, April 20th, 11:59pm

It turns out that PBT is so effective, that there is an implementation of a PBT in almost every language (even C!). In this assignment, you will explore property-based testing in a language of your choice.

Start with the template code provided for one of the following languages:

When implementing, keep the following in mind:

  • Replace each function with a TODO comment / error with your implementation.
  • Be sure to test your solutions alongside your submission (with unit tests). Ensure your submission works locally before uploading to Gradescope.
  • Submit your version of hw12.??? (where .??? is the extension of your choosing.
  • In each template file there will be a link to the documentation for the PBT framework in your language of choice.
  • At the top of the template file, there is a comment for you to write in approximately how many hours you spent on the assignment. Filling this in is optional, but helpful feedback for the instructors.

1. Warm-up.

Test out your property-based testing library by implementing the following functionality in your language, writing property-based tests, and running them:

1.1. Show that sorting a collection (array, list, etc) does not change the size of the collection.

1.2. Write code that adds an element to a collection (array, list, etc), if it doesn’t already exist in it, and show that it does not decrease the size of the collection.

Q1: Grading Criteria

(10 points, autograded) Correctness of properties

2. the JSON round-trip

In this problem, we want you to use an existing JSON serialization/deserialization library (you can also implement one by hand, of course, but that's not our intention).

2.1. First, you need to define a data structure (in your programming language) that represents a map from student ids (as strings) to structures representing students (with a name, list of interests, and address). This data structure is part of a hypothetical application for matching students with people who live near them and have similar interests, the rest of the application will not be implemented!

2.2. Now, you should write code that turns that data structure into JSON (using whatever functionality is available in your language’s JSON library), and separate code that does the reverse. This would be used to either store the data, or send it over the network.

3.3. Now, write property-based tests that show that for random examples of your data structure, the serialization and deserialization functions are inverses of each other; i.e., that deserialize(serialize(x)) = x for all x.

Q2: Grading Criteria

(5 points, autograded) Correctness of the JSON roundtrip property

3. (Un)packing Colors with sensible errors

3.1. In this problem, first define a native data type for a color, which should be able to represent colors in the following three ways:

  • Named colors (Red, Green, Blue, etc: how many is up to you, but a finite number)
  • RGB colors (three numbers, 0 to 255, that represent the amount of Red, Green, or Blue in the color)
  • CMYK (four numbers, 0 to 255, that represent how much Cyan, Magenta, Yellow, and Black, in the color)

3.2. Now, write code that turns your color into a "packed" representation as an array (or list) of numbers. You are welcome to choose any representation as you like, but we'd suggest using the first number as a "tag" that indicates which possibility you are in.

3.3. Next, write code that takes a "packed" representation as an array (or list) of numbers, and convert it back into the native color data structure.

3.4. Finally, write property based tests that show that for random examples of your color, the "unpacking" and "packing" functions are inverses of each other; i.e., that unpack(pack(x)) = x for all x.

3.5. You should also show that for any array (or list) of numbers, the "unpacking" function either gives a sensible error, or returns a color that, when "packed", gives the same array (or list) of numbers.

Q3: Grading Criteria

(10 points, autograded) Correctness of both roundtrip properties