// Copyright 2019 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 #import #import "mediapipe/objc/CFHolder.h" #include @interface CFHolderTests : XCTestCase { UInt8 _bytes[4]; CFDataRef _data; } @end @implementation CFHolderTests - (void)setUp { _data = CFDataCreate(NULL, _bytes, sizeof(_bytes)); } - (void)tearDown { CFRelease(_data); } - (void)testCreateAndDestroy { XCTAssertEqual(CFGetRetainCount(_data), 1); { CFHolder holder(_data); XCTAssertEqual(CFGetRetainCount(_data), 2); } XCTAssertEqual(CFGetRetainCount(_data), 1); } - (void)testDereference { CFHolder holder(_data); XCTAssertEqual(*holder, _data); } - (void)testCopy { XCTAssertEqual(CFGetRetainCount(_data), 1); { CFHolder holder(_data); XCTAssertEqual(CFGetRetainCount(_data), 2); { CFHolder holder2(holder); XCTAssertEqual(CFGetRetainCount(_data), 3); { CFHolder holder3 = holder; XCTAssertEqual(CFGetRetainCount(_data), 4); } XCTAssertEqual(CFGetRetainCount(_data), 3); } XCTAssertEqual(CFGetRetainCount(_data), 2); } XCTAssertEqual(CFGetRetainCount(_data), 1); } - (void)testOverwriteWithNull { XCTAssertEqual(CFGetRetainCount(_data), 1); { // Copy assignment. CFHolder holder(_data); XCTAssertEqual(CFGetRetainCount(_data), 2); CFHolder holder2; holder = holder2; XCTAssertEqual(CFGetRetainCount(_data), 1); } { // Move assignment. CFHolder holder(_data); XCTAssertEqual(CFGetRetainCount(_data), 2); holder = CFHolder(); XCTAssertEqual(CFGetRetainCount(_data), 1); } } - (void)testOverwriteWithOther { CFDataRef data2 = CFDataCreate(NULL, _bytes, sizeof(_bytes)); XCTAssertEqual(CFGetRetainCount(_data), 1); XCTAssertEqual(CFGetRetainCount(data2), 1); { // Copy assignment. CFHolder holder(_data); XCTAssertEqual(CFGetRetainCount(_data), 2); CFHolder holder2(data2); XCTAssertEqual(CFGetRetainCount(data2), 2); holder = holder2; XCTAssertEqual(CFGetRetainCount(_data), 1); XCTAssertEqual(CFGetRetainCount(data2), 3); } { // Move assignment. CFHolder holder(_data); XCTAssertEqual(CFGetRetainCount(_data), 2); holder = CFHolder(data2); XCTAssertEqual(CFGetRetainCount(_data), 1); XCTAssertEqual(CFGetRetainCount(data2), 2); } CFRelease(data2); } - (void)testCompare { CFDataRef data2 = CFDataCreate(NULL, _bytes, sizeof(_bytes)); CFHolder holder(_data); CFHolder holdersame(_data); CFHolder holderother(data2); CFHolder empty; // Compare with other holder. XCTAssertEqual(holder, holder); XCTAssertEqual(holder, holdersame); XCTAssertNotEqual(holder, holderother); XCTAssertNotEqual(holder, empty); // Compare with held type. XCTAssertEqual(holder, _data); XCTAssertNotEqual(holder, data2); XCTAssertNotEqual(holder, nil); XCTAssertEqual(empty, nil); XCTAssertNotEqual(empty, _data); CFRelease(data2); } - (void)testReset { XCTAssertEqual(CFGetRetainCount(_data), 1); { CFHolder holder; holder.reset(_data); XCTAssertEqual(CFGetRetainCount(_data), 2); CFDataRef data2 = CFDataCreate(NULL, _bytes, sizeof(_bytes)); holder.reset(data2); XCTAssertEqual(CFGetRetainCount(_data), 1); XCTAssertEqual(CFGetRetainCount(data2), 2); CFRelease(data2); } XCTAssertEqual(CFGetRetainCount(_data), 1); } - (void)testAdopt { CFRetain(_data); XCTAssertEqual(CFGetRetainCount(_data), 2); { CFHolder holder; holder.adopt(_data); XCTAssertEqual(CFGetRetainCount(_data), 2); } XCTAssertEqual(CFGetRetainCount(_data), 1); } - (void)testMove { XCTAssertEqual(CFGetRetainCount(_data), 1); { CFHolder holder(_data); XCTAssertEqual(CFGetRetainCount(_data), 2); CFHolder holder2(std::move(holder)); XCTAssertEqual(CFGetRetainCount(_data), 2); CFHolder holder3(std::move(holder2)); XCTAssertEqual(CFGetRetainCount(_data), 2); } XCTAssertEqual(CFGetRetainCount(_data), 1); } @end