subscripting.m 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. // TEST_CFLAGS -framework Foundation
  2. #import <Foundation/Foundation.h>
  3. #import <Foundation/NSDictionary.h>
  4. #import <objc/runtime.h>
  5. #import <objc/objc-abi.h>
  6. #include "test.h"
  7. @interface TestIndexed : NSObject <NSFastEnumeration> {
  8. NSMutableArray *indexedValues;
  9. }
  10. @property(readonly) NSUInteger count;
  11. - (id)objectAtIndexedSubscript:(NSUInteger)index;
  12. - (void)setObject:(id)object atIndexedSubscript:(NSUInteger)index;
  13. @end
  14. @implementation TestIndexed
  15. - (id)init {
  16. if ((self = [super init])) {
  17. indexedValues = [NSMutableArray new];
  18. }
  19. return self;
  20. }
  21. #if !__has_feature(objc_arc)
  22. - (void)dealloc {
  23. [indexedValues release];
  24. [super dealloc];
  25. }
  26. #endif
  27. - (NSUInteger)count { return [indexedValues count]; }
  28. - (id)objectAtIndexedSubscript:(NSUInteger)index { return [indexedValues objectAtIndex:index]; }
  29. - (void)setObject:(id)object atIndexedSubscript:(NSUInteger)index {
  30. if (index == NSNotFound)
  31. [indexedValues addObject:object];
  32. else
  33. [indexedValues replaceObjectAtIndex:index withObject:object];
  34. }
  35. - (NSString *)description {
  36. return [NSString stringWithFormat:@"indexedValues = %@", indexedValues];
  37. }
  38. - (NSUInteger)countByEnumeratingWithState:(NSFastEnumerationState *)state objects:(id __unsafe_unretained [])buffer count:(NSUInteger)len {
  39. return [indexedValues countByEnumeratingWithState:state objects:buffer count:len];
  40. }
  41. @end
  42. @interface TestKeyed : NSObject <NSFastEnumeration> {
  43. NSMutableDictionary *keyedValues;
  44. }
  45. @property(readonly) NSUInteger count;
  46. - (id)objectForKeyedSubscript:(id)key;
  47. - (void)setObject:(id)object forKeyedSubscript:(id)key;
  48. @end
  49. @implementation TestKeyed
  50. - (id)init {
  51. if ((self = [super init])) {
  52. keyedValues = [NSMutableDictionary new];
  53. }
  54. return self;
  55. }
  56. #if !__has_feature(objc_arc)
  57. - (void)dealloc {
  58. [keyedValues release];
  59. [super dealloc];
  60. }
  61. #endif
  62. - (NSUInteger)count { return [keyedValues count]; }
  63. - (id)objectForKeyedSubscript:(id)key { return [keyedValues objectForKey:key]; }
  64. - (void)setObject:(id)object forKeyedSubscript:(id)key {
  65. [keyedValues setObject:object forKey:key];
  66. }
  67. - (NSString *)description {
  68. return [NSString stringWithFormat:@"keyedValues = %@", keyedValues];
  69. }
  70. - (NSUInteger)countByEnumeratingWithState:(NSFastEnumerationState *)state objects:(id __unsafe_unretained [])buffer count:(NSUInteger)len {
  71. return [keyedValues countByEnumeratingWithState:state objects:buffer count:len];
  72. }
  73. @end
  74. int main() {
  75. PUSH_POOL {
  76. #if __has_feature(objc_bool) // placeholder until we get a more precise macro.
  77. TestIndexed *testIndexed = [TestIndexed new];
  78. id objects[] = { @1, @2, @3, @4, @5 };
  79. size_t i, count = sizeof(objects) / sizeof(id);
  80. for (i = 0; i < count; ++i) {
  81. testIndexed[NSNotFound] = objects[i];
  82. }
  83. for (i = 0; i < count; ++i) {
  84. id object = testIndexed[i];
  85. testassert(object == objects[i]);
  86. }
  87. if (testverbose()) {
  88. i = 0;
  89. for (id object in testIndexed) {
  90. NSString *message = [NSString stringWithFormat:@"testIndexed[%zu] = %@\n", i++, object];
  91. testprintf([message UTF8String]);
  92. }
  93. }
  94. TestKeyed *testKeyed = [TestKeyed new];
  95. id keys[] = { @"One", @"Two", @"Three", @"Four", @"Five" };
  96. for (i = 0; i < count; ++i) {
  97. id key = keys[i];
  98. testKeyed[key] = objects[i];
  99. }
  100. for (i = 0; i < count; ++i) {
  101. id key = keys[i];
  102. id object = testKeyed[key];
  103. testassert(object == objects[i]);
  104. }
  105. if (testverbose()) {
  106. for (id key in testKeyed) {
  107. NSString *message = [NSString stringWithFormat:@"testKeyed[@\"%@\"] = %@\n", key, testKeyed[key]];
  108. testprintf([message UTF8String]);
  109. }
  110. }
  111. #endif
  112. } POP_POOL;
  113. succeed(__FILE__);
  114. return 0;
  115. }