PageRenderTime 70ms CodeModel.GetById 17ms app.highlight 49ms RepoModel.GetById 1ms 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
  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
 19#import "GTMSenTestCase.h"
 20#import "GTMFourCharCode.h"
 21
 22@interface GTMFourCharCodeTest : GTMTestCase {
 23 @private
 24  NSString *lowAsciiString_;
 25  NSString *highMacOSRomanString_;
 26}
 27
 28@end
 29
 30@implementation GTMFourCharCodeTest
 31
 32static const FourCharCode kGTMHighMacOSRomanCode = 0xA5A8A9AA; // '•ŽŠ™'
 33
 34- (void)setUp {
 35  // There appears to be a bug in the gcc 4.0 that is included with Xcode
 36  // 3.2.5 where in release mode it doesn't like some string constants
 37  // that include high or low ascii using the @"blah" string style.
 38  // So we build them by hand.
 39  // Use 8 bytes instead of 4, because stack protection gives us a warning
 40  // if we have a buffer smaller than 8 bytes.
 41  char string[8] = { 0, 0, 0, 1, 0, 0, 0, 0 };
 42  lowAsciiString_ = [[NSString alloc] initWithBytes:string
 43                                             length:4
 44                                           encoding:NSASCIIStringEncoding];
 45
 46  // Must make sure our bytes are in the right order for building strings with,
 47  // otherwise the string comes out in the wrong order on low-endian systems.
 48  FourCharCode orderedString = htonl(kGTMHighMacOSRomanCode);
 49  highMacOSRomanString_
 50    = [[NSString alloc] initWithBytes:&orderedString
 51                               length:sizeof(orderedString)
 52                             encoding:NSMacOSRomanStringEncoding];
 53}
 54
 55- (void)tearDown {
 56  [lowAsciiString_ release];
 57  [highMacOSRomanString_ release];
 58}
 59
 60- (void)testFourCharCode {
 61  GTMFourCharCode *fcc = [GTMFourCharCode fourCharCodeWithString:@"APPL"];
 62  STAssertNotNil(fcc, nil);
 63  STAssertEqualObjects([fcc stringValue], @"APPL", nil);
 64  STAssertEqualObjects([fcc numberValue],
 65                       [NSNumber numberWithUnsignedInt:'APPL'], nil);
 66  STAssertEquals([fcc fourCharCode], (FourCharCode)'APPL', nil);
 67
 68  STAssertEqualObjects([fcc description],
 69                       @"GTMFourCharCode - APPL (0x4150504C)", nil);
 70  STAssertEquals([fcc hash], (NSUInteger)'APPL', nil);
 71
 72  NSData *data = [NSKeyedArchiver archivedDataWithRootObject:fcc];
 73  STAssertNotNil(data, nil);
 74  GTMFourCharCode *fcc2
 75    = (GTMFourCharCode*)[NSKeyedUnarchiver unarchiveObjectWithData:data];
 76  STAssertNotNil(fcc2, nil);
 77  STAssertEqualObjects(fcc, fcc2, nil);
 78
 79  fcc = [[[GTMFourCharCode alloc] initWithFourCharCode:'\?\?\?\?'] autorelease];
 80  STAssertNotNil(fcc, nil);
 81  STAssertEqualObjects([fcc stringValue], @"????", nil);
 82  STAssertEqualObjects([fcc numberValue],
 83                       [NSNumber numberWithUnsignedInt:'\?\?\?\?'], nil);
 84  STAssertEquals([fcc fourCharCode], (FourCharCode)'\?\?\?\?', nil);
 85
 86  fcc = [[[GTMFourCharCode alloc] initWithString:@"????"] autorelease];
 87  STAssertNotNil(fcc, nil);
 88  STAssertEqualObjects([fcc stringValue], @"????", nil);
 89  STAssertEqualObjects([fcc numberValue],
 90                       [NSNumber numberWithUnsignedInt:'\?\?\?\?'], nil);
 91  STAssertEquals([fcc fourCharCode], (FourCharCode)'\?\?\?\?', nil);
 92
 93  fcc = [GTMFourCharCode fourCharCodeWithFourCharCode:1];
 94  STAssertNotNil(fcc, nil);
 95  STAssertTrue([[fcc stringValue] isEqualToString:lowAsciiString_], nil);
 96  STAssertEqualObjects([fcc numberValue],
 97                       [NSNumber numberWithUnsignedInt:1], nil);
 98  STAssertEquals([fcc fourCharCode], (FourCharCode)1, nil);
 99
100  fcc = [GTMFourCharCode fourCharCodeWithString:@"BADDSTRING"];
101  STAssertNil(fcc, nil);
102
103  fcc2 = [GTMFourCharCode fourCharCodeWithFourCharCode:kGTMHighMacOSRomanCode];
104  STAssertNotNil(fcc2, nil);
105  STAssertEqualObjects([fcc2 stringValue], highMacOSRomanString_, nil);
106  STAssertEqualObjects([fcc2 numberValue],
107                       [NSNumber numberWithUnsignedInt:kGTMHighMacOSRomanCode],
108                       nil);
109  STAssertEquals([fcc2 fourCharCode],
110                 (FourCharCode)kGTMHighMacOSRomanCode, nil);
111}
112
113- (void)testStringWithCode {
114  STAssertEqualObjects([GTMFourCharCode stringWithFourCharCode:'APPL'],
115                       @"APPL", nil);
116  STAssertEqualObjects([GTMFourCharCode stringWithFourCharCode:1],
117                       lowAsciiString_, nil);
118  STAssertEqualObjects([GTMFourCharCode stringWithFourCharCode:kGTMHighMacOSRomanCode],
119                       highMacOSRomanString_, nil);
120}
121
122@end