From 7df41b4f1aea076143afc930b11bb1e3d2a4c06a Mon Sep 17 00:00:00 2001 From: rightfold Date: Fri, 10 Feb 2017 15:00:29 +0100 Subject: [PATCH 1/2] Fix getAtOffset bugs Fixes #14. --- src/Node/Buffer.js | 10 +++++----- test/Main.purs | 13 ++++++++++++- 2 files changed, 17 insertions(+), 6 deletions(-) diff --git a/src/Node/Buffer.js b/src/Node/Buffer.js index 09c95c9..8c60fed 100644 --- a/src/Node/Buffer.js +++ b/src/Node/Buffer.js @@ -89,14 +89,14 @@ exports.toArray = function (buff) { }; }; -exports.getAtOffsetImpl = function (nothing) { - return function (just) { - return function (buff) { - return function (offset) { +exports.getAtOffsetImpl = function (just) { + return function (nothing) { + return function (offset) { + return function (buff) { return function() { var octet = buff[offset]; return octet == null ? nothing - : just(buff[i]); + : just(octet); }; }; }; diff --git a/test/Main.purs b/test/Main.purs index 45956d7..b569898 100644 --- a/test/Main.purs +++ b/test/Main.purs @@ -3,8 +3,9 @@ module Test.Main where import Prelude import Control.Monad.Eff (Eff) import Control.Monad.Eff.Console (log, CONSOLE()) +import Data.Maybe (Maybe(..)) import Data.Traversable (traverse) -import Node.Buffer (BUFFER, BufferValueType(..), toArray, concat', fromArray, fill, copy, readString, fromString, toString, read, write, create) +import Node.Buffer (BUFFER, BufferValueType(..), toArray, concat', fromArray, fill, copy, readString, fromString, toString, read, write, create, getAtOffset) import Node.Encoding (Encoding(..)) import Test.Assert (ASSERT, assert') @@ -41,6 +42,9 @@ main = do log "concat'" testConcat' + log "getAtOffset" + testGetAtOffset + testReadWrite :: Test testReadWrite = do buf <- create 1 @@ -120,6 +124,13 @@ testConcat' = do assertEq [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14] out +testGetAtOffset :: Test +testGetAtOffset = do + buf <- fromArray [1, 2, 3, 4] + assertEq (Just 2) =<< getAtOffset 1 buf + assertEq Nothing =<< getAtOffset 4 buf + assertEq Nothing =<< getAtOffset (-1) buf + assertEq :: forall a. (Eq a, Show a) => a -> a -> Test assertEq x y = if x == y From aaade1dd250cb1c7dc5eee9cb91f8a06b410b7fb Mon Sep 17 00:00:00 2001 From: rightfold Date: Fri, 10 Feb 2017 15:01:13 +0100 Subject: [PATCH 2/2] Octets are modulo 256, not 255 Fixes #15. --- src/Node/Buffer.purs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Node/Buffer.purs b/src/Node/Buffer.purs index 415a96d..e07bc23 100644 --- a/src/Node/Buffer.purs +++ b/src/Node/Buffer.purs @@ -28,7 +28,7 @@ import Data.Maybe (Maybe(..)) import Node.Encoding (Encoding, encodingToNode) -- | Type synonym indicating the value should be an octet (0-255). If the value --- | provided is outside this range it will be used as modulo 255. +-- | provided is outside this range it will be used as modulo 256. type Octet = Int -- | Type synonym indicating the value refers to an offset in a buffer.