kellabyte/Haywire — pull request #32 by boredzo (2013-07-07 20:26:10+00:00)
Title: Adds test harness for itoa function; fixes bugs in that function

itoa accepted negative numbers, but didn't generate negative representations. It also did not correctly handle numbers that used all of the characters in its buffer before the null terminator; i.e., negative numbers in at least binary.

This patch introduces a small test program that tests a couple of cases (one simple case, one extreme case), and fixes the bugs that one of the test cases confirmed.

With this patch, itoa expects unsigned integers for both value and base, making explicit that it should only be expected to represent non-negative numbers, and it correctly represents even those numbers that are a full 32 bits long in binary.

This patch uses GCC and Clang's binary numeric literal syntax, which is non-standard; if you wish to make the test program portable to other compilers, you'll need to replace that literal with a decimal or hexadecimal literal.