Skip to content

Commit

Permalink
Scaffold ring buffer with simple Property
Browse files Browse the repository at this point in the history
  • Loading branch information
IOG Engineering committed Mar 17, 2023
1 parent 388cd66 commit 62bfd0e
Show file tree
Hide file tree
Showing 6 changed files with 107 additions and 1 deletion.
11 changes: 10 additions & 1 deletion 2023-03-17/README.md
Expand Up @@ -6,6 +6,15 @@

## Retrospective

* There were more people, 6 of us?
* We did not go very far and as always wasted time setting up the environment which never work out of the box :(
* Goal was to implement a fixed size LIFO buffer using TDD
* The problem is interesting esp. on the testing side -> what tests to write
* we stopped before generalising to more than 1 place buffer
* we did not try to write one or more interesting Property
* we did tiny steps not assuming any prior knowledge => maybe a Property would have helped and lead us to do bigger steps
* Teh problem is instersting also because it's in IO which is not where Haskell usually shines -> make good IO/statefule code in Haskell is still tricky

## Topic

Proposals:
Expand All @@ -14,4 +23,4 @@ Proposals:
|---------------|----------|------------|------|
| Ring buffer | Haskell | Randori | xxxx |

**Topic selected: **
**Topic selected: Ring Buffer**
5 changes: 5 additions & 0 deletions 2023-03-17/ringbuffer/CHANGELOG.md
@@ -0,0 +1,5 @@
# Revision history for ringbuffer

## 0.1.0.0 -- YYYY-mm-dd

* First version. Released on an unsuspecting world.
50 changes: 50 additions & 0 deletions 2023-03-17/ringbuffer/ringbuffer.cabal
@@ -0,0 +1,50 @@
cabal-version: 2.4
name: ringbuffer
version: 0.1.0.0

-- A short (one-line) description of the package.
-- synopsis:

-- A longer description of the package.
-- description:

-- A URL where users can report bugs.
-- bug-reports:

-- The license under which the package is released.
-- license:
author: IOG Engineering
maintainer: engineering@iohk.io

-- A copyright notice.
-- copyright:
-- category:
extra-source-files: CHANGELOG.md

library
exposed-modules: MyLib

-- Modules included in this library but not exported.
-- other-modules:

-- LANGUAGE extensions used by modules in this package.
-- other-extensions:
build-depends: base ^>=4.16.4.0
hs-source-dirs: src
default-language: Haskell2010

test-suite tests
ghc-options: -threaded -rtsopts -with-rtsopts=-N
hs-source-dirs: test
other-modules: RingBufferSpec
main-is: Spec.hs
type: exitcode-stdio-1.0
build-depends:
, base
, hspec
, hspec-core
, QuickCheck
, vector
, ringbuffer
build-tool-depends: hspec-discover:hspec-discover
ghc-options: -threaded -rtsopts
4 changes: 4 additions & 0 deletions 2023-03-17/ringbuffer/src/MyLib.hs
@@ -0,0 +1,4 @@
module MyLib (someFunc) where

someFunc :: IO ()
someFunc = putStrLn "someFunc"
37 changes: 37 additions & 0 deletions 2023-03-17/ringbuffer/test/RingBufferSpec.hs
@@ -0,0 +1,37 @@
module RingBufferSpec where

import Control.Monad (liftM)
import Data.IORef (IORef, newIORef, readIORef, writeIORef)
import Test.Hspec (Spec, it, pending, shouldBe, shouldReturn)
import Test.Hspec.QuickCheck (prop)
import Test.QuickCheck (Property, arbitrary, forAll)
import Test.QuickCheck.Monadic (assert, monadicIO, run)

-- a "ring" buffer where you can:
-- - push an element to the tail if not full
-- - pop an element from the head if not empty
-- - fixed capacity in the queue

spec :: Spec
spec =
prop "pushing an element then popping it gives back same element" pushPopIsIdempotence

pushPopIsIdempotence :: Property
pushPopIsIdempotence = forAll arbitrary $ \x -> monadicIO $ do
y <- run $ do
buffer <- newBuffer
push buffer x
pop buffer

assert (x == y)

pop :: RingBuffer -> IO Int
pop (RingBuffer b) = readIORef b

push :: RingBuffer -> Int -> IO ()
push (RingBuffer b) x = writeIORef b x

data RingBuffer = RingBuffer (IORef Int)

newBuffer :: IO RingBuffer
newBuffer = liftM RingBuffer (newIORef 0)
1 change: 1 addition & 0 deletions 2023-03-17/ringbuffer/test/Spec.hs
@@ -0,0 +1 @@
{-# OPTIONS_GHC -F -pgmF hspec-discover #-}

0 comments on commit 62bfd0e

Please sign in to comment.