|
| 1 | +using Test as test; |
| 2 | + |
| 3 | +methods { |
| 4 | + function createSimpleStruct(uint256 x, int128 y) external returns (Test.SimpleStruct memory) envfree; |
| 5 | + function createIntArray(uint256 a, uint256 b, uint256 c) external returns (uint256[] memory) envfree; |
| 6 | + function createStructWithArray(uint256 id, uint256 a, uint256 b) external returns (Test.StructWithArray memory) envfree; |
| 7 | +} |
| 8 | + |
| 9 | +// Test 1: Compare primitive arrays (uint256[]) |
| 10 | +rule comparePrimitiveArrays { |
| 11 | + uint256[] arr1; uint256[] arr2; |
| 12 | + |
| 13 | + // Can't use `<=>` here because that causes double polarity of the quantifier and breaks grounding |
| 14 | + assert arr1 == arr2 => (arr1.length == arr2.length && (forall uint256 i. i < arr1.length => arr1[i] == arr2[i])); |
| 15 | + assert (arr1.length == arr2.length && (forall uint256 i. i < arr1.length => arr1[i] == arr2[i])) => arr1 == arr2; |
| 16 | +} |
| 17 | + |
| 18 | +// Test 2: Reflexivity of array comparison |
| 19 | +rule arrayComparisonReflexive { |
| 20 | + uint256[] arr; |
| 21 | + assert arr == arr; |
| 22 | +} |
| 23 | + |
| 24 | +// Test 3: Symmetry of array comparison |
| 25 | +rule arrayComparisonSymmetric { |
| 26 | + uint256[] arr1; uint256[] arr2; |
| 27 | + assert (arr1 == arr2) == (arr2 == arr1); |
| 28 | +} |
| 29 | + |
| 30 | +// Test 4: Compare function return array |
| 31 | +rule compareFunctionReturnArray { |
| 32 | + uint256 a; uint256 b; uint256 c; |
| 33 | + uint256[] arr; |
| 34 | + require arr.length == 3; |
| 35 | + require arr[0] == a && arr[1] == b && arr[2] == c; |
| 36 | + assert createIntArray(a, b, c) == arr; |
| 37 | +} |
| 38 | + |
| 39 | +// Test 5: Compare structs containing arrays |
| 40 | +rule compareStructsWithArrays { |
| 41 | + Test.StructWithArray s1; Test.StructWithArray s2; |
| 42 | + assert s1 == s2 <=> (s1.id == s2.id && s1.values == s2.values); |
| 43 | +} |
| 44 | + |
| 45 | +// Test 6: Compare structs with array - reflexive |
| 46 | +rule structWithArrayReflexive { |
| 47 | + Test.StructWithArray s; |
| 48 | + assert s == s; |
| 49 | +} |
| 50 | + |
| 51 | +// Test 7: Function returning struct with array |
| 52 | +rule compareFunctionReturnStructWithArray { |
| 53 | + uint256 id; uint256 a; uint256 b; |
| 54 | + Test.StructWithArray s; |
| 55 | + require s.id == id; |
| 56 | + require s.values.length == 2; |
| 57 | + require s.values[0] == a && s.values[1] == b; |
| 58 | + assert createStructWithArray(id, a, b) == s; |
| 59 | +} |
| 60 | + |
| 61 | +// Test 8: Different lengths means not equal |
| 62 | +rule differentLengthsNotEqual { |
| 63 | + uint256[] arr1; uint256[] arr2; |
| 64 | + require arr1.length != arr2.length; |
| 65 | + assert arr1 != arr2; |
| 66 | +} |
| 67 | + |
| 68 | +// Test 9: Empty arrays are equal |
| 69 | +rule emptyArraysEqual { |
| 70 | + uint256[] arr1; uint256[] arr2; |
| 71 | + require arr1.length == 0; |
| 72 | + require arr2.length == 0; |
| 73 | + assert arr1 == arr2; |
| 74 | +} |
| 75 | + |
| 76 | +// Test 10: Compare int256 arrays |
| 77 | +rule compareIntArrays { |
| 78 | + int256[] arr1; int256[] arr2; |
| 79 | + |
| 80 | + // Can't use `<=>` here because that causes double polarity of the quantifier and breaks grounding |
| 81 | + assert arr1 == arr2 => (arr1.length == arr2.length && (forall uint256 i. i < arr1.length => arr1[i] == arr2[i])); |
| 82 | + assert (arr1.length == arr2.length && (forall uint256 i. i < arr1.length => arr1[i] == arr2[i])) => arr1 == arr2; |
| 83 | +} |
| 84 | + |
| 85 | +// Test 11: Compare struct with nested struct and array field |
| 86 | +rule compareStructWithNestedAndArray { |
| 87 | + Test.StructWithNestedAndArray s1; Test.StructWithNestedAndArray s2; |
| 88 | + assert s1 == s2 <=> (s1.inner.x == s2.inner.x && s1.inner.y == s2.inner.y && s1.arr == s2.arr); |
| 89 | +} |
0 commit comments