GPBCodedOuputStreamTests.m 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  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 "GPBTestUtilities.h"
  31. #import "GPBCodedOutputStream.h"
  32. #import "GPBCodedInputStream.h"
  33. #import "GPBUtilities_PackagePrivate.h"
  34. #import "google/protobuf/Unittest.pbobjc.h"
  35. @interface CodedOutputStreamTests : GPBTestCase
  36. @end
  37. @implementation CodedOutputStreamTests
  38. - (NSData*)bytes_with_sentinel:(int32_t)unused, ... {
  39. va_list list;
  40. va_start(list, unused);
  41. NSMutableData* values = [NSMutableData dataWithCapacity:0];
  42. int32_t i;
  43. while ((i = va_arg(list, int32_t)) != 256) {
  44. NSAssert(i >= 0 && i < 256, @"");
  45. uint8_t u = (uint8_t)i;
  46. [values appendBytes:&u length:1];
  47. }
  48. va_end(list);
  49. return values;
  50. }
  51. #define bytes(...) [self bytes_with_sentinel:0, __VA_ARGS__, 256]
  52. - (void)assertWriteLittleEndian32:(NSData*)data value:(int32_t)value {
  53. NSOutputStream* rawOutput = [NSOutputStream outputStreamToMemory];
  54. GPBCodedOutputStream* output =
  55. [GPBCodedOutputStream streamWithOutputStream:rawOutput];
  56. [output writeRawLittleEndian32:(int32_t)value];
  57. [output flush];
  58. NSData* actual =
  59. [rawOutput propertyForKey:NSStreamDataWrittenToMemoryStreamKey];
  60. XCTAssertEqualObjects(data, actual);
  61. // Try different block sizes.
  62. for (int blockSize = 1; blockSize <= 16; blockSize *= 2) {
  63. rawOutput = [NSOutputStream outputStreamToMemory];
  64. output = [GPBCodedOutputStream streamWithOutputStream:rawOutput
  65. bufferSize:blockSize];
  66. [output writeRawLittleEndian32:(int32_t)value];
  67. [output flush];
  68. actual = [rawOutput propertyForKey:NSStreamDataWrittenToMemoryStreamKey];
  69. XCTAssertEqualObjects(data, actual);
  70. }
  71. }
  72. - (void)assertWriteLittleEndian64:(NSData*)data value:(int64_t)value {
  73. NSOutputStream* rawOutput = [NSOutputStream outputStreamToMemory];
  74. GPBCodedOutputStream* output =
  75. [GPBCodedOutputStream streamWithOutputStream:rawOutput];
  76. [output writeRawLittleEndian64:value];
  77. [output flush];
  78. NSData* actual =
  79. [rawOutput propertyForKey:NSStreamDataWrittenToMemoryStreamKey];
  80. XCTAssertEqualObjects(data, actual);
  81. // Try different block sizes.
  82. for (int blockSize = 1; blockSize <= 16; blockSize *= 2) {
  83. rawOutput = [NSOutputStream outputStreamToMemory];
  84. output = [GPBCodedOutputStream streamWithOutputStream:rawOutput
  85. bufferSize:blockSize];
  86. [output writeRawLittleEndian64:value];
  87. [output flush];
  88. actual = [rawOutput propertyForKey:NSStreamDataWrittenToMemoryStreamKey];
  89. XCTAssertEqualObjects(data, actual);
  90. }
  91. }
  92. - (void)assertWriteVarint:(NSData*)data value:(int64_t)value {
  93. // Only do 32-bit write if the value fits in 32 bits.
  94. if (GPBLogicalRightShift64(value, 32) == 0) {
  95. NSOutputStream* rawOutput = [NSOutputStream outputStreamToMemory];
  96. GPBCodedOutputStream* output =
  97. [GPBCodedOutputStream streamWithOutputStream:rawOutput];
  98. [output writeRawVarint32:(int32_t)value];
  99. [output flush];
  100. NSData* actual =
  101. [rawOutput propertyForKey:NSStreamDataWrittenToMemoryStreamKey];
  102. XCTAssertEqualObjects(data, actual);
  103. // Also try computing size.
  104. XCTAssertEqual(GPBComputeRawVarint32Size((int32_t)value),
  105. (size_t)data.length);
  106. }
  107. {
  108. NSOutputStream* rawOutput = [NSOutputStream outputStreamToMemory];
  109. GPBCodedOutputStream* output =
  110. [GPBCodedOutputStream streamWithOutputStream:rawOutput];
  111. [output writeRawVarint64:value];
  112. [output flush];
  113. NSData* actual =
  114. [rawOutput propertyForKey:NSStreamDataWrittenToMemoryStreamKey];
  115. XCTAssertEqualObjects(data, actual);
  116. // Also try computing size.
  117. XCTAssertEqual(GPBComputeRawVarint64Size(value), (size_t)data.length);
  118. }
  119. // Try different block sizes.
  120. for (int blockSize = 1; blockSize <= 16; blockSize *= 2) {
  121. // Only do 32-bit write if the value fits in 32 bits.
  122. if (GPBLogicalRightShift64(value, 32) == 0) {
  123. NSOutputStream* rawOutput = [NSOutputStream outputStreamToMemory];
  124. GPBCodedOutputStream* output =
  125. [GPBCodedOutputStream streamWithOutputStream:rawOutput
  126. bufferSize:blockSize];
  127. [output writeRawVarint32:(int32_t)value];
  128. [output flush];
  129. NSData* actual =
  130. [rawOutput propertyForKey:NSStreamDataWrittenToMemoryStreamKey];
  131. XCTAssertEqualObjects(data, actual);
  132. }
  133. {
  134. NSOutputStream* rawOutput = [NSOutputStream outputStreamToMemory];
  135. GPBCodedOutputStream* output =
  136. [GPBCodedOutputStream streamWithOutputStream:rawOutput
  137. bufferSize:blockSize];
  138. [output writeRawVarint64:value];
  139. [output flush];
  140. NSData* actual =
  141. [rawOutput propertyForKey:NSStreamDataWrittenToMemoryStreamKey];
  142. XCTAssertEqualObjects(data, actual);
  143. }
  144. }
  145. }
  146. - (void)testWriteVarint1 {
  147. [self assertWriteVarint:bytes(0x00) value:0];
  148. }
  149. - (void)testWriteVarint2 {
  150. [self assertWriteVarint:bytes(0x01) value:1];
  151. }
  152. - (void)testWriteVarint3 {
  153. [self assertWriteVarint:bytes(0x7f) value:127];
  154. }
  155. - (void)testWriteVarint4 {
  156. // 14882
  157. [self assertWriteVarint:bytes(0xa2, 0x74) value:(0x22 << 0) | (0x74 << 7)];
  158. }
  159. - (void)testWriteVarint5 {
  160. // 2961488830
  161. [self assertWriteVarint:bytes(0xbe, 0xf7, 0x92, 0x84, 0x0b)
  162. value:(0x3e << 0) | (0x77 << 7) | (0x12 << 14) |
  163. (0x04 << 21) | (0x0bLL << 28)];
  164. }
  165. - (void)testWriteVarint6 {
  166. // 64-bit
  167. // 7256456126
  168. [self assertWriteVarint:bytes(0xbe, 0xf7, 0x92, 0x84, 0x1b)
  169. value:(0x3e << 0) | (0x77 << 7) | (0x12 << 14) |
  170. (0x04 << 21) | (0x1bLL << 28)];
  171. }
  172. - (void)testWriteVarint7 {
  173. // 41256202580718336
  174. [self assertWriteVarint:bytes(0x80, 0xe6, 0xeb, 0x9c, 0xc3, 0xc9, 0xa4, 0x49)
  175. value:(0x00 << 0) | (0x66 << 7) | (0x6b << 14) |
  176. (0x1c << 21) | (0x43LL << 28) | (0x49LL << 35) |
  177. (0x24LL << 42) | (0x49LL << 49)];
  178. }
  179. - (void)testWriteVarint8 {
  180. // 11964378330978735131
  181. [self assertWriteVarint:bytes(0x9b, 0xa8, 0xf9, 0xc2, 0xbb, 0xd6, 0x80, 0x85,
  182. 0xa6, 0x01)
  183. value:(0x1b << 0) | (0x28 << 7) | (0x79 << 14) |
  184. (0x42 << 21) | (0x3bLL << 28) | (0x56LL << 35) |
  185. (0x00LL << 42) | (0x05LL << 49) | (0x26LL << 56) |
  186. (0x01LL << 63)];
  187. }
  188. - (void)testWriteLittleEndian {
  189. [self assertWriteLittleEndian32:bytes(0x78, 0x56, 0x34, 0x12)
  190. value:0x12345678];
  191. [self assertWriteLittleEndian32:bytes(0xf0, 0xde, 0xbc, 0x9a)
  192. value:0x9abcdef0];
  193. [self assertWriteLittleEndian64:bytes(0xf0, 0xde, 0xbc, 0x9a, 0x78, 0x56,
  194. 0x34, 0x12)
  195. value:0x123456789abcdef0LL];
  196. [self assertWriteLittleEndian64:bytes(0x78, 0x56, 0x34, 0x12, 0xf0, 0xde,
  197. 0xbc, 0x9a)
  198. value:0x9abcdef012345678LL];
  199. }
  200. - (void)testEncodeZigZag {
  201. XCTAssertEqual(0U, GPBEncodeZigZag32(0));
  202. XCTAssertEqual(1U, GPBEncodeZigZag32(-1));
  203. XCTAssertEqual(2U, GPBEncodeZigZag32(1));
  204. XCTAssertEqual(3U, GPBEncodeZigZag32(-2));
  205. XCTAssertEqual(0x7FFFFFFEU, GPBEncodeZigZag32(0x3FFFFFFF));
  206. XCTAssertEqual(0x7FFFFFFFU, GPBEncodeZigZag32(0xC0000000));
  207. XCTAssertEqual(0xFFFFFFFEU, GPBEncodeZigZag32(0x7FFFFFFF));
  208. XCTAssertEqual(0xFFFFFFFFU, GPBEncodeZigZag32(0x80000000));
  209. XCTAssertEqual(0ULL, GPBEncodeZigZag64(0));
  210. XCTAssertEqual(1ULL, GPBEncodeZigZag64(-1));
  211. XCTAssertEqual(2ULL, GPBEncodeZigZag64(1));
  212. XCTAssertEqual(3ULL, GPBEncodeZigZag64(-2));
  213. XCTAssertEqual(0x000000007FFFFFFEULL,
  214. GPBEncodeZigZag64(0x000000003FFFFFFFLL));
  215. XCTAssertEqual(0x000000007FFFFFFFULL,
  216. GPBEncodeZigZag64(0xFFFFFFFFC0000000LL));
  217. XCTAssertEqual(0x00000000FFFFFFFEULL,
  218. GPBEncodeZigZag64(0x000000007FFFFFFFLL));
  219. XCTAssertEqual(0x00000000FFFFFFFFULL,
  220. GPBEncodeZigZag64(0xFFFFFFFF80000000LL));
  221. XCTAssertEqual(0xFFFFFFFFFFFFFFFEULL,
  222. GPBEncodeZigZag64(0x7FFFFFFFFFFFFFFFLL));
  223. XCTAssertEqual(0xFFFFFFFFFFFFFFFFULL,
  224. GPBEncodeZigZag64(0x8000000000000000LL));
  225. // Some easier-to-verify round-trip tests. The inputs (other than 0, 1, -1)
  226. // were chosen semi-randomly via keyboard bashing.
  227. XCTAssertEqual(0U, GPBEncodeZigZag32(GPBDecodeZigZag32(0)));
  228. XCTAssertEqual(1U, GPBEncodeZigZag32(GPBDecodeZigZag32(1)));
  229. XCTAssertEqual(-1U, GPBEncodeZigZag32(GPBDecodeZigZag32(-1)));
  230. XCTAssertEqual(14927U, GPBEncodeZigZag32(GPBDecodeZigZag32(14927)));
  231. XCTAssertEqual(-3612U, GPBEncodeZigZag32(GPBDecodeZigZag32(-3612)));
  232. XCTAssertEqual(0ULL, GPBEncodeZigZag64(GPBDecodeZigZag64(0)));
  233. XCTAssertEqual(1ULL, GPBEncodeZigZag64(GPBDecodeZigZag64(1)));
  234. XCTAssertEqual(-1ULL, GPBEncodeZigZag64(GPBDecodeZigZag64(-1)));
  235. XCTAssertEqual(14927ULL, GPBEncodeZigZag64(GPBDecodeZigZag64(14927)));
  236. XCTAssertEqual(-3612ULL, GPBEncodeZigZag64(GPBDecodeZigZag64(-3612)));
  237. XCTAssertEqual(856912304801416ULL,
  238. GPBEncodeZigZag64(GPBDecodeZigZag64(856912304801416LL)));
  239. XCTAssertEqual(-75123905439571256ULL,
  240. GPBEncodeZigZag64(GPBDecodeZigZag64(-75123905439571256LL)));
  241. }
  242. - (void)testWriteWholeMessage {
  243. // Not kGPBDefaultRepeatCount because we are comparing to a golden master file
  244. // that was generated with 2.
  245. TestAllTypes* message = [self allSetRepeatedCount:2];
  246. NSData* rawBytes = message.data;
  247. NSData* goldenData =
  248. [self getDataFileNamed:@"golden_message" dataToWrite:rawBytes];
  249. XCTAssertEqualObjects(rawBytes, goldenData);
  250. // Try different block sizes.
  251. for (int blockSize = 1; blockSize < 256; blockSize *= 2) {
  252. NSOutputStream* rawOutput = [NSOutputStream outputStreamToMemory];
  253. GPBCodedOutputStream* output =
  254. [GPBCodedOutputStream streamWithOutputStream:rawOutput
  255. bufferSize:blockSize];
  256. [message writeToCodedOutputStream:output];
  257. [output flush];
  258. NSData* actual =
  259. [rawOutput propertyForKey:NSStreamDataWrittenToMemoryStreamKey];
  260. XCTAssertEqualObjects(rawBytes, actual);
  261. }
  262. // Not kGPBDefaultRepeatCount because we are comparing to a golden master file
  263. // that was generated with 2.
  264. TestAllExtensions* extensions = [self allExtensionsSetRepeatedCount:2];
  265. rawBytes = extensions.data;
  266. goldenData = [self getDataFileNamed:@"golden_packed_fields_message"
  267. dataToWrite:rawBytes];
  268. XCTAssertEqualObjects(rawBytes, goldenData);
  269. }
  270. @end