Skip to content

Commit 2bd91b1

Browse files
committed
Add regression tests for #178 pollEvent space use
1 parent c24388c commit 2bd91b1

File tree

2 files changed

+52
-0
lines changed

2 files changed

+52
-0
lines changed

bench/Space.hs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
{-# LANGUAGE OverloadedStrings #-}
2+
3+
-- | Test that space usage is low and avoid GCs where possible.
4+
5+
module Main where
6+
7+
import SDL
8+
import Weigh
9+
10+
-- | Main entry point.
11+
main :: IO ()
12+
main =
13+
mainWith
14+
(do setColumns [Case, Allocated, GCs]
15+
sequence_
16+
[ validateAction
17+
("pollEvent " ++ show i)
18+
pollEventTest
19+
i
20+
(\weight ->
21+
if weightGCs weight > 0
22+
then Just "Non-zero number of garbage collections!"
23+
else if weightAllocatedBytes weight > 2000
24+
then Just
25+
"Allocated >2KB! Allocations should be constant."
26+
else Nothing)
27+
| i <- [1, 10, 100, 1000, 10000]
28+
])
29+
30+
-- | Test that merely polling does not allocate or engage the GC.
31+
-- <https://github.com/haskell-game/sdl2/issues/178>
32+
pollEventTest :: Int -> IO ()
33+
pollEventTest iters = do
34+
initializeAll
35+
let go :: Int -> IO ()
36+
go 0 = pure ()
37+
go i = do
38+
_ <- pollEvent
39+
go (i - 1)
40+
go iters

sdl2.cabal

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,18 @@ library
140140
if os(windows)
141141
cpp-options: -D_SDL_main_h -DSDL_main_h_
142142

143+
test-suite sdl-space
144+
type: exitcode-stdio-1.0
145+
main-is: Space.hs
146+
hs-source-dirs: bench
147+
build-depends: base
148+
, weigh
149+
, linear
150+
, sdl2
151+
, deepseq
152+
ghc-options: -Wall -rtsopts -O2
153+
default-language: Haskell2010
154+
143155
executable lazyfoo-lesson-01
144156
if flag(examples)
145157
build-depends: base, sdl2

0 commit comments

Comments
 (0)