IntegrationListEncodingTest
A list of integers encoded as a one-byte count followed by one byte per element round-trips through elm/bytes. The decoder reads the count and then uses `Bytes.Decode.loop` to read that many bytes, reversing the accumulated list at the end. The list `[ 1, 2, 3, 4, 5 ]` is encoded and decoded, and the program logs `True` only if the result equals the original.
Results
elm-0.19.1
passran in 75 msOutput
Compiled in DEV mode. Follow the advice at https://elm-lang.org/0.19.1/optimize for better performance and smaller assets. IntegrationListEncodingTest: True
elm-0.19.2
passran in 77 msOutput
Compiled in DEV mode. Follow the advice at https://elm-lang.org/0.19.2/optimize for better performance and smaller assets. IntegrationListEncodingTest: True
elm-0.19.3
passran in 51 msOutput
Compiled in DEV mode. Follow the advice at https://elm-lang.org/0.19.3/optimize for better performance and smaller assets. IntegrationListEncodingTest: True
lamdera-1.5.0
passran in 48 msOutput
IntegrationListEncodingTest: True
Discussions
Discuss this test·Search the discussions on GitHub
Discussions are on GitHub; one about this test has [t:elm-bytes/IntegrationListEncodingTest] in its title.
Source
The -- CHECK: lines give the expected output.
module IntegrationListEncodingTest exposing (main)
{-| A list of integers encoded as a one-byte count followed by one byte per element round-trips through elm/bytes.
The decoder reads the count and then uses `Bytes.Decode.loop` to read that many
bytes, reversing the accumulated list at the end. The list `[ 1, 2, 3, 4, 5 ]` is
encoded and decoded, and the program logs `True` only if the result equals the
original.
-}
-- CHECK: IntegrationListEncodingTest: True
import Bytes exposing (Bytes, Endianness(..))
import Bytes.Decode as D
import Bytes.Encode as E
import Html exposing (text)
encodeList : List Int -> E.Encoder
encodeList items =
E.sequence
[ E.unsignedInt8 (List.length items)
, E.sequence (List.map E.unsignedInt8 items)
]
decodeList : D.Decoder (List Int)
decodeList =
D.unsignedInt8
|> D.andThen
(\len ->
D.loop ( len, [] )
(\( remaining, acc ) ->
if remaining <= 0 then
D.succeed (D.Done (List.reverse acc))
else
D.unsignedInt8
|> D.map (\n -> D.Loop ( remaining - 1, n :: acc ))
)
)
main =
let
original =
[ 1, 2, 3, 4, 5 ]
encoded =
E.encode (encodeList original)
decoded =
D.decode decodeList encoded
|> Maybe.withDefault []
result =
decoded == original
_ =
Debug.log "IntegrationListEncodingTest" result
in
text (if result then "True" else "False")