OLD | NEW |
---|---|
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/files/file_path.h" | 8 #include "base/files/file_path.h" |
9 #include "base/format_macros.h" | |
9 #include "base/mac/scoped_cftyperef.h" | 10 #include "base/mac/scoped_cftyperef.h" |
10 #include "base/mac/scoped_nsautorelease_pool.h" | 11 #include "base/mac/scoped_nsautorelease_pool.h" |
12 #include "base/macros.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 Loading... | |
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 = &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.
| |
335 ignore_result(pointer_to_some_nsinteger); | |
336 | |
337 NSUInteger some_nsuinteger; | |
338 FormatNSUIntegerAsType* pointer_to_some_nsuinteger = &some_nsuinteger; | |
339 ignore_result(pointer_to_some_nsuinteger); | |
340 | |
341 // Check that format specifier works correctly for NSInteger. | |
342 struct { | |
Mark Mentovai
2014/03/06 17:02:42
const? Same on line 363.
sdefresne
2014/03/06 18:10:54
Done.
| |
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 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 {137451299150u, "137451299150", "2000bc614e"}, | |
Mark Mentovai
2014/03/06 17:02:42
ul, not just u, right?
sdefresne
2014/03/06 18:10:54
Done.
| |
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 |
OLD | NEW |