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
|
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
|
-
+
-
-
-
+
+
-
+
-
+
-
-
+
+
-
-
+
+
-
-
+
+
-
+
-
+
-
-
-
-
-
+
+
+
+
+
-
-
+
+
-
-
-
-
-
+
+
+
+
+
-
-
+
-
-
+
-
+
-
-
-
+
+
+
-
-
+
+
-
+
-
-
-
+
+
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
-
-
-
-
+
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
-
-
-
-
+
+
+
+
+
-
+
-
-
-
-
+
+
-
-
+
+
-
-
-
-
+
+
-
-
+
+
-
+
+
+
+
-
+
-
-
+
+
+
-
+
+
+
-
-
+
+
+
+
-
-
+
+
+
+
-
-
+
+
+
+
-
-
+
+
+
+
+
-
+
-
-
+
+
-
+
-
-
-
+
+
-
+
-
-
+
+
-
+
-
-
+
+
|
#include "config.h"
#include <string.h>
#import "TestsAppDelegate.h"
static OFString *module = @"OFData";
static OFString *const module = @"OFData";
const char *str = "Hello!";
@implementation TestsAppDelegate (OFDataTests)
- (void)dataTests
{
void *pool = objc_autoreleasePoolPush();
OFMutableData *mutable;
OFData *immutable;
OFMutableData *mutableData;
OFData *data;
void *raw[2];
of_range_t range;
OFRange range;
TEST(@"+[dataWithItemSize:]",
(mutable = [OFMutableData dataWithItemSize: 4096]))
(mutableData = [OFMutableData dataWithItemSize: 4096]))
raw[0] = of_alloc(1, 4096);
raw[1] = of_alloc(1, 4096);
raw[0] = OFAllocMemory(1, 4096);
raw[1] = OFAllocMemory(1, 4096);
memset(raw[0], 0xFF, 4096);
memset(raw[1], 0x42, 4096);
TEST(@"-[addItem:]", R([mutable addItem: raw[0]]) &&
R([mutable addItem: raw[1]]))
TEST(@"-[addItem:]", R([mutableData addItem: raw[0]]) &&
R([mutableData addItem: raw[1]]))
TEST(@"-[itemAtIndex:]",
memcmp([mutable itemAtIndex: 0], raw[0], 4096) == 0 &&
memcmp([mutable itemAtIndex: 1], raw[1], 4096) == 0)
memcmp([mutableData itemAtIndex: 0], raw[0], 4096) == 0 &&
memcmp([mutableData itemAtIndex: 1], raw[1], 4096) == 0)
TEST(@"-[lastItem]", memcmp(mutable.lastItem, raw[1], 4096) == 0)
TEST(@"-[lastItem]", memcmp(mutableData.lastItem, raw[1], 4096) == 0)
TEST(@"-[count]", mutable.count == 2)
TEST(@"-[count]", mutableData.count == 2)
TEST(@"-[isEqual:]",
(immutable = [OFData dataWithItems: mutable.items
count: mutable.count
itemSize: mutable.itemSize]) &&
[immutable isEqual: mutable] &&
R([mutable removeLastItem]) && ![mutable isEqual: immutable])
(data = [OFData dataWithItems: mutableData.items
count: mutableData.count
itemSize: mutableData.itemSize]) &&
[data isEqual: mutableData] &&
R([mutableData removeLastItem]) && ![mutableData isEqual: data])
TEST(@"-[mutableCopy]",
(mutable = [[immutable mutableCopy] autorelease]) &&
[mutable isEqual: immutable])
(mutableData = [[data mutableCopy] autorelease]) &&
[mutableData isEqual: data])
TEST(@"-[compare]", [mutable compare: immutable] == 0 &&
R([mutable removeLastItem]) &&
[immutable compare: mutable] == OF_ORDERED_DESCENDING &&
[mutable compare: immutable] == OF_ORDERED_ASCENDING &&
[[OFData dataWithItems: "aa"
TEST(@"-[compare]", [mutableData compare: data] == 0 &&
R([mutableData removeLastItem]) &&
[data compare: mutableData] == OFOrderedDescending &&
[mutableData compare: data] == OFOrderedAscending &&
[[OFData dataWithItems: "aa" count: 2] compare:
count: 2] compare:
[OFData dataWithItems: "z"
[OFData dataWithItems: "z" count: 1]] == OFOrderedAscending)
count: 1]] == OF_ORDERED_ASCENDING)
TEST(@"-[hash]", immutable.hash == 0x634A529F)
TEST(@"-[hash]", data.hash == 0x634A529F)
mutable = [OFMutableData dataWithItems: "abcdef"
mutableData = [OFMutableData dataWithItems: "abcdef" count: 6];
count: 6];
TEST(@"-[removeLastItem]", R([mutable removeLastItem]) &&
mutable.count == 5 && memcmp(mutable.items, "abcde", 5) == 0)
TEST(@"-[removeLastItem]",
R([mutableData removeLastItem]) && mutableData.count == 5 &&
memcmp(mutableData.items, "abcde", 5) == 0)
TEST(@"-[removeItemsInRange:]",
R([mutable removeItemsInRange: of_range(1, 2)]) &&
mutable.count == 3 && memcmp(mutable.items, "ade", 3) == 0)
R([mutableData removeItemsInRange: OFRangeMake(1, 2)]) &&
mutableData.count == 3 && memcmp(mutableData.items, "ade", 3) == 0)
TEST(@"-[insertItems:atIndex:count:]",
R([mutable insertItems: "bc"
R([mutableData insertItems: "bc" atIndex: 1 count: 2]) &&
atIndex: 1
count: 2]) && mutable.count == 5 &&
memcmp(mutable.items, "abcde", 5) == 0)
mutableData.count == 5 &&
memcmp(mutableData.items, "abcde", 5) == 0)
immutable = [OFData dataWithItems: "aaabaccdacaabb"
count: 7
itemSize: 2];
TEST(@"-[rangeOfData:options:range:]",
R(range = [immutable rangeOfData: [OFData dataWithItems: "aa"
count: 1
itemSize: 2]
options: 0
range: of_range(0, 7)]) &&
range.location == 0 && range.length == 1 &&
R(range = [immutable rangeOfData: [OFData dataWithItems: "aa"
count: 1
itemSize: 2]
options: OF_DATA_SEARCH_BACKWARDS
range: of_range(0, 7)]) &&
data = [OFData dataWithItems: "aaabaccdacaabb" count: 7 itemSize: 2];
range = [data rangeOfData: [OFData dataWithItems: "aa"
count: 1
itemSize: 2]
options: 0
range: OFRangeMake(0, 7)];
TEST(@"-[rangeOfData:options:range:] #1",
range.location == 0 && range.length == 1)
range = [data rangeOfData: [OFData dataWithItems: "aa"
count: 1
itemSize: 2]
options: OFDataSearchBackwards
range: OFRangeMake(0, 7)];
TEST(@"-[rangeOfData:options:range:] #2",
range.location == 5 && range.length == 1)
range = [data rangeOfData: [OFData dataWithItems: "ac"
count: 1
itemSize: 2]
options: 0
range: OFRangeMake(0, 7)];
range.location == 5 && range.length == 1 &&
R(range = [immutable rangeOfData: [OFData dataWithItems: "ac"
count: 1
itemSize: 2]
options: 0
TEST(@"-[rangeOfData:options:range:] #3",
range: of_range(0, 7)]) &&
range.location == 2 && range.length == 1 &&
R(range = [immutable rangeOfData: [OFData dataWithItems: "aabb"
count: 2
itemSize: 2]
options: 0
range: of_range(0, 7)]) &&
range.location == 5 && range.length == 2 &&
R(range = [immutable rangeOfData: [OFData dataWithItems: "aa"
count: 1
itemSize: 2]
options: 0
range: of_range(1, 6)]) &&
range.location == 5 && range.length == 1 &&
R(range = [immutable rangeOfData: [OFData dataWithItems: "aa"
count: 1
itemSize: 2]
options: OF_DATA_SEARCH_BACKWARDS
range: of_range(0, 5)]) &&
range.location == 2 && range.length == 1)
range = [data rangeOfData: [OFData dataWithItems: "aabb"
count: 2
itemSize: 2]
options: 0
range: OFRangeMake(0, 7)];
TEST(@"-[rangeOfData:options:range:] #4",
range.location == 5 && range.length == 2)
TEST(@"-[rangeOfData:options:range:] #5",
R(range = [data rangeOfData: [OFData dataWithItems: "aa"
count: 1
itemSize: 2]
options: 0
range: OFRangeMake(1, 6)]) &&
range.location == 5 && range.length == 1)
range = [data rangeOfData: [OFData dataWithItems: "aa"
count: 1
itemSize: 2]
options: OFDataSearchBackwards
range: OFRangeMake(0, 5)];
TEST(@"-[rangeOfData:options:range:] #6",
range.location == 0 && range.length == 1)
EXPECT_EXCEPTION(
@"-[rangeOfData:options:range:] failing on different itemSize",
OFInvalidArgumentException,
[immutable rangeOfData: [OFData dataWithItems: "aaa"
count: 1
itemSize: 3]
options: 0
range: of_range(0, 1)])
[data rangeOfData: [OFData dataWithItems: "aaa"
count: 1
itemSize: 3]
options: 0
range: OFRangeMake(0, 1)])
EXPECT_EXCEPTION(
@"-[rangeOfData:options:range:] failing on out of range",
OFOutOfRangeException,
[immutable rangeOfData: [OFData dataWithItems: ""
[data rangeOfData: [OFData dataWithItems: "" count: 0 itemSize: 2]
count: 0
itemSize: 2]
options: 0
range: of_range(8, 1)])
options: 0
range: OFRangeMake(8, 1)])
TEST(@"-[subdataWithRange:]",
[[immutable subdataWithRange: of_range(2, 4)]
isEqual: [OFData dataWithItems: "accdacaa"
[[data subdataWithRange: OFRangeMake(2, 4)]
isEqual: [OFData dataWithItems: "accdacaa" count: 4 itemSize: 2]] &&
count: 4
itemSize: 2]] &&
[[mutable subdataWithRange: of_range(2, 3)]
isEqual: [OFData dataWithItems: "cde"
[[mutableData subdataWithRange: OFRangeMake(2, 3)]
isEqual: [OFData dataWithItems: "cde" count: 3]])
count: 3]])
EXPECT_EXCEPTION(@"-[subdataWithRange:] failing on out of range #1",
OFOutOfRangeException, [immutable subdataWithRange: of_range(7, 1)])
OFOutOfRangeException,
[data subdataWithRange: OFRangeMake(7, 1)])
EXPECT_EXCEPTION(@"-[subdataWithRange:] failing on out of range #2",
OFOutOfRangeException, [mutable subdataWithRange: of_range(6, 1)])
OFOutOfRangeException,
[mutableData subdataWithRange: OFRangeMake(6, 1)])
TEST(@"-[stringByMD5Hashing]",
[mutableData.stringByMD5Hashing
TEST(@"-[MD5Hash]", [mutable.MD5Hash isEqual: @"abcde".MD5Hash])
isEqual: @"ab56b4d92b40713acc5af89985d4b786"])
TEST(@"-[RIPEMD160Hash]", [mutable.RIPEMD160Hash
isEqual: @"abcde".RIPEMD160Hash])
TEST(@"-[stringByRIPEMD160Hashing]",
[mutableData.stringByRIPEMD160Hashing
isEqual: @"973398b6e6c6cfa6b5e6a5173f195ce3274bf828"])
TEST(@"-[SHA1Hash]", [mutable.SHA1Hash isEqual: @"abcde".SHA1Hash])
TEST(@"-[stringBySHA1Hashing]",
[mutableData.stringBySHA1Hashing
isEqual: @"03de6c570bfe24bfc328ccd7ca46b76eadaf4334"])
TEST(@"-[SHA224Hash]", [mutable.SHA224Hash
isEqual: @"abcde".SHA224Hash])
TEST(@"-[stringBySHA224Hashing]",
[mutableData.stringBySHA224Hashing
isEqual: @"bdd03d560993e675516ba5a50638b6531ac2ac3d5847c61916cfced6"
])
TEST(@"-[SHA256Hash]", [mutable.SHA256Hash
isEqual: @"abcde".SHA256Hash])
TEST(@"-[stringBySHA256Hashing]",
[mutableData.stringBySHA256Hashing
isEqual: @"36bbe50ed96841d10443bcb670d6554f0a34b761be67ec9c4a8ad2c0"
@"c44ca42c"])
TEST(@"-[SHA384Hash]", [mutable.SHA384Hash
isEqual: @"abcde".SHA384Hash])
TEST(@"-[stringBySHA384Hashing]",
[mutableData.stringBySHA384Hashing
isEqual: @"4c525cbeac729eaf4b4665815bc5db0c84fe6300068a727cf74e2813"
@"521565abc0ec57a37ee4d8be89d097c0d2ad52f0"])
TEST(@"-[SHA512Hash]", [mutable.SHA512Hash
isEqual: @"abcde".SHA512Hash])
TEST(@"-[stringBySHA512Hashing]",
[mutableData.stringBySHA512Hashing
isEqual: @"878ae65a92e86cac011a570d4c30a7eaec442b85ce8eca0c2952b5e3"
@"cc0628c2e79d889ad4d5c7c626986d452dd86374b6ffaa7cd8b67665"
@"bef2289a5c70b0a1"])
TEST(@"-[stringByBase64Encoding]",
[mutable.stringByBase64Encoding isEqual: @"YWJjZGU="])
[mutableData.stringByBase64Encoding isEqual: @"YWJjZGU="])
TEST(@"+[dataWithBase64EncodedString:]",
memcmp([[OFData dataWithBase64EncodedString: @"YWJjZGU="]
items], "abcde", 5) == 0)
memcmp([[OFData dataWithBase64EncodedString: @"YWJjZGU="] items],
"abcde", 5) == 0)
TEST(@"Building strings",
(mutable = [OFMutableData dataWithItems: str
(mutableData = [OFMutableData dataWithItems: "Hello!" count: 6]) &&
count: 6]) &&
R([mutable addItem: ""]) &&
strcmp(mutable.items, str) == 0)
R([mutableData addItem: ""]) &&
strcmp(mutableData.items, "Hello!") == 0)
EXPECT_EXCEPTION(@"Detect out of range in -[itemAtIndex:]",
OFOutOfRangeException, [mutable itemAtIndex: mutable.count])
OFOutOfRangeException, [mutableData itemAtIndex: mutableData.count])
EXPECT_EXCEPTION(@"Detect out of range in -[addItems:count:]",
OFOutOfRangeException, [mutable addItems: raw[0]
count: SIZE_MAX])
OFOutOfRangeException,
[mutableData addItems: raw[0] count: SIZE_MAX])
EXPECT_EXCEPTION(@"Detect out of range in -[removeItemsInRange:]",
OFOutOfRangeException,
[mutable removeItemsInRange: of_range(mutable.count, 1)])
[mutableData removeItemsInRange: OFRangeMake(mutableData.count, 1)])
free(raw[0]);
free(raw[1]);
OFFreeMemory(raw[0]);
OFFreeMemory(raw[1]);
objc_autoreleasePoolPop(pool);
}
@end
|