GPBDictionaryTests.m 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. // Protocol Buffers - Google's data interchange format
  2. // Copyright 2017 Google Inc. All rights reserved.
  3. // https://developers.google.com/protocol-buffers/
  4. //
  5. // Redistribution and use in source and binary forms, with or without
  6. // modification, are permitted provided that the following conditions are
  7. // met:
  8. //
  9. // * Redistributions of source code must retain the above copyright
  10. // notice, this list of conditions and the following disclaimer.
  11. // * Redistributions in binary form must reproduce the above
  12. // copyright notice, this list of conditions and the following disclaimer
  13. // in the documentation and/or other materials provided with the
  14. // distribution.
  15. // * Neither the name of Google Inc. nor the names of its
  16. // contributors may be used to endorse or promote products derived from
  17. // this software without specific prior written permission.
  18. //
  19. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  20. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  21. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  22. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  23. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  24. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  25. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  26. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  27. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  28. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  29. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. #import <Foundation/Foundation.h>
  31. #import <XCTest/XCTest.h>
  32. #import "GPBDictionary.h"
  33. #import "GPBDictionary_PackagePrivate.h"
  34. #import "GPBTestUtilities.h"
  35. #pragma mark - GPBAutocreatedDictionary Tests
  36. // These are hand written tests to double check some behaviors of the
  37. // GPBAutocreatedDictionary. The GPBDictionary+[type]Tests files are generate
  38. // tests.
  39. // NOTE: GPBAutocreatedDictionary is private to the library, users of the
  40. // library should never have to directly deal with this class.
  41. @interface GPBAutocreatedDictionaryTests : XCTestCase
  42. @end
  43. @implementation GPBAutocreatedDictionaryTests
  44. - (void)testEquality {
  45. GPBAutocreatedDictionary *dict = [[GPBAutocreatedDictionary alloc] init];
  46. XCTAssertTrue([dict isEqual:@{}]);
  47. XCTAssertTrue([dict isEqualToDictionary:@{}]);
  48. XCTAssertFalse([dict isEqual:@{ @"foo" : @"bar" }]);
  49. XCTAssertFalse([dict isEqualToDictionary:@{ @"foo" : @"bar" }]);
  50. [dict setObject:@"bar" forKey:@"foo"];
  51. XCTAssertFalse([dict isEqual:@{}]);
  52. XCTAssertFalse([dict isEqualToDictionary:@{}]);
  53. XCTAssertTrue([dict isEqual:@{ @"foo" : @"bar" }]);
  54. XCTAssertTrue([dict isEqualToDictionary:@{ @"foo" : @"bar" }]);
  55. XCTAssertFalse([dict isEqual:@{ @"bar" : @"baz" }]);
  56. XCTAssertFalse([dict isEqualToDictionary:@{ @"bar" : @"baz" }]);
  57. GPBAutocreatedDictionary *dict2 = [[GPBAutocreatedDictionary alloc] init];
  58. XCTAssertFalse([dict isEqual:dict2]);
  59. XCTAssertFalse([dict isEqualToDictionary:dict2]);
  60. [dict2 setObject:@"mumble" forKey:@"foo"];
  61. XCTAssertFalse([dict isEqual:dict2]);
  62. XCTAssertFalse([dict isEqualToDictionary:dict2]);
  63. [dict2 setObject:@"bar" forKey:@"foo"];
  64. XCTAssertTrue([dict isEqual:dict2]);
  65. XCTAssertTrue([dict isEqualToDictionary:dict2]);
  66. [dict2 release];
  67. [dict release];
  68. }
  69. - (void)testCopy {
  70. {
  71. GPBAutocreatedDictionary *dict = [[GPBAutocreatedDictionary alloc] init];
  72. NSDictionary *cpy = [dict copy];
  73. XCTAssertTrue(cpy != dict); // Ptr compare
  74. XCTAssertTrue([cpy isKindOfClass:[NSDictionary class]]);
  75. XCTAssertFalse([cpy isKindOfClass:[GPBAutocreatedDictionary class]]);
  76. XCTAssertEqual(cpy.count, (NSUInteger)0);
  77. NSDictionary *cpy2 = [dict copy];
  78. XCTAssertTrue(cpy2 != dict); // Ptr compare
  79. XCTAssertTrue(cpy2 != cpy); // Ptr compare
  80. XCTAssertTrue([cpy2 isKindOfClass:[NSDictionary class]]);
  81. XCTAssertFalse([cpy2 isKindOfClass:[GPBAutocreatedDictionary class]]);
  82. XCTAssertEqual(cpy2.count, (NSUInteger)0);
  83. [cpy2 release];
  84. [cpy release];
  85. [dict release];
  86. }
  87. {
  88. GPBAutocreatedDictionary *dict = [[GPBAutocreatedDictionary alloc] init];
  89. NSMutableDictionary *cpy = [dict mutableCopy];
  90. XCTAssertTrue(cpy != dict); // Ptr compare
  91. XCTAssertTrue([cpy isKindOfClass:[NSMutableDictionary class]]);
  92. XCTAssertFalse([cpy isKindOfClass:[GPBAutocreatedDictionary class]]);
  93. XCTAssertEqual(cpy.count, (NSUInteger)0);
  94. NSMutableDictionary *cpy2 = [dict mutableCopy];
  95. XCTAssertTrue(cpy2 != dict); // Ptr compare
  96. XCTAssertTrue(cpy2 != cpy); // Ptr compare
  97. XCTAssertTrue([cpy2 isKindOfClass:[NSMutableDictionary class]]);
  98. XCTAssertFalse([cpy2 isKindOfClass:[GPBAutocreatedDictionary class]]);
  99. XCTAssertEqual(cpy2.count, (NSUInteger)0);
  100. [cpy2 release];
  101. [cpy release];
  102. [dict release];
  103. }
  104. {
  105. GPBAutocreatedDictionary *dict = [[GPBAutocreatedDictionary alloc] init];
  106. dict[@"foo"] = @"bar";
  107. dict[@"baz"] = @"mumble";
  108. NSDictionary *cpy = [dict copy];
  109. XCTAssertTrue(cpy != dict); // Ptr compare
  110. XCTAssertTrue([cpy isKindOfClass:[NSDictionary class]]);
  111. XCTAssertFalse([cpy isKindOfClass:[GPBAutocreatedDictionary class]]);
  112. XCTAssertEqual(cpy.count, (NSUInteger)2);
  113. XCTAssertEqualObjects(cpy[@"foo"], @"bar");
  114. XCTAssertEqualObjects(cpy[@"baz"], @"mumble");
  115. NSDictionary *cpy2 = [dict copy];
  116. XCTAssertTrue(cpy2 != dict); // Ptr compare
  117. XCTAssertTrue(cpy2 != cpy); // Ptr compare
  118. XCTAssertTrue([cpy2 isKindOfClass:[NSDictionary class]]);
  119. XCTAssertFalse([cpy2 isKindOfClass:[GPBAutocreatedDictionary class]]);
  120. XCTAssertEqual(cpy2.count, (NSUInteger)2);
  121. XCTAssertEqualObjects(cpy2[@"foo"], @"bar");
  122. XCTAssertEqualObjects(cpy2[@"baz"], @"mumble");
  123. [cpy2 release];
  124. [cpy release];
  125. [dict release];
  126. }
  127. {
  128. GPBAutocreatedDictionary *dict = [[GPBAutocreatedDictionary alloc] init];
  129. dict[@"foo"] = @"bar";
  130. dict[@"baz"] = @"mumble";
  131. NSMutableDictionary *cpy = [dict mutableCopy];
  132. XCTAssertTrue(cpy != dict); // Ptr compare
  133. XCTAssertTrue([cpy isKindOfClass:[NSMutableDictionary class]]);
  134. XCTAssertFalse([cpy isKindOfClass:[GPBAutocreatedDictionary class]]);
  135. XCTAssertEqual(cpy.count, (NSUInteger)2);
  136. XCTAssertEqualObjects(cpy[@"foo"], @"bar");
  137. XCTAssertEqualObjects(cpy[@"baz"], @"mumble");
  138. NSMutableDictionary *cpy2 = [dict mutableCopy];
  139. XCTAssertTrue(cpy2 != dict); // Ptr compare
  140. XCTAssertTrue(cpy2 != cpy); // Ptr compare
  141. XCTAssertTrue([cpy2 isKindOfClass:[NSMutableDictionary class]]);
  142. XCTAssertFalse([cpy2 isKindOfClass:[GPBAutocreatedDictionary class]]);
  143. XCTAssertEqual(cpy2.count, (NSUInteger)2);
  144. XCTAssertEqualObjects(cpy2[@"foo"], @"bar");
  145. XCTAssertEqualObjects(cpy2[@"baz"], @"mumble");
  146. [cpy2 release];
  147. [cpy release];
  148. [dict release];
  149. }
  150. }
  151. @end