|
| 1 | +module Test.Node.Buffer.Class (testMutableBuffer) where |
| 2 | + |
| 3 | +import Prelude |
| 4 | + |
| 5 | +import Data.ArrayBuffer.Types (ArrayBuffer) |
| 6 | +import Data.Maybe (Maybe(..)) |
| 7 | +import Data.Traversable (traverse) |
| 8 | +import Effect (Effect) |
| 9 | +import Effect.Console (log) |
| 10 | +import Node.Buffer (class MutableBuffer, BufferValueType(..), concat', copy, create, fill, freeze, fromArray, fromArrayBuffer, fromString, getAtOffset, read, readString, setAtOffset, slice, thaw, toArray, toArrayBuffer, toString, write) |
| 11 | +import Node.Buffer.Immutable as Immutable |
| 12 | +import Node.Encoding (Encoding(..)) |
| 13 | +import Test.Assert (assertEqual) |
| 14 | +import Type.Proxy (Proxy) |
| 15 | + |
| 16 | +testMutableBuffer :: forall buf m. MutableBuffer buf m => |
| 17 | + Proxy buf -> (forall a. m a -> Effect a) -> Effect Unit |
| 18 | +testMutableBuffer _ run = do |
| 19 | + |
| 20 | + log " - create" |
| 21 | + testCreate |
| 22 | + |
| 23 | + log " - freeze" |
| 24 | + testFreeze |
| 25 | + |
| 26 | + log " - thaw" |
| 27 | + testThaw |
| 28 | + |
| 29 | + log " - Reading and writing" |
| 30 | + testReadWrite |
| 31 | + |
| 32 | + log " - fromArray" |
| 33 | + testFromArray |
| 34 | + |
| 35 | + log " - toArray" |
| 36 | + testToArray |
| 37 | + |
| 38 | + log " - fromString" |
| 39 | + testFromString |
| 40 | + |
| 41 | + log " - (to/from)ArrayBuffer" |
| 42 | + testToFromArrayBuffer |
| 43 | + |
| 44 | + log " - toString" |
| 45 | + testToString |
| 46 | + |
| 47 | + log " - readString" |
| 48 | + testReadString |
| 49 | + |
| 50 | + log " - slice" |
| 51 | + testSlice |
| 52 | + |
| 53 | + log " - copy" |
| 54 | + testCopy |
| 55 | + |
| 56 | + log " - fill" |
| 57 | + testFill |
| 58 | + |
| 59 | + log " - concat'" |
| 60 | + testConcat' |
| 61 | + |
| 62 | + log " - getAtOffset" |
| 63 | + testGetAtOffset |
| 64 | + |
| 65 | + where |
| 66 | + testCreate :: Effect Unit |
| 67 | + testCreate = do |
| 68 | + buf <- run ((create 3 :: m buf) >>= toArray) |
| 69 | + assertEqual {expected: [0, 0, 0], actual: buf} |
| 70 | + |
| 71 | + testFreeze :: Effect Unit |
| 72 | + testFreeze = do |
| 73 | + buf <- Immutable.toArray <$> run ((fromArray [1, 2, 3] :: m buf) >>= freeze) |
| 74 | + assertEqual {expected: [1, 2, 3], actual: buf} |
| 75 | + |
| 76 | + testThaw :: Effect Unit |
| 77 | + testThaw = do |
| 78 | + buf <- run ((thaw (Immutable.fromArray [1, 2, 3]) :: m buf) >>= toArray) |
| 79 | + assertEqual {expected: [1, 2, 3], actual: buf} |
| 80 | + |
| 81 | + testReadWrite :: Effect Unit |
| 82 | + testReadWrite = do |
| 83 | + let val = 42 |
| 84 | + readVal <- run do |
| 85 | + buf <- create 1 :: m buf |
| 86 | + write UInt8 val 0 buf |
| 87 | + read UInt8 0 buf |
| 88 | + |
| 89 | + assertEqual {expected: val, actual: readVal} |
| 90 | + |
| 91 | + testFromArray :: Effect Unit |
| 92 | + testFromArray = do |
| 93 | + readVal <- run do |
| 94 | + buf <- fromArray [1,2,3,4,5] :: m buf |
| 95 | + read UInt8 2 buf |
| 96 | + |
| 97 | + assertEqual {expected: 3, actual: readVal} |
| 98 | + |
| 99 | + testToArray :: Effect Unit |
| 100 | + testToArray = do |
| 101 | + let val = [1,2,67,3,3,7,8,3,4,237] |
| 102 | + valOut <- run do |
| 103 | + buf <- fromArray val :: m buf |
| 104 | + toArray buf |
| 105 | + |
| 106 | + assertEqual {expected: val, actual: valOut} |
| 107 | + |
| 108 | + testFromString :: Effect Unit |
| 109 | + testFromString = do |
| 110 | + let str = "hello, world" |
| 111 | + val <- run do |
| 112 | + buf <- fromString str ASCII :: m buf |
| 113 | + read UInt8 6 buf |
| 114 | + |
| 115 | + assertEqual {expected: 32, actual: val} -- ASCII space |
| 116 | + |
| 117 | + testToFromArrayBuffer :: Effect Unit |
| 118 | + testToFromArrayBuffer = do |
| 119 | + buf <- run $ |
| 120 | + fromArray [1, 2, 3] |
| 121 | + >>= (toArrayBuffer :: buf -> m ArrayBuffer) |
| 122 | + >>= (fromArrayBuffer :: ArrayBuffer -> m buf) |
| 123 | + >>= toArray |
| 124 | + assertEqual {expected: [1, 2, 3], actual: buf} |
| 125 | + |
| 126 | + testToString :: Effect Unit |
| 127 | + testToString = do |
| 128 | + let str = "hello, world" |
| 129 | + strOut <- run do |
| 130 | + buf <- fromString str ASCII :: m buf |
| 131 | + toString ASCII buf |
| 132 | + |
| 133 | + assertEqual {expected: str, actual: strOut} |
| 134 | + |
| 135 | + testReadString :: Effect Unit |
| 136 | + testReadString = do |
| 137 | + let str = "hello, world" |
| 138 | + strOut <- run do |
| 139 | + buf <- fromString str ASCII :: m buf |
| 140 | + readString ASCII 7 12 buf |
| 141 | + |
| 142 | + assertEqual {expected: "world", actual: strOut} |
| 143 | + |
| 144 | + testSlice :: Effect Unit |
| 145 | + testSlice = do |
| 146 | + {bufArr, bufSliceArr} <- run do |
| 147 | + buf <- fromArray [1, 2, 3, 4] :: m buf |
| 148 | + let bufSlice = slice 1 3 buf |
| 149 | + setAtOffset 42 1 bufSlice |
| 150 | + bufArr <- toArray buf |
| 151 | + bufSliceArr <- toArray bufSlice |
| 152 | + pure {bufArr, bufSliceArr} |
| 153 | + |
| 154 | + assertEqual {expected: [1, 2, 42, 4], actual: bufArr} |
| 155 | + assertEqual {expected: [2, 42], actual: bufSliceArr} |
| 156 | + |
| 157 | + testCopy :: Effect Unit |
| 158 | + testCopy = do |
| 159 | + {copied, out} <- run do |
| 160 | + buf1 <- fromArray [1,2,3,4,5] :: m buf |
| 161 | + buf2 <- fromArray [10,9,8,7,6] |
| 162 | + copied <- copy 0 3 buf1 2 buf2 |
| 163 | + out <- toArray buf2 |
| 164 | + pure {copied, out} |
| 165 | + |
| 166 | + assertEqual {expected: 3, actual: copied} |
| 167 | + assertEqual {expected: [10,9,1,2,3], actual: out} |
| 168 | + |
| 169 | + testFill :: Effect Unit |
| 170 | + testFill = do |
| 171 | + out <- run do |
| 172 | + buf <- fromArray [1,1,1,1,1] :: m buf |
| 173 | + fill 42 2 4 buf |
| 174 | + toArray buf |
| 175 | + |
| 176 | + assertEqual {expected: [1,1,42,42,1], actual: out} |
| 177 | + |
| 178 | + testConcat' :: Effect Unit |
| 179 | + testConcat' = do |
| 180 | + out <- run do |
| 181 | + bufs <- traverse (fromArray :: Array Int -> m buf) $ map (\x -> [x, x+1, x+2]) [0,3,6,9,12] |
| 182 | + buf <- concat' bufs 15 |
| 183 | + toArray buf |
| 184 | + |
| 185 | + assertEqual {expected: [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14], actual: out} |
| 186 | + |
| 187 | + testGetAtOffset :: Effect Unit |
| 188 | + testGetAtOffset = do |
| 189 | + {o1, o4, om1} <- run do |
| 190 | + buf <- fromArray [1, 2, 3, 4] :: m buf |
| 191 | + o1 <- getAtOffset 1 buf |
| 192 | + o4 <- getAtOffset 4 buf |
| 193 | + om1 <- getAtOffset (-1) buf |
| 194 | + pure {o1, o4, om1} |
| 195 | + |
| 196 | + assertEqual {expected: Just 2, actual: o1} |
| 197 | + assertEqual {expected: Nothing, actual: o4} |
| 198 | + assertEqual {expected: Nothing, actual: om1} |
0 commit comments