aboutsummaryrefslogtreecommitdiff
Testing in Haskell

Testing in Haskell

1. Task

Create a project in stack. In the stack project, there is a tests directory, which contains the file Spec.hs, where you can also write main to display the test results. You need to test two functions:

  1. isCongruent :: Int -> Int -> Int -> Bool
  2. zipWithSum :: [Int] -> [Int] -> [Int]

1.1. isCongruent :: Int -> Int -> Int -> Bool Int -> Bool

The modulo equality check function isCongruent :: Int -> Int -> Int -> Bool Int -> Bool checks whether two numbers are congruent modulo a third number.

Using QuickCheck, verify the following properties:

  1. If two numbers are congruent modulo, then their difference is divisible by the modulus:
    • isCongruent a b m == (modulus (a - b) m == 0).
  2. Equality modulo is symmetric:
    • isCongruent a b m == isCongruent b a m.
  3. If one number is equal to another, then they are equal modulo any value:
    • if a = b, then isCongruent a b m == True.

1.2. zipWithSum :: [Int] -> [Int] -> [Int]

The function zipWithSum :: [Int] -> [Int] -> [Int] takes two lists of integers and returns a new list where each element is the sum of the corresponding elements of the two lists. If the list lengths differ, the remaining elements of the longer list should be added to the end of the result.

Using QuickCheck, verify the following properties:

  1. The length of the result is equal to the maximum length of the input lists:
    • length (zipWithSum xs ys) == max (length xs) (length ys).
  2. If one of the lists is empty, then the result is the same as the non‑empty list:
    • if xs is empty, then zipWithSum [] ys == ys.
  3. The sum of the elements of the result is equal to the sum of all elements of the input lists:
    • sum (zipWithSum xs ys) == sum xs + sum ys.

2. Documentation

The exported report can be found at this link.

3. Unlicense

This is free and unencumbered software released into the public domain.

Anyone is free to copy, modify, publish, use, compile, sell, or
distribute this software, either in source code form or as a compiled
binary, for any purpose, commercial or non-commercial, and by any
means.

In jurisdictions that recognize copyright laws, the author or authors
of this software dedicate any and all copyright interest in the
software to the public domain. We make this dedication for the benefit
of the public at large and to the detriment of our heirs and
successors. We intend this dedication to be an overt act of
relinquishment in perpetuity of all present and future rights to this
software under copyright law.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.

For more information, please refer to https://unlicense.org/

Date: 2024H2

Author: Martin Michalec

Created: 2026-11-08 Sun 07:29