GPBUtilitiesTests.m 6.8 KB

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