Skip to content

Commit

Permalink
Fix equality check for bytes.
Browse files Browse the repository at this point in the history
Should fix gren-lang#48

Using suggested fix from https://github.com/elm/core/pull/1092/files
  • Loading branch information
blaix committed Mar 21, 2024
1 parent 1b80a0e commit 109ae0b
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 0 deletions.
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)
]

0 comments on commit 109ae0b

Please sign in to comment.