Skip to content

Commit

Permalink
prevent runtime error for partial functions for ST arrays (#111)
Browse files Browse the repository at this point in the history
* prevent runtime error for partial functions for ST arrays

* add Data.Array.ST.Partial tests
  • Loading branch information
mhuisi authored and paf31 committed Jun 25, 2017
1 parent e300101 commit b363605
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 4 deletions.
4 changes: 2 additions & 2 deletions src/Data/Array/ST/Partial.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
"use strict";

exports.peekSTArray = function (xs) {
exports.peekSTArrayImpl = function (xs) {
return function (i) {
return function () {
return xs[i];
};
};
};

exports.pokeSTArray = function (xs) {
exports.pokeSTArrayImpl = function (xs) {
return function (i) {
return function (a) {
return function () {
Expand Down
19 changes: 17 additions & 2 deletions src/Data/Array/ST/Partial.purs
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,33 @@ import Data.Array.ST (STArray)
import Data.Unit (Unit)

-- | Read the value at the specified index in a mutable array.
foreign import peekSTArray
peekSTArray
:: forall a h r
. Partial
=> STArray h a
-> Int
-> Eff (st :: ST h | r) a
peekSTArray = peekSTArrayImpl

foreign import peekSTArrayImpl
:: forall a h r
. STArray h a
-> Int
-> Eff (st :: ST h | r) a

-- | Change the value at the specified index in a mutable array.
foreign import pokeSTArray
pokeSTArray
:: forall a h r
. Partial
=> STArray h a
-> Int
-> a
-> Eff (st :: ST h | r) Unit
pokeSTArray = pokeSTArrayImpl

foreign import pokeSTArrayImpl
:: forall a h r
. STArray h a
-> Int
-> a
-> Eff (st :: ST h | r) Unit
28 changes: 28 additions & 0 deletions test/Test/Data/Array/ST/Partial.purs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
module Test.Data.Array.ST.Partial (testArraySTPartial) where

import Prelude

import Control.Monad.Eff (Eff)
import Control.Monad.Eff.Console (log, CONSOLE)
import Control.Monad.ST (pureST)

import Data.Array.ST (thaw, unsafeFreeze)
import Data.Array.ST.Partial (peekSTArray, pokeSTArray)

import Partial.Unsafe (unsafePartial)

import Test.Assert (assert, ASSERT)

testArraySTPartial :: forall eff. Eff (console :: CONSOLE, assert :: ASSERT | eff) Unit
testArraySTPartial = do

log "peekSTArray should return the value at the specified index"
assert $ 2 == pureST do
a <- thaw [1, 2, 3]
unsafePartial $ peekSTArray a 1

log "pokeSTArray should modify the value at the specified index"
assert $ [1, 4, 3] == pureST do
a <- thaw [1, 2, 3]
unsafePartial $ pokeSTArray a 1 4
unsafeFreeze a
2 changes: 2 additions & 0 deletions test/Test/Main.purs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@ import Test.Assert (ASSERT)
import Test.Data.Array (testArray)
import Test.Data.Array.Partial (testArrayPartial)
import Test.Data.Array.ST (testArrayST)
import Test.Data.Array.ST.Partial (testArraySTPartial)

main :: forall eff. Eff (console :: CONSOLE, assert :: ASSERT | eff) Unit
main = do
testArray
testArrayST
testArrayPartial
testArraySTPartial

0 comments on commit b363605

Please sign in to comment.