home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 116.lha / SmallTalk / Sources / TEST.ST < prev    next >
Encoding:
Text File  |  1986-11-20  |  2.5 KB  |  106 lines

  1. *
  2. *
  3. * Little Smalltalk, version 2
  4. * Written by Tim Budd, Oregon State University, July 1987
  5. *
  6. *  a few test cases.
  7. * invoke by messages to global variable ``test'', i.e.
  8. *        test collection
  9. *
  10. * all test cases can be run by sending the message all to test
  11. *         test all
  12. *
  13. Declare Test Object
  14. Declare One Object
  15. Declare Two One
  16. Declare Three Two
  17. Declare Four Three
  18. Instance Test test
  19. Class One
  20.         test
  21.                 ^ 1
  22. |
  23.     result1
  24.                 ^ self test
  25. ]
  26. Class Two
  27.         test
  28.                 ^ 2
  29. ]
  30. Class Three
  31.         result2
  32.                 ^ self result1
  33. |
  34.     result3
  35.                 ^ super test
  36. ]
  37. Class Four
  38.         test
  39.                 ^ 4
  40. ]
  41. Class Test
  42.     all
  43.         self super.
  44.         self conversions.
  45.         self collections.
  46.         self factorial.
  47.         self filein.
  48.         'all tests completed' print
  49. |
  50.     conversions
  51.         " test a few conversion routines "
  52.         ( (#abc == #abc asString asSymbol) and: [
  53.         ($A == $A asInteger asCharacter) and: [
  54.         (12 == 12 asDigit digitValue) and: [
  55.         (237 == 237 asString asInteger) and: [
  56.         (43 = 43 asFloat truncated) and: [
  57.         $A == ($A asString at: 1) ] ] ] ] ] )
  58.             ifFalse: [^ smalltalk error: 'conversion failure'].
  59.         'conversion test passed' print.
  60. |
  61.     collections
  62.         " test the collection classes a little"
  63.         ( (#(1 2 3 3 2 4 2) asSet = #(1 2 3 4) asSet) and: [
  64.         (#(1 5 3 2 4) sort asArray = #(1 2 3 4 5)) and: [
  65.         ((#+ respondsTo occurrencesOf: Float) = 1) and: [
  66.         ('First' < 'last') ] ] ] )
  67.             ifFalse: [^smalltalk error: 'collection failure'].
  68.         'collection test passed' print.
  69. |
  70.     factorial    | t |
  71.         t <- [:x | (x = 1) ifTrue: [ 1 ] 
  72.                 ifFalse: [ x * (t value: x - 1) ] ].
  73.         ((t value: 5) = 5 factorial)
  74.             ifFalse: [ smalltalk error: 'factorial failure'].
  75.         'factorial test passed' print
  76.         
  77. |
  78.     filein
  79.         File new; name: 'queen.st'; open: 'r'; fileIn.
  80.         (globalNames includesKey: #Queen )
  81.             ifFalse: [ smalltalk error: 'fileIn failure'].
  82.         'file in test passed' print.
  83.         self queen
  84. |
  85.     super2         | x1 x2 x3 x4 |
  86.                 x1 <- One new.
  87.                 x2 <- Two new.
  88.                 x3 <- Three new.
  89.                 x4 <- Four new.
  90.         ^ List new; addLast: x1 test;
  91.             addLast: x1 result1;
  92.             addLast: x2 test;
  93.             addLast: x2 result1;
  94.             addLast: x3 test;
  95.                     addLast: x4 result1;
  96.                     addLast: x3 result2;
  97.             addLast: x4 result2;
  98.                     addLast: x3 result3;
  99.                     addLast: x4 result3
  100. |
  101.     super
  102.         (self super2 asArray = #(1 1 2 2 2 4 2 4 2 2) )
  103.             ifTrue: ['super test passed' print]
  104.             ifFalse: [ smalltalk error: 'super test failed']
  105. ]
  106.