Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(584)

Side by Side Diff: third_party/ocmock/OCMock/NSValue+OCMAdditions.m

Issue 2410583002: Test update OCMock (Closed)
Patch Set: Patch in exactly 3.1.5 Created 3 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 /*
2 * Copyright (c) 2014-2015 Erik Doernenburg and contributors
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License"); you may
5 * not use these files except in compliance with the License. You may obtain
6 * a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 * License for the specific language governing permissions and limitations
14 * under the License.
15 */
16
17 #import "NSValue+OCMAdditions.h"
18
19 @implementation NSValue (OCMAdditions)
20
21 static CFNumberType OCMNumberTypeForObjCType(const char* objcType) {
22 switch (objcType[0]) {
23 case 'c':
24 return kCFNumberCharType;
25 case 'C':
26 return kCFNumberCharType;
27 case 'B':
28 return kCFNumberCharType;
29 case 's':
30 return kCFNumberShortType;
31 case 'S':
32 return kCFNumberShortType;
33 case 'i':
34 return kCFNumberIntType;
35 case 'I':
36 return kCFNumberIntType;
37 case 'l':
38 return kCFNumberLongType;
39 case 'L':
40 return kCFNumberLongType;
41 case 'q':
42 return kCFNumberLongLongType;
43 case 'Q':
44 return kCFNumberLongLongType;
45 case 'f':
46 return kCFNumberFloatType;
47 case 'd':
48 return kCFNumberDoubleType;
49 default:
50 return 0;
51 }
52 }
53
54 static NSNumber* OCMNumberForValue(NSValue* value) {
55 #define CREATE_NUM(_type) \
56 ({ \
57 _type _v; \
58 [value getValue:&_v]; \
59 @(_v); \
60 })
61 switch ([value objCType][0]) {
62 case 'c':
63 return CREATE_NUM(char);
64 case 'C':
65 return CREATE_NUM(unsigned char);
66 case 'B':
67 return CREATE_NUM(bool);
68 case 's':
69 return CREATE_NUM(short);
70 case 'S':
71 return CREATE_NUM(unsigned short);
72 case 'i':
73 return CREATE_NUM(int);
74 case 'I':
75 return CREATE_NUM(unsigned int);
76 case 'l':
77 return CREATE_NUM(long);
78 case 'L':
79 return CREATE_NUM(unsigned long);
80 case 'q':
81 return CREATE_NUM(long long);
82 case 'Q':
83 return CREATE_NUM(unsigned long long);
84 case 'f':
85 return CREATE_NUM(float);
86 case 'd':
87 return CREATE_NUM(double);
88 default:
89 return nil;
90 }
91 }
92
93 - (BOOL)getBytes:(void*)outputBuf objCType:(const char*)targetType {
94 /*
95 * See if they are similar number types, and if we can convert losslessly
96 * between them.
97 * For the most part, we set things up to use CFNumberGetValue, which returns
98 * false if
99 * conversion will be lossy.
100 */
101 CFNumberType inputType = OCMNumberTypeForObjCType([self objCType]);
102 CFNumberType outputType = OCMNumberTypeForObjCType(targetType);
103
104 if (inputType == 0 || outputType == 0) // one or both are non-number types
105 return NO;
106
107 NSNumber* inputNumber = [self isKindOfClass:[NSNumber class]]
108 ? (NSNumber*)self
109 : OCMNumberForValue(self);
110
111 /*
112 * Due to some legacy, back-compatible requirements in CFNumber.c,
113 * CFNumberGetValue can return true for
114 * some conversions which should not be allowed (by reading source,
115 * conversions from integer types to
116 * 8-bit or 16-bit integer types). So, check ourselves.
117 */
118 long long min;
119 long long max;
120 long long val = [inputNumber longLongValue];
121 switch (targetType[0]) {
122 case 'B':
123 case 'c':
124 min = CHAR_MIN;
125 max = CHAR_MAX;
126 break;
127 case 'C':
128 min = 0;
129 max = UCHAR_MAX;
130 break;
131 case 's':
132 min = SHRT_MIN;
133 max = SHRT_MAX;
134 break;
135 case 'S':
136 min = 0;
137 max = USHRT_MAX;
138 break;
139 default:
140 min = LLONG_MIN;
141 max = LLONG_MAX;
142 break;
143 }
144 if (val < min || val > max)
145 return NO;
146
147 /* Get the number, and return NO if the value was out of range or conversion
148 * was lossy */
149 return CFNumberGetValue((CFNumberRef)inputNumber, outputType, outputBuf);
150 }
151
152 @end
OLDNEW
« no previous file with comments | « third_party/ocmock/OCMock/NSValue+OCMAdditions.h ('k') | third_party/ocmock/OCMock/OCClassMockObject.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698