by Aaron Bedra
This chapter will dive into a number of programming techniques at once. The goal is to explain an advanced form of testing that can be very useful. We’re going to use a higher-level language, Haskell, to test some legacy C code. We do this because Haskell (or any more expressive language) takes less code to express the essence, or intended functionality, of a program. We will reimplement the code already built in our legacy codebase to determine if it is functioning as intended.
If you want to follow along with this chapter, you’ll need a few tools installed. You’ll need a C compiler capable of compiling C11 standard code. This can be a current GCC or Clang. You’ll also need a Haskell environment. There are a few different ways to set up your Haskell environment. If you already have Glasgow Haskell Compiler (GHC), GHCi, and Cabal installed, you can continue to use your setup. If you’re installing Haskell for the first time, stack[26] is highly recommended. We’ll be using stack in this chapter, so if you wish to follow along exactly, you’ll need to install it.
We’ll start by making sure we have the necessary tools.
| $ stack setup |
| ... GHC will be installed ... |
| $ stack install QuickCheck |
| ... QuickCheck will be installed ... |
| $ stack exec ghci |
| Prelude> import Test.QuickCheck |
| Prelude Test.QuickCheck> :q |
This ensures that you have GHC, installed, along with the QuickCheck library. If for some reason this doesn’t work for you, consult the stack documentation.