124 lines
3.1 KiB
Smalltalk
124 lines
3.1 KiB
Smalltalk
|
test1note <- '
|
||
|
Derived from:
|
||
|
Little Smalltalk, version 2
|
||
|
Written by Tim Budd, Oregon State University, July 1987
|
||
|
|
||
|
Function:
|
||
|
A few test cases.
|
||
|
|
||
|
Example:
|
||
|
File new fileIn: ''test1.st''
|
||
|
Test1 new all'!
|
||
|
Object
|
||
|
subclass: #One
|
||
|
instanceVariableNames: ''!
|
||
|
{!
|
||
|
One methods!
|
||
|
result1
|
||
|
^ self test!
|
||
|
test
|
||
|
^ 1!
|
||
|
}!
|
||
|
One
|
||
|
subclass: #Two
|
||
|
instanceVariableNames: ''!
|
||
|
{!
|
||
|
Two methods!
|
||
|
test
|
||
|
^ 2!
|
||
|
}!
|
||
|
Two
|
||
|
subclass: #Three
|
||
|
instanceVariableNames: ''!
|
||
|
{!
|
||
|
Three methods!
|
||
|
result2
|
||
|
^ self result1!
|
||
|
result3
|
||
|
^ super test!
|
||
|
}!
|
||
|
Three
|
||
|
subclass: #Four
|
||
|
instanceVariableNames: ''!
|
||
|
{!
|
||
|
Four methods!
|
||
|
test
|
||
|
^ 4!
|
||
|
}!
|
||
|
Object
|
||
|
subclass: #Test1
|
||
|
instanceVariableNames: ''!
|
||
|
{!
|
||
|
Test1 methods!
|
||
|
all
|
||
|
self super.
|
||
|
self conversions.
|
||
|
self collections.
|
||
|
self factorial.
|
||
|
self filein.
|
||
|
'all tests completed' print!
|
||
|
collections
|
||
|
" test the collection classes a little"
|
||
|
( (#(1 2 3 3 2 4 2) asSet = #(1 2 3 4) asSet) and: [
|
||
|
(#(1 5 3 2 4) sort asArray = #(1 2 3 4 5)) and: [
|
||
|
(1 "(#+ respondsTo occurrencesOf: Float)" = 1) and: [
|
||
|
('First' < 'last') ] ] ] )
|
||
|
ifFalse: [^smalltalk error: 'collection failure'].
|
||
|
'collection test passed' print.!
|
||
|
conversions
|
||
|
" test a few conversion routines "
|
||
|
( (#abc == #abc asString asSymbol) and: [
|
||
|
($A == $A asInteger asCharacter) and: [
|
||
|
(12 == 12 asDigit digitValue) and: [
|
||
|
(237 == 237 asString asInteger) and: [
|
||
|
(43 = 43 asFloat truncated) and: [
|
||
|
$A == ($A asString at: 1) ] ] ] ] ] )
|
||
|
ifFalse: [^ smalltalk error: 'conversion failure'].
|
||
|
'conversion test passed' print.!
|
||
|
factorial | t |
|
||
|
t <- [:x | (x = 1) ifTrue: [ 1 ]
|
||
|
ifFalse: [ x * (t value: x - 1) ] ].
|
||
|
((t value: 5) = 5 factorial)
|
||
|
ifFalse: [ smalltalk error: 'factorial failure'].
|
||
|
'factorial test passed' print!
|
||
|
filein
|
||
|
(File name: 'queen.st' open: 'r') fileIn; close.
|
||
|
( (#QueenMeta value class ~~ Metaclass)
|
||
|
or: [ (#Queen value class ~~ #QueenMeta value)
|
||
|
or: [#Queen value name ~~ #Queen] ] )
|
||
|
ifTrue: [ smalltalk error: 'fileIn failure'].
|
||
|
((classes includesKey: #Queen) and: [(classes at: #Queen) == Queen])
|
||
|
ifFalse: [ smalltalk error: 'fileIn failure'].
|
||
|
'file in test passed' print.
|
||
|
self queen!
|
||
|
queen | lastQueen |
|
||
|
lastQueen <- NullQueen new.
|
||
|
(1 to: 8) do: [:i | lastQueen <- Queen new
|
||
|
setColumn: i neighbor: lastQueen;
|
||
|
yourself ].
|
||
|
lastQueen first.
|
||
|
(lastQueen result asArray = #(1 5 8 6 3 7 2 4) )
|
||
|
ifTrue: ['8 queens test passed' print]
|
||
|
ifFalse: [smalltalk error: '8queen test failed']!
|
||
|
super
|
||
|
(self super2 asArray = #(1 1 2 2 2 4 2 4 2 2) )
|
||
|
ifTrue: ['super test passed' print]
|
||
|
ifFalse: [ smalltalk error: 'super test failed']!
|
||
|
super2 | x1 x2 x3 x4 |
|
||
|
x1 <- One new.
|
||
|
x2 <- Two new.
|
||
|
x3 <- Three new.
|
||
|
x4 <- Four new.
|
||
|
^ List new addLast: x1 test;
|
||
|
addLast: x1 result1;
|
||
|
addLast: x2 test;
|
||
|
addLast: x2 result1;
|
||
|
addLast: x3 test;
|
||
|
addLast: x4 result1;
|
||
|
addLast: x3 result2;
|
||
|
addLast: x4 result2;
|
||
|
addLast: x3 result3;
|
||
|
addLast: x4 result3;
|
||
|
yourself!
|
||
|
}!
|