Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1383)

Unified Diff: core/src/fxcrt/fx_system_unittest.cpp

Issue 1274423003: Fix FXSYS_itoa() implementation. (Closed) Base URL: https://pdfium.googlesource.com/pdfium.git@master
Patch Set: Actually include test, fix comment. Created 5 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: core/src/fxcrt/fx_system_unittest.cpp
diff --git a/core/src/fxcrt/fx_system_unittest.cpp b/core/src/fxcrt/fx_system_unittest.cpp
new file mode 100644
index 0000000000000000000000000000000000000000..f1a4aee6bdab82ebfa85c0222c4fb8138737a807
--- /dev/null
+++ b/core/src/fxcrt/fx_system_unittest.cpp
@@ -0,0 +1,212 @@
+// Copyright 2015 PDFium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include <string>
+
+#include "testing/gtest/include/gtest/gtest.h"
+#include "../../../testing/fx_string_testhelpers.h"
+#include "../../include/fxcrt/fx_system.h"
+
+// Unit test covering cases where PDFium replaces well-known library
+// functionality on any given platformn.
+
+TEST(fxcrt, FXSYS_itoa) {
+ const size_t kBase2SentinelLocation = 34; // "-" + 32 digits + null == 34.
brucedawson 2015/08/07 20:55:49 I think that the preferred abbreviation for the nu
+ const size_t kBase10SentinelLocation = 12; // "-" + 10 digits + null == 12.
+ const size_t kBase16SentinelLocation = 10; // "-" + 8 digits + null == 10.
+ const size_t kBufLen = 35;
+ const FX_CHAR kSentinel = 0x7f;
+
+ FX_CHAR buf[kBufLen];
+
+ FXSYS_itoa(42, buf, 17);
+ EXPECT_EQ(std::string(""), buf);
+
+ FXSYS_itoa(42, buf, 1);
+ EXPECT_EQ(std::string(""), buf);
+
+ FXSYS_itoa(42, buf, 0);
+ EXPECT_EQ(std::string(""), buf);
+
+ FXSYS_itoa(42, buf, -1);
+ EXPECT_EQ(std::string(""), buf);
+
+ buf[kBase16SentinelLocation] = kSentinel;
Lei Zhang 2015/08/07 20:50:12 Maybe write CheckBase16ItoA(int32_t input, const c
Tom Sepez 2015/08/07 21:58:14 Done.
+ FXSYS_itoa(std::numeric_limits<int32_t>::min(), buf, 16);
+ EXPECT_EQ(std::string("-80000000"), buf);
+ EXPECT_EQ(kSentinel, buf[kBase16SentinelLocation]);
+
+ buf[kBase10SentinelLocation] = kSentinel;
+ FXSYS_itoa(std::numeric_limits<int32_t>::min(), buf, 10);
+ EXPECT_EQ(std::string("-2147483648"), buf);
+ EXPECT_EQ(kSentinel, buf[kBase10SentinelLocation]);
+
+ buf[kBase2SentinelLocation] = kSentinel;
+ FXSYS_itoa(std::numeric_limits<int32_t>::min(), buf, 2);
+ EXPECT_EQ(std::string("-10000000000000000000000000000000"), buf);
+ EXPECT_EQ(kSentinel, buf[kBase2SentinelLocation]);
+ EXPECT_EQ(kSentinel, buf[kBase2SentinelLocation]);
Lei Zhang 2015/08/07 20:50:12 Repeat of last line?
Tom Sepez 2015/08/07 21:58:14 Done.
+
+ buf[kBase16SentinelLocation] = kSentinel;
+ FXSYS_itoa(-1, buf, 16);
+ EXPECT_EQ(std::string("-1"), buf);
+ EXPECT_EQ(kSentinel, buf[kBase16SentinelLocation]);
+
+ buf[kBase10SentinelLocation] = kSentinel;
+ FXSYS_itoa(-1, buf, 10);
+ EXPECT_EQ(std::string("-1"), buf);
+ EXPECT_EQ(kSentinel, buf[kBase10SentinelLocation]);
+
+ buf[kBase2SentinelLocation] = kSentinel;
+ FXSYS_itoa(-1, buf, 2);
+ EXPECT_EQ(std::string("-1"), buf);
+ EXPECT_EQ(kSentinel, buf[kBase2SentinelLocation]);
+
+ buf[kBase16SentinelLocation] = kSentinel;
+ FXSYS_itoa(0, buf, 16);
+ EXPECT_EQ(std::string("0"), buf);
+ EXPECT_EQ(kSentinel, buf[kBase16SentinelLocation]);
+
+ buf[kBase10SentinelLocation] = kSentinel;
+ FXSYS_itoa(0, buf, 10);
+ EXPECT_EQ(std::string("0"), buf);
+ EXPECT_EQ(kSentinel, buf[kBase10SentinelLocation]);
+
+ buf[kBase2SentinelLocation] = kSentinel;
+ FXSYS_itoa(0, buf, 2);
+ EXPECT_EQ(std::string("0"), buf);
+ EXPECT_EQ(kSentinel, buf[kBase2SentinelLocation]);
+
+ buf[kBase16SentinelLocation] = kSentinel;
+ FXSYS_itoa(42, buf, 16);
+ EXPECT_EQ(std::string("2a"), buf);
+ EXPECT_EQ(kSentinel, buf[kBase16SentinelLocation]);
+
+ buf[kBase10SentinelLocation] = kSentinel;
+ FXSYS_itoa(42, buf, 10);
+ EXPECT_EQ(std::string("42"), buf);
+ EXPECT_EQ(kSentinel, buf[kBase10SentinelLocation]);
+
+ buf[kBase2SentinelLocation] = kSentinel;
+ FXSYS_itoa(42, buf, 2);
+ EXPECT_EQ(std::string("101010"), buf);
+ EXPECT_EQ(kSentinel, buf[kBase2SentinelLocation]);
+
+ buf[kBase16SentinelLocation] = kSentinel;
+ FXSYS_itoa(std::numeric_limits<int32_t>::max(), buf, 16);
+ EXPECT_EQ(std::string("7fffffff"), buf);
+ EXPECT_EQ(kSentinel, buf[kBase16SentinelLocation]);
+
+ buf[kBase10SentinelLocation] = kSentinel;
+ FXSYS_itoa(std::numeric_limits<int32_t>::max(), buf, 10);
+ EXPECT_EQ(std::string("2147483647"), buf);
+ EXPECT_EQ(kSentinel, buf[kBase10SentinelLocation]);
+
+ buf[kBase2SentinelLocation] = kSentinel;
+ FXSYS_itoa(std::numeric_limits<int32_t>::max(), buf, 2);
+ EXPECT_EQ(std::string("1111111111111111111111111111111"), buf);
+ EXPECT_EQ(kSentinel, buf[kBase2SentinelLocation]);
+}
+
+TEST(fxcrt, FXSYS_i64toa) {
+ const size_t kBufLen = 67;
+ const size_t kBase2SentinelLocation = 66; // "-" + 64 digits + null.
+ const size_t kBase10SentinelLocation = 21; // "-" + 19 digits + null.
+ const size_t kBase16SentinelLocation = 18; // "-" + 16 digits + null.
+ const FX_CHAR kSentinel = 0x7f;
+
+ FX_CHAR buf[kBufLen];
+
+ FXSYS_i64toa(42, buf, 17);
+ EXPECT_EQ(std::string(""), buf);
+
+ FXSYS_i64toa(42, buf, 1);
+ EXPECT_EQ(std::string(""), buf);
+
+ FXSYS_i64toa(42, buf, 0);
+ EXPECT_EQ(std::string(""), buf);
+
+ FXSYS_i64toa(42, buf, -1);
+ EXPECT_EQ(std::string(""), buf);
+
+ buf[kBase16SentinelLocation] = kSentinel;
+ FXSYS_i64toa(std::numeric_limits<int64_t>::min(), buf, 16);
+ EXPECT_EQ(std::string("-8000000000000000"), buf);
+ EXPECT_EQ(kSentinel, buf[kBase16SentinelLocation]);
+
+ buf[kBase10SentinelLocation] = kSentinel;
+ FXSYS_i64toa(std::numeric_limits<int64_t>::min(), buf, 10);
+ EXPECT_EQ(std::string("-9223372036854775808"), buf);
+ EXPECT_EQ(kSentinel, buf[kBase10SentinelLocation]);
+
+ buf[kBase2SentinelLocation] = kSentinel;
+ FXSYS_i64toa(std::numeric_limits<int64_t>::min(), buf, 2);
+ EXPECT_EQ(std::string(
+ "-1000000000000000000000000000000000000000000000000000000000000000"),
+ buf);
+ EXPECT_EQ(kSentinel, buf[kBase2SentinelLocation]);
+ EXPECT_EQ(kSentinel, buf[kBase2SentinelLocation]);
+
+ buf[kBase16SentinelLocation] = kSentinel;
+ FXSYS_i64toa(-1, buf, 16);
+ EXPECT_EQ(std::string("-1"), buf);
+ EXPECT_EQ(kSentinel, buf[kBase16SentinelLocation]);
+
+ buf[kBase10SentinelLocation] = kSentinel;
+ FXSYS_i64toa(-1, buf, 10);
+ EXPECT_EQ(std::string("-1"), buf);
+ EXPECT_EQ(kSentinel, buf[kBase10SentinelLocation]);
+
+ buf[kBase2SentinelLocation] = kSentinel;
+ FXSYS_i64toa(-1, buf, 2);
+ EXPECT_EQ(std::string("-1"), buf);
+ EXPECT_EQ(kSentinel, buf[kBase2SentinelLocation]);
+
+ buf[kBase16SentinelLocation] = kSentinel;
+ FXSYS_i64toa(0, buf, 16);
+ EXPECT_EQ(std::string("0"), buf);
+ EXPECT_EQ(kSentinel, buf[kBase16SentinelLocation]);
+
+ buf[kBase10SentinelLocation] = kSentinel;
+ FXSYS_i64toa(0, buf, 10);
+ EXPECT_EQ(std::string("0"), buf);
+ EXPECT_EQ(kSentinel, buf[kBase10SentinelLocation]);
+
+ buf[kBase2SentinelLocation] = kSentinel;
+ FXSYS_i64toa(0, buf, 2);
+ EXPECT_EQ(std::string("0"), buf);
+ EXPECT_EQ(kSentinel, buf[kBase2SentinelLocation]);
+
+ buf[kBase16SentinelLocation] = kSentinel;
+ FXSYS_i64toa(42, buf, 16);
+ EXPECT_EQ(std::string("2a"), buf);
+ EXPECT_EQ(kSentinel, buf[kBase16SentinelLocation]);
+
+ buf[kBase10SentinelLocation] = kSentinel;
+ FXSYS_i64toa(42, buf, 10);
+ EXPECT_EQ(std::string("42"), buf);
+ EXPECT_EQ(kSentinel, buf[kBase10SentinelLocation]);
+
+ buf[kBase2SentinelLocation] = kSentinel;
+ FXSYS_i64toa(42, buf, 2);
+ EXPECT_EQ(std::string("101010"), buf);
+ EXPECT_EQ(kSentinel, buf[kBase2SentinelLocation]);
+
+ buf[kBase16SentinelLocation] = kSentinel;
+ FXSYS_i64toa(std::numeric_limits<int64_t>::max(), buf, 16);
+ EXPECT_EQ(std::string("7fffffffffffffff"), buf);
+ EXPECT_EQ(kSentinel, buf[kBase16SentinelLocation]);
+
+ buf[kBase10SentinelLocation] = kSentinel;
+ FXSYS_i64toa(std::numeric_limits<int64_t>::max(), buf, 10);
+ EXPECT_EQ(std::string("9223372036854775807"), buf);
+ EXPECT_EQ(kSentinel, buf[kBase10SentinelLocation]);
+
+ buf[kBase2SentinelLocation] = kSentinel;
+ FXSYS_i64toa(std::numeric_limits<int64_t>::max(), buf, 2);
+ EXPECT_EQ(std::string(
+ "111111111111111111111111111111111111111111111111111111111111111"),
+ buf);
+ EXPECT_EQ(kSentinel, buf[kBase2SentinelLocation]);
+}

Powered by Google App Engine
This is Rietveld 408576698