Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Json::op_equal returns false when the keys of 2 maps have different order #1191

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion builtin/builtin.mbti
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ impl Map {
keys[K, V](Self[K, V]) -> Iter[K]
new[K, V](~capacity : Int = ..) -> Self[K, V]
of[K : Hash + Eq, V](FixedArray[(K, V)]) -> Self[K, V]
op_equal[K : Eq, V : Eq](Self[K, V], Self[K, V]) -> Bool
op_equal[K : Hash + Eq, V : Eq](Self[K, V], Self[K, V]) -> Bool
op_get[K : Hash + Eq, V](Self[K, V], K) -> V?
op_set[K : Hash + Eq, V](Self[K, V], K, V) -> Unit
remove[K : Hash + Eq, V](Self[K, V], K) -> Unit
Expand Down
20 changes: 10 additions & 10 deletions builtin/linked_hash_map.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -479,20 +479,20 @@ pub fn to_array[K, V](self : Map[K, V]) -> Array[(K, V)] {
arr
}

pub fn op_equal[K : Eq, V : Eq](self : Map[K, V], that : Map[K, V]) -> Bool {
pub fn op_equal[K : Hash + Eq, V : Eq](
self : Map[K, V],
that : Map[K, V]
) -> Bool {
if self.size != that.size {
return false
}
loop self.head, that.head {
None, None => true
Some({ key: k1, value: v1, idx: i1, .. }),
Some({ key: k2, value: v2, idx: i2, .. }) => {
if k1 != k2 || v1 != v2 {
return false
}
continue self.list[i1].next, that.list[i2].next
for k, v in self {
match that.get(k) {
None => break false
Some(v_) => if v_ != v { break false }
}
_, _ => false
} else {
true
}
}

Expand Down
10 changes: 9 additions & 1 deletion builtin/linked_hash_map_wbtest.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -632,7 +632,7 @@ test "to_array" {
assert_eq!(m.to_array(), [("a", 1), ("b", 2), ("c", 3)])
}

test "op_equal" {
test "op_equal_1" {
let m1 : Map[String, Int] = Map::new()
m1.set("a", 1)
m1.set("b", 2)
Expand All @@ -643,6 +643,14 @@ test "op_equal" {
inspect!({ 1: 2, 2: 3 } == { 1: 3, 2: 4 }, content="false")
}

test "op_equal_2" {
let map1 = { "a": 1, "b": 2 }
let map2 = { "b": 2, "a": 1 }
let map3 = { "a": 1, "b": 2 }
inspect!(map1 == map2, content="true")
inspect!(map1 == map3, content="true")
}

test "to_string" {
let m = {}
inspect!(m.to_string(), content="{}")
Expand Down