Skip to content

Commit

Permalink
Fix warnings on Windows (#176)
Browse files Browse the repository at this point in the history
* Improve check_eq.

Previously it was using `int` and risked loss of precision.

* Use booleans for check_eq.

* Fix warnings.

* Avoid signed/unsigned comparison.
  • Loading branch information
floitsch committed Jan 30, 2022
1 parent b0977f9 commit e634f26
Show file tree
Hide file tree
Showing 10 changed files with 86 additions and 61 deletions.
5 changes: 3 additions & 2 deletions double-conversion/bignum.cc
Expand Up @@ -136,7 +136,7 @@ void Bignum::AssignHexString(Vector<const char> value) {
DOUBLE_CONVERSION_ASSERT(sizeof(uint64_t) * 8 >= kBigitSize + 4); // TODO: static_assert
// Accumulates converted hex digits until at least kBigitSize bits.
// Works with non-factor-of-four kBigitSizes.
uint64_t tmp = 0; // Accumulates converted hex digits until at least
uint64_t tmp = 0;
for (int cnt = 0; !value.is_empty(); value.pop_back()) {
tmp |= (HexCharValue(value.last()) << cnt);
if ((cnt += 4) >= kBigitSize) {
Expand All @@ -146,7 +146,8 @@ void Bignum::AssignHexString(Vector<const char> value) {
}
}
if (tmp > 0) {
RawBigit(used_bigits_++) = tmp;
DOUBLE_CONVERSION_ASSERT(tmp <= kBigitMask);
RawBigit(used_bigits_++) = (tmp & kBigitMask);
}
Clamp();
}
Expand Down
15 changes: 12 additions & 3 deletions test/cctest/cctest.cc
Expand Up @@ -33,6 +33,15 @@

CcTest* CcTest::last_ = NULL;

// The windows compiler doesn't like to use `strdup`, and claims it's a
// deprecated name.
// For simplicity just implement it ourselves.
static char* Strdup(const char* str) {
size_t len = strlen(str);
char* result = reinterpret_cast<char*>(malloc(len + 1));
memcpy(result, str, len + 1);
return result;
}

CcTest::CcTest(TestFunction* callback, const char* test_file,
const char* test_name, const char* test_dependency,
Expand All @@ -45,9 +54,9 @@ CcTest::CcTest(TestFunction* callback, const char* test_file,
basename = strrchr(const_cast<char *>(test_file), '\\');
}
if (!basename) {
basename = strdup(test_file);
basename = Strdup(test_file);
} else {
basename = strdup(basename + 1);
basename = Strdup(basename + 1);
}
// Drop the extension, if there is one.
char *extension = strrchr(basename, '.');
Expand Down Expand Up @@ -93,7 +102,7 @@ int main(int argc, char* argv[]) {
print_run_count = false;

} else {
char* arg_copy = strdup(arg);
char* arg_copy = Strdup(arg);
char* testname = strchr(arg_copy, '/');
if (testname) {
// Split the string in two by nulling the slash and then run
Expand Down
78 changes: 45 additions & 33 deletions test/cctest/cctest.h
Expand Up @@ -30,6 +30,7 @@

#include <stdio.h>
#include <string.h>
#include <inttypes.h>

#include "double-conversion/utils.h"

Expand Down Expand Up @@ -69,11 +70,43 @@ static inline void CheckHelper(const char* file,

#define CHECK_EQ(a, b) CheckEqualsHelper(__FILE__, __LINE__, #a, a, #b, b)

static inline void CheckEqualsHelper(const char* file, int line,
const char* expected_source,
const char* expected,
const char* value_source,
const char* value) {
template<typename T> inline void PrintfValue(T x);
template<> inline void PrintfValue(int x) { printf("%d", x); }
template<> inline void PrintfValue(unsigned int x) { printf("%u", x); }
template<> inline void PrintfValue(short x) { printf("%hd", x); }
template<> inline void PrintfValue(unsigned short x) { printf("%hu", x); }
template<> inline void PrintfValue(int64_t x) { printf("%" PRId64, x); }
template<> inline void PrintfValue(uint64_t x) { printf("%" PRIu64, x); }
template<> inline void PrintfValue(float x) { printf("%.30e", static_cast<double>(x)); }
template<> inline void PrintfValue(double x) { printf("%.30e", x); }
template<> inline void PrintfValue(bool x) { printf("%s", x ? "true" : "false"); }

template<typename T1, typename T2>
inline void CheckEqualsHelper(const char* file, int line,
const char* expected_source,
T1 expected,
const char* value_source,
T2 value) {
// If expected and value are NaNs then expected != value.
if (expected != value && (expected == expected || value == value)) {
printf("%s:%d:\n CHECK_EQ(%s, %s) failed\n",
file, line, expected_source, value_source);
printf("# Expected: ");
PrintfValue(expected);
printf("\n");
printf("# Found: ");
PrintfValue(value);
printf("\n");
abort();
}
}

template<>
inline void CheckEqualsHelper(const char* file, int line,
const char* expected_source,
const char* expected,
const char* value_source,
const char* value) {
if ((expected == NULL && value != NULL) ||
(expected != NULL && value == NULL)) {
abort();
Expand All @@ -88,36 +121,15 @@ static inline void CheckEqualsHelper(const char* file, int line,
}
}

static inline void CheckEqualsHelper(const char* file, int line,
const char* expected_source,
int expected,
const char* value_source,
int value) {
if (expected != value) {
printf("%s:%d:\n CHECK_EQ(%s, %s) failed\n"
"# Expected: %d\n"
"# Found: %d\n",
file, line, expected_source, value_source, expected, value);
abort();
}
template<>
inline void CheckEqualsHelper(const char* file, int line,
const char* expected_source,
const char* expected,
const char* value_source,
char* value) {
CheckEqualsHelper(file, line, expected_source, expected, value_source, static_cast<const char*>(value));
}

static inline void CheckEqualsHelper(const char* file, int line,
const char* expected_source,
double expected,
const char* value_source,
double value) {
// If expected and value are NaNs then expected != value.
if (expected != value && (expected == expected || value == value)) {
printf("%s:%d:\n CHECK_EQ(%s, %s) failed\n"
"# Expected: %.30e\n"
"# Found: %.30e\n",
file, line, expected_source, value_source, expected, value);
abort();
}
}


class CcTest {
public:
typedef void (TestFunction)();
Expand Down
2 changes: 1 addition & 1 deletion test/cctest/test-bignum-dtoa.cc
Expand Up @@ -43,7 +43,7 @@ using namespace double_conversion;
// Removes trailing '0' digits.
// Can return the empty string if all digits are 0.
static void TrimRepresentation(Vector<char> representation) {
int len = strlen(representation.start());
int len = static_cast<int>(strlen(representation.start()));
int i;
for (i = len - 1; i >= 0; --i) {
if (representation[i] != '0') break;
Expand Down
6 changes: 4 additions & 2 deletions test/cctest/test-bignum.cc
Expand Up @@ -39,12 +39,14 @@ using namespace double_conversion;
static const int kBufferSize = 1024;

static void AssignHexString(Bignum* bignum, const char* str) {
bignum->AssignHexString(Vector<const char>(str, strlen(str)));
int len = static_cast<int>(strlen(str));
bignum->AssignHexString(Vector<const char>(str, len));
}


static void AssignDecimalString(Bignum* bignum, const char* str) {
bignum->AssignDecimalString(Vector<const char>(str, strlen(str)));
int len = static_cast<int>(strlen(str));
bignum->AssignDecimalString(Vector<const char>(str, len));
}


Expand Down
14 changes: 7 additions & 7 deletions test/cctest/test-conversions.cc
Expand Up @@ -2072,14 +2072,14 @@ static double StrToD(const char* str, int flags, double empty_string_value,
uc16 separator = StringToDoubleConverter::kNoSeparator) {
StringToDoubleConverter converter(flags, empty_string_value, Double::NaN(),
NULL, NULL, separator);
double result = converter.StringToDouble(str, strlen(str),
int len = static_cast<int>(strlen(str));
double result = converter.StringToDouble(str, len,
processed_characters_count);
*processed_all =
((strlen(str) == static_cast<unsigned>(*processed_characters_count)));

uc16 buffer16[256];
DOUBLE_CONVERSION_ASSERT(strlen(str) < DOUBLE_CONVERSION_ARRAY_SIZE(buffer16));
int len = strlen(str);
for (int i = 0; i < len; i++) {
buffer16[i] = str[i];
}
Expand Down Expand Up @@ -4186,25 +4186,25 @@ static float StrToF16(const uc16* str16, int length, int flags,
bool* processed_all) {
StringToDoubleConverter converter(flags, empty_string_value, Double::NaN(),
NULL, NULL);
double result =
float result =
converter.StringToFloat(str16, length, processed_characters_count);
*processed_all = (length == *processed_characters_count);
return result;
}


static double StrToF(const char* str, int flags, double empty_string_value,
int* processed_characters_count, bool* processed_all) {
static float StrToF(const char* str, int flags, double empty_string_value,
int* processed_characters_count, bool* processed_all) {
StringToDoubleConverter converter(flags, empty_string_value, Single::NaN(),
NULL, NULL);
float result = converter.StringToFloat(str, strlen(str),
int len = static_cast<int>(strlen(str));
float result = converter.StringToFloat(str, len,
processed_characters_count);
*processed_all =
((strlen(str) == static_cast<unsigned>(*processed_characters_count)));

uc16 buffer16[256];
DOUBLE_CONVERSION_ASSERT(strlen(str) < DOUBLE_CONVERSION_ARRAY_SIZE(buffer16));
int len = strlen(str);
for (int i = 0; i < len; i++) {
buffer16[i] = str[i];
}
Expand Down
16 changes: 8 additions & 8 deletions test/cctest/test-dtoa.cc
Expand Up @@ -66,7 +66,7 @@ static void DoubleToAscii(double v, DtoaMode test_mode, int requested_digits,

// Removes trailing '0' digits.
static void TrimRepresentation(Vector<char> representation) {
int len = strlen(representation.start());
int len = static_cast<int>(strlen(representation.start()));
int i;
for (i = len - 1; i >= 0; --i) {
if (representation[i] != '0') break;
Expand Down Expand Up @@ -229,41 +229,41 @@ TEST(DtoaVariousDoubles) {

DoubleToAscii(-2147483648.0, SHORTEST, 0,
buffer, &sign, &length, &point);
CHECK_EQ(1, sign);
CHECK_EQ(true, sign);
CHECK_EQ("2147483648", buffer.start());
CHECK_EQ(10, point);

DoubleToAscii(-2147483648.0, SHORTEST_SINGLE, 0,
buffer, &sign, &length, &point);
CHECK_EQ(1, sign);
CHECK_EQ(true, sign);
CHECK_EQ("21474836", buffer.start());
CHECK_EQ(10, point);


DoubleToAscii(-2147483648.0, FIXED, 2, buffer, &sign, &length, &point);
CHECK_GE(2, length - point);
TrimRepresentation(buffer);
CHECK_EQ(1, sign);
CHECK_EQ(true, sign);
CHECK_EQ("2147483648", buffer.start());
CHECK_EQ(10, point);

DoubleToAscii(-2147483648.0, PRECISION, 5,
buffer, &sign, &length, &point);
CHECK_GE(5, length);
TrimRepresentation(buffer);
CHECK_EQ(1, sign);
CHECK_EQ(true, sign);
CHECK_EQ("21475", buffer.start());
CHECK_EQ(10, point);

DoubleToAscii(-3.5844466002796428e+298, SHORTEST, 0,
buffer, &sign, &length, &point);
CHECK_EQ(1, sign);
CHECK_EQ(true, sign);
CHECK_EQ("35844466002796428", buffer.start());
CHECK_EQ(299, point);

DoubleToAscii(-3.5844466002796428e+298, PRECISION, 10,
buffer, &sign, &length, &point);
CHECK_EQ(1, sign);
CHECK_EQ(true, sign);
CHECK_GE(10, length);
TrimRepresentation(buffer);
CHECK_EQ("35844466", buffer.start());
Expand Down Expand Up @@ -307,7 +307,7 @@ TEST(DtoaVariousDoubles) {

DoubleToAscii(4128420500802942e-24, SHORTEST, 0,
buffer, &sign, &length, &point);
CHECK_EQ(0, sign);
CHECK_EQ(false, sign);
CHECK_EQ("4128420500802942", buffer.start());
CHECK_EQ(-8, point);

Expand Down
2 changes: 1 addition & 1 deletion test/cctest/test-fast-dtoa.cc
Expand Up @@ -43,7 +43,7 @@ static const int kBufferSize = 100;

// Removes trailing '0' digits.
static void TrimRepresentation(Vector<char> representation) {
int len = strlen(representation.start());
int len = static_cast<int>(strlen(representation.start()));
int i;
for (i = len - 1; i >= 0; --i) {
if (representation[i] != '0') break;
Expand Down
6 changes: 3 additions & 3 deletions test/cctest/test-ieee.cc
Expand Up @@ -88,18 +88,18 @@ TEST(Single_AsDiyFp) {
DiyFp diy_fp = Single(ordered).AsDiyFp();
CHECK_EQ(0x2 - 0x7F - 23, diy_fp.e());
// The 23 mantissa bits, plus the implicit 1 in bit 24 as a uint32_t.
CHECK_EQ(0xA34567, diy_fp.f());
CHECK_EQ(0xA34567u, diy_fp.f());

uint32_t min_float32 = 0x00000001;
diy_fp = Single(min_float32).AsDiyFp();
CHECK_EQ(-0x7F - 23 + 1, diy_fp.e());
// This is a denormal; so no hidden bit.
CHECK_EQ(1, diy_fp.f());
CHECK_EQ(1u, diy_fp.f());

uint32_t max_float32 = 0x7f7fffff;
diy_fp = Single(max_float32).AsDiyFp();
CHECK_EQ(0xFE - 0x7F - 23, diy_fp.e());
CHECK_EQ(0x00ffffff, diy_fp.f());
CHECK_EQ(0x00ffffffu, diy_fp.f());
}


Expand Down
3 changes: 2 additions & 1 deletion test/cctest/test-strtod.cc
Expand Up @@ -37,7 +37,8 @@
using namespace double_conversion;

static Vector<const char> StringToVector(const char* str) {
return Vector<const char>(str, strlen(str));
int len = static_cast<int>(strlen(str));
return Vector<const char>(str, len);
}


Expand Down

0 comments on commit e634f26

Please sign in to comment.