Skip to content

Commit ea4652a

Browse files
authored
Merge pull request #25 from DurieuxPol/cleanTests
Clean up tests
2 parents 246ba02 + 60d1079 commit ea4652a

15 files changed

Lines changed: 263 additions & 249 deletions

src/AI-LinearModels-Tests/AIAbstractLinearModelTest.class.st

Lines changed: 24 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -2,38 +2,49 @@ Class {
22
#name : 'AIAbstractLinearModelTest',
33
#superclass : 'TestCase',
44
#instVars : [
5-
'model'
5+
'model',
6+
'fixture'
67
],
78
#category : 'AI-LinearModels-Tests',
89
#package : 'AI-LinearModels-Tests'
910
}
1011

1112
{ #category : 'testing' }
12-
AIAbstractLinearModelTest class >> isAbstract [
13-
^self == AIAbstractLinearModelTest
13+
AIAbstractLinearModelTest class >> isAbstract [
14+
15+
^ self == AIAbstractLinearModelTest
16+
]
17+
18+
{ #category : 'testing' }
19+
AIAbstractLinearModelTest class >> shouldInheritSelectors [
20+
21+
^ true
1422
]
1523

1624
{ #category : 'running' }
17-
AIAbstractLinearModelTest >> regression [
25+
AIAbstractLinearModelTest >> fixtureInput [
1826

19-
^ self subclassResponsibility
27+
^ fixture inputMatrix
2028
]
2129

2230
{ #category : 'running' }
23-
AIAbstractLinearModelTest >> setUp [
31+
AIAbstractLinearModelTest >> fixtureOutput [
2432

25-
super setUp.
26-
model := self regression
33+
^ fixture outputVector
2734
]
2835

29-
{ #category : 'tests' }
30-
AIAbstractLinearModelTest >> testDivergingException [
36+
{ #category : 'running' }
37+
AIAbstractLinearModelTest >> regression [
38+
3139
^ self subclassResponsibility
3240
]
3341

34-
{ #category : 'tests' }
35-
AIAbstractLinearModelTest >> testExactFitSingleVariable [
36-
^ self subclassResponsibility
42+
{ #category : 'running' }
43+
AIAbstractLinearModelTest >> setUp [
44+
45+
super setUp.
46+
model := self regression.
47+
fixture := AILinearRegressionFixture new
3748
]
3849

3950
{ #category : 'tests' }
@@ -46,22 +57,6 @@ AIAbstractLinearModelTest >> testInconsistentFitOnDimension [
4657
self should: [ model fitX: input y: output ] raise: Error
4758
]
4859

49-
{ #category : 'tests' }
50-
AIAbstractLinearModelTest >> testInitializeWeightsToZeroOfSize [
51-
52-
| expectedInitWeigths |
53-
expectedInitWeigths := #(0 0 0).
54-
model initializeWeightsToZeroOfSize: 3.
55-
56-
self assert: model weights equals: expectedInitWeigths
57-
]
58-
59-
{ #category : 'tests' }
60-
AIAbstractLinearModelTest >> testPerformedIterationsIsInitialized [
61-
62-
self assert: model performedIterations equals: nil
63-
]
64-
6560
{ #category : 'tests' }
6661
AIAbstractLinearModelTest >> testPredictionWithNonFittedModel [
6762

src/AI-LinearModels-Tests/AILinearRegressionFixture.class.st

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,13 @@ AILinearRegressionFixture >> initialize [
2626

2727
| function |
2828
super initialize.
29-
29+
3030
bias := 3.
3131
inputMatrix := #( #( 2 ) #( 3 ) #( 1 ) #( 5 ) #( 2 ) #( 6 ) ).
3232
weights := #( 2 ).
33-
34-
function := [ :x | ( weights at: 1) * x + self bias ].
33+
34+
function := [ :x | weights first * x + self bias ].
3535
outputVector := inputMatrix collect: [ :x | function value: x first ]
36-
37-
3836
]
3937

4038
{ #category : 'accessing' }
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
Class {
2+
#name : 'AILinearRegressionGradientDescentTest',
3+
#superclass : 'AIAbstractLinearModelTest',
4+
#category : 'AI-LinearModels-Tests',
5+
#package : 'AI-LinearModels-Tests'
6+
}
7+
8+
{ #category : 'testing' }
9+
AILinearRegressionGradientDescentTest class >> isAbstract [
10+
11+
^ self == AILinearRegressionGradientDescentTest
12+
]
13+
14+
{ #category : 'tests' }
15+
AILinearRegressionGradientDescentTest >> testDivergingException [
16+
17+
"Training the model with non-sense, completely unproportioned data and with a very high learning rate to raise the diverging exception."
18+
19+
|input output |
20+
21+
input := #( #( 13421525235235235235 ) #( 3 ) #( 0.1 ) #( 0.000005 ) #( 241241241124124124 ) #( 6412412412414 ) #(45345) #(5) #(53) #(5) #(3) #(1) #(2) #(1) #(0.09) #(0.4) #(0.0009) #(5) #(234242342423423) #(0.9888) #(0.0000009) ).
22+
23+
output := (1 to: input size) collect: [ :e | 0 ].
24+
25+
model learningRate: 100.
26+
27+
self should: [model fitX: input y: output] raise: ModelStartingToDivergeException
28+
]
29+
30+
{ #category : 'tests' }
31+
AILinearRegressionGradientDescentTest >> testInitializeWeightsToZeroOfSize [
32+
33+
| expectedInitWeigths |
34+
expectedInitWeigths := #(0 0 0).
35+
model initializeWeightsToZeroOfSize: 3.
36+
37+
self assert: model weights equals: expectedInitWeigths
38+
]
39+
40+
{ #category : 'tests' }
41+
AILinearRegressionGradientDescentTest >> testPerformedIterationsIsInitialized [
42+
43+
self assert: model performedIterations equals: nil
44+
]
Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,50 @@
11
Class {
22
#name : 'AILinearRegressionLeastSquaresTest',
3-
#superclass : 'AILinearRegressionTest',
3+
#superclass : 'AIAbstractLinearModelTest',
44
#category : 'AI-LinearModels-Tests',
55
#package : 'AI-LinearModels-Tests'
66
}
77

88
{ #category : 'running' }
9-
AILinearRegressionLeastSquaresTest >> regression [
9+
AILinearRegressionLeastSquaresTest >> fixtureInput [
1010

11-
^ AILinearRegressionLeastSquares new
11+
^ self inputType rows: fixture inputMatrix
1212
]
1313

14-
{ #category : 'tests' }
15-
AILinearRegressionLeastSquaresTest >> testInitializeWeightsToZeroOfSize [
14+
{ #category : 'running' }
15+
AILinearRegressionLeastSquaresTest >> inputType [
1616

17-
self skip
17+
^ AIColumnMajorMatrix
1818
]
1919

20-
{ #category : 'tests' }
21-
AILinearRegressionLeastSquaresTest >> testLearningRateIsInitialized [
20+
{ #category : 'running' }
21+
AILinearRegressionLeastSquaresTest >> regression [
2222

23-
self skip
23+
^ AILinearRegressionLeastSquares new
2424
]
2525

2626
{ #category : 'tests' }
27-
AILinearRegressionLeastSquaresTest >> testMaxIterationsIsInitialized [
27+
AILinearRegressionLeastSquaresTest >> testExactFitSingleVariable [
2828

29-
self skip
30-
]
29+
| newInput expectedOutput actualOutput |
30+
newInput := #( #( 4 ) #( 1 ) #( 7 ) #( 0 ) ).
31+
expectedOutput := #( 11 5 17 3 ).
3132

32-
{ #category : 'tests' }
33-
AILinearRegressionLeastSquaresTest >> testPerformedIterationsIsInitialized [
33+
model fitX: self fixtureInput y: self fixtureOutput.
34+
actualOutput := model predict: newInput.
3435

35-
self skip
36+
actualOutput with: expectedOutput do: [ :actual :expected | self assert: actual closeTo: expected precision: 0.001 ]
3637
]
3738

3839
{ #category : 'tests' }
39-
AILinearRegressionLeastSquaresTest >> testWeightDerivativeforXCostDerivative [
40+
AILinearRegressionLeastSquaresTest >> testPredictionWithNonFittedModel [
41+
42+
| input |
43+
input := AIColumnMajorMatrix rows: #(
44+
(1 2 3)
45+
(2 3 4)
46+
(3 4 5)
47+
).
4048

41-
self skip
49+
self assert: (model predict: input) equals: Array new
4250
]

src/AI-LinearModels-Tests/AILinearRegressionLeastSquaresVanillaTest.class.st

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,26 +5,39 @@ Class {
55
#package : 'AI-LinearModels-Tests'
66
}
77

8+
{ #category : 'running' }
9+
AILinearRegressionLeastSquaresVanillaTest >> inputType [
10+
11+
^ PMMatrix
12+
]
13+
814
{ #category : 'running' }
915
AILinearRegressionLeastSquaresVanillaTest >> regression [
1016

1117
^ AILinearRegressionLeastSquaresVanilla new
1218
]
1319

1420
{ #category : 'tests' }
15-
AILinearRegressionLeastSquaresVanillaTest >> testBiasDerivative [
21+
AILinearRegressionLeastSquaresVanillaTest >> testExactFitDoubleVariable [
22+
"| newInput expectedOutput actualOutput initialInput initialOutput |
23+
initialInput := #( #( 2 4 ) #( 3 2 ) #( 1 2 ) #( 5 1 ) #( 2 6 ) #( 6 3 ) ).
24+
initialOutput := initialInput collect: [ :x | 2 + (x first * 3) + x second ].
25+
newInput := #( #( 4 3 ) #( 1 2 ) #( 7 3 ) #( 0 4 ) ).
26+
expectedOutput := #( 17 7 26 6 ).
1627
17-
self skip
18-
]
28+
model fitX: (PMMatrix rows: initialInput) y: initialOutput asPMVector.
29+
actualOutput := model predict: newInput.
1930
20-
{ #category : 'tests' }
21-
AILinearRegressionLeastSquaresVanillaTest >> testCostDerivativeActual [
31+
""initialInput collect: [ :x | model bias + (x first * model weights first) + (x second * model weights second) ].""
32+
33+
actualOutput with: expectedOutput do: [ :actual :expected | self assert: actual closeTo: expected precision: 0.001 ]"
34+
"should work but doesn't give the correct answer, vanilla needs to be fixes"
2235

2336
self skip
2437
]
2538

2639
{ #category : 'tests' }
27-
AILinearRegressionLeastSquaresVanillaTest >> testWeightedSumOf [
40+
AILinearRegressionLeastSquaresVanillaTest >> testExactFitSingleVariable [
2841

2942
self skip
3043
]

src/AI-LinearModels-Tests/AILinearRegressionTest.class.st

Lines changed: 5 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
Class {
22
#name : 'AILinearRegressionTest',
3-
#superclass : 'AIAbstractLinearModelTest',
4-
#instVars : [
5-
'fixture'
6-
],
3+
#superclass : 'AILinearRegressionGradientDescentTest',
74
#category : 'AI-LinearModels-Tests',
85
#package : 'AI-LinearModels-Tests'
96
}
@@ -17,21 +14,10 @@ AILinearRegressionTest >> regression [
1714
yourself
1815
]
1916

20-
{ #category : 'running' }
21-
AILinearRegressionTest >> setUp [
22-
23-
super setUp.
24-
25-
fixture := AILinearRegressionFixture new.
26-
27-
]
28-
2917
{ #category : 'tests' }
3018
AILinearRegressionTest >> testBiasAlmostEqual [
3119

32-
model
33-
fitX: fixture inputMatrix
34-
y: fixture outputVector.
20+
model fitX: self fixtureInput y: self fixtureOutput.
3521

3622
self assert: model bias closeTo: fixture bias precision: 0.001
3723
]
@@ -56,31 +42,16 @@ AILinearRegressionTest >> testCostDerivativeActual [
5642
equals: #(1 2 3)
5743
]
5844

59-
{ #category : 'tests' }
60-
AILinearRegressionTest >> testDivergingException [
61-
62-
"Training the model with non-sense, completely unproportioned data and with a very high learning rate to raise the diverging exception."
63-
64-
|input output |
65-
66-
input := #( #( 13421525235235235235 ) #( 3 ) #( 0.1 ) #( 0.000005 ) #( 241241241124124124 ) #( 6412412412414 ) #(45345) #(5) #(53) #(5) #(3) #(1) #(2) #(1) #(0.09) #(0.4) #(0.0009) #(5) #(234242342423423) #(0.9888) #(0.0000009) ).
67-
68-
output := #( 4 234 523 523 5 63456346346346 636463 63463 0.253 0.84234 0.00042 243 4 2 2 5 2 5235235 0.0005 3 3 ).
69-
70-
model learningRate: 100.
71-
72-
self should: [model fitX: input y: output] raise: ModelStartingToDivergeException
73-
]
74-
7545
{ #category : 'tests' }
7646
AILinearRegressionTest >> testExactFitSingleVariable [
7747

7848
| newInput expectedOutput actualOutput |
7949
newInput := #( #( 4 ) #( 1 ) #( 7 ) #( 0 ) ).
8050
expectedOutput := #( 11 5 17 3 ).
8151

82-
model fitX: fixture inputMatrix y: fixture outputVector.
52+
model fitX: self fixtureInput y: self fixtureOutput.
8353
actualOutput := model predict: newInput.
54+
8455
actualOutput with: expectedOutput do: [ :actual :expected | self assert: actual closeTo: expected precision: 0.001 ]
8556
]
8657

@@ -95,9 +66,7 @@ AILinearRegressionTest >> testWeightDerivativeforXCostDerivative [
9566
{ #category : 'tests' }
9667
AILinearRegressionTest >> testWeightsAlmostEqual [
9768

98-
model
99-
fitX: fixture inputMatrix
100-
y: fixture outputVector.
69+
model fitX: self fixtureInput y: self fixtureOutput.
10170

10271
fixture weights with: model weights do: [ :expected :actual |
10372
self assert: actual closeTo: expected precision: 0.0001 ]

0 commit comments

Comments
 (0)