From 68efa5233c3fb338a52c38730a04d0da1c50ff30 Mon Sep 17 00:00:00 2001 From: ridiculousfish Date: Sun, 18 Jan 2015 16:03:21 -0800 Subject: [PATCH] Fix and add tests for format_long_safe with negative numbers --- common.cpp | 3 +-- fish_tests.cpp | 7 ++++++- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/common.cpp b/common.cpp index c6fa91298..3440f075f 100644 --- a/common.cpp +++ b/common.cpp @@ -806,10 +806,9 @@ void format_long_safe(wchar_t buff[64], long val) size_t idx = 0; bool negative = (val < 0); - while (val > 0) + while (val != 0) { long rem = val % 10; - /* Here we're assuming that wide character digits are contiguous - is that a correct assumption? */ buff[idx++] = L'0' + (wchar_t)(rem < 0 ? -rem : rem); val /= 10; } diff --git a/fish_tests.cpp b/fish_tests.cpp index 28089bdab..6cab5a663 100644 --- a/fish_tests.cpp +++ b/fish_tests.cpp @@ -296,6 +296,12 @@ static void test_format(void) format_long_safe(buff1, j); sprintf(buff2, "%d", j); do_test(! strcmp(buff1, buff2)); + + wchar_t wbuf1[128], wbuf2[128]; + format_long_safe(wbuf1, j); + swprintf(wbuf2, 128, L"%d", j); + do_test(! wcscmp(wbuf1, wbuf2)); + } long q = LONG_MIN; @@ -303,7 +309,6 @@ static void test_format(void) format_long_safe(buff1, q); sprintf(buff2, "%ld", q); do_test(! strcmp(buff1, buff2)); - } /**