FSTFieldValueTests.mm 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555
  1. /*
  2. * Copyright 2017 Google
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #import "Firestore/Source/Model/FSTFieldValue.h"
  17. #import <FirebaseFirestore/FIRGeoPoint.h>
  18. #import <FirebaseFirestore/FIRTimestamp.h>
  19. #import <XCTest/XCTest.h>
  20. #import "Firestore/Source/API/FIRFirestore+Internal.h"
  21. #import "Firestore/Source/API/FSTUserDataConverter.h"
  22. #import "Firestore/Example/Tests/API/FSTAPIHelpers.h"
  23. #import "Firestore/Example/Tests/Util/FSTHelpers.h"
  24. #include "Firestore/core/src/firebase/firestore/model/database_id.h"
  25. #include "Firestore/core/src/firebase/firestore/util/string_apple.h"
  26. #include "Firestore/core/test/firebase/firestore/testutil/testutil.h"
  27. namespace testutil = firebase::firestore::testutil;
  28. namespace util = firebase::firestore::util;
  29. using firebase::firestore::model::DatabaseId;
  30. /** Helper to wrap the values in a set of equality groups using FSTTestFieldValue(). */
  31. NSArray *FSTWrapGroups(NSArray *groups) {
  32. NSMutableArray *wrapped = [NSMutableArray array];
  33. for (NSArray<id> *group in groups) {
  34. NSMutableArray *wrappedGroup = [NSMutableArray array];
  35. for (id value in group) {
  36. FSTFieldValue *wrappedValue;
  37. // Server Timestamp values can't be parsed directly, so we have a couple predefined sentinel
  38. // strings that can be used instead.
  39. if ([value isEqual:@"server-timestamp-1"]) {
  40. wrappedValue = [FSTServerTimestampValue
  41. serverTimestampValueWithLocalWriteTime:FSTTestTimestamp(2016, 5, 20, 10, 20, 0)
  42. previousValue:nil];
  43. } else if ([value isEqual:@"server-timestamp-2"]) {
  44. wrappedValue = [FSTServerTimestampValue
  45. serverTimestampValueWithLocalWriteTime:FSTTestTimestamp(2016, 10, 21, 15, 32, 0)
  46. previousValue:nil];
  47. } else if ([value isKindOfClass:[FSTDocumentKeyReference class]]) {
  48. // We directly convert these here so that the databaseIDs can be different.
  49. FSTDocumentKeyReference *reference = (FSTDocumentKeyReference *)value;
  50. wrappedValue = [FSTReferenceValue referenceValue:reference.key
  51. databaseID:reference.databaseID];
  52. } else {
  53. wrappedValue = FSTTestFieldValue(value);
  54. }
  55. [wrappedGroup addObject:wrappedValue];
  56. }
  57. [wrapped addObject:wrappedGroup];
  58. }
  59. return wrapped;
  60. }
  61. @interface FSTFieldValueTests : XCTestCase
  62. @end
  63. @implementation FSTFieldValueTests {
  64. NSDate *date1;
  65. NSDate *date2;
  66. }
  67. - (void)setUp {
  68. [super setUp];
  69. // Create a couple date objects for use in tests.
  70. date1 = FSTTestDate(2016, 5, 20, 10, 20, 0);
  71. date2 = FSTTestDate(2016, 10, 21, 15, 32, 0);
  72. }
  73. - (void)testWrapIntegers {
  74. NSArray *values = @[
  75. @(INT_MIN), @(-1), @0, @1, @2, @(UCHAR_MAX), @(INT_MAX), // Standard integers
  76. @(LONG_MIN), @(LONG_MAX), @(LLONG_MIN), @(LLONG_MAX) // Larger values
  77. ];
  78. for (id value in values) {
  79. FSTFieldValue *wrapped = FSTTestFieldValue(value);
  80. XCTAssertEqualObjects([wrapped class], [FSTIntegerValue class]);
  81. XCTAssertEqualObjects([wrapped value], @([value longLongValue]));
  82. }
  83. }
  84. - (void)testWrapsDoubles {
  85. // Note that 0x1.0p-1074 is a hex floating point literal representing the minimum subnormal
  86. // number: <https://en.wikipedia.org/wiki/Denormal_number>.
  87. NSArray *values = @[
  88. @(-INFINITY), @(-DBL_MAX), @(LLONG_MIN * -1.0), @(-1.1), @(-0x1.0p-1074), @(-0.0), @(0.0),
  89. @(0x1.0p-1074), @(DBL_MIN), @(1.1), @(LLONG_MAX * 1.0), @(DBL_MAX), @(INFINITY)
  90. ];
  91. for (id value in values) {
  92. FSTFieldValue *wrapped = FSTTestFieldValue(value);
  93. XCTAssertEqualObjects([wrapped class], [FSTDoubleValue class]);
  94. XCTAssertEqualObjects([wrapped value], value);
  95. }
  96. }
  97. - (void)testWrapsNilAndNSNull {
  98. FSTNullValue *nullValue = [FSTNullValue nullValue];
  99. XCTAssertEqual(FSTTestFieldValue(nil), nullValue);
  100. XCTAssertEqual(FSTTestFieldValue([NSNull null]), nullValue);
  101. XCTAssertEqual([nullValue value], [NSNull null]);
  102. }
  103. - (void)testWrapsBooleans {
  104. NSArray *values = @[ @YES, @NO, [NSNumber numberWithChar:1], [NSNumber numberWithChar:0] ];
  105. for (id value in values) {
  106. FSTFieldValue *wrapped = FSTTestFieldValue(value);
  107. XCTAssertEqualObjects([wrapped class], [FSTBooleanValue class]);
  108. XCTAssertEqualObjects([wrapped value], value);
  109. }
  110. // Unsigned chars could conceivably be handled consistently with signed chars but on arm64 these
  111. // end up being stored as signed shorts.
  112. FSTFieldValue *wrapped = FSTTestFieldValue([NSNumber numberWithUnsignedChar:1]);
  113. XCTAssertEqualObjects(wrapped, [FSTIntegerValue integerValue:1]);
  114. }
  115. union DoubleBits {
  116. double d;
  117. uint64_t bits;
  118. };
  119. - (void)testNormalizesNaNs {
  120. // NOTE: With v1 query semantics, it's no longer as important that our NaN representation matches
  121. // the backend, since all NaNs are defined to sort as equal, but we preserve the normalization and
  122. // this test regardless for now.
  123. // We use a canonical NaN bit pattern that's common for both Java and Objective-C. Specifically:
  124. // - sign: 0
  125. // - exponent: 11 bits, all 1
  126. // - significand: 52 bits, MSB=1, rest=0
  127. //
  128. // This matches the Firestore backend which uses Java's Double.doubleToLongBits which is defined
  129. // to normalize all NaNs to this value.
  130. union DoubleBits canonical = {.bits = 0x7ff8000000000000ULL};
  131. // IEEE 754 specifies that NaN isn't equal to itself.
  132. XCTAssertTrue(isnan(canonical.d));
  133. XCTAssertEqual(canonical.bits, canonical.bits);
  134. XCTAssertNotEqual(canonical.d, canonical.d);
  135. // All permutations of the 51 other non-MSB significand bits are also NaNs.
  136. union DoubleBits alternate = {.bits = 0x7fff000000000000ULL};
  137. XCTAssertTrue(isnan(alternate.d));
  138. XCTAssertNotEqual(alternate.bits, canonical.bits);
  139. XCTAssertNotEqual(alternate.d, canonical.d);
  140. // Even though at the C-level assignment preserves non-canonical NaNs, NSNumber normalizes all
  141. // NaNs to single shared instance, kCFNumberNaN. That NaN has no public definition for its value
  142. // but it happens to match what we need.
  143. union DoubleBits normalized = {.d = [[NSNumber numberWithDouble:alternate.d] doubleValue]};
  144. XCTAssertEqual(normalized.bits, canonical.bits);
  145. // Ensure we get the same normalization behavior (currently implemented explicitly by checking
  146. // for isnan() and then explicitly assigning NAN).
  147. union DoubleBits result;
  148. result.d = [[FSTDoubleValue doubleValue:canonical.d] internalValue];
  149. XCTAssertEqual(result.bits, canonical.bits);
  150. result.d = [[FSTDoubleValue doubleValue:alternate.d] internalValue];
  151. XCTAssertEqual(result.bits, canonical.bits);
  152. // A NaN that's canonical except it has the sign bit set (would be negative if signs mattered)
  153. union DoubleBits negative = {.bits = 0xfff8000000000000ULL};
  154. result.d = [[FSTDoubleValue doubleValue:negative.d] internalValue];
  155. XCTAssertTrue(isnan(negative.d));
  156. XCTAssertEqual(result.bits, canonical.bits);
  157. // A signaling NaN with significand where MSB is 0, and some non-MSB bit is one.
  158. union DoubleBits signaling = {.bits = 0xfff4000000000000ULL};
  159. XCTAssertTrue(isnan(signaling.d));
  160. result.d = [[FSTDoubleValue doubleValue:signaling.d] internalValue];
  161. XCTAssertEqual(result.bits, canonical.bits);
  162. }
  163. - (void)testZeros {
  164. // Floating point numbers have an explicit sign bit so it's possible to end up with negative
  165. // zero as a distinct value from positive zero.
  166. union DoubleBits zero = {.d = 0.0};
  167. union DoubleBits negativeZero = {.d = -0.0};
  168. // IEEE 754 requires these two zeros to compare equal.
  169. XCTAssertNotEqual(zero.bits, negativeZero.bits);
  170. XCTAssertEqual(zero.d, negativeZero.d);
  171. // NSNumber preserves the negative zero value but compares equal according to IEEE 754.
  172. union DoubleBits normalized = {.d = [[NSNumber numberWithDouble:negativeZero.d] doubleValue]};
  173. XCTAssertEqual(normalized.bits, negativeZero.bits);
  174. XCTAssertEqualObjects([NSNumber numberWithDouble:0.0], [NSNumber numberWithDouble:-0.0]);
  175. // FSTDoubleValue preserves positive/negative zero
  176. union DoubleBits result;
  177. result.d = [[[FSTDoubleValue doubleValue:zero.d] value] doubleValue];
  178. XCTAssertEqual(result.bits, zero.bits);
  179. result.d = [[[FSTDoubleValue doubleValue:negativeZero.d] value] doubleValue];
  180. XCTAssertEqual(result.bits, negativeZero.bits);
  181. // ... but compares positive/negative zero as unequal, compatibly with Firestore.
  182. XCTAssertNotEqualObjects([FSTDoubleValue doubleValue:0.0], [FSTDoubleValue doubleValue:-0.0]);
  183. }
  184. - (void)testWrapStrings {
  185. NSArray *values = @[ @"", @"abc" ];
  186. for (id value in values) {
  187. FSTFieldValue *wrapped = FSTTestFieldValue(value);
  188. XCTAssertEqualObjects([wrapped class], [FSTStringValue class]);
  189. XCTAssertEqualObjects([wrapped value], value);
  190. }
  191. }
  192. - (void)testWrapDates {
  193. NSArray *values = @[ FSTTestDate(1900, 12, 1, 1, 20, 30), FSTTestDate(2017, 4, 24, 13, 20, 30) ];
  194. for (id value in values) {
  195. FSTFieldValue *wrapped = FSTTestFieldValue(value);
  196. XCTAssertEqualObjects([wrapped class], [FSTTimestampValue class]);
  197. XCTAssertEqualObjects([[wrapped value] class], [FIRTimestamp class]);
  198. XCTAssertEqualObjects([wrapped value], [FIRTimestamp timestampWithDate:value]);
  199. }
  200. }
  201. - (void)testWrapGeoPoints {
  202. NSArray *values = @[ FSTTestGeoPoint(1.24, 4.56), FSTTestGeoPoint(-20, 100) ];
  203. for (id value in values) {
  204. FSTFieldValue *wrapped = FSTTestFieldValue(value);
  205. XCTAssertEqualObjects([wrapped class], [FSTGeoPointValue class]);
  206. XCTAssertEqualObjects([wrapped value], value);
  207. }
  208. }
  209. - (void)testWrapBlobs {
  210. NSArray *values = @[ FSTTestData(1, 2, 3), FSTTestData(1, 2) ];
  211. for (id value in values) {
  212. FSTFieldValue *wrapped = FSTTestFieldValue(value);
  213. XCTAssertEqualObjects([wrapped class], [FSTBlobValue class]);
  214. XCTAssertEqualObjects([wrapped value], value);
  215. }
  216. }
  217. - (void)testWrapResourceNames {
  218. NSArray *values = @[
  219. FSTTestRef("project", DatabaseId::kDefault, @"foo/bar"),
  220. FSTTestRef("project", DatabaseId::kDefault, @"foo/baz")
  221. ];
  222. for (FSTDocumentKeyReference *value in values) {
  223. FSTFieldValue *wrapped = FSTTestFieldValue(value);
  224. XCTAssertEqualObjects([wrapped class], [FSTReferenceValue class]);
  225. XCTAssertEqualObjects([wrapped value], value.key);
  226. XCTAssertTrue(*((FSTReferenceValue *)wrapped).databaseID ==
  227. *(const DatabaseId *)(value.databaseID));
  228. }
  229. }
  230. - (void)testWrapsEmptyObjects {
  231. XCTAssertEqualObjects(FSTTestFieldValue(@{}), [FSTObjectValue objectValue]);
  232. }
  233. - (void)testWrapsSimpleObjects {
  234. FSTObjectValue *actual =
  235. FSTTestObjectValue(@{@"a" : @"foo", @"b" : @(1L), @"c" : @YES, @"d" : [NSNull null]});
  236. FSTObjectValue *expected = [[FSTObjectValue alloc] initWithDictionary:@{
  237. @"a" : [FSTStringValue stringValue:@"foo"],
  238. @"b" : [FSTIntegerValue integerValue:1LL],
  239. @"c" : [FSTBooleanValue trueValue],
  240. @"d" : [FSTNullValue nullValue]
  241. }];
  242. XCTAssertEqualObjects(actual, expected);
  243. }
  244. - (void)testWrapsNestedObjects {
  245. FSTObjectValue *actual = FSTTestObjectValue(@{@"a" : @{@"b" : @{@"c" : @"foo"}, @"d" : @YES}});
  246. FSTObjectValue *expected = [[FSTObjectValue alloc] initWithDictionary:@{
  247. @"a" : [[FSTObjectValue alloc] initWithDictionary:@{
  248. @"b" :
  249. [[FSTObjectValue alloc] initWithDictionary:@{@"c" : [FSTStringValue stringValue:@"foo"]}],
  250. @"d" : [FSTBooleanValue booleanValue:YES]
  251. }]
  252. }];
  253. XCTAssertEqualObjects(actual, expected);
  254. }
  255. - (void)testExtractsFields {
  256. FSTObjectValue *obj = FSTTestObjectValue(@{@"foo" : @{@"a" : @YES, @"b" : @"string"}});
  257. FSTAssertIsKindOfClass(obj, FSTObjectValue);
  258. FSTAssertIsKindOfClass([obj valueForPath:testutil::Field("foo")], FSTObjectValue);
  259. XCTAssertEqualObjects([obj valueForPath:testutil::Field("foo.a")], [FSTBooleanValue trueValue]);
  260. XCTAssertEqualObjects([obj valueForPath:testutil::Field("foo.b")],
  261. [FSTStringValue stringValue:@"string"]);
  262. XCTAssertNil([obj valueForPath:testutil::Field("foo.a.b")]);
  263. XCTAssertNil([obj valueForPath:testutil::Field("bar")]);
  264. XCTAssertNil([obj valueForPath:testutil::Field("bar.a")]);
  265. }
  266. - (void)testOverwritesExistingFields {
  267. FSTObjectValue *old = FSTTestObjectValue(@{@"a" : @"old"});
  268. FSTObjectValue *mod = [old objectBySettingValue:FSTTestFieldValue(@"mod")
  269. forPath:testutil::Field("a")];
  270. // Should return a new object, leaving the old one unmodified.
  271. XCTAssertNotEqual(old, mod);
  272. XCTAssertEqualObjects(old, FSTTestFieldValue(@{@"a" : @"old"}));
  273. XCTAssertEqualObjects(mod, FSTTestFieldValue(@{@"a" : @"mod"}));
  274. }
  275. - (void)testAddsNewFields {
  276. FSTObjectValue *empty = [FSTObjectValue objectValue];
  277. FSTObjectValue *mod = [empty objectBySettingValue:FSTTestFieldValue(@"mod")
  278. forPath:testutil::Field("a")];
  279. XCTAssertNotEqual(empty, mod);
  280. XCTAssertEqualObjects(empty, FSTTestFieldValue(@{}));
  281. XCTAssertEqualObjects(mod, FSTTestFieldValue(@{@"a" : @"mod"}));
  282. FSTObjectValue *old = mod;
  283. mod = [old objectBySettingValue:FSTTestFieldValue(@1) forPath:testutil::Field("b")];
  284. XCTAssertNotEqual(old, mod);
  285. XCTAssertEqualObjects(old, FSTTestFieldValue(@{@"a" : @"mod"}));
  286. XCTAssertEqualObjects(mod, FSTTestFieldValue(@{@"a" : @"mod", @"b" : @1}));
  287. }
  288. - (void)testImplicitlyCreatesObjects {
  289. FSTObjectValue *old = FSTTestObjectValue(@{@"a" : @"old"});
  290. FSTObjectValue *mod = [old objectBySettingValue:FSTTestFieldValue(@"mod")
  291. forPath:testutil::Field("b.c.d")];
  292. XCTAssertNotEqual(old, mod);
  293. XCTAssertEqualObjects(old, FSTTestFieldValue(@{@"a" : @"old"}));
  294. XCTAssertEqualObjects(mod,
  295. FSTTestFieldValue(@{@"a" : @"old", @"b" : @{@"c" : @{@"d" : @"mod"}}}));
  296. }
  297. - (void)testCanOverwritePrimitivesWithObjects {
  298. FSTObjectValue *old = FSTTestObjectValue(@{@"a" : @{@"b" : @"old"}});
  299. FSTObjectValue *mod = [old objectBySettingValue:FSTTestFieldValue(@{@"b" : @"mod"})
  300. forPath:testutil::Field("a")];
  301. XCTAssertNotEqual(old, mod);
  302. XCTAssertEqualObjects(old, FSTTestFieldValue(@{@"a" : @{@"b" : @"old"}}));
  303. XCTAssertEqualObjects(mod, FSTTestFieldValue(@{@"a" : @{@"b" : @"mod"}}));
  304. }
  305. - (void)testAddsToNestedObjects {
  306. FSTObjectValue *old = FSTTestObjectValue(@{@"a" : @{@"b" : @"old"}});
  307. FSTObjectValue *mod = [old objectBySettingValue:FSTTestFieldValue(@"mod")
  308. forPath:testutil::Field("a.c")];
  309. XCTAssertNotEqual(old, mod);
  310. XCTAssertEqualObjects(old, FSTTestFieldValue(@{@"a" : @{@"b" : @"old"}}));
  311. XCTAssertEqualObjects(mod, FSTTestFieldValue(@{@"a" : @{@"b" : @"old", @"c" : @"mod"}}));
  312. }
  313. - (void)testDeletesKeys {
  314. FSTObjectValue *old = FSTTestObjectValue(@{@"a" : @1, @"b" : @2});
  315. FSTObjectValue *mod = [old objectByDeletingPath:testutil::Field("a")];
  316. XCTAssertNotEqual(old, mod);
  317. XCTAssertEqualObjects(old, FSTTestFieldValue(@{@"a" : @1, @"b" : @2}));
  318. XCTAssertEqualObjects(mod, FSTTestFieldValue(@{@"b" : @2}));
  319. FSTObjectValue *empty = [mod objectByDeletingPath:testutil::Field("b")];
  320. XCTAssertNotEqual(mod, empty);
  321. XCTAssertEqualObjects(mod, FSTTestFieldValue(@{@"b" : @2}));
  322. XCTAssertEqualObjects(empty, FSTTestFieldValue(@{}));
  323. }
  324. - (void)testDeletesHandleMissingKeys {
  325. FSTObjectValue *old = FSTTestObjectValue(@{@"a" : @{@"b" : @1, @"c" : @2}});
  326. FSTObjectValue *mod = [old objectByDeletingPath:testutil::Field("b")];
  327. XCTAssertEqualObjects(old, mod);
  328. XCTAssertEqualObjects(mod, FSTTestFieldValue(@{@"a" : @{@"b" : @1, @"c" : @2}}));
  329. mod = [old objectByDeletingPath:testutil::Field("a.d")];
  330. XCTAssertEqualObjects(old, mod);
  331. XCTAssertEqualObjects(mod, FSTTestFieldValue(@{@"a" : @{@"b" : @1, @"c" : @2}}));
  332. mod = [old objectByDeletingPath:testutil::Field("a.b.c")];
  333. XCTAssertEqualObjects(old, mod);
  334. XCTAssertEqualObjects(mod, FSTTestFieldValue(@{@"a" : @{@"b" : @1, @"c" : @2}}));
  335. }
  336. - (void)testDeletesNestedKeys {
  337. FSTObjectValue *old = FSTTestObjectValue(@{@"a" : @{@"b" : @1, @"c" : @{@"d" : @2, @"e" : @3}}});
  338. FSTObjectValue *mod = [old objectByDeletingPath:testutil::Field("a.c.d")];
  339. XCTAssertNotEqual(old, mod);
  340. XCTAssertEqualObjects(old,
  341. FSTTestFieldValue(@{@"a" : @{@"b" : @1, @"c" : @{@"d" : @2, @"e" : @3}}}));
  342. XCTAssertEqualObjects(mod, FSTTestFieldValue(@{@"a" : @{@"b" : @1, @"c" : @{@"e" : @3}}}));
  343. old = mod;
  344. mod = [old objectByDeletingPath:testutil::Field("a.c")];
  345. XCTAssertEqualObjects(old, FSTTestFieldValue(@{@"a" : @{@"b" : @1, @"c" : @{@"e" : @3}}}));
  346. XCTAssertEqualObjects(mod, FSTTestFieldValue(@{@"a" : @{@"b" : @1}}));
  347. old = mod;
  348. mod = [old objectByDeletingPath:testutil::Field("a")];
  349. XCTAssertEqualObjects(old, FSTTestFieldValue(@{@"a" : @{@"b" : @1}}));
  350. XCTAssertEqualObjects(mod, FSTTestFieldValue(@{}));
  351. }
  352. - (void)testArrays {
  353. FSTArrayValue *expected = [[FSTArrayValue alloc]
  354. initWithValueNoCopy:@[ [FSTStringValue stringValue:@"value"], [FSTBooleanValue trueValue] ]];
  355. FSTArrayValue *actual = (FSTArrayValue *)FSTTestFieldValue(@[ @"value", @YES ]);
  356. XCTAssertEqualObjects(actual, expected);
  357. }
  358. - (void)testValueEquality {
  359. DatabaseId database_id = DatabaseId("project", DatabaseId::kDefault);
  360. NSArray *groups = @[
  361. @[ FSTTestFieldValue(@YES), [FSTBooleanValue booleanValue:YES] ],
  362. @[ FSTTestFieldValue(@NO), [FSTBooleanValue booleanValue:NO] ],
  363. @[ FSTTestFieldValue([NSNull null]), [FSTNullValue nullValue] ],
  364. @[ FSTTestFieldValue(@(0.0 / 0.0)), FSTTestFieldValue(@(NAN)), [FSTDoubleValue nanValue] ],
  365. // -0.0 and 0.0 compare: the same (but are not isEqual:)
  366. @[ FSTTestFieldValue(@(-0.0)) ], @[ FSTTestFieldValue(@0.0) ],
  367. @[ FSTTestFieldValue(@1), FSTTestFieldValue(@1LL), [FSTIntegerValue integerValue:1LL] ],
  368. // double and unit64_t values can compare: the same (but won't be isEqual:)
  369. @[ FSTTestFieldValue(@1.0), [FSTDoubleValue doubleValue:1.0] ],
  370. @[ FSTTestFieldValue(@1.1), [FSTDoubleValue doubleValue:1.1] ],
  371. @[
  372. FSTTestFieldValue(FSTTestData(0, 1, 2, -1)), [FSTBlobValue blobValue:FSTTestData(0, 1, 2, -1)]
  373. ],
  374. @[ FSTTestFieldValue(FSTTestData(0, 1, -1)) ],
  375. @[ FSTTestFieldValue(@"string"), [FSTStringValue stringValue:@"string"] ],
  376. @[ FSTTestFieldValue(@"strin") ],
  377. @[ FSTTestFieldValue(@"e\u0301b") ], // latin small letter e + combining acute accent
  378. @[ FSTTestFieldValue(@"\u00e9a") ], // latin small letter e with acute accent
  379. @[
  380. FSTTestFieldValue(date1),
  381. [FSTTimestampValue timestampValue:[FIRTimestamp timestampWithDate:date1]]
  382. ],
  383. @[ FSTTestFieldValue(date2) ],
  384. @[
  385. // NOTE: ServerTimestampValues can't be parsed via FSTTestFieldValue().
  386. [FSTServerTimestampValue
  387. serverTimestampValueWithLocalWriteTime:[FIRTimestamp timestampWithDate:date1]
  388. previousValue:nil],
  389. [FSTServerTimestampValue
  390. serverTimestampValueWithLocalWriteTime:[FIRTimestamp timestampWithDate:date1]
  391. previousValue:nil]
  392. ],
  393. @[ [FSTServerTimestampValue
  394. serverTimestampValueWithLocalWriteTime:[FIRTimestamp timestampWithDate:date2]
  395. previousValue:nil] ],
  396. @[
  397. FSTTestFieldValue(FSTTestGeoPoint(0, 1)),
  398. [FSTGeoPointValue geoPointValue:FSTTestGeoPoint(0, 1)]
  399. ],
  400. @[ FSTTestFieldValue(FSTTestGeoPoint(1, 0)) ],
  401. @[
  402. [FSTReferenceValue referenceValue:FSTTestDocKey(@"coll/doc1") databaseID:&database_id],
  403. FSTTestFieldValue(FSTTestRef("project", DatabaseId::kDefault, @"coll/doc1"))
  404. ],
  405. @[ FSTTestRef("project", "(default)", @"coll/doc2") ],
  406. @[ FSTTestFieldValue(@[ @"foo", @"bar" ]), FSTTestFieldValue(@[ @"foo", @"bar" ]) ],
  407. @[ FSTTestFieldValue(@[ @"foo", @"bar", @"baz" ]) ], @[ FSTTestFieldValue(@[ @"foo" ]) ],
  408. @[
  409. FSTTestFieldValue(@{@"bar" : @1, @"foo" : @2}), FSTTestFieldValue(@{@"foo" : @2, @"bar" : @1})
  410. ],
  411. @[ FSTTestFieldValue(@{@"bar" : @2, @"foo" : @1}) ],
  412. @[ FSTTestFieldValue(@{@"bar" : @1, @"foo" : @1}) ], @[ FSTTestFieldValue(@{@"foo" : @1}) ]
  413. ];
  414. FSTAssertEqualityGroups(groups);
  415. }
  416. - (void)testValueOrdering {
  417. NSArray *groups = @[
  418. // null first
  419. @[ [NSNull null] ],
  420. // booleans
  421. @[ @NO ], @[ @YES ],
  422. // numbers
  423. @[ @(0.0 / 0.0) ], @[ @(-INFINITY) ], @[ @(-DBL_MAX) ], @[ @(LLONG_MIN) ], @[ @(-1.1) ],
  424. @[ @(-1.0), @(-1LL) ], // longs and doubles compare the same
  425. @[ @(-DBL_MIN) ],
  426. @[ @(-0x1.0p-1074) ], // negative smallest subnormal
  427. @[ @(-0.0), @(0.0), @(0LL) ], // zeros all compare the same
  428. @[ @(0x1.0p-1074) ], // positive smallest subnormal
  429. @[ @(DBL_MIN) ], @[ @1.0, @1LL ], // longs and doubles compare the same
  430. @[ @1.1 ], @[ @(LLONG_MAX) ], @[ @(DBL_MAX) ], @[ @(INFINITY) ],
  431. // timestamps
  432. @[ date1 ], @[ date2 ],
  433. // server timestamps come after all concrete timestamps.
  434. // NOTE: server timestamps can't be parsed directly, so we have special sentinel strings (see
  435. // FSTWrapGroups()).
  436. @[ @"server-timestamp-1" ], @[ @"server-timestamp-2" ],
  437. // strings
  438. @[ @"" ], @[ @"\000\ud7ff\ue000\uffff" ], @[ @"(╯°□°)╯︵ ┻━┻" ], @[ @"a" ], @[ @"abc def" ],
  439. @[ @"e\u0301b" ], // latin small letter e + combining acute accent + latin small letter b
  440. @[ @"æ" ],
  441. @[ @"\u00e9a" ], // latin small letter e with acute accent + latin small letter a
  442. // blobs
  443. @[ FSTTestData(-1) ], @[ FSTTestData(0, -1) ], @[ FSTTestData(0, 1, 2, 3, 4, -1) ],
  444. @[ FSTTestData(0, 1, 2, 4, 3, -1) ], @[ FSTTestData(255, -1) ],
  445. // resource names
  446. @[ FSTTestRef("p1", "d1", @"c1/doc1") ], @[ FSTTestRef("p1", "d1", @"c1/doc2") ],
  447. @[ FSTTestRef("p1", "d1", @"c10/doc1") ], @[ FSTTestRef("p1", "d1", @"c2/doc1") ],
  448. @[ FSTTestRef("p1", "d2", @"c1/doc1") ], @[ FSTTestRef("p2", "d1", @"c1/doc1") ],
  449. // Geo points
  450. @[ FSTTestGeoPoint(-90, -180) ], @[ FSTTestGeoPoint(-90, 0) ], @[ FSTTestGeoPoint(-90, 180) ],
  451. @[ FSTTestGeoPoint(0, -180) ], @[ FSTTestGeoPoint(0, 0) ], @[ FSTTestGeoPoint(0, 180) ],
  452. @[ FSTTestGeoPoint(1, -180) ], @[ FSTTestGeoPoint(1, 0) ], @[ FSTTestGeoPoint(1, 180) ],
  453. @[ FSTTestGeoPoint(90, -180) ], @[ FSTTestGeoPoint(90, 0) ], @[ FSTTestGeoPoint(90, 180) ],
  454. // Arrays
  455. @[ @[] ], @[ @[ @"bar" ] ], @[ @[ @"foo" ] ], @[ @[ @"foo", @1 ] ], @[ @[ @"foo", @2 ] ],
  456. @[ @[ @"foo", @"0" ] ],
  457. // Objects
  458. @[ @{@"bar" : @0} ], @[ @{@"bar" : @0, @"foo" : @1} ], @[ @{@"foo" : @1} ], @[ @{@"foo" : @2} ],
  459. @[ @{@"foo" : @"0"} ]
  460. ];
  461. NSArray *wrapped = FSTWrapGroups(groups);
  462. FSTAssertComparisons(wrapped);
  463. }
  464. - (void)testValue {
  465. NSDate *date = [NSDate date];
  466. id input = @{@"array" : @[ @1, date ], @"obj" : @{@"date" : date, @"string" : @"hi"}};
  467. FSTObjectValue *value = FSTTestObjectValue(input);
  468. id output = [value value];
  469. {
  470. XCTAssertTrue([output[@"array"][1] isKindOfClass:[FIRTimestamp class]]);
  471. FIRTimestamp *actual = output[@"array"][1];
  472. XCTAssertEqualObjects([FIRTimestamp timestampWithDate:date], actual);
  473. }
  474. {
  475. XCTAssertTrue([output[@"obj"][@"date"] isKindOfClass:[FIRTimestamp class]]);
  476. FIRTimestamp *actual = output[@"array"][1];
  477. XCTAssertEqualObjects([FIRTimestamp timestampWithDate:date], actual);
  478. }
  479. }
  480. @end