-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathTestItem.m
More file actions
323 lines (251 loc) · 11.7 KB
/
TestItem.m
File metadata and controls
323 lines (251 loc) · 11.7 KB
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
/*
Copyright (C) 2013 Eric Wasylishen, Quentin Mathe
Date: August 2013
License: MIT (see COPYING)
*/
#import "TestCommon.h"
#import "COItem+Binary.h"
#import "COItem+JSON.h"
@interface TestItem : NSObject <UKTest>
{
}
@end
@implementation TestItem
- (void)validateJSONRoundTrip: (COItem *)item
{
NSData *data = item.JSONData;
COItem *roundTrip = [[COItem alloc] initWithJSONData: data];
UKObjectsEqual(item, roundTrip);
}
- (void)validateBinaryRoundTrip: (COItem *)item
{
NSData *data = item.dataValue;
COItem *roundTrip = [[COItem alloc] initWithData: data];
UKObjectsEqual(item, roundTrip);
}
- (void)validateRoundTrips: (COItem *)item
{
[self validateJSONRoundTrip: item];
[self validateBinaryRoundTrip: item];
}
- (void)testInt
{
COMutableItem *item = [COMutableItem item];
[item setValue: @1 forAttribute: @"1" type: kCOTypeInt64];
[item setValue: @-1 forAttribute: @"-1" type: kCOTypeInt64];
[item setValue: @256 forAttribute: @"256" type: kCOTypeInt64];
[item setValue: @-256 forAttribute: @"-256" type: kCOTypeInt64];
[item setValue: @-65535 forAttribute: @"-65535" type: kCOTypeInt64];
[item setValue: @65535 forAttribute: @"65535" type: kCOTypeInt64];
[item setValue: @2000000000 forAttribute: @"2000000000" type: kCOTypeInt64];
[item setValue: @-2000000000 forAttribute: @"-2000000000" type: kCOTypeInt64];
[item setValue: @8000000000LL forAttribute: @"8000000000" type: kCOTypeInt64];
[item setValue: @-8000000000LL forAttribute: @"-8000000000" type: kCOTypeInt64];
[item setValue: [NSNull null] forAttribute: @"null" type: kCOTypeInt64];
[item setValue: @[@1, @8000000000LL]
forAttribute: @"[1, 8000000000]"
type: kCOTypeArray | kCOTypeInt64];
[item setValue: S(@1, @8000000000LL)
forAttribute: @"(1, 8000000000)"
type: kCOTypeSet | kCOTypeInt64];
[self validateRoundTrips: item];
}
/* See basicNumberFromDecimalNumber() in COItem+JSON.m */
- (void)testJSONDoubleEquality
{
NSNumber *value = @123.456789012;
UKTrue(strcmp([value objCType], "d") == 0);
NSNumber *decimalValue = [NSDecimalNumber numberWithDouble: 123.456789012];
NSData *data = [NSJSONSerialization dataWithJSONObject: @{@"number": value}
options: 0
error: NULL];
NSNumber *roundTripValue =
[NSJSONSerialization JSONObjectWithData: data options: 0 error: NULL][@"number"];
NSNumber *newValue = @(roundTripValue.doubleValue);
NSNumber *newValueFromDesc = @(roundTripValue.description.doubleValue);
#ifndef GNUSTEP
// NOTE: Doesn't matter on GNUstep since newValue is not a NSDecimalNumber.
UKTrue([[NSDecimalNumber defaultBehavior] scale] == NSDecimalNoScale);
#endif
NSLog(@"Double representation in JSON: %@",
[[NSString alloc] initWithData: data encoding: NSUTF8StringEncoding]);
NSLog(@"value doubleValue: %.20f, description: %@, class: %@",
value.doubleValue, value, [value class]);
NSLog(@"decimalValue doubleValue: %.20f, description: %@, class: %@",
decimalValue.doubleValue, decimalValue, [decimalValue class]);
NSLog(@"roundTripValue doubleValue: %.20f, description: %@, class: %@",
roundTripValue.doubleValue, roundTripValue, [roundTripValue class]);
NSLog(@"newValue doubleValue: %.20f, description: %@, class: %@",
newValue.doubleValue, newValue, [newValue class]);
NSLog(@"newValueFromDesc doubleValue: %.20f, description: %@, class: %@",
newValueFromDesc.doubleValue, newValueFromDesc, [newValueFromDesc class]);
UKTrue([value compare: newValueFromDesc] == NSOrderedSame);
UKTrue([newValueFromDesc compare: value] == NSOrderedSame);
}
- (void)testDouble
{
COMutableItem *item = [COMutableItem item];
[item setValue: @3.14 forAttribute: @"3.14" type: kCOTypeDouble];
[item setValue: @123.456789012 forAttribute: @"123.456789012" type: kCOTypeDouble];
[item setValue: [NSNull null] forAttribute: @"null" type: kCOTypeDouble];
[item setValue: @[@3.14, @123.456789012]
forAttribute: @"[3.14, 123.456789012]"
type: kCOTypeArray | kCOTypeDouble];
[item setValue: S(@3.14, @123.456789012)
forAttribute: @"(3.14, 123.456789012)"
type: kCOTypeSet | kCOTypeDouble];
[self validateRoundTrips: item];
}
- (void)testString
{
COMutableItem *item = [COMutableItem item];
[item setValue: @"abc" forAttribute: @"abc" type: kCOTypeString];
[item setValue: [NSNull null] forAttribute: @"null" type: kCOTypeString];
[item setValue: @[@"abc", @"def"]
forAttribute: @"[abc, def]"
type: kCOTypeArray | kCOTypeString];
[item setValue: S(@"abc", @"def")
forAttribute: @"(abc, def)"
type: kCOTypeSet | kCOTypeString];
[self validateRoundTrips: item];
}
- (void)testBlob
{
NSUInteger big = UINT8_MAX * 2;
void *buffer = malloc(big);
NSData *threeBytes = [NSData dataWithBytes: "xyz" length: 3];
NSData *zeroBytes = [NSData data];
NSData *bigBlob = [NSData dataWithBytes: buffer length: big];
free(buffer);
ETAssert([bigBlob length] > UINT8_MAX);
COMutableItem *item = [COMutableItem item];
[item setValue: zeroBytes forAttribute: @"zeroBytes" type: kCOTypeBlob];
[item setValue: threeBytes forAttribute: @"xyz" type: kCOTypeBlob];
[item setValue: [NSNull null] forAttribute: @"null" type: kCOTypeBlob];
[item setValue: @[zeroBytes, threeBytes, bigBlob]
forAttribute: @"[zeroBytes, xyz, bigBlob]"
type: kCOTypeArray | kCOTypeBlob];
[item setValue: S(zeroBytes, threeBytes, bigBlob)
forAttribute: @"(zeroBytes, xyz, bigBlob)"
type: kCOTypeSet | kCOTypeBlob];
[self validateRoundTrips: item];
}
- (void)testReference
{
ETUUID *persistentRoot = [ETUUID UUID];
ETUUID *branch = [ETUUID UUID];
ETUUID *rootObject = [ETUUID UUID];
COPath *persistentRootPath = [COPath pathWithPersistentRoot: persistentRoot];
COPath *branchPath = [COPath pathWithPersistentRoot: persistentRoot branch: branch];
COMutableItem *item = [COMutableItem item];
[item setValue: persistentRootPath forAttribute: @"persistentRootPath" type: kCOTypeReference];
[item setValue: branchPath forAttribute: @"branchPath" type: kCOTypeReference];
[item setValue: rootObject forAttribute: @"rootObject" type: kCOTypeReference];
[item setValue: [NSNull null] forAttribute: @"null" type: kCOTypeReference];
[item setValue: @[persistentRootPath, branchPath, rootObject]
forAttribute: @"[persistentRootPath, branchPath, rootObject]"
type: kCOTypeArray | kCOTypeReference];
[item setValue: S(persistentRootPath, branchPath, rootObject)
forAttribute: @"(persistentRootPath, branchPath, rootObject)"
type: kCOTypeSet | kCOTypeReference];
[self validateRoundTrips: item];
}
- (void)testCompositeReference
{
ETUUID *rootObject = [ETUUID UUID];
ETUUID *rootObject2 = [ETUUID UUID];
COMutableItem *item = [COMutableItem item];
[item setValue: rootObject forAttribute: @"rootObject" type: kCOTypeCompositeReference];
[item setValue: [NSNull null] forAttribute: @"null" type: kCOTypeCompositeReference];
[item setValue: @[rootObject, rootObject2]
forAttribute: @"[rootObject, rootObject2]"
type: kCOTypeArray | kCOTypeCompositeReference];
[item setValue: S(rootObject, rootObject2)
forAttribute: @"(rootObject, rootObject2)"
type: kCOTypeSet | kCOTypeCompositeReference];
[self validateRoundTrips: item];
}
- (void)testAttachment
{
COAttachmentID *threeBytes = [[COAttachmentID alloc] initWithData: [NSData dataWithBytes: "xyz"
length: 3]];
COAttachmentID *zeroBytes = [[COAttachmentID alloc] initWithData: [NSData data]];
COMutableItem *item = [COMutableItem item];
[item setValue: zeroBytes forAttribute: @"zeroBytes" type: kCOTypeAttachment];
[item setValue: threeBytes forAttribute: @"xyz" type: kCOTypeAttachment];
[item setValue: [NSNull null] forAttribute: @"null" type: kCOTypeAttachment];
[item setValue: @[zeroBytes, threeBytes]
forAttribute: @"[zeroBytes, xyz]"
type: kCOTypeArray | kCOTypeAttachment];
[item setValue: S(zeroBytes, threeBytes)
forAttribute: @"(zeroBytes, xyz)"
type: kCOTypeSet | kCOTypeAttachment];
[self validateRoundTrips: item];
}
- (COItem *)roundTrip: (COItem *)anItem
{
return [[COMutableItem alloc] initWithData: anItem.dataValue];
}
- (void)testMutability
{
COItem *immutable = [COItem itemWithTypesForAttributes: @{@"key1": @(kCOTypeString | kCOTypeSet),
@"key2": @(kCOTypeString | kCOTypeArray),
@"name": @(kCOTypeString)}
valuesForAttributes: @{@"key1": [NSMutableSet setWithObject: @"a"],
@"key2": [NSMutableArray arrayWithObject: @"A"],
@"name": @"my name"}];
UKRaisesException([(COMutableItem *)immutable setValue: @"foo"
forAttribute: @"bar"
type: kCOTypeString]);
UKRaisesException([[immutable valueForAttribute: @"key1"] addObject: @"b"]);
UKRaisesException([[immutable valueForAttribute: @"key2"] addObject: @"B"]);
COMutableItem *mutable = [immutable mutableCopy];
UKDoesNotRaiseException([[mutable valueForAttribute: @"key1"] addObject: @"b"]);
UKDoesNotRaiseException([[mutable valueForAttribute: @"key2"] addObject: @"B"]);
UKIntsEqual(1, [[immutable valueForAttribute: @"key1"] count]);
UKIntsEqual(1, [[immutable valueForAttribute: @"key2"] count]);
UKIntsEqual(2, [[mutable valueForAttribute: @"key1"] count]);
UKIntsEqual(2, [[mutable valueForAttribute: @"key2"] count]);
UKRaisesException([[mutable valueForAttribute: @"name"] appendString: @"xxx"]);
}
- (void)testEquality
{
COItem *immutable = [COItem itemWithTypesForAttributes:
@{@"key1": @(kCOTypeString | kCOTypeSet),
@"key2": @(kCOTypeString | kCOTypeArray),
@"name": @(kCOTypeString)}
valuesForAttributes:
@{@"key1": [NSMutableSet setWithObject: @"a"],
@"key2": [NSMutableArray arrayWithObject: @"A"],
@"name": @"my name"}];
COMutableItem *mutable = [immutable mutableCopy];
UKObjectsEqual(immutable, mutable);
UKObjectsEqual(mutable, immutable);
[mutable setValue: @"name 2" forAttribute: @"name"];
UKObjectsNotEqual(immutable, mutable);
}
- (void)testEmptySet
{
COMutableItem *item1 = [COMutableItem item];
[item1 setValue: [NSSet set] forAttribute: @"set" type: kCOTypeString | kCOTypeSet];
[self validateRoundTrips: item1];
COMutableItem *item2 = [COMutableItem item];
UKObjectsNotEqual(item2, item1);
}
- (void)testEmptyObject
{
COMutableItem *item1 = [COMutableItem item];
[self validateRoundTrips: item1];
}
- (void)testNullInCollections
{
COMutableItem *item = [COMutableItem item];
[item setValue: @[[NSNull null]]
forAttribute: @"array of null"
type: kCOTypeArray | kCOTypeString];
[item setValue: S([NSNull null])
forAttribute: @"array of null"
type: kCOTypeSet | kCOTypeString];
[self validateRoundTrips: item];
}
@end