Overfflow in Assert error messages

Anonymouse zorael at gmail.com
Thu Feb 13 08:34:48 UTC 2020


On Thursday, 13 February 2020 at 07:49:13 UTC, Adnan wrote:
> However my test fails saying something like:
> source/binary_search.d(33): [unittest] 18446744073709551615 != 1
> core.exception.AssertError at source/binary_search.d(33): 
> 18446744073709551615 != 1
>
> What's causing this underflow?

It's ulong -1, which is the type idx is of on 64-bit systems. On 
32-bit systems it will be uint -1 and say "4294967295 != 0". 
indexOf is probably not doing what you think it's doing.

int indexOf(T)(const T[] list, const T key) {
     return -1;
}

void main()
{
     int[] arr = [ 0 ];
     foreach (idx, i; arr)
         assert(indexOf(arr, i) == idx);  // 18446744073709551615 
!= 0
}


More information about the Digitalmars-d-learn mailing list