PageRenderTime 64ms CodeModel.GetById 9ms app.highlight 52ms RepoModel.GetById 1ms app.codeStats 0ms

/core/externals/google-toolbox-for-mac/Foundation/GTMNSScanner+UnsignedTest.m

http://macfuse.googlecode.com/
Objective C | 116 lines | 91 code | 8 blank | 17 comment | 9 complexity | ca12d2d46ffba049d71f1625d247c810 MD5 | raw file
  1//
  2//  GTMNSScanner+UnsignedTest.m
  3//
  4//  Copyright 2010 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 "GTMNSScanner+Unsigned.h"
 21
 22@interface GTMNSScanner_UnsignedTest : GTMTestCase
 23@end
 24
 25@implementation GTMNSScanner_UnsignedTest
 26
 27#define TEST_BLOCK(A_MAX_VALUE) \
 28  { @"-1", 0, NO, 0 }, \
 29  { @"- 1", 0, NO, 0 }, \
 30  { @" - 1", 0, NO, 0 }, \
 31  { @"+ 1", 1, NO, 0 }, \
 32  { @" + 1", 1, NO, 0 }, \
 33  { @"0", 0, YES, 1 }, \
 34  { @"a", 0, NO, 0 }, \
 35  { @" ", 0, NO, 0 }, \
 36  { @"-1a", 0, NO, 0 }, \
 37  { @"a1", 0, NO, 0 }, \
 38  { @"1 ", 1, YES, 1 }, \
 39  { @"2 1 ", 2, YES, 1 }, \
 40  { @" 2 1 ", 2, YES, 2 }, \
 41  { @"99999999999999999999999999999999999", A_MAX_VALUE, YES, 35 }
 42
 43- (void)testScanUnsignedInt {
 44  struct {
 45    NSString *string;
 46    unsigned int val;
 47    BOOL goodScan;
 48    NSUInteger location;
 49  } testStruct[] = {
 50    TEST_BLOCK(UINT_MAX),
 51  };
 52  for (size_t i = 0; i < sizeof(testStruct) / sizeof(testStruct[0]); ++i) {
 53    NSScanner *scanner = [NSScanner scannerWithString:testStruct[i].string];
 54    STAssertNotNil(scanner, nil);
 55    unsigned int value;
 56    BOOL isGood = [scanner gtm_scanUnsignedInt:&value];
 57    STAssertEquals((int)isGood, (int)testStruct[i].goodScan, 
 58                   @"%@", testStruct[i].string);
 59    if (isGood && testStruct[i].goodScan) {
 60      STAssertEquals(value, testStruct[i].val, @"%@", testStruct[i].string);
 61    }
 62    STAssertEquals(testStruct[i].location, [scanner scanLocation], 
 63                   @"%@", testStruct[i].string);
 64  }
 65}
 66
 67- (void)testScanUInteger {
 68  struct {
 69    NSString *string;
 70    NSUInteger val;
 71    BOOL goodScan;
 72    NSUInteger location;
 73  } testStruct[] = {
 74    TEST_BLOCK(NSUIntegerMax),
 75  };
 76  for (size_t i = 0; i < sizeof(testStruct) / sizeof(testStruct[0]); ++i) {
 77    NSScanner *scanner = [NSScanner scannerWithString:testStruct[i].string];
 78    STAssertNotNil(scanner, nil);
 79    NSUInteger value;
 80    BOOL isGood = [scanner gtm_scanUInteger:&value];
 81    STAssertEquals((int)isGood, (int)testStruct[i].goodScan, 
 82                   @"%@", testStruct[i].string);
 83    if (isGood && testStruct[i].goodScan) {
 84      STAssertEquals(value, testStruct[i].val, @"%@", testStruct[i].string);
 85    }
 86    STAssertEquals(testStruct[i].location, [scanner scanLocation], 
 87                   @"%@", testStruct[i].string);
 88  }
 89}
 90
 91- (void)testScanUnsignedLongLong {
 92  struct {
 93    NSString *string;
 94    unsigned long long val;
 95    BOOL goodScan;
 96    NSUInteger location;
 97  } testStruct[] = {
 98    TEST_BLOCK(ULLONG_MAX),
 99    { @"4294967296", ((unsigned long long)UINT_MAX) + 1, YES, 10 }
100  };
101  for (size_t i = 0; i < sizeof(testStruct) / sizeof(testStruct[0]); ++i) {
102    NSScanner *scanner = [NSScanner scannerWithString:testStruct[i].string];
103    STAssertNotNil(scanner, nil);
104    unsigned long long value;
105    BOOL isGood = [scanner gtm_scanUnsignedLongLong:&value];
106    STAssertEquals((int)isGood, (int)testStruct[i].goodScan, 
107                   @"%@", testStruct[i].string);
108    if (isGood && testStruct[i].goodScan) {
109      STAssertEquals(value, testStruct[i].val, @"%@", testStruct[i].string);
110    }
111    STAssertEquals(testStruct[i].location, [scanner scanLocation], 
112                   @"%@", testStruct[i].string);
113  }
114}
115
116@end