"Pharo Collections-Tests slice — Array, Dictionary, Set."

TestCase subclass: #ArrayTest
  instanceVariableNames: ''!

!ArrayTest methodsFor: 'creation'!
testNewSize self assert: (Array new: 5) size equals: 5!
testLiteralSize self assert: #(1 2 3) size equals: 3!
testEmpty self assert: #() isEmpty!
testNotEmpty self assert: #(1) notEmpty!
testFirst self assert: #(10 20 30) first equals: 10!
testLast self assert: #(10 20 30) last equals: 30! !

!ArrayTest methodsFor: 'access'!
testAt self assert: (#(10 20 30) at: 2) equals: 20!
testAtPut
  | a |
  a := Array new: 3.
  a at: 1 put: 'x'.
  a at: 2 put: 'y'.
  a at: 3 put: 'z'.
  self assert: (a at: 2) equals: 'y'! !

!ArrayTest methodsFor: 'iteration'!
testDoSum
  | s |
  s := 0.
  #(1 2 3 4 5) do: [:e | s := s + e].
  self assert: s equals: 15!

testInjectInto self assert: (#(1 2 3 4) inject: 0 into: [:a :b | a + b]) equals: 10!

testCollect self assert: (#(1 2 3) collect: [:x | x * x]) equals: #(1 4 9)!

testSelect self assert: (#(1 2 3 4 5) select: [:x | x > 2]) equals: #(3 4 5)!

testReject self assert: (#(1 2 3 4 5) reject: [:x | x > 2]) equals: #(1 2)!

testDetect self assert: (#(1 3 5 7) detect: [:x | x > 4]) equals: 5!

testCount self assert: (#(1 2 3 4 5) count: [:x | x even]) equals: 2!

testAnySatisfy self assert: (#(1 2 3) anySatisfy: [:x | x > 2])!

testAllSatisfy self assert: (#(2 4 6) allSatisfy: [:x | x even])!

testIncludes self assert: (#(1 2 3) includes: 2)!

testIncludesNot self deny: (#(1 2 3) includes: 99)!

testIndexOf self assert: (#(10 20 30) indexOf: 30) equals: 3!

testIndexOfMissing self assert: (#(1 2 3) indexOf: 99) equals: 0! !

TestCase subclass: #DictionaryTest
  instanceVariableNames: ''!

!DictionaryTest methodsFor: 'fixture'!
setUp ^ self! !

!DictionaryTest methodsFor: 'tests'!
testEmpty self assert: Dictionary new isEmpty!

testAtPutThenAt
  | d |
  d := Dictionary new.
  d at: #a put: 1.
  self assert: (d at: #a) equals: 1!

testAtMissingNil self assert: (Dictionary new at: #nope) equals: nil!

testAtIfAbsent
  self assert: (Dictionary new at: #nope ifAbsent: [#absent]) equals: #absent!

testSize
  | d |
  d := Dictionary new.
  d at: #a put: 1. d at: #b put: 2. d at: #c put: 3.
  self assert: d size equals: 3!

testIncludesKey
  | d |
  d := Dictionary new.
  d at: #a put: 1.
  self assert: (d includesKey: #a)!

testRemoveKey
  | d |
  d := Dictionary new.
  d at: #a put: 1. d at: #b put: 2.
  d removeKey: #a.
  self deny: (d includesKey: #a)!

testOverwrite
  | d |
  d := Dictionary new.
  d at: #x put: 1. d at: #x put: 99.
  self assert: (d at: #x) equals: 99! !

TestCase subclass: #SetTest
  instanceVariableNames: ''!

!SetTest methodsFor: 'tests'!
testEmpty self assert: Set new isEmpty!

testAdd
  | s |
  s := Set new.
  s add: 1.
  self assert: (s includes: 1)!

testDedup
  | s |
  s := Set new.
  s add: 1. s add: 1. s add: 1.
  self assert: s size equals: 1!

testRemove
  | s |
  s := Set new.
  s add: 1. s add: 2.
  s remove: 1.
  self deny: (s includes: 1)!

testAddAll
  | s |
  s := Set new.
  s addAll: #(1 2 3 2 1).
  self assert: s size equals: 3!

testDoSum
  | s sum |
  s := Set new.
  s add: 10. s add: 20. s add: 30.
  sum := 0.
  s do: [:e | sum := sum + e].
  self assert: sum equals: 60! !
