Skip to content

Commit

Permalink
Merge pull request #67 from blaix/bytes-equality-fix
Browse files Browse the repository at this point in the history
Bytes equality fix
  • Loading branch information
robinheghan authored Mar 21, 2024
2 parents b8e8fab + 109ae0b commit 256538f
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 1 deletion.
14 changes: 14 additions & 0 deletions src/Gren/Kernel/Utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,20 @@ function _Utils_eqHelp(x, y, depth, stack) {
}
//*/

if (typeof DataView === "function" && x instanceof DataView) {
var length = x.byteLength;

if (y.byteLength !== length) {
return false;
}

for (var i = 0; i < length; ++i) {
if (x.getUint8(i) !== y.getUint8(i)) {
return false;
}
}
}

if (Array.isArray(x) && x.length !== y.length) {
return false;
}
Expand Down
2 changes: 2 additions & 0 deletions tests/src/Main.gren
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import Test exposing (..)
import Test.Array as Array
import Test.Basics as Basics
import Test.Bitwise as Bitwise
import Test.Bytes as Bytes
import Test.Char as Char
import Test.CodeGen as CodeGen
import Test.Dict as Dict
Expand All @@ -28,6 +29,7 @@ main =
[ Array.tests
, Basics.tests
, Bitwise.tests
, Bytes.tests
, Char.tests
, CodeGen.tests
, Dict.tests
Expand Down
35 changes: 35 additions & 0 deletions tests/src/Test/Bytes.gren
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
module Test.Bytes exposing (tests)

import Bytes exposing (Bytes)
import Bytes.Encode
import Expect
import Fuzz
import Test exposing (..)


encodeString : String -> Bytes
encodeString s =
Bytes.Encode.encode <|
Bytes.Encode.string s


encodeInt : Int -> Bytes
encodeInt i =
Bytes.Encode.encode <|
Bytes.Encode.signedInt8 i


tests : Test
tests =
describe "Byte comparison"
[ fuzz Fuzz.string "same strings are equal" <| \s ->
Expect.equal (encodeString s) (encodeString s)
, fuzz (Fuzz.intRange -128 127) "same ints are equal" <| \i ->
Expect.equal (encodeInt i) (encodeInt i)
, fuzz Fuzz.string "different strings are not equal" <| \s ->
Expect.notEqual (encodeString s) (encodeString (s ++ "a"))
, fuzz (Fuzz.intRange -100 100) "different ints are not equal" <| \i ->
Expect.notEqual (encodeInt i) (encodeInt (i + 1))
, fuzz2 Fuzz.string (Fuzz.intRange 1 127) "string and int are not equal" <| \s i ->
Expect.notEqual (encodeString s) (encodeInt i)
]
4 changes: 3 additions & 1 deletion tests/src/Test/Json.gren
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,9 @@ customTests : Test
customTests =
let
jsonString =
"""{ "foo": "bar" }"""
"""
{ "foo": "bar" }
"""

customErrorMessage =
"I want to see this message!"
Expand Down

0 comments on commit 256538f

Please sign in to comment.