File: OFMutableDataTests.m

package info (click to toggle)
objfw 1.4.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 8,644 kB
  • sloc: objc: 101,491; asm: 5,083; sh: 4,092; makefile: 1,574; ansic: 709; xml: 367; pascal: 214
file content (260 lines) | stat: -rw-r--r-- 5,613 bytes parent folder | download
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
/*
 * Copyright (c) 2008-2025 Jonathan Schleifer <js@nil.im>
 *
 * All rights reserved.
 *
 * This program is free software: you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License version 3.0 only,
 * as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
 * version 3.0 for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * version 3.0 along with this program. If not, see
 * <https://www.gnu.org/licenses/>.
 */

#include "config.h"

#include <string.h>

#import "OFMutableDataTests.h"

@interface CustomMutableData: OFMutableData
{
	OFMutableData *_data;
}
@end

@implementation OFMutableDataTests
- (Class)dataClass
{
	return [CustomMutableData class];
}

- (void)setUp
{
	[super setUp];

	_mutableData = [[OFMutableData alloc] initWithItems: "abcdef" count: 6];
}

- (void)dealloc
{
	objc_release(_mutableData);

	[super dealloc];
}

- (void)testMutableCopy
{
	OTAssertEqualObjects(objc_autorelease([_data mutableCopy]), _data);
	OTAssertNotEqual(objc_autorelease([_data mutableCopy]), _data);
}

- (void)testAddItem
{
	[_mutableData addItem: "g"];

	OTAssertEqualObjects(_mutableData,
	    [OFData dataWithItems: "abcdefg" count: 7]);
}

- (void)testAddItemsCount
{
	[_mutableData addItems: "gh" count: 2];

	OTAssertEqualObjects(_mutableData,
	    [OFData dataWithItems: "abcdefgh" count: 8]);
}

- (void)testAddItemsCountThrowsOnOutOfRange
{
	OTAssertThrowsSpecific([_mutableData addItems: "" count: SIZE_MAX],
	    OFOutOfRangeException);
}

- (void)testRemoveLastItem
{
	[_mutableData removeLastItem];

	OTAssertEqualObjects(_mutableData,
	    [OFData dataWithItems: "abcde" count: 5]);
}

- (void)testRemoveItemsInRange
{
	[_mutableData removeItemsInRange: OFMakeRange(1, 2)];

	OTAssertEqualObjects(_mutableData,
	    [OFData dataWithItems: "adef" count: 4]);
}

- (void)testRemoveItemsInRangeThrowsOnOutOfRangeRange
{
	OTAssertThrowsSpecific(
	    [_mutableData removeItemsInRange: OFMakeRange(6, 1)],
	    OFOutOfRangeException);

	OTAssertThrowsSpecific(
	    [_mutableData removeItemsInRange: OFMakeRange(7, 0)],
	    OFOutOfRangeException);
}

- (void)testRemoveItemsAtIndexes
{
	OFMutableIndexSet *indexes = [OFMutableIndexSet indexSet];

	[indexes addIndex: 1];
	[indexes addIndex: 3];
	[_mutableData removeItemsAtIndexes: indexes];

	OTAssertEqualObjects(_mutableData,
	    [OFData dataWithItems: "acef" count: 4]);
}

- (void)testRemoveItemsAtIndexesThrowsOnOutOfRangeRange
{
	OFIndexSet *indexes;

	indexes = [OFIndexSet indexSetWithIndexesInRange: OFMakeRange(6, 1)];
	OTAssertThrowsSpecific([_mutableData removeItemsAtIndexes: indexes],
	    OFOutOfRangeException);

	indexes = [OFIndexSet indexSetWithIndexesInRange: OFMakeRange(7, 0)];
	OTAssertThrowsSpecific([_mutableData removeItemsAtIndexes: indexes],
	    OFOutOfRangeException);
}

- (void)testInsertItemsAtIndexCount
{
	[_mutableData insertItems: "BC" atIndex: 1 count: 2];

	OTAssertEqualObjects(_mutableData,
	    [OFData dataWithItems: "aBCbcdef" count: 8]);
}

- (void)testInsertItemsAtIndexCountThrowsOnOutOfRangeIndex
{
	OTAssertThrowsSpecific(
	    [_mutableData insertItems: "a" atIndex: 7 count: 1],
	    OFOutOfRangeException);
}

- (void)testInsertItemsAtIndexes
{
	OFMutableIndexSet *indexes = [OFMutableIndexSet indexSet];
	[indexes addIndexesInRange: OFMakeRange(1, 3)];
	[indexes addIndexesInRange: OFMakeRange(5, 2)];
	[indexes addIndexesInRange: OFMakeRange(11, 2)];

	[_mutableData insertItems: "1234567" atIndexes: indexes];

	OTAssertEqualObjects(_mutableData,
	    [OFData dataWithItems: "a123b45cdef67" count: 13]);
}

- (void)testInsertItemsAtIndexesThrowsOnOutOfRangeIndex
{
	OFIndexSet *indexes =
	    [OFIndexSet indexSetWithIndexesInRange: OFMakeRange(7, 1)];
	OTAssertThrowsSpecific(
	    [_mutableData insertItems: "a" atIndexes: indexes],
	    OFOutOfRangeException);
}
@end

@implementation CustomMutableData
- (instancetype)initWithItemSize: (size_t)itemSize
{
	self = [super init];

	@try {
		_data = [[OFMutableData alloc] initWithItemSize: itemSize];
	} @catch (id e) {
		objc_release(self);
		@throw e;
	}

	return self;
}

- (instancetype)initWithItems: (const void *)items
			count: (size_t)count
		     itemSize: (size_t)itemSize
{
	self = [super init];

	@try {
		_data = [[OFMutableData alloc] initWithItems: items
						       count: count
						    itemSize: itemSize];
	} @catch (id e) {
		objc_release(self);
		@throw e;
	}

	return self;
}

- (instancetype)initWithItemsNoCopy: (void *)items
			      count: (size_t)count
			   itemSize: (size_t)itemSize
		       freeWhenDone: (bool)freeWhenDone
{
	self = [super init];

	@try {
		_data = [[OFMutableData alloc]
		    initWithItemsNoCopy: items
				  count: count
			       itemSize: itemSize
			   freeWhenDone: freeWhenDone];
	} @catch (id e) {
		objc_release(self);
		@throw e;
	}

	return self;
}

- (size_t)count
{
	return _data.count;
}

- (size_t)itemSize
{
	return _data.itemSize;
}

- (const void *)items
{
	return _data.items;
}

- (void *)mutableItems
{
	return _data.mutableItems;
}

- (void)insertItems: (const void *)items
	    atIndex: (size_t)idx
	      count: (size_t)count
{
	[_data insertItems: items atIndex: idx count: count];
}

- (void)increaseCountBy: (size_t)count
{
	[_data increaseCountBy: count];
}

- (void)removeItemsInRange: (OFRange)range
{
	[_data removeItemsInRange: range];
}
@end