Index: base/mac/foundation_util_unittest.mm |
diff --git a/base/mac/foundation_util_unittest.mm b/base/mac/foundation_util_unittest.mm |
index 3b72b1225a84e1ab78c9ccd7d27f63004274e297..8edc8e29c0348f70cb503b571d9b7250582dddee 100644 |
--- a/base/mac/foundation_util_unittest.mm |
+++ b/base/mac/foundation_util_unittest.mm |
@@ -6,8 +6,11 @@ |
#include "base/basictypes.h" |
#include "base/files/file_path.h" |
+#include "base/format_macros.h" |
#include "base/mac/scoped_cftyperef.h" |
#include "base/mac/scoped_nsautorelease_pool.h" |
+#include "base/macros.h" |
+#include "base/strings/stringprintf.h" |
#include "testing/gtest/include/gtest/gtest.h" |
#import "testing/gtest_mac.h" |
@@ -314,5 +317,69 @@ TEST(FoundationUtilTest, NSStringToFilePath) { |
EXPECT_EQ(FilePath("/a/b"), NSStringToFilePath(@"/a/b")); |
} |
+TEST(StringNumberConversionsTest, FormatNSInteger) { |
+ // The PRI[dxu]NS macro assumes that NSInteger is a typedef to "int" on |
+ // 32-bit architecture and a typedef to "long" on 64-bit architecture |
+ // (respectively "unsigned int" and "unsigned long" for NSUInteger). Use |
+ // pointer incompatibility to validate this at compilation. |
+#if defined(ARCH_CPU_64_BITS) |
+ typedef long FormatNSIntegerAsType; |
+ typedef unsigned long FormatNSUIntegerAsType; |
+#else |
+ typedef int FormatNSIntegerAsType; |
+ typedef unsigned int FormatNSUIntegerAsType; |
+#endif // defined(ARCH_CPU_64_BITS) |
+ |
+ NSInteger some_nsinteger; |
+ FormatNSIntegerAsType* pointer_to_some_nsinteger = &some_nsinteger; |
Mark Mentovai
2014/03/06 17:02:42
You could have also used UNUSED from base/compiler
sdefresne
2014/03/06 18:10:54
Done.
|
+ ignore_result(pointer_to_some_nsinteger); |
+ |
+ NSUInteger some_nsuinteger; |
+ FormatNSUIntegerAsType* pointer_to_some_nsuinteger = &some_nsuinteger; |
+ ignore_result(pointer_to_some_nsuinteger); |
+ |
+ // Check that format specifier works correctly for NSInteger. |
+ struct { |
Mark Mentovai
2014/03/06 17:02:42
const? Same on line 363.
sdefresne
2014/03/06 18:10:54
Done.
|
+ NSInteger value; |
+ const char* expected; |
+ const char* expected_hex; |
+ } nsinteger_cases[] = { |
+ {12345678, "12345678", "bc614e"}, |
+ {-12345678, "-12345678", "ff439eb2"}, |
+#if defined(ARCH_CPU_64_BITS) |
+ {137451299150l, "137451299150", "2000bc614e"}, |
+ {-137451299150l, "-137451299150", "ffffffdfff439eb2"}, |
+#endif |
+ }; |
+ |
+ for (size_t i = 0; i < arraysize(nsinteger_cases); ++i) { |
+ EXPECT_EQ(nsinteger_cases[i].expected, |
+ StringPrintf("%" PRIdNS, nsinteger_cases[i].value)); |
+ EXPECT_EQ(nsinteger_cases[i].expected_hex, |
+ StringPrintf("%" PRIxNS, nsinteger_cases[i].value)); |
+ } |
+ |
+ // Check that format specifier works correctly for NSUInteger. |
+ struct { |
+ NSUInteger value; |
+ const char* expected; |
+ const char* expected_hex; |
+ } nsuinteger_cases[] = { |
+ {12345678u, "12345678", "bc614e"}, |
+ {4282621618u, "4282621618", "ff439eb2"}, |
+#if defined(ARCH_CPU_64_BITS) |
+ {137451299150u, "137451299150", "2000bc614e"}, |
Mark Mentovai
2014/03/06 17:02:42
ul, not just u, right?
sdefresne
2014/03/06 18:10:54
Done.
|
+ {18446743936258252466ul, "18446743936258252466", "ffffffdfff439eb2"}, |
+#endif |
+ }; |
+ |
+ for (size_t i = 0; i < arraysize(nsuinteger_cases); ++i) { |
+ EXPECT_EQ(nsuinteger_cases[i].expected, |
+ StringPrintf("%" PRIuNS, nsuinteger_cases[i].value)); |
+ EXPECT_EQ(nsuinteger_cases[i].expected_hex, |
+ StringPrintf("%" PRIxNS, nsuinteger_cases[i].value)); |
+ } |
+} |
+ |
} // namespace mac |
} // namespace base |