Skip to content

Commit 2121ce1

Browse files
author
José Valim
committed
Guarantee HashDict.equal?/1 returns proper value when comparing subsets
Conflicts: lib/elixir/test/elixir/hash_dict_test.exs
1 parent fd8d1f1 commit 2121ce1

File tree

2 files changed

+13
-0
lines changed

2 files changed

+13
-0
lines changed

lib/elixir/lib/dict/behaviour.ex

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,9 @@ defmodule Dict.Behaviour do
6363
6464
"""
6565
defmacro __using__(_) do
66+
# Use this import to guarantee proper code expansion
67+
import Kernel, except: [size: 1]
68+
6669
quote do
6770
@behaviour Dict
6871

@@ -120,7 +123,9 @@ defmodule Dict.Behaviour do
120123
end
121124

122125
def equal?(dict1, dict2) do
126+
# Use this import to avoid conflicts in the user code
123127
import Kernel, except: [size: 1]
128+
124129
case size(dict1) == size(dict2) do
125130
false -> false
126131
true ->

lib/elixir/test/elixir/hash_dict_test.exs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,14 @@ defmodule HashDictTest do
5252
assert inspect(filled_dict(8)) =~ "#HashDict<"
5353
end
5454

55+
test "comparison when subsets" do
56+
d1 = HashDict.new(a: 0)
57+
d2 = HashDict.new(a: 0, b: 1)
58+
59+
refute HashDict.equal?(d1, d2)
60+
refute HashDict.equal?(d2, d1)
61+
end
62+
5563
defp smoke_test(range) do
5664
{ dict, _ } = Enum.reduce range, { HashDict.new, 1 }, fn(x, { acc, i }) ->
5765
acc = HashDict.put(acc, x, x)

0 commit comments

Comments
 (0)