Skip to content

Commit edf3d8b

Browse files
committed
Addition of the PMNDArrayTest class replacing PMArrayTest
1 parent 8a713dc commit edf3d8b

File tree

1 file changed

+191
-0
lines changed

1 file changed

+191
-0
lines changed
Lines changed: 191 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,191 @@
1+
Class {
2+
#name : #PMNDArrayTest,
3+
#superclass : #TestCase,
4+
#category : #'Math-Matrix'
5+
}
6+
7+
{ #category : #tests }
8+
PMNDArrayTest >> testArray [
9+
10+
| t1 t2 |
11+
t1 := PMNDArray fromNestedArray: #( #( 1 2 3 4 ) #( 5 6 7 8 ) ).
12+
self assert: t1 array equals: #( 1 2 3 4 5 6 7 8 ).
13+
14+
t2 := PMNDArray fromNestedArray: #( #( #( 1 2 ) #( 3 4 ) ) #( #( 5 6 ) #( 7 8 ) )
15+
#( #( 9 10 ) #( 11 12 ) ) #( #( 13 14 ) #( 15 16 ) ) ).
16+
self
17+
assert: t2 array
18+
equals: #( 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 )
19+
]
20+
21+
{ #category : #tests }
22+
PMNDArrayTest >> testCreateScalarTensor [
23+
24+
| s |
25+
s := PMNDArray newWith: 2.
26+
self assert: (s get: #( )) equals: 2.
27+
self should: [ s get: #( 1 1 ) ] raise: Error.
28+
self assert: s rank equals: 0.
29+
s set: #( ) value: 1.
30+
self assert: (s get: #( )) equals: 1.
31+
self assert: s shape equals: #( ).
32+
self assert: s size equals: 1
33+
]
34+
35+
{ #category : #tests }
36+
PMNDArrayTest >> testFirst [
37+
38+
| a b |
39+
a := PMNDArray fromNestedArray: (1 to: 6) asArray.
40+
self assert: a first equals: #( 1).
41+
b := a reshape: #( 3 2 ).
42+
self assert: b first equals: #( 1 1 )
43+
]
44+
45+
{ #category : #tests }
46+
PMNDArrayTest >> testFlattenedIndexOf [
47+
48+
| t1 t2 |
49+
t1 := PMNDArray fromNestedArray: #( #( 1 2 3 4 ) #( 5 6 7 8 ) ).
50+
self assert: (t1 flattenedIndexOf: #( 1 2 )) equals: 2.
51+
52+
t2 := PMNDArray fromNestedArray: #( #( #( 1 2 ) #( 3 4 ) ) #( #( 5 6 ) #( 7 8 ) )
53+
#( #( 9 10 ) #( 11 12 ) ) #( #( 13 14 ) #( 15 16 ) ) ).
54+
self assert: (t2 flattenedIndexOf: #( 1 2 2 )) equals: 4
55+
]
56+
57+
{ #category : #tests }
58+
PMNDArrayTest >> testFromNestedArray [
59+
60+
| t1 t2 |
61+
t1 := PMNDArray fromNestedArray: #( #( 1 2 3 4 )
62+
#( 5 6 7 8 ) ).
63+
self assert: t1 class equals: PMNDArray.
64+
65+
t2 := PMNDArray fromNestedArray: #( #( #( 1 1 ) #( 2 2 ) )
66+
#( #( 3 3 ) #( 4 4 ) )
67+
#( #( 4 4 ) #( 4 4 ) )
68+
#( #( 4 4 ) #( 4 4 ) ) ).
69+
self assert: t2 class equals: PMNDArray.
70+
71+
]
72+
73+
{ #category : #tests }
74+
PMNDArrayTest >> testGet [
75+
76+
| t1 t2 |
77+
t1 := PMNDArray fromNestedArray: #( #( 1 2 3 4 ) #( 5 6 7 8 ) ).
78+
self assert: (t1 get: #( 2 2 )) equals: 6.
79+
80+
t2 := PMNDArray fromNestedArray: #( #( #( 1 2 ) #( 3 4 ) ) #( #( 5 6 ) #( 7 8 ) )
81+
#( #( 9 10 ) #( 11 12 ) ) #( #( 13 14 ) #( 15 16 ) ) ).
82+
self assert: (t2 get: #( 3 2 1 )) equals: 11.
83+
84+
self should:[t1 get: #( 4 4 )] raise:Error
85+
]
86+
87+
{ #category : #tests }
88+
PMNDArrayTest >> testRank [
89+
90+
| t1 t2 |
91+
92+
t1 := PMNDArray fromNestedArray: #( #( 1 2 3 4 ) #( 5 6 7 8 ) ).
93+
self assert: t1 rank equals: 2.
94+
95+
t2 := PMNDArray fromNestedArray: #( #( #( 1 2 ) #( 3 4 ) ) #( #( 5 6 ) #( 7 8 ) )
96+
#( #( 9 10 ) #( 11 12 ) ) #( #( 13 14 ) #( 15 16 ) ) ).
97+
self assert: t2 rank equals: 3
98+
]
99+
100+
{ #category : #tests }
101+
PMNDArrayTest >> testReshape [
102+
103+
| t t1 |
104+
t := PMNDArray fromNestedArray: #( #( 0 1 ) #( 2 3 ) #( 4 5 ) ).
105+
t1 := t reshape: #( 2 3 ).
106+
107+
self assert: t shape equals: #( 3 2 ).
108+
self assert: t1 shape equals: #( 2 3 ).
109+
self assert: t1 array == t array equals: true
110+
]
111+
112+
{ #category : #tests }
113+
PMNDArrayTest >> testSetValue [
114+
115+
| t1 t2 |
116+
t1 := PMNDArray fromNestedArray: #( #( 1 2 3 4 ) #( 5 6 7 8 ) ).
117+
t1 set: #( 2 2 ) value: 3.
118+
self assert: (t1 get: #( 2 2 ) ) equals: 3.
119+
120+
t2 := PMNDArray fromNestedArray: #( #( #( 1 2 ) #( 3 4 ) )
121+
#( #( 5 6 ) #( 7 8 ) )
122+
#( #( 9 10 ) #( 11 12 ) )
123+
#( #( 13 14 ) #( 15 16 ) ) ).
124+
t2 set: #( 2 2 1) value: 10.
125+
self assert: (t2 get: #( 2 2 1 )) equals: 10
126+
]
127+
128+
{ #category : #tests }
129+
PMNDArrayTest >> testShape [
130+
131+
| t1 t2 |
132+
t1 := PMNDArray fromNestedArray: #( #( 1 2 3 4 )
133+
#( 5 6 7 8 ) ).
134+
self assert: t1 shape equals: #( 2 4 ).
135+
136+
t2 := PMNDArray fromNestedArray: #( #( #( 1 1 ) #( 2 2 ) )
137+
#( #( 3 3 ) #( 4 4 ) )
138+
#( #( 4 4 ) #( 4 4 ) )
139+
#( #( 4 4 ) #( 4 4 ) ) ).
140+
self assert: t2 shape equals: #( 4 2 2 )
141+
]
142+
143+
{ #category : #tests }
144+
PMNDArrayTest >> testSize [
145+
146+
| t1 t2 |
147+
t1 := PMNDArray fromNestedArray: #( #( 1 2 3 4 ) #( 5 6 7 8 ) ).
148+
self assert: t1 size equals: 8.
149+
150+
t2 := PMNDArray fromNestedArray: #( #( #( 1 2 ) #( 3 4 ) ) #( #( 5 6 ) #( 7 8 ) )
151+
#( #( 9 10 ) #( 11 12 ) ) #( #( 13 14 ) #( 15 16 ) ) ).
152+
self assert: t2 size equals: 16
153+
]
154+
155+
{ #category : #tests }
156+
PMNDArrayTest >> testStrides [
157+
158+
| a b |
159+
a := PMNDArray fromNestedArray: (1 to: 24) asArray.
160+
self assert: a strides equals: #( 1 ).
161+
b := a reshape: #( 4 6 ).
162+
self assert: b strides equals: #( 6 1 ).
163+
b := a reshape: #( 6 4 ).
164+
self assert: b strides equals: #( 4 1 ).
165+
self assert: (b flattenedIndexOf: #( 4 2 )) equals: 14.
166+
b := a reshape: #( 3 4 2 ).
167+
self assert: b strides equals: #( 8 2 1 ).
168+
self assert: (b flattenedIndexOf: #( 3 2 1)) equals: 19.
169+
b := a reshape: #( 2 3 4 ).
170+
self assert: b strides equals: #( 12 4 1 ).
171+
self assert: (b flattenedIndexOf: #( 2 2 3 )) equals: 19
172+
]
173+
174+
{ #category : #tests }
175+
PMNDArrayTest >> testView [
176+
177+
| t t1 |
178+
t := PMNDArray fromNestedArray:
179+
#( #( 10 11 12 ) #( 13 14 15 ) #( 16 17 18 ) #( #( 20 21 22 )
180+
#( 23 24 25 ) #( 26 27 28 ) )
181+
#( #( 30 31 32 ) #( 33 34 35 ) #( 36 37 38 ) ) ).
182+
t1 := t view.
183+
self assert: t array == t1 array equals: true.
184+
self assert: t shape equals: t1 shape.
185+
self assert: t shape == t1 shape equals: false.
186+
self assert: t strides equals: t1 strides.
187+
self assert: t strides == t1 strides equals: false.
188+
self assert: t first equals: t1 first.
189+
self assert: t first == t1 first equals: false.
190+
191+
]

0 commit comments

Comments
 (0)