diff --git a/mediapipe/tasks/ios/test/vision/core/BUILD b/mediapipe/tasks/ios/test/vision/core/BUILD index 5932968e5..e8c63f2f6 100644 --- a/mediapipe/tasks/ios/test/vision/core/BUILD +++ b/mediapipe/tasks/ios/test/vision/core/BUILD @@ -54,3 +54,22 @@ ios_unit_test( ":MPPImageObjcTestLibrary", ], ) + +objc_library( + name = "MPPMaskObjcTestLibrary", + testonly = 1, + srcs = ["MPPMaskTests.m"], + deps = [ + "//mediapipe/tasks/ios/vision/core:MPPMask", + ], +) + +ios_unit_test( + name = "MPPMaskObjcTest", + minimum_os_version = MPP_TASK_MINIMUM_OS_VERSION, + runner = tflite_ios_lab_runner("IOS_LATEST"), + tags = TFL_DEFAULT_TAGS + TFL_DISABLED_SANITIZER_TAGS, + deps = [ + ":MPPMaskObjcTestLibrary", + ], +) diff --git a/mediapipe/tasks/ios/test/vision/core/MPPMaskTests.m b/mediapipe/tasks/ios/test/vision/core/MPPMaskTests.m new file mode 100644 index 000000000..05b8de023 --- /dev/null +++ b/mediapipe/tasks/ios/test/vision/core/MPPMaskTests.m @@ -0,0 +1,127 @@ +// Copyright 2023 The MediaPipe Authors. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#import "mediapipe/tasks/ios/vision/core/sources/MPPMask.h" + +#import + +/** Unit tests for `MPPMask`. */ +@interface MPPMaskTests : XCTestCase + +@end + +@implementation MPPMaskTests + +#pragma mark - Tests + +- (void)testInitWithUInt8ArrayNoCopySucceeds { + + NSInteger width = 2; + NSInteger height = 3; + + UInt8 uint8Data[] = {128, 128, 128, 128, 128, 128}; + float float32Data[] = {0.501f, 0.501f, 0.501f, 0.501f, 0.501f, 0.501f}; + + MPPMask *mask = [[MPPMask alloc] initWithUInt8Data:&uint8Data width:width height:height shouldCopy:NO]; + + XCTAssertEqual(mask.width, width); + XCTAssertEqual(mask.height, height); + + // Test if UInt8 mask is not copied. + XCTAssertEqual(mask.uint8Data, &uint8Data); + XCTAssertNotEqual(mask.float32Data, NULL); + + for (int i = 0 ; i < width * height ; i ++) { + XCTAssertEqualWithAccuracy(mask.float32Data[i], float32Data[i], 1e-3f, @"index i = %d", i); + } + + // Test if repeated Float32 mask accesses return the same array in memory. + XCTAssertEqual(mask.float32Data, mask.float32Data); +} + +- (void)testInitWithUInt8ArrayCopySucceeds { + + NSInteger width = 2; + NSInteger height = 3; + + UInt8 uint8Data[] = {128, 128, 128, 128, 128, 128}; + float float32Data[] = {0.501f, 0.501f, 0.501f, 0.501f, 0.501f, 0.501f}; + + MPPMask *mask = [[MPPMask alloc] initWithUInt8Data:&uint8Data width:width height:height shouldCopy:YES]; + + XCTAssertEqual(mask.width, width); + XCTAssertEqual(mask.height, height); + + // Test if UInt8 mask is copied. + XCTAssertNotEqual(mask.uint8Data, &uint8Data); + XCTAssertNotEqual(mask.float32Data, NULL); + + for (int i = 0 ; i < width * height ; i ++) { + XCTAssertEqualWithAccuracy(mask.float32Data[i], float32Data[i], 1e-3f); + } + + // Test if repeated Float32 mask accesses return the same array in memory. + XCTAssertEqual(mask.float32Data, mask.float32Data); +} + +- (void)testInitWithFloat32ArrayNoCopySucceeds { + + NSInteger width = 2; + NSInteger height = 3; + + UInt8 uint8Data[] = {132, 132, 132, 132, 132, 132}; + float float32Data[] = {0.52f, 0.52f, 0.52f, 0.52f, 0.52f, 0.52f}; + MPPMask *mask = [[MPPMask alloc] initWithFloat32Data:&float32Data width:width height:height shouldCopy:NO]; + + XCTAssertEqual(mask.width, width); + XCTAssertEqual(mask.height, height); + + // Test if Float32 mask is not copied. + XCTAssertEqual(mask.float32Data, &float32Data); + XCTAssertNotEqual(mask.uint8Data, NULL); + + for (int i = 0 ; i < width * height ; i ++) { + XCTAssertEqual(mask.uint8Data[i], uint8Data[i]); + } + + // Test if repeated UInt8 mask accesses return the same array in memory. + XCTAssertEqual(mask.uint8Data, mask.uint8Data); +} + +- (void)testInitWithFloat32ArrayCopySucceeds { + + NSInteger width = 2; + NSInteger height = 3; + + UInt8 uint8Data[] = {132, 132, 132, 132, 132, 132}; + float float32Data[] = {0.52f, 0.52f, 0.52f, 0.52f, 0.52f, 0.52f}; + + MPPMask *mask = [[MPPMask alloc] initWithFloat32Data:&float32Data width:width height:height shouldCopy:YES]; + + XCTAssertEqual(mask.width, width); + XCTAssertEqual(mask.height, height); + + // Test if Float32 mask is copied. + XCTAssertNotEqual(mask.float32Data, &float32Data); + XCTAssertNotEqual(mask.uint8Data, NULL); + + for (int i = 0 ; i < width * height ; i ++) { + XCTAssertEqual(mask.uint8Data[i], uint8Data[i]); + } + + // Test if repeated UInt8 mask accesses return the same array in memory. + XCTAssertEqual(mask.uint8Data, mask.uint8Data); +} + +@end