PageRenderTime 28ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/core/externals/update-engine/externals/google-toolbox-for-mac/Foundation/GTMFourCharCodeTest.m

http://macfuse.googlecode.com/
Objective C | 122 lines | 79 code | 18 blank | 25 comment | 0 complexity | ba199c2150f6c54cd0de464909229cdf MD5 | raw file
Possible License(s): Apache-2.0, BSD-3-Clause, GPL-2.0
  1. //
  2. // GTMFourCharCodeTest.m
  3. //
  4. // Copyright 2008 Google Inc.
  5. //
  6. // Licensed under the Apache License, Version 2.0 (the "License"); you may not
  7. // use this file except in compliance with the License. You may obtain a copy
  8. // of the License at
  9. //
  10. // http://www.apache.org/licenses/LICENSE-2.0
  11. //
  12. // Unless required by applicable law or agreed to in writing, software
  13. // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  14. // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  15. // License for the specific language governing permissions and limitations under
  16. // the License.
  17. //
  18. #import "GTMSenTestCase.h"
  19. #import "GTMFourCharCode.h"
  20. @interface GTMFourCharCodeTest : GTMTestCase {
  21. @private
  22. NSString *lowAsciiString_;
  23. NSString *highMacOSRomanString_;
  24. }
  25. @end
  26. @implementation GTMFourCharCodeTest
  27. static const FourCharCode kGTMHighMacOSRomanCode = 0xA5A8A9AA; // '•ŽŠ™'
  28. - (void)setUp {
  29. // There appears to be a bug in the gcc 4.0 that is included with Xcode
  30. // 3.2.5 where in release mode it doesn't like some string constants
  31. // that include high or low ascii using the @"blah" string style.
  32. // So we build them by hand.
  33. // Use 8 bytes instead of 4, because stack protection gives us a warning
  34. // if we have a buffer smaller than 8 bytes.
  35. char string[8] = { 0, 0, 0, 1, 0, 0, 0, 0 };
  36. lowAsciiString_ = [[NSString alloc] initWithBytes:string
  37. length:4
  38. encoding:NSASCIIStringEncoding];
  39. // Must make sure our bytes are in the right order for building strings with,
  40. // otherwise the string comes out in the wrong order on low-endian systems.
  41. FourCharCode orderedString = htonl(kGTMHighMacOSRomanCode);
  42. highMacOSRomanString_
  43. = [[NSString alloc] initWithBytes:&orderedString
  44. length:sizeof(orderedString)
  45. encoding:NSMacOSRomanStringEncoding];
  46. }
  47. - (void)tearDown {
  48. [lowAsciiString_ release];
  49. [highMacOSRomanString_ release];
  50. }
  51. - (void)testFourCharCode {
  52. GTMFourCharCode *fcc = [GTMFourCharCode fourCharCodeWithString:@"APPL"];
  53. STAssertNotNil(fcc, nil);
  54. STAssertEqualObjects([fcc stringValue], @"APPL", nil);
  55. STAssertEqualObjects([fcc numberValue],
  56. [NSNumber numberWithUnsignedInt:'APPL'], nil);
  57. STAssertEquals([fcc fourCharCode], (FourCharCode)'APPL', nil);
  58. STAssertEqualObjects([fcc description],
  59. @"GTMFourCharCode - APPL (0x4150504C)", nil);
  60. STAssertEquals([fcc hash], (NSUInteger)'APPL', nil);
  61. NSData *data = [NSKeyedArchiver archivedDataWithRootObject:fcc];
  62. STAssertNotNil(data, nil);
  63. GTMFourCharCode *fcc2
  64. = (GTMFourCharCode*)[NSKeyedUnarchiver unarchiveObjectWithData:data];
  65. STAssertNotNil(fcc2, nil);
  66. STAssertEqualObjects(fcc, fcc2, nil);
  67. fcc = [[[GTMFourCharCode alloc] initWithFourCharCode:'\?\?\?\?'] autorelease];
  68. STAssertNotNil(fcc, nil);
  69. STAssertEqualObjects([fcc stringValue], @"????", nil);
  70. STAssertEqualObjects([fcc numberValue],
  71. [NSNumber numberWithUnsignedInt:'\?\?\?\?'], nil);
  72. STAssertEquals([fcc fourCharCode], (FourCharCode)'\?\?\?\?', nil);
  73. fcc = [[[GTMFourCharCode alloc] initWithString:@"????"] autorelease];
  74. STAssertNotNil(fcc, nil);
  75. STAssertEqualObjects([fcc stringValue], @"????", nil);
  76. STAssertEqualObjects([fcc numberValue],
  77. [NSNumber numberWithUnsignedInt:'\?\?\?\?'], nil);
  78. STAssertEquals([fcc fourCharCode], (FourCharCode)'\?\?\?\?', nil);
  79. fcc = [GTMFourCharCode fourCharCodeWithFourCharCode:1];
  80. STAssertNotNil(fcc, nil);
  81. STAssertTrue([[fcc stringValue] isEqualToString:lowAsciiString_], nil);
  82. STAssertEqualObjects([fcc numberValue],
  83. [NSNumber numberWithUnsignedInt:1], nil);
  84. STAssertEquals([fcc fourCharCode], (FourCharCode)1, nil);
  85. fcc = [GTMFourCharCode fourCharCodeWithString:@"BADDSTRING"];
  86. STAssertNil(fcc, nil);
  87. fcc2 = [GTMFourCharCode fourCharCodeWithFourCharCode:kGTMHighMacOSRomanCode];
  88. STAssertNotNil(fcc2, nil);
  89. STAssertEqualObjects([fcc2 stringValue], highMacOSRomanString_, nil);
  90. STAssertEqualObjects([fcc2 numberValue],
  91. [NSNumber numberWithUnsignedInt:kGTMHighMacOSRomanCode],
  92. nil);
  93. STAssertEquals([fcc2 fourCharCode],
  94. (FourCharCode)kGTMHighMacOSRomanCode, nil);
  95. }
  96. - (void)testStringWithCode {
  97. STAssertEqualObjects([GTMFourCharCode stringWithFourCharCode:'APPL'],
  98. @"APPL", nil);
  99. STAssertEqualObjects([GTMFourCharCode stringWithFourCharCode:1],
  100. lowAsciiString_, nil);
  101. STAssertEqualObjects([GTMFourCharCode stringWithFourCharCode:kGTMHighMacOSRomanCode],
  102. highMacOSRomanString_, nil);
  103. }
  104. @end