Skip to content

Commit fd1a6dd

Browse files
committed
some editing, update profiling, use Hspec
1 parent dcd832b commit fd1a6dd

File tree

6 files changed

+28
-46
lines changed

6 files changed

+28
-46
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,4 @@ cabal-dev
2626
cabal.sandbox.config
2727
dist
2828
dist-*
29+
report.cabal

Howto.tex

+3-9
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,9 @@
11

22
\section{How to use this?}
33

4-
To generate the PDF, open the report.tex in your favorite LaTeX editor and
5-
hit compile, or manually do this:
6-
7-
\begin{verbatim}
8-
pdflatex report
9-
bibtex report
10-
pdflatex report
11-
pdflatex report
12-
\end{verbatim}
4+
To generate the PDF, open \texttt{report.tex} in your favorite \LaTeX editor and compile.
5+
Alternatively, you can manually do
6+
\texttt{pdflatex report; bibtex report; pdflatex report; pdflatex report} in a terminal.
137

148
You should have stack installed (see \url{https://haskellstack.org/}) and
159
open a terminal in the same folder.

Profiling.tex

+5-11
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,17 @@
11

2-
\section{Profiling}
3-
\label{sec:Profiling}
2+
\section{Optional: Profiling}\label{sec:Profiling}
43

54
The GHC compiler comes with a profiling system to keep track of which
6-
functions Are executed how often and how much time and memory they take.
5+
functions are executed how often and how much time and memory they take.
76
To activate this RTS, we compile and execute our program as follows:
87

98
\begin{verbatim}
109
stack clean
11-
stack build --executable-profiling --library-profiling \
12-
--ghc-options="-fprof-auto -rtsopts"
13-
stack exec -- myprogram +RTS -p -h
10+
stack build --profile
11+
stack exec --profile myprogram -- +RTS -p
1412
\end{verbatim}
1513

16-
The results are saved in the file \texttt{myprogram.prof} which looks like this.
14+
Results are saved in the file \texttt{myprogram.prof} which looks as follows.
1715
Note for example, that funnyfunction was called on 14 entries.
1816

1917
\begin{small}
@@ -32,7 +30,3 @@ \section{Profiling}
3230

3331
For many more RTS options, see the GHC documentation online at
3432
\url{https://downloads.haskell.org/~ghc/latest/docs/html/users_guide/profiling.html}.
35-
36-
To speed up our program, especially in GHCi, we can also add the
37-
parameter \texttt{-fobject-code} to the \texttt{ghc-options} field
38-
in the \texttt{.cabal} file.

report.pdf

-24.3 KB
Binary file not shown.

stack.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
resolver: lts-18.18
1+
resolver: lts-18.21

test/simpletests.lhs

+18-25
Original file line numberDiff line numberDiff line change
@@ -10,36 +10,29 @@ module Main where
1010
1111
import Basics
1212
13-
import Text.Printf (printf)
13+
import Test.Hspec
1414
import Test.QuickCheck
15-
import Test.QuickCheck.Test (isSuccess)
16-
import System.Exit (exitSuccess, exitFailure)
1715
\end{code}
1816

19-
The tuples in the following list consist of a name for the test and a
20-
quickCheckResult applied to a property. For example, we check that the output
21-
of funnyfunction is one of three possibilities. The second test checks that
22-
reversing twice gives the same string back.
23-
24-
% TODO: add tests using thenumbers, somenumbers, randomnumbers
17+
The following uses the HSpec library to define different tests.
18+
Note that the first test is a specific test with fixed inputs.
19+
The second and third test use QuickCheck.
2520

2621
\begin{code}
27-
tests :: [(String, IO Result)]
28-
tests =
29-
[ ("funnytest1", quickCheckResult (\n -> funnyfunction n `elem` [42, n*100, (n-1)*100]) )
30-
, ("reversetest", quickCheckResult (\str -> myreverse (myreverse str) == (str::String)) )
31-
]
32-
3322
main :: IO ()
34-
main = do
35-
results <- mapM (\(s,a) -> printf "\n%-20s: " s >> a) tests
36-
if all isSuccess results
37-
then exitSuccess
38-
else exitFailure
23+
main = hspec $ do
24+
describe "Basics" $ do
25+
it "somenumbers should be the same as [1..10]" $
26+
somenumbers `shouldBe` [1..10]
27+
it "funnyfunction: result is within [1..100]" $
28+
property (\n -> funnyfunction n `elem` [1..100])
29+
it "myreverse: using it twice gives back the same list" $
30+
property $ \str -> myreverse (myreverse str) == (str::String)
3931
\end{code}
4032

41-
To run this, use \verb|stack clean && stack test --coverage|. In particular
42-
this will generate a nice report using hpc. Look for ``The coverage report
43-
for ... is available at ... .html'' and open this html file in your browser.
44-
See \url{https://wiki.haskell.org/Haskell_program_coverage} for more examples
45-
and information how to read the report.
33+
To run the tests, use \verb|stack test|.
34+
35+
To also find out which part of your program is actually used for these tests,
36+
run \verb|stack clean && stack test --coverage|. Then look for ``The coverage
37+
report for ... is available at ... .html'' and open this file in your browser.
38+
See also: \url{https://wiki.haskell.org/Haskell_program_coverage}.

0 commit comments

Comments
 (0)