OLD | NEW |
(Empty) | |
| 1 // Protocol Buffers - Google's data interchange format |
| 2 // Copyright 2008 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 <XCTest/XCTest.h> |
| 32 |
| 33 #import "GPBUtilities_PackagePrivate.h" |
| 34 |
| 35 #import <objc/runtime.h> |
| 36 |
| 37 #import "GPBTestUtilities.h" |
| 38 |
| 39 #import "GPBDescriptor.h" |
| 40 #import "GPBDescriptor_PackagePrivate.h" |
| 41 #import "GPBMessage.h" |
| 42 |
| 43 #import "google/protobuf/MapUnittest.pbobjc.h" |
| 44 #import "google/protobuf/Unittest.pbobjc.h" |
| 45 #import "google/protobuf/UnittestObjc.pbobjc.h" |
| 46 |
| 47 @interface UtilitiesTests : GPBTestCase |
| 48 @end |
| 49 |
| 50 @implementation UtilitiesTests |
| 51 |
| 52 - (void)testRightShiftFunctions { |
| 53 XCTAssertEqual((1UL << 31) >> 31, 1UL); |
| 54 XCTAssertEqual((1 << 31) >> 31, -1); |
| 55 XCTAssertEqual((1ULL << 63) >> 63, 1ULL); |
| 56 XCTAssertEqual((1LL << 63) >> 63, -1LL); |
| 57 |
| 58 XCTAssertEqual(GPBLogicalRightShift32((1 << 31), 31), 1); |
| 59 XCTAssertEqual(GPBLogicalRightShift64((1LL << 63), 63), 1LL); |
| 60 } |
| 61 |
| 62 - (void)testGPBDecodeTextFormatName { |
| 63 uint8_t decodeData[] = { |
| 64 0x6, |
| 65 // An inlined string (first to make sure the leading null is handled |
| 66 // correctly, and with a key of zero to check that). |
| 67 0x0, 0x0, 'z', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'I', 'J', 0x0, |
| 68 // All as is (00 op) |
| 69 0x1, 0x0A, 0x0, |
| 70 // Underscore, upper + 9 (10 op) |
| 71 0x3, 0xCA, 0x0, |
| 72 // Upper + 3 (10 op), underscore, upper + 5 (10 op) |
| 73 0x2, 0x44, 0xC6, 0x0, |
| 74 // All Upper for 4 (11 op), underscore, underscore, upper + 5 (10 op), |
| 75 // underscore, lower + 0 (01 op) |
| 76 0x4, 0x64, 0x80, 0xC5, 0xA1, 0x0, |
| 77 // 2 byte key: as is + 3 (00 op), underscore, lower + 4 (01 op), |
| 78 // underscore, lower + 3 (01 op), underscore, lower + 1 (01 op), |
| 79 // underscore, lower + 30 (01 op), as is + 30 (00 op), as is + 13 (00 op), |
| 80 // underscore, as is + 3 (00 op) |
| 81 0xE8, 0x07, 0x04, 0xA5, 0xA4, 0xA2, 0xBF, 0x1F, 0x0E, 0x84, 0x0, |
| 82 }; |
| 83 NSString *inputStr = @"abcdefghIJ"; |
| 84 |
| 85 // Empty inputs |
| 86 |
| 87 XCTAssertNil(GPBDecodeTextFormatName(nil, 1, NULL)); |
| 88 XCTAssertNil(GPBDecodeTextFormatName(decodeData, 1, NULL)); |
| 89 XCTAssertNil(GPBDecodeTextFormatName(nil, 1, inputStr)); |
| 90 |
| 91 // Keys not found. |
| 92 |
| 93 XCTAssertNil(GPBDecodeTextFormatName(decodeData, 5, inputStr)); |
| 94 XCTAssertNil(GPBDecodeTextFormatName(decodeData, -1, inputStr)); |
| 95 |
| 96 // Some name decodes. |
| 97 |
| 98 XCTAssertEqualObjects(GPBDecodeTextFormatName(decodeData, 1, inputStr), @"abcd
efghIJ"); |
| 99 XCTAssertEqualObjects(GPBDecodeTextFormatName(decodeData, 2, inputStr), @"Abcd
_EfghIJ"); |
| 100 XCTAssertEqualObjects(GPBDecodeTextFormatName(decodeData, 3, inputStr), @"_Abc
defghIJ"); |
| 101 XCTAssertEqualObjects(GPBDecodeTextFormatName(decodeData, 4, inputStr), @"ABCD
__EfghI_j"); |
| 102 |
| 103 // An inlined string (and key of zero). |
| 104 XCTAssertEqualObjects(GPBDecodeTextFormatName(decodeData, 0, inputStr), @"zbcd
efghIJ"); |
| 105 |
| 106 // Long name so multiple decode ops are needed. |
| 107 inputStr = @"longFieldNameIsLooooooooooooooooooooooooooooooooooooooooooooooooo
oooooooooooooooooooooooong1000"; |
| 108 XCTAssertEqualObjects(GPBDecodeTextFormatName(decodeData, 1000, inputStr), |
| 109 @"long_field_name_is_loooooooooooooooooooooooooooooooooo
ooooooooooooooooooooooooooooooooooooooong_1000"); |
| 110 } |
| 111 |
| 112 - (void)testTextFormat { |
| 113 TestAllTypes *message = [TestAllTypes message]; |
| 114 |
| 115 // Not kGPBDefaultRepeatCount because we are comparing to golden master file |
| 116 // which was generated with 2. |
| 117 [self setAllFields:message repeatedCount:2]; |
| 118 |
| 119 NSString *result = GPBTextFormatForMessage(message, nil); |
| 120 |
| 121 NSString *fileName = @"text_format_unittest_data.txt"; |
| 122 NSData *resultData = [result dataUsingEncoding:NSUTF8StringEncoding]; |
| 123 NSData *expectedData = |
| 124 [self getDataFileNamed:fileName dataToWrite:resultData]; |
| 125 NSString *expected = [[NSString alloc] initWithData:expectedData |
| 126 encoding:NSUTF8StringEncoding]; |
| 127 XCTAssertEqualObjects(expected, result); |
| 128 [expected release]; |
| 129 } |
| 130 |
| 131 - (void)testTextFormatExtra { |
| 132 // -testTextFormat uses all protos with fields that don't require special |
| 133 // handing for figuring out the names. The ObjC proto has a bunch of oddball |
| 134 // field and enum names that require the decode info to get right, so this |
| 135 // confirms they generated and decoded correctly. |
| 136 |
| 137 self_Class *message = [self_Class message]; |
| 138 message.cmd = YES; |
| 139 message.isProxy_p = YES; |
| 140 message.subEnum = self_autorelease_RetainCount; |
| 141 message.new_p.copy_p = @"foo"; |
| 142 |
| 143 NSString *expected = @"_cmd: true\n" |
| 144 @"isProxy: true\n" |
| 145 @"SubEnum: retainCount\n" |
| 146 @"New {\n" |
| 147 @" copy: \"foo\"\n" |
| 148 @"}\n"; |
| 149 NSString *result = GPBTextFormatForMessage(message, nil); |
| 150 XCTAssertEqualObjects(expected, result); |
| 151 } |
| 152 |
| 153 - (void)testTextFormatMaps { |
| 154 TestMap *message = [TestMap message]; |
| 155 |
| 156 // Map iteration order doesn't have to be stable, so use only one entry. |
| 157 [self setAllMapFields:message numEntries:1]; |
| 158 |
| 159 NSString *result = GPBTextFormatForMessage(message, nil); |
| 160 |
| 161 NSString *fileName = @"text_format_map_unittest_data.txt"; |
| 162 NSData *resultData = [result dataUsingEncoding:NSUTF8StringEncoding]; |
| 163 NSData *expectedData = |
| 164 [self getDataFileNamed:fileName dataToWrite:resultData]; |
| 165 NSString *expected = [[NSString alloc] initWithData:expectedData |
| 166 encoding:NSUTF8StringEncoding]; |
| 167 XCTAssertEqualObjects(expected, result); |
| 168 [expected release]; |
| 169 } |
| 170 |
| 171 // TODO(thomasvl): add test with extensions once those format with correct names
. |
| 172 |
| 173 @end |
OLD | NEW |