GPBWellKnownTypesTest.m 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. // Protocol Buffers - Google's data interchange format
  2. // Copyright 2015 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 "GPBWellKnownTypes.h"
  31. #import <XCTest/XCTest.h>
  32. #import "GPBTestUtilities.h"
  33. #import "google/protobuf/AnyTest.pbobjc.h"
  34. // Nanosecond time accuracy
  35. static const NSTimeInterval kTimeAccuracy = 1e-9;
  36. @interface WellKnownTypesTest : XCTestCase
  37. @end
  38. @implementation WellKnownTypesTest
  39. - (void)testTimeStamp {
  40. // Test negative and positive values.
  41. NSTimeInterval values[] = {
  42. -428027599.483999967, -1234567.0, -0.5, 0, 0.75, 54321.0, 2468086,483999967
  43. };
  44. for (size_t i = 0; i < GPBARRAYSIZE(values); ++i) {
  45. NSTimeInterval value = values[i];
  46. // Test Creation - date.
  47. NSDate *date = [NSDate dateWithTimeIntervalSince1970:value];
  48. GPBTimestamp *timeStamp = [[GPBTimestamp alloc] initWithDate:date];
  49. XCTAssertGreaterThanOrEqual(timeStamp.nanos, 0,
  50. @"Offset %f - Date: %@", (double)value, date);
  51. XCTAssertLessThan(timeStamp.nanos, 1e9,
  52. @"Offset %f - Date: %@", (double)value, date);
  53. // Comparing timeIntervals instead of directly comparing dates because date
  54. // equality requires the time intervals to be exactly the same, and the
  55. // timeintervals go through a bit of floating point error as they are
  56. // converted back and forth from the internal representation.
  57. XCTAssertEqualWithAccuracy(value, timeStamp.date.timeIntervalSince1970,
  58. kTimeAccuracy,
  59. @"Offset %f - Date: %@", (double)value, date);
  60. [timeStamp release];
  61. // Test Creation - timeIntervalSince1970.
  62. timeStamp = [[GPBTimestamp alloc] initWithTimeIntervalSince1970:value];
  63. XCTAssertGreaterThanOrEqual(timeStamp.nanos, 0,
  64. @"Offset %f - Date: %@", (double)value, date);
  65. XCTAssertLessThan(timeStamp.nanos, 1e9,
  66. @"Offset %f - Date: %@", (double)value, date);
  67. XCTAssertEqualWithAccuracy(value, timeStamp.timeIntervalSince1970,
  68. kTimeAccuracy,
  69. @"Offset %f - Date: %@", (double)value, date);
  70. [timeStamp release];
  71. // Test Mutation - date.
  72. timeStamp = [[GPBTimestamp alloc] init];
  73. timeStamp.date = date;
  74. XCTAssertGreaterThanOrEqual(timeStamp.nanos, 0,
  75. @"Offset %f - Date: %@", (double)value, date);
  76. XCTAssertLessThan(timeStamp.nanos, 1e9,
  77. @"Offset %f - Date: %@", (double)value, date);
  78. XCTAssertEqualWithAccuracy(value, timeStamp.date.timeIntervalSince1970,
  79. kTimeAccuracy,
  80. @"Offset %f - Date: %@", (double)value, date);
  81. [timeStamp release];
  82. // Test Mutation - timeIntervalSince1970.
  83. timeStamp = [[GPBTimestamp alloc] init];
  84. timeStamp.timeIntervalSince1970 = value;
  85. XCTAssertGreaterThanOrEqual(timeStamp.nanos, 0,
  86. @"Offset %f - Date: %@", (double)value, date);
  87. XCTAssertLessThan(timeStamp.nanos, 1e9,
  88. @"Offset %f - Date: %@", (double)value, date);
  89. XCTAssertEqualWithAccuracy(value, timeStamp.date.timeIntervalSince1970,
  90. kTimeAccuracy,
  91. @"Offset %f - Date: %@", (double)value, date);
  92. [timeStamp release];
  93. }
  94. }
  95. - (void)testDuration {
  96. // Test negative and positive values.
  97. NSTimeInterval values[] = { -1000.0001, -500.0, -0.5, 0, 0.75, 1000.0, 2000.0002 };
  98. for (size_t i = 0; i < GPBARRAYSIZE(values); ++i) {
  99. NSTimeInterval value = values[i];
  100. // Test Creation.
  101. GPBDuration *duration =
  102. [[GPBDuration alloc] initWithTimeInterval:value];
  103. XCTAssertEqualWithAccuracy(value, duration.timeInterval, kTimeAccuracy,
  104. @"For interval %f", (double)value);
  105. if (value > 0) {
  106. XCTAssertGreaterThanOrEqual(duration.seconds, 0,
  107. @"For interval %f", (double)value);
  108. XCTAssertGreaterThanOrEqual(duration.nanos, 0,
  109. @"For interval %f", (double)value);
  110. } else {
  111. XCTAssertLessThanOrEqual(duration.seconds, 0,
  112. @"For interval %f", (double)value);
  113. XCTAssertLessThanOrEqual(duration.nanos, 0,
  114. @"For interval %f", (double)value);
  115. }
  116. [duration release];
  117. // Test Mutation.
  118. duration = [[GPBDuration alloc] init];
  119. duration.timeInterval = value;
  120. XCTAssertEqualWithAccuracy(value, duration.timeInterval, kTimeAccuracy,
  121. @"For interval %f", (double)value);
  122. if (value > 0) {
  123. XCTAssertGreaterThanOrEqual(duration.seconds, 0,
  124. @"For interval %f", (double)value);
  125. XCTAssertGreaterThanOrEqual(duration.nanos, 0,
  126. @"For interval %f", (double)value);
  127. } else {
  128. XCTAssertLessThanOrEqual(duration.seconds, 0,
  129. @"For interval %f", (double)value);
  130. XCTAssertLessThanOrEqual(duration.nanos, 0,
  131. @"For interval %f", (double)value);
  132. }
  133. [duration release];
  134. }
  135. }
  136. - (void)testAnyHelpers {
  137. // Set and extract covers most of the code.
  138. TestAny *subMessage = [TestAny message];
  139. subMessage.int32Value = 12345;
  140. TestAny *message = [TestAny message];
  141. NSError *err = nil;
  142. message.anyValue = [GPBAny anyWithMessage:subMessage error:&err];
  143. XCTAssertNil(err);
  144. NSData *data = message.data;
  145. XCTAssertNotNil(data);
  146. TestAny *message2 = [TestAny parseFromData:data error:&err];
  147. XCTAssertNil(err);
  148. XCTAssertNotNil(message2);
  149. XCTAssertTrue(message2.hasAnyValue);
  150. TestAny *subMessage2 =
  151. (TestAny *)[message.anyValue unpackMessageClass:[TestAny class]
  152. error:&err];
  153. XCTAssertNil(err);
  154. XCTAssertNotNil(subMessage2);
  155. XCTAssertEqual(subMessage2.int32Value, 12345);
  156. // NULL errorPtr in the two calls.
  157. message.anyValue = [GPBAny anyWithMessage:subMessage error:NULL];
  158. NSData *data2 = message.data;
  159. XCTAssertEqualObjects(data2, data);
  160. TestAny *subMessage3 =
  161. (TestAny *)[message.anyValue unpackMessageClass:[TestAny class]
  162. error:NULL];
  163. XCTAssertNotNil(subMessage3);
  164. XCTAssertEqualObjects(subMessage2, subMessage3);
  165. // Try to extract wrong type.
  166. GPBTimestamp *wrongMessage =
  167. (GPBTimestamp *)[message.anyValue unpackMessageClass:[GPBTimestamp class]
  168. error:&err];
  169. XCTAssertNotNil(err);
  170. XCTAssertNil(wrongMessage);
  171. XCTAssertEqualObjects(err.domain, GPBWellKnownTypesErrorDomain);
  172. XCTAssertEqual(err.code, GPBWellKnownTypesErrorCodeTypeURLMismatch);
  173. wrongMessage =
  174. (GPBTimestamp *)[message.anyValue unpackMessageClass:[GPBTimestamp class]
  175. error:NULL];
  176. XCTAssertNil(wrongMessage);
  177. }
  178. @end