On 03/19/2017 03:13 PM, Ervin Bosenbacher wrote:
> Its my 2nd day into D, I am already in deep love (:D),  and I would like
> to understand whether this is normal behavior or something went terribly
> wrong, so all help is greatly appreciated.
>
> Following the book of The D Programming language I have the code below:
>
> bool binarySearch(T)(T[] input, T value) {
>     while (!input.empty) {
>         auto i = input.length / 2;
>         auto mid = input[i];
>         if (mid > value) input = input[0 .. i];
>         else if (mid < value) input = input[i + 1 .. $];
>         else return true;
>     }
>     return false;
> }
>
> @safe nothrow unittest {
>     assert(binarySearch([ 1, 3, 6, 7, 9, 15 ], 6));

That should return true and the assertion will pass.

>     assert(binarySearch([ 1, 3, 6, 7, 9, 15 ], 5));

Since there is no 5 in the array, that will return false and the assertion will fail, which means that the tests failed.

> }
>
> void main() {
>
>     bool s1 = binarySearch([1, 3, 6, 7, 9, 15], 6);
>     bool s2 = binarySearch!(int)([1, 3, 6, 7, 9, 15], 5);
>     writeln(s1, s2);
> }
>
> Then I compile using the command
> $ dmd source/app.d -unittest
>
> and execute as follows
> $ ./app

So, effectively you requested a unit test run and it failed.

>
> Then the output
> core.exception.AssertError@source/app.d(62): unittest failure
> ----------------

Unless I'm missing something in your question, that's exactly the expected outcome. :)

Ali

Reply via email to