1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
|
#import <Foundation/NSKeyValueCoding.h>
#import <Foundation/NSValue.h>
#import "Testing.h"
// For ivars: _<key>, _is<Key>, <key>, or is<Key>, in that order.
// For methods: get<Key>, <key>, is<Key>, or _<key> in that order.
@interface SearchOrder : NSObject
{
long long _longLong;
long long _isLongLong;
long long longLong;
long long isLongLong;
unsigned char _isUnsignedChar;
unsigned char unsignedChar;
unsigned char isUnsignedChar;
unsigned int unsignedInt;
unsigned int isUnsignedInt;
unsigned long isUnsignedLong;
}
- (instancetype)init;
- (signed char)getChar;
- (signed char)char;
- (signed char)isChar;
- (signed char)_char;
- (int)int;
- (int)isInt;
- (int)_int;
- (short)isShort;
- (short)_short;
- (long)_long;
@end
@implementation SearchOrder
- (instancetype)init
{
self = [super init];
if (self)
{
_longLong = LLONG_MAX;
_isLongLong = LLONG_MAX - 1;
longLong = LLONG_MAX - 2;
isLongLong = LLONG_MAX - 3;
_isUnsignedChar = UCHAR_MAX;
unsignedChar = UCHAR_MAX - 1;
isUnsignedChar = UCHAR_MAX - 2;
unsignedInt = UINT_MAX;
isUnsignedInt = UINT_MAX - 1;
isUnsignedLong = ULONG_MAX;
}
return self;
}
- (signed char)getChar
{
return SCHAR_MAX;
}
- (signed char)char
{
return SCHAR_MAX - 1;
}
- (signed char)isChar
{
return SCHAR_MAX - 2;
}
- (signed char)_char
{
return SCHAR_MAX - 3;
}
- (int)int
{
return INT_MAX;
}
- (int)isInt
{
return INT_MAX - 1;
}
- (int)_int
{
return INT_MAX - 2;
}
- (short)isShort
{
return SHRT_MAX;
}
- (short)_short
{
return SHRT_MAX - 1;
}
- (long)_long
{
return LONG_MAX;
}
@end
@interface SearchOrderNoIvarAccess : NSObject
{
bool _boolVal;
bool _isBoolVal;
bool boolVal;
bool isBoolVal;
}
@end
@implementation SearchOrderNoIvarAccess
+ (BOOL)accessInstanceVariablesDirectly
{
return NO;
}
@end
static void
testSearchOrder(void)
{
SearchOrder *so = [SearchOrder new];
START_SET("Search Order");
PASS_EQUAL([so valueForKey:@"char"], [NSNumber numberWithChar:SCHAR_MAX],
"get<Key> is used when available");
PASS_EQUAL([so valueForKey:@"int"], [NSNumber numberWithInt:INT_MAX],
"<key> is used when get<Key> is not available");
PASS_EQUAL([so valueForKey:@"short"], [NSNumber numberWithShort:SHRT_MAX],
"is<Key> is used when get<Key> and <key> is not available");
PASS_EQUAL(
[so valueForKey:@"long"], [NSNumber numberWithLong:LONG_MAX],
"_<key> is used when get<Key>, <key>, and is<Key> is not available");
PASS_EQUAL(
[so valueForKey:@"longLong"], [NSNumber numberWithLongLong:LLONG_MAX],
"_<key> ivar is used when get<Key>, <key>, and is<Key> is not available");
PASS_EQUAL(
[so valueForKey:@"unsignedChar"],
[NSNumber numberWithUnsignedChar:UCHAR_MAX],
"_is<Key> ivar is used when get<Key>, <key>, and is<Key> is not available");
PASS_EQUAL(
[so valueForKey:@"unsignedInt"], [NSNumber numberWithUnsignedInt:UINT_MAX],
"<key> ivar is used when get<Key>, <key>, and is<Key> is not available");
PASS_EQUAL(
[so valueForKey:@"unsignedLong"],
[NSNumber numberWithUnsignedLong:ULONG_MAX],
"is<Key> ivar is used when get<Key>, <key>, and is<Key> is not available");
END_SET("Search Order");
[so release];
}
static void
testIvarAccess(void)
{
SearchOrderNoIvarAccess *so = [SearchOrderNoIvarAccess new];
START_SET("Search Order Ivar Access");
PASS_EXCEPTION([so valueForKey:@"boolVal"], NSUndefinedKeyException,
"Does not return protected ivar");
END_SET("Search Order Ivar Access");
[so release];
}
int
main(int argc, char *argv[])
{
testSearchOrder();
testIvarAccess();
return 0;
}
|