Last active
August 29, 2015 13:56
-
-
Save hfossli/9067052 to your computer and use it in GitHub Desktop.
Boolean testing in Objective-C
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Links | |
// http://stackoverflow.com/a/20720173/202451 | |
// http://blog.bignerdranch.com/564-bools-sharp-corners/ | |
#import <Foundation/Foundation.h> | |
void assignObjectToBOOL() | |
{ | |
NSLog(@"Starting %s", __PRETTY_FUNCTION__); | |
NSMutableSet *set = [NSMutableSet new]; | |
int numberOfFails = 0; | |
int numberOfTries = 100; | |
for(int i = 0; i < numberOfTries; i++) | |
{ | |
NSObject *obj = [NSObject new]; | |
[set addObject:obj]; // just making sure same pointers aren't reused | |
BOOL objExists = obj; | |
if(objExists) | |
{ | |
} | |
else | |
{ | |
numberOfFails++; | |
NSLog(@"Failed with value %i", i); | |
} | |
} | |
NSLog(@"Total number of fails %i / %i. Failed for all objects with memory adress 0x.....00", numberOfFails, numberOfTries); | |
} | |
void ifCheckObject() | |
{ | |
NSLog(@"Starting %s", __PRETTY_FUNCTION__); | |
NSMutableSet *set = [NSMutableSet new]; | |
int numberOfFails = 0; | |
int numberOfTries = 1000; | |
for(int i = 0; i < numberOfTries; i++) | |
{ | |
NSObject *obj = [NSObject new]; | |
[set addObject:obj]; // just making sure same pointers aren't reused | |
if(obj) | |
{ | |
} | |
else | |
{ | |
numberOfFails++; | |
NSLog(@"Failed with value %i", i); | |
} | |
} | |
NSLog(@"Total number of fails %i / %i. Shouldn't fail for any objects.", numberOfFails, numberOfTries); | |
} | |
void assignPrimitiveToBOOL() | |
{ | |
NSLog(@"Starting %s", __PRETTY_FUNCTION__); | |
int numberOfFails = 0; | |
int numberOfTries = 1000; | |
for(int i = -10; i < numberOfTries; i++) | |
{ | |
BOOL valueIsTrue = i; | |
if(valueIsTrue) | |
{ | |
} | |
else | |
{ | |
numberOfFails++; | |
NSLog(@"Failed with value %i", i); | |
} | |
} | |
NSLog(@"Total number of fails %i / %i. Should fail for 0, 256, 512, 768 because their last two values is 0 (bitwise)", numberOfFails, numberOfTries); | |
} | |
void questionMarkOperatorOnObject() | |
{ | |
NSLog(@"Starting %s", __PRETTY_FUNCTION__); | |
NSMutableSet *set = [NSMutableSet new]; | |
int numberOfFails = 0; | |
int numberOfTries = 1000; | |
for(int i = 0; i < numberOfTries; i++) | |
{ | |
NSObject *obj = [NSObject new]; | |
[set addObject:obj]; // just making sure same pointers aren't reused | |
NSObject *newObj = obj ? : @"hello"; | |
if(obj == newObj) | |
{ | |
} | |
else | |
{ | |
numberOfFails++; | |
NSLog(@"Failed with value %i", i); | |
} | |
} | |
NSLog(@"Total number of fails %i / %i. Shouldn't fail for any objects.", numberOfFails, numberOfTries); | |
} | |
void questionMarkOperatorOnPrimitive() | |
{ | |
NSLog(@"Starting %s", __PRETTY_FUNCTION__); | |
int numberOfFails = 0; | |
int numberOfTries = 1000; | |
for(int i = -10; i < numberOfTries; i++) | |
{ | |
int newValue = i ? : 40000; | |
if(newValue == i) | |
{ | |
} | |
else | |
{ | |
numberOfFails++; | |
NSLog(@"Failed with value %i", i); | |
} | |
} | |
NSLog(@"Total number of fails %i / %i. Should fail for primitive with value 0", numberOfFails, numberOfTries); | |
} | |
int main(int argc, char *argv[]) { | |
@autoreleasepool { | |
bool myBool = 2; | |
if (myBool != true) NSLog(@"myBool != true\n"); | |
BOOL myBOOL = 2; | |
if (myBOOL != YES) NSLog(@"myBOOL != YES\n"); | |
assignPrimitiveToBOOL(); | |
assignObjectToBOOL(); | |
ifCheckObject(); | |
questionMarkOperatorOnObject(); | |
questionMarkOperatorOnPrimitive(); | |
} | |
} | |
void assignObjectToBOOL() | |
{ | |
NSLog(@"Starting %s", __PRETTY_FUNCTION__); | |
NSMutableSet *set = [NSMutableSet new]; | |
int numberOfFails = 0; | |
int numberOfTries = 100; | |
for(int i = 0; i < numberOfTries; i++) | |
{ | |
NSObject *obj = [NSObject new]; | |
[set addObject:obj]; // just making sure same pointers aren't reused | |
BOOL objExists = obj; | |
if(objExists) | |
{ | |
} | |
else | |
{ | |
numberOfFails++; | |
NSLog(@"Failed for object %p", obj); | |
} | |
} | |
NSLog(@"Total number of fails %i / %i. Failed for all objects with memory adress 0x.....00", numberOfFails, numberOfTries); | |
} | |
void ifCheckObject() | |
{ | |
NSLog(@"Starting %s", __PRETTY_FUNCTION__); | |
NSMutableSet *set = [NSMutableSet new]; | |
int numberOfFails = 0; | |
int numberOfTries = 1000; | |
for(int i = 0; i < numberOfTries; i++) | |
{ | |
NSObject *obj = [NSObject new]; | |
[set addObject:obj]; // just making sure same pointers aren't reused | |
if(obj) | |
{ | |
} | |
else | |
{ | |
numberOfFails++; | |
NSLog(@"Does NOT exist %@", obj); | |
} | |
} | |
NSLog(@"Total number of fails %i / %i. Shouldn't fail for any objects.", numberOfFails, numberOfTries); | |
} | |
void assignPrimitiveToBOOL() | |
{ | |
NSLog(@"Starting %s", __PRETTY_FUNCTION__); | |
int numberOfFails = 0; | |
int numberOfTries = 1000; | |
for(int i = -10; i < numberOfTries; i++) | |
{ | |
BOOL valueIsTrue = i; | |
if(valueIsTrue) | |
{ | |
} | |
else | |
{ | |
numberOfFails++; | |
NSLog(@"Failed with value %i", i); | |
} | |
} | |
NSLog(@"Total number of fails %i / %i. Should fail for 0, 256, 512, 768 because their last two values is 0 (bitwise)", numberOfFails, numberOfTries); | |
} | |
void questionMarkOperatorOnObject() | |
{ | |
NSLog(@"Starting %s", __PRETTY_FUNCTION__); | |
NSMutableSet *set = [NSMutableSet new]; | |
int numberOfFails = 0; | |
int numberOfTries = 1000; | |
for(int i = 0; i < numberOfTries; i++) | |
{ | |
NSObject *obj = [NSObject new]; | |
[set addObject:obj]; // just making sure same pointers aren't reused | |
NSObject *newObj = obj ? : @"hello"; | |
if(obj == newObj) | |
{ | |
} | |
else | |
{ | |
numberOfFails++; | |
NSLog(@"Does NOT exist %@", obj); | |
} | |
} | |
NSLog(@"Total number of fails %i / %i. Shouldn't fail for any objects.", numberOfFails, numberOfTries); | |
} | |
void questionMarkOperatorOnPrimitive() | |
{ | |
NSLog(@"Starting %s", __PRETTY_FUNCTION__); | |
int numberOfFails = 0; | |
int numberOfTries = 1000; | |
for(int i = -10; i < numberOfTries; i++) | |
{ | |
int newValue = i ? : 40000; | |
if(newValue == i) | |
{ | |
} | |
else | |
{ | |
numberOfFails++; | |
NSLog(@"Failed with value %i", i); | |
} | |
} | |
NSLog(@"Total number of fails %i / %i. Should fail for primitive with value 0", numberOfFails, numberOfTries); | |
} | |
int main(int argc, char *argv[]) { | |
@autoreleasepool { | |
bool myBool = 2; | |
if (myBool != true) NSLog(@"myBool != true\n"); | |
BOOL myBOOL = 2; | |
if (myBOOL != YES) NSLog(@"myBOOL != YES\n"); | |
assignPrimitiveToBOOL(); | |
assignObjectToBOOL(); | |
ifCheckObject(); | |
questionMarkOperatorOnObject(); | |
questionMarkOperatorOnPrimitive(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Untitled.m:13:11: warning: incompatible pointer to integer conversion initializing 'BOOL' (aka 'signed char') with an expression of type 'NSObject *' [-Wint-conversion] | |
BOOL objExists = obj; | |
^ ~~~ | |
1 warning generated. | |
2014-02-18 09:44:10.739 Untitled[46411:507] myBOOL != YES | |
2014-02-18 09:44:10.741 Untitled[46411:507] Starting void assignPrimitiveToBOOL() | |
2014-02-18 09:44:10.741 Untitled[46411:507] Failed with value 0 | |
2014-02-18 09:44:10.741 Untitled[46411:507] Failed with value 256 | |
2014-02-18 09:44:10.741 Untitled[46411:507] Failed with value 512 | |
2014-02-18 09:44:10.742 Untitled[46411:507] Failed with value 768 | |
2014-02-18 09:44:10.742 Untitled[46411:507] Total number of fails 4 / 1000. Should fail for 0, 256, 512, 768 because their last two values is 0 (bitwise) | |
2014-02-18 09:44:10.742 Untitled[46411:507] Starting void assignObjectToBOOL() | |
2014-02-18 09:44:10.743 Untitled[46411:507] Failed for object 0x7f840a501d00 | |
2014-02-18 09:44:10.743 Untitled[46411:507] Failed for object 0x7f840a501b00 | |
2014-02-18 09:44:10.743 Untitled[46411:507] Failed for object 0x7f840c800000 | |
2014-02-18 09:44:10.743 Untitled[46411:507] Failed for object 0x7f840c800300 | |
2014-02-18 09:44:10.744 Untitled[46411:507] Failed for object 0x7f840c800700 | |
2014-02-18 09:44:10.744 Untitled[46411:507] Failed for object 0x7f840a501c00 | |
2014-02-18 09:44:10.744 Untitled[46411:507] Failed for object 0x7f840a501e00 | |
2014-02-18 09:44:10.745 Untitled[46411:507] Failed for object 0x7f840c800200 | |
2014-02-18 09:44:10.745 Untitled[46411:507] Total number of fails 8 / 100. Failed for all objects with memory adress 0x.....00 | |
2014-02-18 09:44:10.745 Untitled[46411:507] Starting void ifCheckObject() | |
2014-02-18 09:44:10.746 Untitled[46411:507] Total number of fails 0 / 1000. Shouldn't fail for any objects. | |
2014-02-18 09:44:10.746 Untitled[46411:507] Starting void questionMarkOperatorOnObject() | |
2014-02-18 09:44:10.746 Untitled[46411:507] Total number of fails 0 / 1000. Shouldn't fail for any objects. | |
2014-02-18 09:44:10.746 Untitled[46411:507] Starting void questionMarkOperatorOnPrimitive() | |
2014-02-18 09:44:10.747 Untitled[46411:507] Failed with value 0 | |
2014-02-18 09:44:10.747 Untitled[46411:507] Total number of fails 1 / 1000. Should fail for primitive with value 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment