forked from core-plot/core-plot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCPTAxisLabelTests.m
More file actions
155 lines (120 loc) · 8.49 KB
/
Copy pathCPTAxisLabelTests.m
File metadata and controls
155 lines (120 loc) · 8.49 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
#import "CPTAxisLabel.h"
#import "CPTAxisLabelTests.h"
#import "CPTBorderedLayer.h"
#import "CPTMutableTextStyle.h"
#import "CPTUtilities.h"
#import <tgmath.h>
static const double precision = 1.0e-6;
@implementation CPTAxisLabelTests
static CGPoint roundPoint(CGPoint position, CGSize contentSize, CGPoint anchor);
static CGPoint roundPoint(CGPoint position, CGSize contentSize, CGPoint anchor)
{
CGPoint newPosition = position;
CGPoint newAnchor = CGPointMake(contentSize.width * anchor.x,
contentSize.height * anchor.y);
newPosition.x = ceil( position.x - newAnchor.x - CPTFloat(0.5) ) + newAnchor.x;
newPosition.y = ceil( position.y - newAnchor.y - CPTFloat(0.5) ) + newAnchor.y;
return newPosition;
}
-(void)testPositionRelativeToViewPointRaisesForInvalidDirection
{
CPTAxisLabel *label;
@try {
label = [[CPTAxisLabel alloc] initWithText:@"CPTAxisLabelTests-testPositionRelativeToViewPointRaisesForInvalidDirection" textStyle:[CPTTextStyle textStyle]];
XCTAssertThrowsSpecificNamed([label positionRelativeToViewPoint:CGPointZero forCoordinate:CPTCoordinateX inDirection:INT_MAX], NSException, NSInvalidArgumentException, @"Should raise NSInvalidArgumentException for invalid direction (type CPTSign)");
}
@finally {
label = nil;
}
}
-(void)testPositionRelativeToViewPointPositionsForXCoordinate
{
CPTAxisLabel *label;
CGFloat start = 100.0;
@try {
label = [[CPTAxisLabel alloc] initWithText:@"CPTAxisLabelTests-testPositionRelativeToViewPointPositionsForXCoordinate" textStyle:[CPTTextStyle textStyle]];
CPTLayer *contentLayer = label.contentLayer;
CGSize contentSize = contentLayer.bounds.size;
label.offset = 20.0;
CGPoint viewPoint = CGPointMake(start, start);
contentLayer.anchorPoint = CGPointZero;
contentLayer.position = CGPointZero;
[label positionRelativeToViewPoint:viewPoint
forCoordinate:CPTCoordinateX
inDirection:CPTSignNone];
CGPoint newPosition = roundPoint(CGPointMake(start - label.offset, start), contentSize, contentLayer.anchorPoint);
XCTAssertEqualWithAccuracy( contentLayer.position.x, newPosition.x, precision, @"Should add negative offset, %@ != %@", CPTStringFromPoint(contentLayer.position), CPTStringFromPoint(newPosition) );
XCTAssertEqualWithAccuracy( contentLayer.position.y, newPosition.y, precision, @"Should add negative offset, %@ != %@", CPTStringFromPoint(contentLayer.position), CPTStringFromPoint(newPosition) );
XCTAssertEqualWithAccuracy(contentLayer.anchorPoint.x, CPTFloat(1.0), precision, @"Should anchor at (1.0, 0.5)");
XCTAssertEqualWithAccuracy(contentLayer.anchorPoint.y, CPTFloat(0.5), precision, @"Should anchor at (1.0, 0.5)");
contentLayer.anchorPoint = CGPointZero;
contentLayer.position = CGPointZero;
[label positionRelativeToViewPoint:viewPoint
forCoordinate:CPTCoordinateX
inDirection:CPTSignNegative];
newPosition = roundPoint(CGPointMake(start - label.offset, start), contentSize, contentLayer.anchorPoint);
XCTAssertEqualWithAccuracy( contentLayer.position.x, newPosition.x, precision, @"Should add negative offset, %@ != %@", CPTStringFromPoint(contentLayer.position), CPTStringFromPoint(newPosition) );
XCTAssertEqualWithAccuracy( contentLayer.position.y, newPosition.y, precision, @"Should add negative offset, %@ != %@", CPTStringFromPoint(contentLayer.position), CPTStringFromPoint(newPosition) );
XCTAssertEqualWithAccuracy(contentLayer.anchorPoint.x, CPTFloat(1.0), precision, @"Should anchor at (1.0, 0.5)");
XCTAssertEqualWithAccuracy(contentLayer.anchorPoint.y, CPTFloat(0.5), precision, @"Should anchor at (1.0, 0.5)");
contentLayer.anchorPoint = CGPointZero;
contentLayer.position = CGPointZero;
[label positionRelativeToViewPoint:viewPoint
forCoordinate:CPTCoordinateX
inDirection:CPTSignPositive];
newPosition = roundPoint(CGPointMake(start + label.offset, start), contentSize, contentLayer.anchorPoint);
XCTAssertEqualWithAccuracy( contentLayer.position.x, newPosition.x, precision, @"Should add positive offset, %@ != %@", CPTStringFromPoint(contentLayer.position), CPTStringFromPoint(newPosition) );
XCTAssertEqualWithAccuracy( contentLayer.position.y, newPosition.y, precision, @"Should add positive offset, %@ != %@", CPTStringFromPoint(contentLayer.position), CPTStringFromPoint(newPosition) );
XCTAssertEqualWithAccuracy(contentLayer.anchorPoint.x, CPTFloat(0.0), precision, @"Should anchor at (0.0, 0.5)");
XCTAssertEqualWithAccuracy(contentLayer.anchorPoint.y, CPTFloat(0.5), precision, @"Should anchor at (0.0, 0.5)");
}
@finally {
label = nil;
}
}
-(void)testPositionRelativeToViewPointPositionsForYCoordinate
{
CPTAxisLabel *label;
CGFloat start = 100.0;
@try {
label = [[CPTAxisLabel alloc] initWithText:@"CPTAxisLabelTests-testPositionRelativeToViewPointPositionsForYCoordinate" textStyle:[CPTTextStyle textStyle]];
CPTLayer *contentLayer = label.contentLayer;
CGSize contentSize = contentLayer.bounds.size;
label.offset = 20.0;
CGPoint viewPoint = CGPointMake(start, start);
contentLayer.anchorPoint = CGPointZero;
contentLayer.position = CGPointZero;
[label positionRelativeToViewPoint:viewPoint
forCoordinate:CPTCoordinateY
inDirection:CPTSignNone];
CGPoint newPosition = roundPoint(CGPointMake(start, start - label.offset), contentSize, contentLayer.anchorPoint);
XCTAssertEqualWithAccuracy( contentLayer.position.x, newPosition.x, precision, @"Should add negative offset, %@ != %@", CPTStringFromPoint(contentLayer.position), CPTStringFromPoint(newPosition) );
XCTAssertEqualWithAccuracy( contentLayer.position.y, newPosition.y, precision, @"Should add negative offset, %@ != %@", CPTStringFromPoint(contentLayer.position), CPTStringFromPoint(newPosition) );
XCTAssertEqualWithAccuracy(contentLayer.anchorPoint.x, CPTFloat(0.5), precision, @"Should anchor at (0.5, 1.0)");
XCTAssertEqualWithAccuracy(contentLayer.anchorPoint.y, CPTFloat(1.0), precision, @"Should anchor at (0.5, 1.0)");
contentLayer.anchorPoint = CGPointZero;
contentLayer.position = CGPointZero;
[label positionRelativeToViewPoint:viewPoint
forCoordinate:CPTCoordinateY
inDirection:CPTSignNegative];
newPosition = roundPoint(CGPointMake(start, start - label.offset), contentSize, contentLayer.anchorPoint);
XCTAssertEqualWithAccuracy( contentLayer.position.x, newPosition.x, precision, @"Should add negative offset, %@ != %@", CPTStringFromPoint(contentLayer.position), CPTStringFromPoint(newPosition) );
XCTAssertEqualWithAccuracy( contentLayer.position.y, newPosition.y, precision, @"Should add negative offset, %@ != %@", CPTStringFromPoint(contentLayer.position), CPTStringFromPoint(newPosition) );
XCTAssertEqualWithAccuracy(contentLayer.anchorPoint.x, CPTFloat(0.5), precision, @"Should anchor at (0.5, 1.0)");
XCTAssertEqualWithAccuracy(contentLayer.anchorPoint.y, CPTFloat(1.0), precision, @"Should anchor at (0.5, 1.0)");
contentLayer.anchorPoint = CGPointZero;
contentLayer.position = CGPointZero;
[label positionRelativeToViewPoint:viewPoint
forCoordinate:CPTCoordinateY
inDirection:CPTSignPositive];
newPosition = roundPoint(CGPointMake(start, start + label.offset), contentSize, contentLayer.anchorPoint);
XCTAssertEqualWithAccuracy( contentLayer.position.x, newPosition.x, precision, @"Should add positive offset, %@ != %@", CPTStringFromPoint(contentLayer.position), CPTStringFromPoint(newPosition) );
XCTAssertEqualWithAccuracy( contentLayer.position.y, newPosition.y, precision, @"Should add positive offset, %@ != %@", CPTStringFromPoint(contentLayer.position), CPTStringFromPoint(newPosition) );
XCTAssertEqualWithAccuracy(contentLayer.anchorPoint.x, CPTFloat(0.5), precision, @"Should anchor at (0.5, 0.0)");
XCTAssertEqualWithAccuracy(contentLayer.anchorPoint.y, CPTFloat(0.0), precision, @"Should anchor at (0.5, 0.0)");
}
@finally {
label = nil;
}
}
@end