Skip to content

Add Value equality API. #9700

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

Merged
merged 1 commit into from
Mar 27, 2025
Merged
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
9 changes: 9 additions & 0 deletions extension/apple/ExecuTorch/Exported/ExecuTorchValue.h
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,15 @@ __attribute__((deprecated("This API is experimental.")))
+ (instancetype)valueWithDouble:(ExecuTorchDoubleValue)value
NS_SWIFT_NAME(init(_:));


/**
* Determines whether the current Value is equal to another Value.
*
* @param other Another ExecuTorchValue instance to compare against.
* @return YES if the values have the same tag and equal underlying values; otherwise, NO.
*/
- (BOOL)isEqualToValue:(nullable ExecuTorchValue *)other;

@end

NS_ASSUME_NONNULL_END
23 changes: 23 additions & 0 deletions extension/apple/ExecuTorch/Exported/ExecuTorchValue.mm
Original file line number Diff line number Diff line change
Expand Up @@ -118,4 +118,27 @@ - (BOOL)isDouble {
return _tag == ExecuTorchValueTagDouble;
}

- (BOOL)isEqualToValue:(nullable ExecuTorchValue *)other {
if (!other) {
return NO;
}
if (_tag != other->_tag) {
return NO;
}
if (_value == nil) {
return other->_value == nil;
}
return [_value isEqual:other->_value];
}

- (BOOL)isEqual:(nullable id)other {
if (self == other) {
return YES;
}
if (![other isKindOfClass:[ExecuTorchValue class]]) {
return NO;
}
return [self isEqualToValue:(ExecuTorchValue *)other];
}

@end
39 changes: 39 additions & 0 deletions extension/apple/ExecuTorch/__tests__/ValueTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,43 @@ class ValueTest: XCTestCase {
XCTAssertTrue(value.isDouble)
XCTAssertEqual(value.double, 3.14)
}

func testIsEqual() {
let noneValue1 = Value()
let noneValue2 = Value()
XCTAssertTrue(noneValue1.isEqual(noneValue2))

let intValue1 = Value(42)
let intValue2 = Value(42)
let intValueDifferent = Value(43)
XCTAssertTrue(intValue1.isEqual(intValue2))
XCTAssertFalse(intValue1.isEqual(intValueDifferent))

let boolValue1 = Value(true)
let boolValue2 = Value(true)
let boolValueDifferent = Value(false)
XCTAssertTrue(boolValue1.isEqual(boolValue2))
XCTAssertFalse(boolValue1.isEqual(boolValueDifferent))

let doubleValue1 = Value(3.14)
let doubleValue2 = Value(3.14)
let doubleValueDifferent = Value(2.71)
XCTAssertTrue(doubleValue1.isEqual(doubleValue2))
XCTAssertFalse(doubleValue1.isEqual(doubleValueDifferent))

let stringValue1 = Value("hello")
let stringValue2 = Value("hello")
let stringValueDifferent = Value("world")
XCTAssertTrue(stringValue1.isEqual(stringValue2))
XCTAssertFalse(stringValue1.isEqual(stringValueDifferent))

let tensor1 = Tensor([1.0, 2.0, 3.0])
let tensor2 = Tensor([1.0, 2.0, 3.0])
let tensorDifferent = Tensor([3.0, 2.0, 1.0])
let tensorValue1 = Value(tensor1)
let tensorValue2 = Value(tensor2)
let tensorValueDifferent = Value(tensorDifferent)
XCTAssertTrue(tensorValue1.isEqual(tensorValue2))
XCTAssertFalse(tensorValue1.isEqual(tensorValueDifferent))
}
}
Loading