-
Notifications
You must be signed in to change notification settings - Fork 4
/
CGVectorTests.m
167 lines (116 loc) · 3.96 KB
/
CGVectorTests.m
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
//
// CGVectorTests.m
// SMG
//
// Created by Sam Green on 3/25/14.
// Copyright (c) 2014 Sam Green. All rights reserved.
//
@import XCTest;
@interface CGVectorTests : XCTestCase
@end
@implementation CGVectorTests
- (void)testSum {
CGVector v1 = CGVectorMake(1, 2);
CGVector v2 = CGVectorMake(2, 3);
CGVector result = CGVectorSum(v1, v2);
XCTAssertEqualWithAccuracy(result.dx, 3, FLT_EPSILON);
XCTAssertEqualWithAccuracy(result.dy, 5, FLT_EPSILON);
}
- (void)testDifference {
CGVector v1 = CGVectorMake(5, 5);
CGVector v2 = CGVectorMake(1, 2);
CGVector result = CGVectorDifference(v1, v2);
XCTAssertEqualWithAccuracy(result.dx, 4, FLT_EPSILON);
XCTAssertEqualWithAccuracy(result.dy, 3, FLT_EPSILON);
}
- (void)testMultiply {
CGVector v1 = CGVectorMake(1, 2);
CGVector v2 = CGVectorMake(2, 3);
CGVector result = CGVectorMultiply(v1, v2);
XCTAssertEqualWithAccuracy(result.dx, 2, FLT_EPSILON);
XCTAssertEqualWithAccuracy(result.dy, 6, FLT_EPSILON);
}
- (void)testMultiplyByScalar {
CGVector v1 = CGVectorMake(1, 2);
CGVector result = CGVectorMultiplyByScalar(v1, 3);
XCTAssertEqualWithAccuracy(result.dx, 3, FLT_EPSILON);
XCTAssertEqualWithAccuracy(result.dy, 6, FLT_EPSILON);
}
- (void)testMakePerpendicular {
CGVector v1 = CGVectorMake(0, 1);
CGVector result = CGVectorMakePerpendicular(v1);
XCTAssertEqualWithAccuracy(result.dx, -1, FLT_EPSILON);
XCTAssertEqualWithAccuracy(result.dy, 0, FLT_EPSILON);
}
- (void)testAngleBetween {
{
CGVector v1 = CGVectorMake(1, 0);
CGVector v2 = CGVectorMake(0, 1);
CGFloat result = CGVectorAngleBetween(v1, v2);
XCTAssertEqualWithAccuracy(result, M_PI_2, FLT_EPSILON);
}
{
CGVector v1 = CGVectorMake(1, 1);
CGVector v2 = CGVectorMake(1, 1);
CGFloat result = CGVectorAngleBetween(v1, v2);
XCTAssertEqualWithAccuracy(result, 0, FLT_EPSILON);
}
{
CGVector v1 = CGVectorMake(1, 1);
CGVector v2 = CGVectorMake(1, 0);
CGFloat result = CGVectorAngleBetween(v1, v2);
XCTAssertEqualWithAccuracy(result, M_PI_4, FLT_EPSILON);
}
}
- (void)testAngle {
CGVector v1 = CGVectorMake(1, 1);
CGFloat result = CGVectorAngle(v1);
XCTAssertEqualWithAccuracy(result, M_PI_4, FLT_EPSILON);
}
- (void)testDotProduct {
// X·Y = 0 if X is perpendicular to Y.
{
CGVector v1 = CGVectorMake(0, 1);
CGVector v2 = CGVectorMake(1, 0);
CGFloat result = CGVectorDotProduct(v1, v2);
XCTAssertEqualWithAccuracy(result, 0, FLT_EPSILON);
}
{
CGVector v1 = CGVectorMake(1, 1);
CGVector v2 = CGVectorMake(1, 0);
CGFloat result = CGVectorDotProduct(v1, v2);
XCTAssertEqualWithAccuracy(result, 1, FLT_EPSILON);
}
{
CGVector v1 = CGVectorMake(0, -1);
CGVector v2 = CGVectorMake(0, 1);
CGFloat result = CGVectorDotProduct(v1, v2);
XCTAssertEqualWithAccuracy(result, -1, FLT_EPSILON);
}
}
- (void)testMagnitude {
CGVector v1 = CGVectorMake(1, 1);
CGFloat result = CGVectorMagnitude(v1);
static const CGFloat kSquareRootOfTwo = 1.4142f;
XCTAssertEqualWithAccuracy(result, kSquareRootOfTwo, 0.0001f);
}
- (void)testNormalize {
CGVector v1 = CGVectorMake(3, 3);
CGVector result = CGVectorNormalize(v1);
static const CGFloat kLengthOfDiagonal = 0.7071f;
XCTAssertEqualWithAccuracy(result.dx, kLengthOfDiagonal, 0.0001f);
XCTAssertEqualWithAccuracy(result.dy, kLengthOfDiagonal, 0.0001f);
}
- (void)testEquality {
CGVector v1 = CGVectorMake(1, 1);
CGVector v2 = CGVectorMake(1, 1);
BOOL result = CGVectorEqualToVector(v1, v2);
XCTAssertTrue(result);
}
- (void)testPerpendicularEquality {
CGVector v1 = CGVectorMake(0, 1);
CGVector v2 = CGVectorMake(1, 0);
BOOL result = CGVectorPerpendicularToVector(v1, v2);
XCTAssertTrue(result);
}
@end