Skip to content
This repository was archived by the owner on Aug 31, 2021. It is now read-only.

Commit 03d3d2f

Browse files
committed
[[ Bug 22024 ]] Fix LCB's 'is among the elements of' operator
This patch fixes an issue with LCB's 'is among the elements of' operator which causes it to return the wrong result when searching for an array in another array. The issue was caused by the internal __MCArrayIsEqualTo method only using pointer equivalence to check for element equality rather than MCValueIsEqualTo. Additionally an explicit test for 'is among the elements of' has been added to the array test file.
1 parent 5bfa859 commit 03d3d2f

File tree

3 files changed

+17
-1
lines changed

3 files changed

+17
-1
lines changed

docs/notes/bugfix-22024.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# Fix LCB's 'is among the elements of' when searching for array elements

libfoundation/src/foundation-array.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -728,7 +728,8 @@ bool __MCArrayIsEqualTo(__MCArray *self, __MCArray *other_self)
728728
return false;
729729

730730
// Otherwise, they are only equal if the values are the same.
731-
if (t_contents -> key_values[i] . value != t_other_contents -> key_values[t_slot] . value)
731+
if (!MCValueIsEqualTo((MCValueRef)t_contents -> key_values[i] . value,
732+
(MCValueRef)t_other_contents -> key_values[t_slot] . value))
732733
return false;
733734

734735
// We've compared one more key, so used count goes down.

tests/lcb/stdlib/array.lcb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,18 @@ public handler TestSubscriptPrecedence()
2626
test "subscript (_ ends with)" when tArray["bar"] ends with "o"
2727
end handler
2828

29+
public handler TestIsAmongTheElementsOf()
30+
/* Non-const variants of in element searches are required to ensure
31+
* it isn't just pointer equality which is occurring (LCB uniques all
32+
* constants so the same constant has the same pointer). */
33+
34+
variable tArray
35+
put {"a": "x", "b": 1, "c": true, "d": { "e": [2] } } into tArray
36+
test "element is among the elements of" when "x" is among the elements of tArray
37+
test "element is among the elements of (non-const)" when ("x" & "") is among the elements of tArray
38+
test "array element is among the elements of" when { "e": [2] } is among the elements of tArray
39+
test "array element is among the elements of (non-const)" when { "e": [1 + 1] } is among the elements of tArray
40+
test "array element is not among the elements of" when not { "e": [1] } is among the elements of tArray
41+
end handler
42+
2943
end module

0 commit comments

Comments
 (0)