Thomas Van Lenten | 988ffe0 | 2017-01-04 15:03:42 -0500 | [diff] [blame] | 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 | |
| 31 | #import <Foundation/Foundation.h> |
| 32 | #import <XCTest/XCTest.h> |
| 33 | |
| 34 | #import "GPBDictionary.h" |
| 35 | #import "GPBDictionary_PackagePrivate.h" |
| 36 | |
| 37 | #import "GPBTestUtilities.h" |
| 38 | |
| 39 | #pragma mark - GPBAutocreatedDictionary Tests |
| 40 | |
| 41 | // These are hand written tests to double check some behaviors of the |
| 42 | // GPBAutocreatedDictionary. The GPBDictionary+[type]Tests files are generate |
| 43 | // tests. |
| 44 | |
| 45 | // NOTE: GPBAutocreatedDictionary is private to the library, users of the |
| 46 | // library should never have to directly deal with this class. |
| 47 | |
| 48 | @interface GPBAutocreatedDictionaryTests : XCTestCase |
| 49 | @end |
| 50 | |
| 51 | @implementation GPBAutocreatedDictionaryTests |
| 52 | |
| 53 | - (void)testEquality { |
| 54 | GPBAutocreatedDictionary *dict = [[GPBAutocreatedDictionary alloc] init]; |
| 55 | |
| 56 | XCTAssertTrue([dict isEqual:@{}]); |
| 57 | XCTAssertTrue([dict isEqualToDictionary:@{}]); |
| 58 | |
| 59 | XCTAssertFalse([dict isEqual:@{ @"foo" : @"bar" }]); |
| 60 | XCTAssertFalse([dict isEqualToDictionary:@{ @"foo" : @"bar" }]); |
| 61 | |
| 62 | [dict setObject:@"bar" forKey:@"foo"]; |
| 63 | |
| 64 | XCTAssertFalse([dict isEqual:@{}]); |
| 65 | XCTAssertFalse([dict isEqualToDictionary:@{}]); |
| 66 | XCTAssertTrue([dict isEqual:@{ @"foo" : @"bar" }]); |
| 67 | XCTAssertTrue([dict isEqualToDictionary:@{ @"foo" : @"bar" }]); |
| 68 | XCTAssertFalse([dict isEqual:@{ @"bar" : @"baz" }]); |
| 69 | XCTAssertFalse([dict isEqualToDictionary:@{ @"bar" : @"baz" }]); |
| 70 | |
| 71 | GPBAutocreatedDictionary *dict2 = [[GPBAutocreatedDictionary alloc] init]; |
| 72 | |
| 73 | XCTAssertFalse([dict isEqual:dict2]); |
| 74 | XCTAssertFalse([dict isEqualToDictionary:dict2]); |
| 75 | |
| 76 | [dict2 setObject:@"mumble" forKey:@"foo"]; |
| 77 | XCTAssertFalse([dict isEqual:dict2]); |
| 78 | XCTAssertFalse([dict isEqualToDictionary:dict2]); |
| 79 | |
| 80 | [dict2 setObject:@"bar" forKey:@"foo"]; |
| 81 | XCTAssertTrue([dict isEqual:dict2]); |
| 82 | XCTAssertTrue([dict isEqualToDictionary:dict2]); |
| 83 | |
| 84 | [dict2 release]; |
| 85 | [dict release]; |
| 86 | } |
| 87 | |
| 88 | - (void)testCopy { |
| 89 | { |
| 90 | GPBAutocreatedDictionary *dict = [[GPBAutocreatedDictionary alloc] init]; |
| 91 | |
| 92 | NSDictionary *cpy = [dict copy]; |
| 93 | XCTAssertTrue(cpy != dict); // Ptr compare |
| 94 | XCTAssertTrue([cpy isKindOfClass:[NSDictionary class]]); |
| 95 | XCTAssertFalse([cpy isKindOfClass:[GPBAutocreatedDictionary class]]); |
| 96 | XCTAssertEqual(cpy.count, (NSUInteger)0); |
| 97 | |
| 98 | NSDictionary *cpy2 = [dict copy]; |
| 99 | XCTAssertTrue(cpy2 != dict); // Ptr compare |
| 100 | XCTAssertTrue(cpy2 != cpy); // Ptr compare |
| 101 | XCTAssertTrue([cpy2 isKindOfClass:[NSDictionary class]]); |
| 102 | XCTAssertFalse([cpy2 isKindOfClass:[GPBAutocreatedDictionary class]]); |
| 103 | XCTAssertEqual(cpy2.count, (NSUInteger)0); |
| 104 | |
| 105 | [cpy2 release]; |
| 106 | [cpy release]; |
| 107 | [dict release]; |
| 108 | } |
| 109 | |
| 110 | { |
| 111 | GPBAutocreatedDictionary *dict = [[GPBAutocreatedDictionary alloc] init]; |
| 112 | |
| 113 | NSMutableDictionary *cpy = [dict mutableCopy]; |
| 114 | XCTAssertTrue(cpy != dict); // Ptr compare |
| 115 | XCTAssertTrue([cpy isKindOfClass:[NSMutableDictionary class]]); |
| 116 | XCTAssertFalse([cpy isKindOfClass:[GPBAutocreatedDictionary class]]); |
| 117 | XCTAssertEqual(cpy.count, (NSUInteger)0); |
| 118 | |
| 119 | NSMutableDictionary *cpy2 = [dict mutableCopy]; |
| 120 | XCTAssertTrue(cpy2 != dict); // Ptr compare |
| 121 | XCTAssertTrue(cpy2 != cpy); // Ptr compare |
| 122 | XCTAssertTrue([cpy2 isKindOfClass:[NSMutableDictionary class]]); |
| 123 | XCTAssertFalse([cpy2 isKindOfClass:[GPBAutocreatedDictionary class]]); |
| 124 | XCTAssertEqual(cpy2.count, (NSUInteger)0); |
| 125 | |
| 126 | [cpy2 release]; |
| 127 | [cpy release]; |
| 128 | [dict release]; |
| 129 | } |
| 130 | |
| 131 | { |
| 132 | GPBAutocreatedDictionary *dict = [[GPBAutocreatedDictionary alloc] init]; |
| 133 | dict[@"foo"] = @"bar"; |
| 134 | dict[@"baz"] = @"mumble"; |
| 135 | |
| 136 | NSDictionary *cpy = [dict copy]; |
| 137 | XCTAssertTrue(cpy != dict); // Ptr compare |
| 138 | XCTAssertTrue([cpy isKindOfClass:[NSDictionary class]]); |
| 139 | XCTAssertFalse([cpy isKindOfClass:[GPBAutocreatedDictionary class]]); |
| 140 | XCTAssertEqual(cpy.count, (NSUInteger)2); |
| 141 | XCTAssertEqualObjects(cpy[@"foo"], @"bar"); |
| 142 | XCTAssertEqualObjects(cpy[@"baz"], @"mumble"); |
| 143 | |
| 144 | NSDictionary *cpy2 = [dict copy]; |
| 145 | XCTAssertTrue(cpy2 != dict); // Ptr compare |
| 146 | XCTAssertTrue(cpy2 != cpy); // Ptr compare |
| 147 | XCTAssertTrue([cpy2 isKindOfClass:[NSDictionary class]]); |
| 148 | XCTAssertFalse([cpy2 isKindOfClass:[GPBAutocreatedDictionary class]]); |
| 149 | XCTAssertEqual(cpy2.count, (NSUInteger)2); |
| 150 | XCTAssertEqualObjects(cpy2[@"foo"], @"bar"); |
| 151 | XCTAssertEqualObjects(cpy2[@"baz"], @"mumble"); |
| 152 | |
| 153 | [cpy2 release]; |
| 154 | [cpy release]; |
| 155 | [dict release]; |
| 156 | } |
| 157 | |
| 158 | { |
| 159 | GPBAutocreatedDictionary *dict = [[GPBAutocreatedDictionary alloc] init]; |
| 160 | dict[@"foo"] = @"bar"; |
| 161 | dict[@"baz"] = @"mumble"; |
| 162 | |
| 163 | NSMutableDictionary *cpy = [dict mutableCopy]; |
| 164 | XCTAssertTrue(cpy != dict); // Ptr compare |
| 165 | XCTAssertTrue([cpy isKindOfClass:[NSMutableDictionary class]]); |
| 166 | XCTAssertFalse([cpy isKindOfClass:[GPBAutocreatedDictionary class]]); |
| 167 | XCTAssertEqual(cpy.count, (NSUInteger)2); |
| 168 | XCTAssertEqualObjects(cpy[@"foo"], @"bar"); |
| 169 | XCTAssertEqualObjects(cpy[@"baz"], @"mumble"); |
| 170 | |
| 171 | NSMutableDictionary *cpy2 = [dict mutableCopy]; |
| 172 | XCTAssertTrue(cpy2 != dict); // Ptr compare |
| 173 | XCTAssertTrue(cpy2 != cpy); // Ptr compare |
| 174 | XCTAssertTrue([cpy2 isKindOfClass:[NSMutableDictionary class]]); |
| 175 | XCTAssertFalse([cpy2 isKindOfClass:[GPBAutocreatedDictionary class]]); |
| 176 | XCTAssertEqual(cpy2.count, (NSUInteger)2); |
| 177 | XCTAssertEqualObjects(cpy2[@"foo"], @"bar"); |
| 178 | XCTAssertEqualObjects(cpy2[@"baz"], @"mumble"); |
| 179 | |
| 180 | [cpy2 release]; |
| 181 | [cpy release]; |
| 182 | [dict release]; |
| 183 | } |
| 184 | } |
| 185 | |
| 186 | @end |