forked from natikgadzhi/XNMaths
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathXNMatrixTest.m
161 lines (115 loc) · 4.17 KB
/
XNMatrixTest.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
//
// XNMatrixTest.m
// Assignation 3.2
//
// Created by Нат Гаджибалаев on 17.11.09.
// Copyright 2009 Нат Гаджибалаев. All rights reserved.
//
#import "XNMatrixTest.h"
#import "XNMatrix.h"
#import "XNVector.h"
@implementation XNMatrixTest
- (void) testCreatesWithCapacitiesAndArrays
{
CGFloat data[] = {
1,2,3,4,
5,6,7,8,
9,10,12,12
};
XNMatrix *matrix = [[XNMatrix alloc] initWithRows: 3 columns: 4 filledWith: &data ];
STAssertEquals( [matrix valueAtRow: 0 column: 0], 1.0f, @"Value at [1,2] should be equal to 3.0f, but was %f", [matrix valueAtRow: 0 column: 0]);
STAssertEquals( [matrix valueAtRow: 1 column: 3], 8.0f, @"Value at [1,2] should be equal to 3.0f, but was %f", [matrix valueAtRow: 1 column: 3]);
STAssertEquals( [matrix valueAtRow: 2 column: 3], 12.0f, @"Value at [1,2] should be equal to 3.0f, but was %f", [matrix valueAtRow: 2 column: 3]);
}
- (void) testAssignsSingleValue
{
CGFloat data[] = {
1,2,3,4,
1,3.5f,3,4,
1,2,3,4
};
XNMatrix *matrix = [[XNMatrix alloc] initWithRows: 3 columns: 4 filledWith: &data ];
[matrix setValue: 10.0f atRow: 2 column: 0];
STAssertEquals( [matrix valueAtRow: 2 column: 0], 10.0f, @"Value at [1,2] should be equal to 10.0f, but was %f", [matrix valueAtRow: 2 column: 0]);
}
- (void) testRetrievesColumnVector
{
CGFloat data[] = {
1,2,3,4,
1,3.5f,3,4,
1,2,3,4
};
XNMatrix *matrix = [[XNMatrix alloc] initWithRows: 3 columns: 4 filledWith: &data ];
XNVector *column = [matrix columnVectorAtIndex:3];
STAssertEquals( [column valueAtIndex:2], [matrix valueAtRow:2 column: 3], @"Value at [1,2] should be equal to 10.0f, but was %f", [column valueAtIndex: 2]);
}
- (void) testCopied
{
CGFloat data[] = {
2.5f, 2, 3, 4,
1, 2.5f, 3, 4,
1, 2, 3, 4
};
XNMatrix *matrix = [[XNMatrix alloc] initWithRows: 3 columns: 4 filledWith: &data ];
XNMatrix *matrixCopy = [matrix copy];
STAssertEquals( [matrix valueAtRow:2 column:3 ], [matrixCopy valueAtRow:2 column:3], @"Copied values should be equal." );
[matrixCopy setValue: 1.0f atRow: 1 column: 1];
STAssertEquals( [matrix valueAtRow:0 column:0 ], 2.5f, @"First matrix should store the old values.");
STAssertEquals( [matrixCopy valueAtRow:1 column:1 ], 1.0f, @"Copied matrix should be an independent copy.");
}
- (void) testRemovesRow
{
CGFloat data[] = {
1.,1.,1.,1.,
2.,2.,2.,2.,
3.,3.,3.,3.
};
XNMatrix *matrix = [[XNMatrix alloc] initWithRows: 3 columns: 4 filledWith: &data ];
[matrix removeRowAtIndex:1];
STAssertEquals([matrix valueAtRow:1 column:0], 3.0f, @"Existing rows should be moved after one column is deleted");
STAssertEquals( matrix.rowsCount, 2u, @"Rows count should be recuced." );
}
- (void) testRemovesColumn
{
CGFloat data[] = {
1.,2.,3.,4.,
1.,2.,3.,4.,
1.,2.,3.,4.
};
XNMatrix *matrix = [[XNMatrix alloc] initWithRows: 3 columns: 4 filledWith: &data ];
[matrix removeColumnAtIndex:2];
STAssertEquals([matrix valueAtRow:0 column:2 ], 4.0f, @"Existing columns should be moved after one column is deleted");
STAssertEquals( matrix.columnsCount, 3u, @"Columns count should be recuced." );
}
- (void) testIsSquare
{
CGFloat data[] = {
2.5f, 2, 3,
1, 2.5f, 3,
1, 2, 3
};
XNMatrix *matrix = [[XNMatrix alloc] initWithRows: 3 columns: 3 filledWith: &data ];
STAssertTrue( [matrix isSquare], @"Matrix 4x4 should respond YES to isSquare selector.");
CGFloat newData[] = {
2.5f, 2, 3, 4,
1, 2.5f, 3, 4,
1, 2, 3, 4
};
matrix = [[XNMatrix alloc] initWithRows: 3 columns: 4 filledWith: &newData ];
STAssertFalse( [matrix isSquare], @"Matrix 4x4 should respond YES to isSquare selector.");
}
- (void) testMultiplication
{
CGFloat data[] = {
1, 2, 3, 4,
5, 6, 7, 8
};
CGFloat vectorData[] = { 1, 1, 1, 1 };
XNMatrix *matrix = [[XNMatrix alloc] initWithRows: 2 columns: 4 filledWith: &data ];
XNVector *vector = [[XNVector alloc] initWithCapacity: 4 filledWith: &vectorData];
XNVector *product = [[matrix multiplyByVector:vector] retain];
STAssertEquals( product.capacity, 2u, @"wrong product capacity");
STAssertEquals( [product valueAtIndex: 0], 10.0f, @"wrong product value");
STAssertEquals( [product valueAtIndex: 1], 26.0f, @"wrong product value");
}
@end