PageRenderTime 27ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/core/externals/update-engine/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
Possible License(s): Apache-2.0, BSD-3-Clause, GPL-2.0
  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. #import "GTMSenTestCase.h"
  19. #import "GTMNSScanner+Unsigned.h"
  20. @interface GTMNSScanner_UnsignedTest : GTMTestCase
  21. @end
  22. @implementation GTMNSScanner_UnsignedTest
  23. #define TEST_BLOCK(A_MAX_VALUE) \
  24. { @"-1", 0, NO, 0 }, \
  25. { @"- 1", 0, NO, 0 }, \
  26. { @" - 1", 0, NO, 0 }, \
  27. { @"+ 1", 1, NO, 0 }, \
  28. { @" + 1", 1, NO, 0 }, \
  29. { @"0", 0, YES, 1 }, \
  30. { @"a", 0, NO, 0 }, \
  31. { @" ", 0, NO, 0 }, \
  32. { @"-1a", 0, NO, 0 }, \
  33. { @"a1", 0, NO, 0 }, \
  34. { @"1 ", 1, YES, 1 }, \
  35. { @"2 1 ", 2, YES, 1 }, \
  36. { @" 2 1 ", 2, YES, 2 }, \
  37. { @"99999999999999999999999999999999999", A_MAX_VALUE, YES, 35 }
  38. - (void)testScanUnsignedInt {
  39. struct {
  40. NSString *string;
  41. unsigned int val;
  42. BOOL goodScan;
  43. NSUInteger location;
  44. } testStruct[] = {
  45. TEST_BLOCK(UINT_MAX),
  46. };
  47. for (size_t i = 0; i < sizeof(testStruct) / sizeof(testStruct[0]); ++i) {
  48. NSScanner *scanner = [NSScanner scannerWithString:testStruct[i].string];
  49. STAssertNotNil(scanner, nil);
  50. unsigned int value;
  51. BOOL isGood = [scanner gtm_scanUnsignedInt:&value];
  52. STAssertEquals((int)isGood, (int)testStruct[i].goodScan,
  53. @"%@", testStruct[i].string);
  54. if (isGood && testStruct[i].goodScan) {
  55. STAssertEquals(value, testStruct[i].val, @"%@", testStruct[i].string);
  56. }
  57. STAssertEquals(testStruct[i].location, [scanner scanLocation],
  58. @"%@", testStruct[i].string);
  59. }
  60. }
  61. - (void)testScanUInteger {
  62. struct {
  63. NSString *string;
  64. NSUInteger val;
  65. BOOL goodScan;
  66. NSUInteger location;
  67. } testStruct[] = {
  68. TEST_BLOCK(NSUIntegerMax),
  69. };
  70. for (size_t i = 0; i < sizeof(testStruct) / sizeof(testStruct[0]); ++i) {
  71. NSScanner *scanner = [NSScanner scannerWithString:testStruct[i].string];
  72. STAssertNotNil(scanner, nil);
  73. NSUInteger value;
  74. BOOL isGood = [scanner gtm_scanUInteger:&value];
  75. STAssertEquals((int)isGood, (int)testStruct[i].goodScan,
  76. @"%@", testStruct[i].string);
  77. if (isGood && testStruct[i].goodScan) {
  78. STAssertEquals(value, testStruct[i].val, @"%@", testStruct[i].string);
  79. }
  80. STAssertEquals(testStruct[i].location, [scanner scanLocation],
  81. @"%@", testStruct[i].string);
  82. }
  83. }
  84. - (void)testScanUnsignedLongLong {
  85. struct {
  86. NSString *string;
  87. unsigned long long val;
  88. BOOL goodScan;
  89. NSUInteger location;
  90. } testStruct[] = {
  91. TEST_BLOCK(ULLONG_MAX),
  92. { @"4294967296", ((unsigned long long)UINT_MAX) + 1, YES, 10 }
  93. };
  94. for (size_t i = 0; i < sizeof(testStruct) / sizeof(testStruct[0]); ++i) {
  95. NSScanner *scanner = [NSScanner scannerWithString:testStruct[i].string];
  96. STAssertNotNil(scanner, nil);
  97. unsigned long long value;
  98. BOOL isGood = [scanner gtm_scanUnsignedLongLong:&value];
  99. STAssertEquals((int)isGood, (int)testStruct[i].goodScan,
  100. @"%@", testStruct[i].string);
  101. if (isGood && testStruct[i].goodScan) {
  102. STAssertEquals(value, testStruct[i].val, @"%@", testStruct[i].string);
  103. }
  104. STAssertEquals(testStruct[i].location, [scanner scanLocation],
  105. @"%@", testStruct[i].string);
  106. }
  107. }
  108. @end