Skip to content

Commit

Permalink
Adding BoxUtil
Browse files Browse the repository at this point in the history
  • Loading branch information
gershnik committed Jan 6, 2024
1 parent 3637b7d commit 6c6aaca
Show file tree
Hide file tree
Showing 7 changed files with 167 additions and 6 deletions.
142 changes: 142 additions & 0 deletions test/BoxUtilTests.mm
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
#include <objc-helpers/BoxUtil.h>

#include "doctest.h"


TEST_SUITE_BEGIN( "BoxUtilTests" );

TEST_CASE( "integer" ) {
auto obj = box(42);
static_assert(std::is_same_v<decltype(obj), NSObject<BoxedValue, NSCopying> *>);
CHECK(boxedValue<int>(obj) == 42);
CHECK([obj.description isEqualToString:@"42"]);
CHECK(obj.hash == std::hash<int>()(42));

@try {
boxedValue<long>(obj);
FAIL("able to unbox wrong type");
} @catch (NSException * exc) {
CHECK([exc.name isEqualToString:NSInvalidArgumentException]);
}

auto objc = (decltype(obj))[obj copy];
CHECK(boxedValue<int>(objc) == 42);

CHECK([objc isEqual:obj]);
CHECK([objc isEqualTo:obj]);

CHECK(![objc isEqual:nullptr]);
CHECK([objc isEqual:objc]);
CHECK(![objc isEqual:@(42)]);

@try {
[[maybe_unused]] auto obj1 = (NSObject *)[[obj.class alloc] init];
FAIL("able to call init");
} @catch (NSException * exc) {
CHECK([exc.name isEqualToString:NSInvalidArgumentException]);
}

@try {
[obj.class new];
FAIL("able to call new");
} @catch (NSException * exc) {
CHECK([exc.name isEqualToString:NSInvalidArgumentException]);
}
}

TEST_CASE( "string" ) {
std::string str("abc");

auto obj = box(std::string("abc"));
static_assert(std::is_same_v<decltype(obj), NSObject<BoxedValue, NSCopying> *>);
CHECK(boxedValue<std::string>(obj) == str);
CHECK([obj.description isEqualToString:@"abc"]);
CHECK(obj.hash == std::hash<std::string>()(str));

auto objc = (decltype(obj))[obj copy];
CHECK(boxedValue<std::string>(objc) == str);

CHECK([objc isEqual:obj]);
CHECK([objc isEqualTo:obj]);

auto objm = box(std::move(str));
CHECK(boxedValue<std::string>(objm) == "abc");
CHECK(str.empty());

auto obje = box<std::string>(size_t(5), 'a');
CHECK(boxedValue<std::string>(obje) == "aaaaa");
}

TEST_CASE( "unique_ptr" ) {

auto obj = box(std::make_unique<int>(5));
static_assert(std::is_same_v<decltype(obj), NSObject<BoxedValue> *>);
CHECK(*boxedValue<std::unique_ptr<int>>(obj) == 5);

@try {
[[maybe_unused]] auto objc = (decltype(obj))[obj copy];
FAIL("exception not thrown");
} @catch (NSException * exc) {
CHECK([exc.name isEqualToString:NSInvalidArgumentException]);
}

auto ptr = std::make_unique<int>(6);
auto hash = std::hash<std::unique_ptr<int>>()(ptr);
void * val = ptr.get();
auto objm = box(std::move(ptr));
CHECK(!ptr);
CHECK(*boxedValue<std::unique_ptr<int>>(objm) == 6);
CHECK(boxedValue<std::unique_ptr<int>>(objm).get() == val);
CHECK(objm.hash == hash);
CHECK([objm.description isEqualToString:[NSString stringWithFormat:@"%p", val]]);
}

TEST_CASE( "struct" ) {

struct foo {
int i;
char c;
};

auto obj1 = box(foo{4, 'b'});
auto obj2 = box(foo{4, 'b'});

CHECK(boxedValue<foo>(obj1).i == 4);
CHECK(boxedValue<foo>(obj2).c == 'b');
CHECK(![obj1 isEqual:obj2]);
CHECK(obj1.hash != obj2.hash);
}

TEST_CASE( "equal-no-hash" ) {

struct foo {
int i;
char c;

bool operator==(const foo & rhs) const = default;
};

auto obj1 = box(foo{4, 'b'});
auto obj2 = box(foo{4, 'b'});

CHECK(boxedValue<foo>(obj1).i == 4);
CHECK(boxedValue<foo>(obj2).c == 'b');
CHECK([obj1 isEqual:obj2]);
@try {
[[maybe_unused]] auto hash = obj1.hash;
FAIL("exception not thrown");
} @catch (NSException * exc) {
CHECK([exc.name isEqualToString:NSInvalidArgumentException]);
}
}

struct qwerty {};

TEST_CASE( "no-desc" ) {
auto obj = box<qwerty>();
CHECK([obj.description isEqualToString:@"Boxed object of type \"qwerty\""]);
}



TEST_SUITE_END();
1 change: 0 additions & 1 deletion test/CoDispatchTestsNoexcept.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#include <objc-helpers/CoDispatch.h>

//#define DOCTEST_CONFIG_NO_EXCEPTIONS
#include "doctest.h"

#include <CoreFoundation/CoreFoundation.h>
Expand Down
4 changes: 4 additions & 0 deletions test/NSNumberUtilTests.mm
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

#include "doctest.h"

TEST_SUITE_BEGIN( "NSNumberUtilTests" );

TEST_CASE("comparison") {

CHECK(NSNumberEqual()(@(1), @(1)));
Expand All @@ -17,3 +19,5 @@
CHECK(!NSNumberLess()(@(0), nullptr));
CHECK(!NSNumberLess()(nullptr, nullptr));
}

TEST_SUITE_END();
4 changes: 4 additions & 0 deletions test/NSObjectUtilTests.mm
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ - (NSString *) description {

@end

TEST_SUITE_BEGIN( "NSObjectUtilTests" );

TEST_CASE("comparison") {

CHECK(NSObjectEqual()(@(1), @(1)));
Expand Down Expand Up @@ -70,3 +72,5 @@ - (NSString *) description {
CHECK(str.str() == "nullptr");
}
}

TEST_SUITE_END();
4 changes: 4 additions & 0 deletions test/NSStringUtilTests.mm
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
#include <ranges>
#include <algorithm>

TEST_SUITE_BEGIN( "NSStringUtilTests" );

TEST_CASE("comparison") {

CHECK(NSStringEqual()(@"abc", @"abc"));
Expand Down Expand Up @@ -112,3 +114,5 @@

CHECK(CFStringCompare(NSStringCharAccess(@"abcd").getCFString(), CFSTR("abcd"), 0) == 0);
}

TEST_SUITE_END();
4 changes: 3 additions & 1 deletion test/main.cpp → test/main.mm
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,7 @@
#include "doctest.h"

int main(int argc, const char * argv[]) {
return doctest::Context(argc, argv).run();
@autoreleasepool {
return doctest::Context(argc, argv).run();
}
}
14 changes: 10 additions & 4 deletions test/test.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,14 @@
/* End PBXAggregateTarget section */

/* Begin PBXBuildFile section */
441779162B20136E0036AF9F /* main.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 441779152B20136E0036AF9F /* main.cpp */; };
441779162B20136E0036AF9F /* main.mm in Sources */ = {isa = PBXBuildFile; fileRef = 441779152B20136E0036AF9F /* main.mm */; };
4417791E2B201E280036AF9F /* NSStringUtilTests.mm in Sources */ = {isa = PBXBuildFile; fileRef = 4417791D2B201E280036AF9F /* NSStringUtilTests.mm */; };
441779202B202DA30036AF9F /* CoDispatchTests.mm in Sources */ = {isa = PBXBuildFile; fileRef = 4417791F2B202DA30036AF9F /* CoDispatchTests.mm */; };
441779352B2235B70036AF9F /* CoDispatchTestsCpp.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 441779342B2235B70036AF9F /* CoDispatchTestsCpp.cpp */; };
441779372B24C4930036AF9F /* NSNumberUtilTests.mm in Sources */ = {isa = PBXBuildFile; fileRef = 441779362B24C4930036AF9F /* NSNumberUtilTests.mm */; };
441779392B24C6B00036AF9F /* NSObjectUtilTests.mm in Sources */ = {isa = PBXBuildFile; fileRef = 441779382B24C6B00036AF9F /* NSObjectUtilTests.mm */; };
4417793B2B26FEA70036AF9F /* CoDispatchTestsNoexcept.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 4417793A2B26FEA60036AF9F /* CoDispatchTestsNoexcept.cpp */; settings = {COMPILER_FLAGS = "-fno-exceptions"; }; };
44B947E72B477A2700B68C7E /* BoxUtilTests.mm in Sources */ = {isa = PBXBuildFile; fileRef = 44B947E62B477A2700B68C7E /* BoxUtilTests.mm */; };
/* End PBXBuildFile section */

/* Begin PBXContainerItemProxy section */
Expand All @@ -55,7 +56,7 @@

/* Begin PBXFileReference section */
441779122B20136E0036AF9F /* test */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = test; sourceTree = BUILT_PRODUCTS_DIR; };
441779152B20136E0036AF9F /* main.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = main.cpp; sourceTree = "<group>"; };
441779152B20136E0036AF9F /* main.mm */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.objcpp; path = main.mm; sourceTree = "<group>"; };
4417791C2B2013E00036AF9F /* doctest.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = doctest.h; sourceTree = "<group>"; };
4417791D2B201E280036AF9F /* NSStringUtilTests.mm */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.objcpp; path = NSStringUtilTests.mm; sourceTree = "<group>"; };
4417791F2B202DA30036AF9F /* CoDispatchTests.mm */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.objcpp; path = CoDispatchTests.mm; sourceTree = "<group>"; };
Expand All @@ -68,6 +69,8 @@
441779362B24C4930036AF9F /* NSNumberUtilTests.mm */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.objcpp; path = NSNumberUtilTests.mm; sourceTree = "<group>"; };
441779382B24C6B00036AF9F /* NSObjectUtilTests.mm */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.objcpp; path = NSObjectUtilTests.mm; sourceTree = "<group>"; };
4417793A2B26FEA60036AF9F /* CoDispatchTestsNoexcept.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = CoDispatchTestsNoexcept.cpp; sourceTree = "<group>"; };
44B947E52B4778EB00B68C7E /* BoxUtil.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = BoxUtil.h; sourceTree = "<group>"; };
44B947E62B477A2700B68C7E /* BoxUtilTests.mm */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.objcpp; path = BoxUtilTests.mm; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand All @@ -86,11 +89,12 @@
children = (
441779212B202F3E0036AF9F /* Library */,
4417791C2B2013E00036AF9F /* doctest.h */,
441779152B20136E0036AF9F /* main.cpp */,
441779152B20136E0036AF9F /* main.mm */,
4417791F2B202DA30036AF9F /* CoDispatchTests.mm */,
441779342B2235B70036AF9F /* CoDispatchTestsCpp.cpp */,
4417793A2B26FEA60036AF9F /* CoDispatchTestsNoexcept.cpp */,
4417791D2B201E280036AF9F /* NSStringUtilTests.mm */,
44B947E62B477A2700B68C7E /* BoxUtilTests.mm */,
441779362B24C4930036AF9F /* NSNumberUtilTests.mm */,
441779382B24C6B00036AF9F /* NSObjectUtilTests.mm */,
441779132B20136E0036AF9F /* Products */,
Expand All @@ -109,6 +113,7 @@
isa = PBXGroup;
children = (
441779252B202F530036AF9F /* BlockUtil.h */,
44B947E52B4778EB00B68C7E /* BoxUtil.h */,
441779222B202F530036AF9F /* CoDispatch.h */,
441779242B202F530036AF9F /* NSNumberUtil.h */,
441779232B202F530036AF9F /* NSObjectUtil.h */,
Expand Down Expand Up @@ -201,8 +206,9 @@
buildActionMask = 2147483647;
files = (
441779202B202DA30036AF9F /* CoDispatchTests.mm in Sources */,
441779162B20136E0036AF9F /* main.cpp in Sources */,
441779162B20136E0036AF9F /* main.mm in Sources */,
441779372B24C4930036AF9F /* NSNumberUtilTests.mm in Sources */,
44B947E72B477A2700B68C7E /* BoxUtilTests.mm in Sources */,
4417791E2B201E280036AF9F /* NSStringUtilTests.mm in Sources */,
441779352B2235B70036AF9F /* CoDispatchTestsCpp.cpp in Sources */,
441779392B24C6B00036AF9F /* NSObjectUtilTests.mm in Sources */,
Expand Down

0 comments on commit 6c6aaca

Please sign in to comment.