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/compiler_specific.h" | |
9 #include "base/files/file_path.h" | 8 #include "base/files/file_path.h" |
10 #include "base/format_macros.h" | |
11 #include "base/mac/scoped_cftyperef.h" | 9 #include "base/mac/scoped_cftyperef.h" |
12 #include "base/mac/scoped_nsautorelease_pool.h" | 10 #include "base/mac/scoped_nsautorelease_pool.h" |
13 #include "base/strings/stringprintf.h" | |
14 #include "testing/gtest/include/gtest/gtest.h" | 11 #include "testing/gtest/include/gtest/gtest.h" |
15 #import "testing/gtest_mac.h" | 12 #import "testing/gtest_mac.h" |
16 | 13 |
17 namespace base { | 14 namespace base { |
18 namespace mac { | 15 namespace mac { |
19 | 16 |
20 TEST(FoundationUtilTest, CFCast) { | 17 TEST(FoundationUtilTest, CFCast) { |
21 // Build out the CF types to be tested as empty containers. | 18 // Build out the CF types to be tested as empty containers. |
22 ScopedCFTypeRef<CFTypeRef> test_array( | 19 ScopedCFTypeRef<CFTypeRef> test_array( |
23 CFArrayCreate(NULL, NULL, 0, &kCFTypeArrayCallBacks)); | 20 CFArrayCreate(NULL, NULL, 0, &kCFTypeArrayCallBacks)); |
(...skipping 286 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
310 EXPECT_NSEQ(nil, FilePathToNSString(FilePath())); | 307 EXPECT_NSEQ(nil, FilePathToNSString(FilePath())); |
311 EXPECT_NSEQ(@"/a/b", FilePathToNSString(FilePath("/a/b"))); | 308 EXPECT_NSEQ(@"/a/b", FilePathToNSString(FilePath("/a/b"))); |
312 } | 309 } |
313 | 310 |
314 TEST(FoundationUtilTest, NSStringToFilePath) { | 311 TEST(FoundationUtilTest, NSStringToFilePath) { |
315 EXPECT_EQ(FilePath(), NSStringToFilePath(nil)); | 312 EXPECT_EQ(FilePath(), NSStringToFilePath(nil)); |
316 EXPECT_EQ(FilePath(), NSStringToFilePath(@"")); | 313 EXPECT_EQ(FilePath(), NSStringToFilePath(@"")); |
317 EXPECT_EQ(FilePath("/a/b"), NSStringToFilePath(@"/a/b")); | 314 EXPECT_EQ(FilePath("/a/b"), NSStringToFilePath(@"/a/b")); |
318 } | 315 } |
319 | 316 |
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 | |
384 } // namespace mac | 317 } // namespace mac |
385 } // namespace base | 318 } // namespace base |
OLD | NEW |