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

Side by Side Diff: base/mac/foundation_util_unittest.mm

Issue 187793003: Define print format macros for NSInteger & NSUInteger (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Rebase and try CQ again Created 6 years, 9 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 unified diff | Download patch
« no previous file with comments | « base/format_macros.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "base/mac/foundation_util.h" 5 #include "base/mac/foundation_util.h"
6 6
7 #include "base/basictypes.h" 7 #include "base/basictypes.h"
8 #include "base/compiler_specific.h"
8 #include "base/files/file_path.h" 9 #include "base/files/file_path.h"
10 #include "base/format_macros.h"
9 #include "base/mac/scoped_cftyperef.h" 11 #include "base/mac/scoped_cftyperef.h"
10 #include "base/mac/scoped_nsautorelease_pool.h" 12 #include "base/mac/scoped_nsautorelease_pool.h"
13 #include "base/strings/stringprintf.h"
11 #include "testing/gtest/include/gtest/gtest.h" 14 #include "testing/gtest/include/gtest/gtest.h"
12 #import "testing/gtest_mac.h" 15 #import "testing/gtest_mac.h"
13 16
14 namespace base { 17 namespace base {
15 namespace mac { 18 namespace mac {
16 19
17 TEST(FoundationUtilTest, CFCast) { 20 TEST(FoundationUtilTest, CFCast) {
18 // Build out the CF types to be tested as empty containers. 21 // Build out the CF types to be tested as empty containers.
19 ScopedCFTypeRef<CFTypeRef> test_array( 22 ScopedCFTypeRef<CFTypeRef> test_array(
20 CFArrayCreate(NULL, NULL, 0, &kCFTypeArrayCallBacks)); 23 CFArrayCreate(NULL, NULL, 0, &kCFTypeArrayCallBacks));
(...skipping 286 matching lines...) Expand 10 before | Expand all | Expand 10 after
307 EXPECT_NSEQ(nil, FilePathToNSString(FilePath())); 310 EXPECT_NSEQ(nil, FilePathToNSString(FilePath()));
308 EXPECT_NSEQ(@"/a/b", FilePathToNSString(FilePath("/a/b"))); 311 EXPECT_NSEQ(@"/a/b", FilePathToNSString(FilePath("/a/b")));
309 } 312 }
310 313
311 TEST(FoundationUtilTest, NSStringToFilePath) { 314 TEST(FoundationUtilTest, NSStringToFilePath) {
312 EXPECT_EQ(FilePath(), NSStringToFilePath(nil)); 315 EXPECT_EQ(FilePath(), NSStringToFilePath(nil));
313 EXPECT_EQ(FilePath(), NSStringToFilePath(@"")); 316 EXPECT_EQ(FilePath(), NSStringToFilePath(@""));
314 EXPECT_EQ(FilePath("/a/b"), NSStringToFilePath(@"/a/b")); 317 EXPECT_EQ(FilePath("/a/b"), NSStringToFilePath(@"/a/b"));
315 } 318 }
316 319
320 TEST(StringNumberConversionsTest, FormatNSInteger) {
321 // The PRI[dxu]NS macro assumes that NSInteger is a typedef to "int" on
322 // 32-bit architecture and a typedef to "long" on 64-bit architecture
323 // (respectively "unsigned int" and "unsigned long" for NSUInteger). Use
324 // pointer incompatibility to validate this at compilation.
325 #if defined(ARCH_CPU_64_BITS)
326 typedef long FormatNSIntegerAsType;
327 typedef unsigned long FormatNSUIntegerAsType;
328 #else
329 typedef int FormatNSIntegerAsType;
330 typedef unsigned int FormatNSUIntegerAsType;
331 #endif // defined(ARCH_CPU_64_BITS)
332
333 NSInteger some_nsinteger;
334 FormatNSIntegerAsType* pointer_to_some_nsinteger ALLOW_UNUSED =
335 &some_nsinteger;
336
337 NSUInteger some_nsuinteger;
338 FormatNSUIntegerAsType* pointer_to_some_nsuinteger ALLOW_UNUSED =
339 &some_nsuinteger;
340
341 // Check that format specifier works correctly for NSInteger.
342 const struct {
343 NSInteger value;
344 const char* expected;
345 const char* expected_hex;
346 } nsinteger_cases[] = {
347 {12345678, "12345678", "bc614e"},
348 {-12345678, "-12345678", "ff439eb2"},
349 #if defined(ARCH_CPU_64_BITS)
350 {137451299150l, "137451299150", "2000bc614e"},
351 {-137451299150l, "-137451299150", "ffffffdfff439eb2"},
352 #endif
353 };
354
355 for (size_t i = 0; i < arraysize(nsinteger_cases); ++i) {
356 EXPECT_EQ(nsinteger_cases[i].expected,
357 StringPrintf("%" PRIdNS, nsinteger_cases[i].value));
358 EXPECT_EQ(nsinteger_cases[i].expected_hex,
359 StringPrintf("%" PRIxNS, nsinteger_cases[i].value));
360 }
361
362 // Check that format specifier works correctly for NSUInteger.
363 const struct {
364 NSUInteger value;
365 const char* expected;
366 const char* expected_hex;
367 } nsuinteger_cases[] = {
368 {12345678u, "12345678", "bc614e"},
369 {4282621618u, "4282621618", "ff439eb2"},
370 #if defined(ARCH_CPU_64_BITS)
371 {137451299150ul, "137451299150", "2000bc614e"},
372 {18446743936258252466ul, "18446743936258252466", "ffffffdfff439eb2"},
373 #endif
374 };
375
376 for (size_t i = 0; i < arraysize(nsuinteger_cases); ++i) {
377 EXPECT_EQ(nsuinteger_cases[i].expected,
378 StringPrintf("%" PRIuNS, nsuinteger_cases[i].value));
379 EXPECT_EQ(nsuinteger_cases[i].expected_hex,
380 StringPrintf("%" PRIxNS, nsuinteger_cases[i].value));
381 }
382 }
383
317 } // namespace mac 384 } // namespace mac
318 } // namespace base 385 } // namespace base
OLDNEW
« no previous file with comments | « base/format_macros.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698