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

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

Issue 192443003: Define print format macros for NSInteger & NSUInteger (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fix 64-bit unit tests 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 #if !defined(ARCH_CPU_64_BITS)
348 {12345678, "12345678", "bc614e"},
349 {-12345678, "-12345678", "ff439eb2"},
350 #else
351 {12345678, "12345678", "bc614e"},
352 {-12345678, "-12345678", "ffffffffff439eb2"},
353 {137451299150l, "137451299150", "2000bc614e"},
354 {-137451299150l, "-137451299150", "ffffffdfff439eb2"},
355 #endif // !defined(ARCH_CPU_64_BITS)
356 };
357
358 for (size_t i = 0; i < arraysize(nsinteger_cases); ++i) {
359 EXPECT_EQ(nsinteger_cases[i].expected,
360 StringPrintf("%" PRIdNS, nsinteger_cases[i].value));
361 EXPECT_EQ(nsinteger_cases[i].expected_hex,
362 StringPrintf("%" PRIxNS, nsinteger_cases[i].value));
363 }
364
365 // Check that format specifier works correctly for NSUInteger.
366 const struct {
367 NSUInteger value;
368 const char* expected;
369 const char* expected_hex;
370 } nsuinteger_cases[] = {
371 #if !defined(ARCH_CPU_64_BITS)
372 {12345678u, "12345678", "bc614e"},
373 {4282621618u, "4282621618", "ff439eb2"},
374 #else
375 {12345678u, "12345678", "bc614e"},
376 {4282621618u, "4282621618", "ff439eb2"},
377 {137451299150ul, "137451299150", "2000bc614e"},
378 {18446743936258252466ul, "18446743936258252466", "ffffffdfff439eb2"},
379 #endif // !defined(ARCH_CPU_64_BITS)
380 };
381
382 for (size_t i = 0; i < arraysize(nsuinteger_cases); ++i) {
383 EXPECT_EQ(nsuinteger_cases[i].expected,
384 StringPrintf("%" PRIuNS, nsuinteger_cases[i].value));
385 EXPECT_EQ(nsuinteger_cases[i].expected_hex,
386 StringPrintf("%" PRIxNS, nsuinteger_cases[i].value));
387 }
388 }
389
317 } // namespace mac 390 } // namespace mac
318 } // namespace base 391 } // 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