Skip to content

Commit

Permalink
Adding new tests & explanations of negative number behavior for round…
Browse files Browse the repository at this point in the history
…ing and truncating
  • Loading branch information
fdodino committed Aug 23, 2016
1 parent dffb6b6 commit d1b26e8
Show file tree
Hide file tree
Showing 3 changed files with 119 additions and 136 deletions.
9 changes: 7 additions & 2 deletions org.uqbar.project.wollok.lib/src/wollok/lang.wlk
Original file line number Diff line number Diff line change
Expand Up @@ -1204,21 +1204,26 @@ class Double inherits Number {
/**
* Answers the next integer greater than self
* 13.224.roundUp() ==> 14
* -13.224.roundUp() ==> -14
* 15.942.roundUp() ==> 16
*/
method roundUp() = self.roundUp(0)

/**
* Rounds up self up to a certain amount of decimals
* Rounds up self up to a certain amount of decimals.
* Amount of decimals must be positive
* 1.223445.roundUp(3) ==> 1.224
* -1.223445.roundUp(3) ==> -1.224
* 14.6165.roundUp(3) ==> 14.617
*/
method roundUp(_decimals) native

/**
* Truncates self up to a certain amount of decimals
* Truncates self up to a certain amount of decimals.
* Amount of decimals must be positive
* 1.223445.truncate(3) ==> 1.223
* 14.6165.truncate(3) ==> 14.616
* -14.6165.truncate(3) ==> -14.616
*/
method truncate(_decimals) native
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,18 @@ class DecimalTestCase extends AbstractWollokInterpreterTestCase {
def void roundUp() {
'''
assert.equals(14, 13.224.roundUp())
assert.equals(-14, -13.224.roundUp())
assert.equals(16, 15.942.roundUp())
assert.equals(15, 15.0.roundUp())
assert.equals(-15, -15.0.roundUp())
'''.test
}

@Test
def void roundUpDecimals() {
'''
assert.equals(1.224, 1.223445.roundUp(3))
assert.equals(-1.224, -1.223445.roundUp(3))
assert.equals(14.617, 14.6165.roundUp(3))
assert.equals(14.6165, 14.6165.roundUp(6))
'''.test
Expand All @@ -43,5 +46,13 @@ class DecimalTestCase extends AbstractWollokInterpreterTestCase {
assert.throwsExceptionWithMessage("Cannot set new scale with -3 decimals", { 1.223445.roundUp(-3) })
'''.test
}

@Test
def void roundUpAlphabeticDecimalsThrowsError() {
'''
assert.throwsException({ 1.223445.truncate("A") })
assert.throwsException({ 1.223445.roundUp("B") })
'''.test
}

}
Loading

0 comments on commit d1b26e8

Please sign in to comment.