Guest User

Untitled

a guest
Jul 23rd, 2018
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. | T U MAXINT |
  2. T:=Transcript clear.
  3. i:=0.
  4. pn := [ :s | i:=i+1. Transcript show: i; show: ': '; show: s ].
  5. p := [ :s | pn value: s. Transcript cr. ].
  6. p3 := [ :s1 :s2 :s3 |
  7.     pn value: s1.
  8.     Transcript show: ' + '; show: s2; show: ' = ';  show: s3; cr.
  9. ].
  10.  
  11. U := UnlimitedInteger.
  12. Digit base: 10.
  13. "assuming class method setter named 'base' "
  14.  
  15. x := U new.
  16. self assert: x digits size = 0.
  17. p value: x.
  18.  
  19. y := U new addMSD: 0.
  20. self assert: y digits size = 1.
  21. p value: y.
  22.  
  23. y := U new addLSD: 0.
  24. self assert: y digits size = 1.
  25. p value: y.
  26.  
  27. y := U new setFrom: 0.
  28. self assert: y digits size = 1.
  29. p value: y.
  30.  
  31. y addMSD: 0.
  32. y addLSD: 0.
  33. self assert: y digits size = 3.
  34. p value: y.
  35. y addMSD: 1.
  36. self assert: y digits size = 4.
  37. "the first that is not 0[base]"
  38. p value: y.
  39.  
  40. y := U new setFrom: 0.
  41. self assert: (x add: y) digits size = 1.
  42. self assert: (y add: x) digits size = 1.
  43. self assert: (x add: x) digits size = 1.
  44. self assert: (y add: y) digits size = 1.
  45. p value: (x add: y).
  46. p value: (y add: x).
  47. p value: (x add: x).
  48. p value: (y add: y).
  49.  
  50. Transcript show: 'Random testing: each couple of rows should be equal'; cr.
  51. MAXINT := 100000.
  52.  
  53. "check setFrom: "
  54. 10 timesRepeat: [
  55.     Digit base: (Random new nextInt: 9) + 1.
  56.     r := Random new nextInt: MAXINT.
  57.     p value: (r radix: Digit base), '[', Digit base, ']'.
  58.     p value: (U new setFrom: r).
  59.     Transcript cr.
  60. ].
  61.  
  62. "check add "
  63. 10 timesRepeat: [
  64.     Digit base: (Random new nextInt: 9) + 1.
  65.     suff := '[', Digit base, ']'.
  66.     r1 := Random new nextInt: MAXINT.
  67.     r2 := Random new nextInt: MAXINT.
  68.     res := r1 + r2.
  69.     ur1 := U new setFrom: r1.
  70.     ur2 := U new setFrom: r2.
  71.    
  72.     p3  value: (r1 radix: Digit base), suff
  73.         value: (r2 radix: Digit base), suff
  74.         value: (res radix: Digit base), suff.
  75.     p3  value: ur1 value: ur2 value: (ur1 add: ur2).
  76.    
  77.     p3  value: (r2 radix: Digit base), suff
  78.         value: (r1 radix: Digit base), suff
  79.         value: (res radix: Digit base), suff.
  80.     p3  value: ur2 value: ur1 value: (ur2 add: ur1).
  81.     Transcript cr.
  82. ].
  83. Transcript show: 'End random testing.'; cr; cr.
  84.  
  85. Transcript show: 'Comparison testing:'.
  86. x := U new.
  87. y := U new setFrom: 0.
  88. y0 := U new setFrom: 0; addMSD: 0.
  89. self assert: (x compareTo: x).
  90. self assert: (x compareTo: y) not.
  91. self assert: (y compareTo: y).
  92. self assert: (y compareTo: y0) not.
  93. self assert: (y compareTo: x) not.
  94. 10 timesRepeat: [
  95.     Digit base: (Random new nextInt: 9) + 1.
  96.     r := (Random new nextInt: MAXINT) radix: Digit base.
  97.     ur1 := U new.  
  98.     ur2 := U new.
  99.     r shuffle do: [ :d | ur1 addMSD: d asInteger ].
  100.     r shuffle do: [ :d | ur2 addMSD: d asInteger ].
  101.    
  102.     self assert: (ur1 compareTo: ur2).
  103.     self assert: (ur2 compareTo: ur1).
  104. ].
  105. Transcript show: ' pass:'; cr.
  106.  
  107. Transcript show: 'Iterator testing:'.
  108. 10 timesRepeat: [
  109.     Digit base: (Random new nextInt: 9) + 1.
  110.     ur1 := U new.
  111.     num := (Random new nextInt: 100).
  112.     num timesRepeat:
  113.         [ ur1 addMSD: (Random new nextInt: Digit base) - 1 ].
  114.     it1 := ur1 iterator.
  115.     it2 := ur1 iterator.
  116.     (num+1) timesRepeat: [
  117.         ur1 addMSD: 1.
  118.         ur1 addLSD: 1.
  119.         ur1 iterator.
  120.         ur1 setFrom: (Random new nextInt: 100).
  121.         self assert: it1 value value = it2 value value.
  122.     ].
  123.     self assert: it1 value = nil.
  124. ].
  125. Transcript show: ' pass:'; cr.
  126. Transcript show: 'Anagrams testing:'.
  127. 10 timesRepeat: [ | x num a b |
  128.     Digit base: (Random new nextInt: 9) + 1.
  129.     num :=  Random new nextInt: 100.
  130.     x := U new.
  131.     100 timesRepeat: [ x addMSD: (Random new nextInt: Digit base) - 1 ].
  132.     b := Array new: num.
  133.     1 to: num  do: [ :j | b at: j put: x digits asArray shuffle ].
  134.     a := Anagrams new.
  135.     b do: [ :t |
  136.         u := U new.
  137.         t do: [ :k | u addMSD: k value ].
  138.         a add: u
  139.         ].
  140.     self assert: (a count: x) = num.
  141. ].
  142. Transcript show: ' pass:'; cr.
Add Comment
Please, Sign In to add comment