Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Scaffold ring buffer with simple Property
- Loading branch information
IOG Engineering
committed
Mar 17, 2023
1 parent
388cd66
commit 62bfd0e
Showing
6 changed files
with
107 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
# Revision history for ringbuffer | ||
|
||
## 0.1.0.0 -- YYYY-mm-dd | ||
|
||
* First version. Released on an unsuspecting world. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
module MyLib (someFunc) where | ||
|
||
someFunc :: IO () | ||
someFunc = putStrLn "someFunc" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{-# OPTIONS_GHC -F -pgmF hspec-discover #-} |