-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Assertion with
ceylon.promise::Promise
- Loading branch information
1 parent
1ec2d72
commit 32d3a6a
Showing
27 changed files
with
590 additions
and
300 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
145 changes: 72 additions & 73 deletions
145
examples/herd/examples/asynctest/fibonacci/fibonacci.ceylon
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,75 +1,74 @@ | ||
import ceylon.promise { | ||
import ceylon.promise { | ||
|
||
Deferred, | ||
Promise | ||
} | ||
import java.lang { | ||
Deferred, | ||
Promise | ||
} | ||
import java.lang { | ||
|
||
Runnable, | ||
Thread | ||
} | ||
|
||
"Calculates Fibonacci number by its index. | ||
" | ||
throws( `class AssertionError`, "passed index of Fibonacci number `indexOfFibonacciNumber` is less or equals to zero" ) | ||
shared Integer positiveFibonacciNumber( Integer indexOfFibonacciNumber ) { | ||
"Fibonnachi number index must be positive" | ||
assert ( indexOfFibonacciNumber > 0 ); | ||
variable Integer n0 = 0; | ||
variable Integer n1 = 1; | ||
variable Integer ret = 1; | ||
variable Integer currentIndex = 1; | ||
while ( currentIndex < indexOfFibonacciNumber ) { | ||
ret = n0 + n1; | ||
n0 = n1; | ||
n1 = ret; | ||
currentIndex ++; | ||
} | ||
return ret; | ||
} | ||
|
||
"Calculates index of positive Fibonacci number. That's may not be correct for index 1 and 2, | ||
which corresponds to equals Fibonacci numbers." | ||
throws( `class AssertionError`, "passed `fibonacciNumber` is not a Fibonacci number" ) | ||
shared Integer fibonacciNumberIndex( Integer fibonacciNumber ) { | ||
"fibonacci number must be positive" | ||
assert ( fibonacciNumber > 0 ); | ||
variable Integer n0 = 0; | ||
variable Integer n1 = 1; | ||
variable Integer ret = 1; | ||
variable Integer currentIndex = 1; | ||
while ( ret < fibonacciNumber ) { | ||
ret = n0 + n1; | ||
n0 = n1; | ||
n1 = ret; | ||
currentIndex ++; | ||
} | ||
"passed `fibonacciNumber` is not a Fibonacci number" | ||
assert ( ret == fibonacciNumber ); | ||
return currentIndex; | ||
} | ||
|
||
|
||
"Calculates Fibonacci number by its index in separated thread and returns result as promise. | ||
This is function to be tested." | ||
shared Promise<Integer> asyncPositiveFibonacciNumber( Integer indexOfFibonacciNumber ) { | ||
Deferred<Integer> ret = Deferred<Integer>(); | ||
|
||
Thread th = Thread ( | ||
object satisfies Runnable { | ||
shared actual void run() { | ||
try { | ||
ret.fulfill( positiveFibonacciNumber( indexOfFibonacciNumber ) ); | ||
} | ||
catch ( Throwable err ) { | ||
ret.reject( err ); | ||
} | ||
} | ||
} | ||
); | ||
th.start(); | ||
|
||
return ret.promise; | ||
} | ||
|
||
Runnable, | ||
Thread | ||
} | ||
|
||
"Calculates Fibonacci number by its index. | ||
Function returns incorrect result in order to demonstrate test framework output. | ||
" | ||
throws( `class AssertionError`, "passed index of Fibonacci number `indexOfFibonacciNumber` is less or equals to zero" ) | ||
shared Integer positiveFibonacciNumber( Integer indexOfFibonacciNumber ) { | ||
"Fibonnachi number index must be positive" | ||
assert ( indexOfFibonacciNumber > 0 ); | ||
variable Integer n0 = 0; | ||
variable Integer n1 = 1; | ||
variable Integer ret = 1; | ||
variable Integer currentIndex = 0; // use 1 to succeed the test! | ||
while ( currentIndex < indexOfFibonacciNumber ) { | ||
ret = n0 + n1; | ||
n0 = n1; | ||
n1 = ret; | ||
currentIndex ++; | ||
} | ||
return ret; | ||
} | ||
|
||
"Calculates index of positive Fibonacci number. That's may not be correct for index 1 and 2, | ||
which corresponds to equals Fibonacci numbers." | ||
throws( `class AssertionError`, "passed `fibonacciNumber` is not a Fibonacci number" ) | ||
shared Integer fibonacciNumberIndex( Integer fibonacciNumber ) { | ||
"fibonacci number must be positive" | ||
assert ( fibonacciNumber > 0 ); | ||
variable Integer n0 = 0; | ||
variable Integer n1 = 1; | ||
variable Integer ret = 1; | ||
variable Integer currentIndex = 1; | ||
while ( ret < fibonacciNumber ) { | ||
ret = n0 + n1; | ||
n0 = n1; | ||
n1 = ret; | ||
currentIndex ++; | ||
} | ||
"passed `fibonacciNumber` is not a Fibonacci number" | ||
assert ( ret == fibonacciNumber ); | ||
return currentIndex; | ||
} | ||
|
||
|
||
"Calculates Fibonacci number by its index in separated thread and returns result as promise. | ||
This is function to be tested." | ||
shared Promise<Integer> asyncPositiveFibonacciNumber( Integer indexOfFibonacciNumber ) { | ||
Deferred<Integer> ret = Deferred<Integer>(); | ||
|
||
Thread th = Thread ( | ||
object satisfies Runnable { | ||
shared actual void run() { | ||
try { | ||
ret.fulfill( positiveFibonacciNumber( indexOfFibonacciNumber ) ); | ||
} | ||
catch ( Throwable err ) { | ||
ret.reject( err ); | ||
} | ||
} | ||
} | ||
); | ||
th.start(); | ||
|
||
return ret.promise; | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,6 @@ | ||
" | ||
Testing of asynchronous calculation of Fibonacci numbers. | ||
Test is performed on [[asyncPositiveFibonacciNumber]] using [[runFibonacciTest]] test function. | ||
>Function [[positiveFibonacciNumber]] returns incorrect results in order to demonstrate test framework output. | ||
" | ||
by( "Lis" ) | ||
shared package herd.examples.asynctest.fibonacci; | ||
" | ||
Testing of asynchronous calculation of Fibonacci numbers. | ||
Test is performed on [[asyncPositiveFibonacciNumber]] using [[runFibonacciTest]] test function. | ||
" | ||
by( "Lis" ) | ||
shared package herd.examples.asynctest.fibonacci; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.