IntegrationLengthPrefixedStringTest
A string prefixed by its length as a big-endian 16-bit integer round-trips through elm/bytes, with `Bytes.Decode.andThen` passing the decoded length to `Bytes.Decode.string`. The string `"hello"` is encoded and decoded, and the program logs the decoded string. The length is taken from `String.length`, which equals the UTF-8 byte count only for ASCII text such as this.
Results
elm-0.19.1
passran in 53 msOutput
Compiled in DEV mode. Follow the advice at https://elm-lang.org/0.19.1/optimize for better performance and smaller assets. IntegrationLengthPrefixedStringTest: "hello"
elm-0.19.2
passran in 52 msOutput
Compiled in DEV mode. Follow the advice at https://elm-lang.org/0.19.2/optimize for better performance and smaller assets. IntegrationLengthPrefixedStringTest: "hello"
elm-0.19.3
passran in 50 msOutput
Compiled in DEV mode. Follow the advice at https://elm-lang.org/0.19.3/optimize for better performance and smaller assets. IntegrationLengthPrefixedStringTest: "hello"
lamdera-1.5.0
passran in 49 msOutput
IntegrationLengthPrefixedStringTest: "hello"
Discussions
Discuss this test·Search the discussions on GitHub
Discussions are on GitHub; one about this test has [t:elm-bytes/IntegrationLengthPrefixedStringTest] in its title.
Source
The -- CHECK: lines give the expected output.
module IntegrationLengthPrefixedStringTest exposing (main)
{-| A string prefixed by its length as a big-endian 16-bit integer round-trips through elm/bytes, with `Bytes.Decode.andThen` passing the decoded length to `Bytes.Decode.string`.
The string `"hello"` is encoded and decoded, and the program logs the decoded
string. The length is taken from `String.length`, which equals the UTF-8 byte
count only for ASCII text such as this.
-}
-- CHECK: IntegrationLengthPrefixedStringTest: "hello"
import Bytes exposing (Bytes, Endianness(..))
import Bytes.Decode as D
import Bytes.Encode as E
import Html exposing (text)
encodeString : String -> E.Encoder
encodeString s =
let
len =
String.length s
in
E.sequence
[ E.unsignedInt16 BE len
, E.string s
]
decodeString : D.Decoder String
decodeString =
D.unsignedInt16 BE
|> D.andThen D.string
main =
let
original =
"hello"
encoded =
E.encode (encodeString original)
decoded =
D.decode decodeString encoded
|> Maybe.withDefault "FAIL"
_ =
Debug.log "IntegrationLengthPrefixedStringTest" decoded
in
text decoded