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

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

Issue 2624143003: Update OCMock to 3.1.5 (Closed)
Patch Set: Patches in more commits Created 3 years, 10 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
20 @implementation NSValue(OCMAdditions)
21
22 static CFNumberType OCMNumberTypeForObjCType(const char *objcType)
23 {
24 switch (objcType[0])
25 {
26 case 'c': return kCFNumberCharType;
27 case 'C': return kCFNumberCharType;
28 case 'B': return kCFNumberCharType;
29 case 's': return kCFNumberShortType;
30 case 'S': return kCFNumberShortType;
31 case 'i': return kCFNumberIntType;
32 case 'I': return kCFNumberIntType;
33 case 'l': return kCFNumberLongType;
34 case 'L': return kCFNumberLongType;
35 case 'q': return kCFNumberLongLongType;
36 case 'Q': return kCFNumberLongLongType;
37 case 'f': return kCFNumberFloatType;
38 case 'd': return kCFNumberDoubleType;
39 default: return 0;
40 }
41 }
42
43
44 static NSNumber *OCMNumberForValue(NSValue *value)
45 {
46 #define CREATE_NUM(_type) ({ _type _v; [value getValue:&_v]; @(_v); })
47 switch([value objCType][0])
48 {
49 case 'c': return CREATE_NUM(char);
50 case 'C': return CREATE_NUM(unsigned char);
51 case 'B': return CREATE_NUM(bool);
52 case 's': return CREATE_NUM(short);
53 case 'S': return CREATE_NUM(unsigned short);
54 case 'i': return CREATE_NUM(int);
55 case 'I': return CREATE_NUM(unsigned int);
56 case 'l': return CREATE_NUM(long);
57 case 'L': return CREATE_NUM(unsigned long);
58 case 'q': return CREATE_NUM(long long);
59 case 'Q': return CREATE_NUM(unsigned long long);
60 case 'f': return CREATE_NUM(float);
61 case 'd': return CREATE_NUM(double);
62 default: return nil;
63 }
64 }
65
66
67 - (BOOL)getBytes:(void *)outputBuf objCType:(const char *)targetType
68 {
69 /*
70 * See if they are similar number types, and if we can convert losslessly be tween them.
71 * For the most part, we set things up to use CFNumberGetValue, which return s false if
72 * conversion will be lossy.
73 */
74 CFNumberType inputType = OCMNumberTypeForObjCType([self objCType]);
75 CFNumberType outputType = OCMNumberTypeForObjCType(targetType);
76
77 if(inputType == 0 || outputType == 0) // one or both are non-number types
78 return NO;
79
80 NSNumber *inputNumber = [self isKindOfClass:[NSNumber class]] ? (NSNumber *) self : OCMNumberForValue(self);
81
82 /*
83 * Due to some legacy, back-compatible requirements in CFNumber.c, CFNumberG etValue can return true for
84 * some conversions which should not be allowed (by reading source, conversi ons from integer types to
85 * 8-bit or 16-bit integer types). So, check ourselves.
86 */
87 long long min;
88 long long max;
89 long long val = [inputNumber longLongValue];
90 switch(targetType[0])
91 {
92 case 'B':
93 case 'c': min = CHAR_MIN; max = CHAR_MAX; break;
94 case 'C': min = 0; max = UCHAR_MAX; break;
95 case 's': min = SHRT_MIN; max = SHRT_MAX; break;
96 case 'S': min = 0; max = USHRT_MAX; break;
97 default: min = LLONG_MIN; max = LLONG_MAX; break;
98 }
99 if(val < min || val > max)
100 return NO;
101
102 /* Get the number, and return NO if the value was out of range or conversion was lossy */
103 return CFNumberGetValue((CFNumberRef)inputNumber, outputType, outputBuf);
104 }
105
106
107 @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