Skip to content

Commit

Permalink
Added some tests. Changed logic about testing if elements are integer…
Browse files Browse the repository at this point in the history
…. We can only compute a mean on objects that can be added.
  • Loading branch information
Ducasse committed Mar 19, 2019
1 parent 4d44d01 commit e3fc2ce
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 4 deletions.
7 changes: 6 additions & 1 deletion src/Collections-Sequenceable/SortedCollection.class.st
Expand Up @@ -235,7 +235,12 @@ SortedCollection >> median [
"Return the middle element, or as close as we can get."
"{1 . 2 . 3 . 4 . 5} asSortedCollection median >>> 3"

^ self at: self size + 1 // 2
| size middle |
size := self size.
middle := (size + 1) // 2.
^ size even
ifTrue: [ ((self at: middle) + (self at: middle + 1)) / 2 ]
ifFalse: [ self at: middle ]
]

{ #category : #private }
Expand Down
24 changes: 21 additions & 3 deletions src/Collections-Tests/SortedCollectionTest.class.st
Expand Up @@ -719,11 +719,21 @@ SortedCollectionTest >> testLastIndexOfStartingAt [
]

{ #category : #'tests - basic' }
SortedCollectionTest >> testMedian [
SortedCollectionTest >> testMedianForEvenSizeCollection [

|aSortedCollection|
aSortedCollection := (1 to: 10) asSortedCollection.
self assert: aSortedCollection median equals: 5.
aSortedCollection := {1 .2 . 3 . 4 . 5 . 6 } asSortedCollection.
self assert: aSortedCollection median equals: 3.5


]

{ #category : #'tests - basic' }
SortedCollectionTest >> testMedianForOddSizeCollection [

|aSortedCollection|
aSortedCollection := {1 .2 . 3 . 4 . 5 . 6 .7} asSortedCollection.
self assert: aSortedCollection median equals: 4.

aSortedCollection := SortedCollection new.
aSortedCollection
Expand All @@ -735,6 +745,14 @@ SortedCollectionTest >> testMedian [

]

{ #category : #'tests - basic' }
SortedCollectionTest >> testMedianWithNumbers [

| aSortedCollection |
aSortedCollection := (1 to: 10) asSortedCollection.
self assert: aSortedCollection median equals: 5.5
]

{ #category : #'tests - basic' }
SortedCollectionTest >> testRemoveAll [
"Allows one to remove all elements of a collection"
Expand Down

0 comments on commit e3fc2ce

Please sign in to comment.