Show input variables in tests

This commit is contained in:
Manuel Thalmann 2024-06-16 21:36:38 +02:00
parent 6d53c63bdf
commit 326ab69152

View file

@ -2,10 +2,11 @@ import Data.Bits
import Data.Char import Data.Char
import Data.Functor.Contravariant import Data.Functor.Contravariant
data Result t = data Result i o =
Result { Result {
actual :: t, input :: i,
expected :: t, actual :: o,
expected :: o,
result :: Bool result :: Bool
} deriving (Show, Eq, Ord) } deriving (Show, Eq, Ord)
@ -17,28 +18,30 @@ newtype Boxed a = Boxed {unbox :: a}
instance Functor Boxed where instance Functor Boxed where
fmap f (Boxed a) = Boxed (f a) fmap f (Boxed a) = Boxed (f a)
test11 = Result actual expected (actual == expected) test11 = Result x actual expected (actual == expected)
where where
x = 1
actual = unbox (fmap id b) actual = unbox (fmap id b)
expected = unbox b expected = unbox b
b = Boxed { unbox = 1 :: Integer } b = Boxed { unbox = x :: Integer }
-- >>> test11 -- >>> test11
-- Result {actual = 1, expected = 1, result = True} -- Result {input = 1, actual = 1, expected = 1, result = True}
-- --
test12 = Result actual expected (actual == expected) test12 = Result x actual expected (actual == expected)
where where
x = 1
actual = unbox (fmap (f . g) b) actual = unbox (fmap (f . g) b)
expected = unbox (fmap f $ fmap g b) expected = unbox (fmap f $ fmap g b)
b = Boxed { unbox = 1 :: Integer } b = Boxed { unbox = x :: Integer }
f = (+9) f = (+9)
g = (*11) g = (*11)
-- >>> test12 -- >>> test12
-- Result {actual = 20, expected = 20, result = True} -- Result {input = 1, actual = 20, expected = 20, result = True}
-- --
-------------------- --------------------
@ -49,30 +52,30 @@ newtype FromInt b = FromInt {fun :: Int -> b}
instance Functor FromInt where instance Functor FromInt where
fmap f (FromInt b) = FromInt (\ x -> f (b x)) fmap f (FromInt b) = FromInt (\ x -> f (b x))
test21 = Result actual expected (actual == expected) test21 = Result x actual expected (actual == expected)
where where
x = 2
actual = ((fun (fmap id converter)) x) actual = ((fun (fmap id converter)) x)
expected = (fun (converter) x) expected = (fun (converter) x)
converter = FromInt {fun = \ val -> val * (-1)} converter = FromInt {fun = \ val -> val * (-1)}
x = 2
-- >>> test21 -- >>> test21
-- Result {actual = -2, expected = -2, result = True} -- Result {input = 2, actual = -2, expected = -2, result = True}
-- --
test22 = Result actual expected (actual == expected) test22 = Result x actual expected (actual == expected)
where where
x = 7
actual = (fun (fmap g $ fmap f converter)) x actual = (fun (fmap g $ fmap f converter)) x
expected = (fun ((g . f) <$> converter)) x expected = (fun ((g . f) <$> converter)) x
converter = FromInt {fun = \ val -> chr ((ord 'A') + val)} converter = FromInt {fun = \ val -> chr ((ord 'A') + val)}
f = \ char -> char:"B" f = \ char -> char:"B"
g = \ str -> str ++ "C" g = \ str -> str ++ "C"
x = 7
-- >>> test22 -- >>> test22
-- Result {actual = "HBC", expected = "HBC", result = True} -- Result {input = 7, actual = "HBC", expected = "HBC", result = True}
-- --
-------------------- --------------------
@ -90,8 +93,8 @@ strHash value = realHash 0 value
realHash acc (char:str) = realHash (acc + ((ord char) .&. 0b11010110)) str realHash acc (char:str) = realHash (acc + ((ord char) .&. 0b11010110)) str
test3 = [ test3 = [
(Result strExpected strActual (strExpected == strActual)), (Result myStr strExpected strActual (strExpected == strActual)),
(Result expected actual (expected == actual)) (Result [myChar] expected actual (expected == actual))
] ]
where where
-- Create object `toInt` for converting strings to an Int hash (using the "strHash" function) -- Create object `toInt` for converting strings to an Int hash (using the "strHash" function)
@ -119,6 +122,6 @@ test3 = [
-- Showing the result of the `String` and the `Char` test -- Showing the result of the `String` and the `Char` test
-- >>> mapM_ putStrLn (map show test3) -- >>> mapM_ putStrLn (map show test3)
-- Result {actual = 712, expected = 712, result = True} -- Result {input = "Hello World", actual = 712, expected = 712, result = True}
-- Result {actual = 86, expected = 86, result = True} -- Result {input = "V", actual = 86, expected = 86, result = True}
-- --