forked from core-plot/core-plot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCPTMutableNumericDataTests.m
More file actions
257 lines (197 loc) · 10.8 KB
/
CPTMutableNumericDataTests.m
File metadata and controls
257 lines (197 loc) · 10.8 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
#import "CPTMutableNumericDataTests.h"
#import "CPTExceptions.h"
#import "CPTMutableNumericData+TypeConversion.h"
#import "CPTNumericData+TypeConversion.h"
@implementation CPTMutableNumericDataTests
-(void)testNilShapeGivesSingleDimension
{
NSMutableData *data = [NSMutableData dataWithLength:1 * sizeof(float)];
CPTMutableNumericData *nd = [[CPTMutableNumericData alloc] initWithData:data
dataTypeString:@"=f4"
shape:nil];
NSUInteger actual = nd.numberOfDimensions;
NSUInteger expected = 1;
XCTAssertEqual(actual, expected, @"numberOfDimensions == 1");
expected = [nd.shape count];
XCTAssertEqual(actual, expected, @"numberOfDimensions == 1");
}
-(void)testNumberOfDimensionsGivesShapeCount
{
id shape = @[@2, @2, @2];
NSUInteger nElems = 2 * 2 * 2;
NSMutableData *data = [NSMutableData dataWithLength:nElems * sizeof(float)];
CPTMutableNumericData *nd = [[CPTMutableNumericData alloc] initWithData:data
dataType:CPTDataType( CPTFloatingPointDataType, sizeof(float), NSHostByteOrder() )
shape:shape];
XCTAssertEqual(nd.numberOfDimensions, nd.shape.count, @"numberOfDimensions == shape.count == 3");
}
-(void)testNilShapeCorrectElementCount
{
NSUInteger nElems = 13;
NSMutableData *data = [NSMutableData dataWithLength:nElems * sizeof(float)];
CPTMutableNumericData *nd = [[CPTMutableNumericData alloc] initWithData:data
dataTypeString:@"=f4"
shape:nil];
XCTAssertEqual(nd.numberOfDimensions, (NSUInteger)1, @"numberOfDimensions == 1");
NSUInteger prod = 1;
for ( NSNumber *num in nd.shape ) {
prod *= [num unsignedIntValue];
}
XCTAssertEqual(prod, nElems, @"prod == nElems");
}
-(void)testIllegalShapeRaisesException
{
id shape = @[@2, @2, @2];
NSUInteger nElems = 5;
CPTNumericData *testData = nil;
NSMutableData *data = [NSMutableData dataWithLength:nElems * sizeof(NSUInteger)];
XCTAssertThrowsSpecificNamed(testData = [[CPTMutableNumericData alloc] initWithData:data
dataType:CPTDataType( CPTUnsignedIntegerDataType, sizeof(NSUInteger), NSHostByteOrder() )
shape:shape],
NSException,
CPTNumericDataException,
@"Illegal shape should throw");
}
-(void)testReturnsDataLength
{
NSMutableData *data = [NSMutableData dataWithLength:10 * sizeof(float)];
CPTMutableNumericData *nd = [[CPTMutableNumericData alloc] initWithData:data
dataTypeString:@"=f4"
shape:nil];
NSUInteger expected = 10 * sizeof(float);
NSUInteger actual = [nd length];
XCTAssertEqual(expected, actual, @"data length");
}
-(void)testBytesEqualDataBytes
{
NSUInteger nElements = 10;
NSMutableData *data = [NSMutableData dataWithLength:nElements * sizeof(NSInteger)];
NSInteger *intData = (NSInteger *)[data mutableBytes];
for ( NSUInteger i = 0; i < nElements; i++ ) {
intData[i] = (NSInteger)i;
}
CPTMutableNumericData *nd = [[CPTMutableNumericData alloc] initWithData:data
dataType:CPTDataType( CPTIntegerDataType, sizeof(NSInteger), NSHostByteOrder() )
shape:nil];
NSMutableData *expected = data;
XCTAssertTrue([expected isEqualToData:nd.data], @"data isEqualToData:");
}
-(void)testArchivingRoundTrip
{
NSUInteger nElems = 10;
NSMutableData *data = [NSMutableData dataWithLength:nElems * sizeof(float)];
float *samples = (float *)[data mutableBytes];
for ( NSUInteger i = 0; i < nElems; i++ ) {
samples[i] = sinf(i);
}
CPTMutableNumericData *nd = [[CPTMutableNumericData alloc] initWithData:data
dataType:CPTDataType( CPTFloatingPointDataType, sizeof(float), NSHostByteOrder() )
shape:nil];
CPTMutableNumericData *nd2 = [NSKeyedUnarchiver unarchiveObjectWithData:[NSKeyedArchiver archivedDataWithRootObject:nd]];
XCTAssertTrue([nd.data isEqualToData:nd2.data], @"equal data");
CPTNumericDataType ndType = nd.dataType;
CPTNumericDataType nd2Type = nd2.dataType;
XCTAssertEqual(ndType.dataTypeFormat, nd2Type.dataTypeFormat, @"dataType.dataTypeFormat equal");
XCTAssertEqual(ndType.sampleBytes, nd2Type.sampleBytes, @"dataType.sampleBytes equal");
XCTAssertEqual(ndType.byteOrder, nd2Type.byteOrder, @"dataType.byteOrder equal");
XCTAssertEqualObjects(nd.shape, nd2.shape, @"shapes equal");
}
-(void)testNumberOfSamplesCorrectForDataType
{
NSUInteger nElems = 10;
NSMutableData *data = [NSMutableData dataWithLength:nElems * sizeof(float)];
float *samples = (float *)[data mutableBytes];
for ( NSUInteger i = 0; i < nElems; i++ ) {
samples[i] = sinf(i);
}
CPTMutableNumericData *nd = [[CPTMutableNumericData alloc] initWithData:data
dataType:CPTDataType( CPTFloatingPointDataType, sizeof(float), NSHostByteOrder() )
shape:nil];
XCTAssertEqual([nd numberOfSamples], nElems, @"numberOfSamples == nElems");
nElems = 10;
data = [NSMutableData dataWithLength:nElems * sizeof(char)];
char *charSamples = (char *)[data mutableBytes];
for ( NSUInteger i = 0; i < nElems; i++ ) {
charSamples[i] = (char)lrint(sin(i) * 100.0);
}
nd = [[CPTMutableNumericData alloc] initWithData:data
dataType:CPTDataType( CPTIntegerDataType, sizeof(char), NSHostByteOrder() )
shape:nil];
XCTAssertEqual([nd numberOfSamples], nElems, @"numberOfSamples == nElems");
}
-(void)testDataTypeAccessorsCorrectForDataType
{
NSUInteger nElems = 10;
NSMutableData *data = [NSMutableData dataWithLength:nElems * sizeof(float)];
float *samples = (float *)[data mutableBytes];
for ( NSUInteger i = 0; i < nElems; i++ ) {
samples[i] = sinf(i);
}
CPTMutableNumericData *nd = [[CPTMutableNumericData alloc] initWithData:data
dataType:CPTDataType( CPTFloatingPointDataType, sizeof(float), NSHostByteOrder() )
shape:nil];
XCTAssertEqual([nd dataTypeFormat], CPTFloatingPointDataType, @"dataTypeFormat");
XCTAssertEqual([nd sampleBytes], sizeof(float), @"sampleBytes");
XCTAssertEqual([nd byteOrder], NSHostByteOrder(), @"byteOrder");
}
-(void)testConvertTypeConvertsType
{
NSUInteger numberOfSamples = 10;
NSMutableData *data = [NSMutableData dataWithLength:numberOfSamples * sizeof(float)];
float *samples = (float *)[data mutableBytes];
for ( NSUInteger i = 0; i < numberOfSamples; i++ ) {
samples[i] = sinf(i);
}
CPTMutableNumericData *fd = [[CPTMutableNumericData alloc] initWithData:data
dataType:CPTDataType( CPTFloatingPointDataType, sizeof(float), NSHostByteOrder() )
shape:nil];
CPTNumericData *dd = [fd dataByConvertingToType:CPTFloatingPointDataType
sampleBytes:sizeof(double)
byteOrder:NSHostByteOrder()];
const double *doubleSamples = (const double *)[dd.data bytes];
for ( NSUInteger i = 0; i < numberOfSamples; i++ ) {
XCTAssertTrue(samples[i] == doubleSamples[i], @"(float)%g != (double)%g", samples[i], doubleSamples[i]);
}
}
-(void)testSamplePointerCorrect
{
NSUInteger nElems = 10;
NSMutableData *data = [NSMutableData dataWithLength:nElems * sizeof(float)];
float *samples = (float *)[data mutableBytes];
for ( NSUInteger i = 0; i < nElems; i++ ) {
samples[i] = sinf(i);
}
CPTMutableNumericData *fd = [[CPTMutableNumericData alloc] initWithData:data
dataType:CPTDataType( CPTFloatingPointDataType, sizeof(float), NSHostByteOrder() )
shape:nil];
XCTAssertEqual( ( (float *)[fd mutableBytes] ) + 4, (float *)[fd mutableSamplePointer:4], @"%p,%p", samples + 4, (float *)[fd mutableSamplePointer:4] );
XCTAssertEqual( ( (float *)[fd mutableBytes] ), (float *)[fd mutableSamplePointer:0], @"" );
XCTAssertEqual( ( (float *)[fd mutableBytes] ) + nElems - 1, (float *)[fd mutableSamplePointer:nElems - 1], @"" );
XCTAssertNil([fd samplePointer:nElems], @"too many samples");
}
-(void)testSampleValueCorrect
{
NSUInteger nElems = 10;
NSMutableData *data = [NSMutableData dataWithLength:nElems * sizeof(float)];
float *samples = (float *)[data mutableBytes];
for ( NSUInteger i = 0; i < nElems; i++ ) {
samples[i] = sinf(i);
}
CPTMutableNumericData *fd = [[CPTMutableNumericData alloc] initWithData:data
dataType:CPTDataType( CPTFloatingPointDataType, sizeof(float), NSHostByteOrder() )
shape:nil];
XCTAssertEqualWithAccuracy([[fd sampleValue:0] doubleValue], sin(0), 0.01, @"sample value");
XCTAssertEqualWithAccuracy([[fd sampleValue:1] doubleValue], sin(1), 0.01, @"sample value");
}
-(void)testMutableCopy
{
const NSUInteger nElems = 10;
NSMutableData *data = [NSMutableData dataWithLength:nElems * sizeof(float)];
CPTNumericData *nd = [[CPTNumericData alloc] initWithData:data
dataType:CPTDataType( CPTFloatingPointDataType, sizeof(float), NSHostByteOrder() )
shape:nil];
CPTMutableNumericData *ndCopy = [nd mutableCopy];
// should be mutable--if not, this will error
ndCopy.dataType = CPTDataType( CPTFloatingPointDataType, sizeof(double), NSHostByteOrder() );
}
@end