FSTFieldValueTests.mm 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583
  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 =
  51. [FSTReferenceValue referenceValue:reference.key 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 v1beta1 query semantics, it's no longer as important that our NaN representation
  121. // matches the backend, since all NaNs are defined to sort as equal, but we preserve the
  122. // normalization and 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 = FSTTestObjectValue(
  235. @{ @"a" : @"foo",
  236. @"b" : @(1L),
  237. @"c" : @YES,
  238. @"d" : [NSNull null] });
  239. FSTObjectValue *expected = [[FSTObjectValue alloc] initWithDictionary:@{
  240. @"a" : [FSTStringValue stringValue:@"foo"],
  241. @"b" : [FSTIntegerValue integerValue:1LL],
  242. @"c" : [FSTBooleanValue trueValue],
  243. @"d" : [FSTNullValue nullValue]
  244. }];
  245. XCTAssertEqualObjects(actual, expected);
  246. }
  247. - (void)testWrapsNestedObjects {
  248. FSTObjectValue *actual = FSTTestObjectValue(@{ @"a" : @{@"b" : @{@"c" : @"foo"}, @"d" : @YES} });
  249. FSTObjectValue *expected = [[FSTObjectValue alloc] initWithDictionary:@{
  250. @"a" : [[FSTObjectValue alloc] initWithDictionary:@{
  251. @"b" :
  252. [[FSTObjectValue alloc] initWithDictionary:@{@"c" : [FSTStringValue stringValue:@"foo"]}],
  253. @"d" : [FSTBooleanValue booleanValue:YES]
  254. }]
  255. }];
  256. XCTAssertEqualObjects(actual, expected);
  257. }
  258. - (void)testExtractsFields {
  259. FSTObjectValue *obj = FSTTestObjectValue(@{ @"foo" : @{@"a" : @YES, @"b" : @"string"} });
  260. FSTAssertIsKindOfClass(obj, FSTObjectValue);
  261. FSTAssertIsKindOfClass([obj valueForPath:testutil::Field("foo")], FSTObjectValue);
  262. XCTAssertEqualObjects([obj valueForPath:testutil::Field("foo.a")], [FSTBooleanValue trueValue]);
  263. XCTAssertEqualObjects([obj valueForPath:testutil::Field("foo.b")],
  264. [FSTStringValue stringValue:@"string"]);
  265. XCTAssertNil([obj valueForPath:testutil::Field("foo.a.b")]);
  266. XCTAssertNil([obj valueForPath:testutil::Field("bar")]);
  267. XCTAssertNil([obj valueForPath:testutil::Field("bar.a")]);
  268. }
  269. - (void)testOverwritesExistingFields {
  270. FSTObjectValue *old = FSTTestObjectValue(@{@"a" : @"old"});
  271. FSTObjectValue *mod =
  272. [old objectBySettingValue:FSTTestFieldValue(@"mod") forPath:testutil::Field("a")];
  273. // Should return a new object, leaving the old one unmodified.
  274. XCTAssertNotEqual(old, mod);
  275. XCTAssertEqualObjects(old, FSTTestFieldValue(@{@"a" : @"old"}));
  276. XCTAssertEqualObjects(mod, FSTTestFieldValue(@{@"a" : @"mod"}));
  277. }
  278. - (void)testAddsNewFields {
  279. FSTObjectValue *empty = [FSTObjectValue objectValue];
  280. FSTObjectValue *mod =
  281. [empty objectBySettingValue:FSTTestFieldValue(@"mod") forPath:testutil::Field("a")];
  282. XCTAssertNotEqual(empty, mod);
  283. XCTAssertEqualObjects(empty, FSTTestFieldValue(@{}));
  284. XCTAssertEqualObjects(mod, FSTTestFieldValue(@{@"a" : @"mod"}));
  285. FSTObjectValue *old = mod;
  286. mod = [old objectBySettingValue:FSTTestFieldValue(@1) forPath:testutil::Field("b")];
  287. XCTAssertNotEqual(old, mod);
  288. XCTAssertEqualObjects(old, FSTTestFieldValue(@{@"a" : @"mod"}));
  289. XCTAssertEqualObjects(mod, FSTTestFieldValue(@{ @"a" : @"mod", @"b" : @1 }));
  290. }
  291. - (void)testImplicitlyCreatesObjects {
  292. FSTObjectValue *old = FSTTestObjectValue(@{@"a" : @"old"});
  293. FSTObjectValue *mod =
  294. [old objectBySettingValue:FSTTestFieldValue(@"mod") forPath:testutil::Field("b.c.d")];
  295. XCTAssertNotEqual(old, mod);
  296. XCTAssertEqualObjects(old, FSTTestFieldValue(@{@"a" : @"old"}));
  297. XCTAssertEqualObjects(mod, FSTTestFieldValue(
  298. @{ @"a" : @"old",
  299. @"b" : @{@"c" : @{@"d" : @"mod"}} }));
  300. }
  301. - (void)testCanOverwritePrimitivesWithObjects {
  302. FSTObjectValue *old = FSTTestObjectValue(@{ @"a" : @{@"b" : @"old"} });
  303. FSTObjectValue *mod =
  304. [old objectBySettingValue:FSTTestFieldValue(@{@"b" : @"mod"}) forPath:testutil::Field("a")];
  305. XCTAssertNotEqual(old, mod);
  306. XCTAssertEqualObjects(old, FSTTestFieldValue(@{ @"a" : @{@"b" : @"old"} }));
  307. XCTAssertEqualObjects(mod, FSTTestFieldValue(@{ @"a" : @{@"b" : @"mod"} }));
  308. }
  309. - (void)testAddsToNestedObjects {
  310. FSTObjectValue *old = FSTTestObjectValue(@{ @"a" : @{@"b" : @"old"} });
  311. FSTObjectValue *mod =
  312. [old objectBySettingValue:FSTTestFieldValue(@"mod") forPath:testutil::Field("a.c")];
  313. XCTAssertNotEqual(old, mod);
  314. XCTAssertEqualObjects(old, FSTTestFieldValue(@{ @"a" : @{@"b" : @"old"} }));
  315. XCTAssertEqualObjects(mod, FSTTestFieldValue(@{ @"a" : @{@"b" : @"old", @"c" : @"mod"} }));
  316. }
  317. - (void)testDeletesKeys {
  318. FSTObjectValue *old = FSTTestObjectValue(@{ @"a" : @1, @"b" : @2 });
  319. FSTObjectValue *mod = [old objectByDeletingPath:testutil::Field("a")];
  320. XCTAssertNotEqual(old, mod);
  321. XCTAssertEqualObjects(old, FSTTestFieldValue(@{ @"a" : @1, @"b" : @2 }));
  322. XCTAssertEqualObjects(mod, FSTTestFieldValue(@{ @"b" : @2 }));
  323. FSTObjectValue *empty = [mod objectByDeletingPath:testutil::Field("b")];
  324. XCTAssertNotEqual(mod, empty);
  325. XCTAssertEqualObjects(mod, FSTTestFieldValue(@{ @"b" : @2 }));
  326. XCTAssertEqualObjects(empty, FSTTestFieldValue(@{}));
  327. }
  328. - (void)testDeletesHandleMissingKeys {
  329. FSTObjectValue *old = FSTTestObjectValue(@{ @"a" : @{@"b" : @1, @"c" : @2} });
  330. FSTObjectValue *mod = [old objectByDeletingPath:testutil::Field("b")];
  331. XCTAssertEqualObjects(old, mod);
  332. XCTAssertEqualObjects(mod, FSTTestFieldValue(@{ @"a" : @{@"b" : @1, @"c" : @2} }));
  333. mod = [old objectByDeletingPath:testutil::Field("a.d")];
  334. XCTAssertEqualObjects(old, mod);
  335. XCTAssertEqualObjects(mod, FSTTestFieldValue(@{ @"a" : @{@"b" : @1, @"c" : @2} }));
  336. mod = [old objectByDeletingPath:testutil::Field("a.b.c")];
  337. XCTAssertEqualObjects(old, mod);
  338. XCTAssertEqualObjects(mod, FSTTestFieldValue(@{ @"a" : @{@"b" : @1, @"c" : @2} }));
  339. }
  340. - (void)testDeletesNestedKeys {
  341. FSTObjectValue *old = FSTTestObjectValue(
  342. @{ @"a" : @{@"b" : @1, @"c" : @{@"d" : @2, @"e" : @3}} });
  343. FSTObjectValue *mod = [old objectByDeletingPath:testutil::Field("a.c.d")];
  344. XCTAssertNotEqual(old, mod);
  345. XCTAssertEqualObjects(old, FSTTestFieldValue(
  346. @{ @"a" : @{@"b" : @1, @"c" : @{@"d" : @2, @"e" : @3}} }));
  347. XCTAssertEqualObjects(mod, FSTTestFieldValue(@{ @"a" : @{@"b" : @1, @"c" : @{@"e" : @3}} }));
  348. old = mod;
  349. mod = [old objectByDeletingPath:testutil::Field("a.c")];
  350. XCTAssertEqualObjects(old, FSTTestFieldValue(@{ @"a" : @{@"b" : @1, @"c" : @{@"e" : @3}} }));
  351. XCTAssertEqualObjects(mod, FSTTestFieldValue(@{ @"a" : @{@"b" : @1} }));
  352. old = mod;
  353. mod = [old objectByDeletingPath:testutil::Field("a")];
  354. XCTAssertEqualObjects(old, FSTTestFieldValue(@{ @"a" : @{@"b" : @1} }));
  355. XCTAssertEqualObjects(mod, FSTTestFieldValue(@{}));
  356. }
  357. - (void)testArrays {
  358. FSTArrayValue *expected = [[FSTArrayValue alloc]
  359. initWithValueNoCopy:@[ [FSTStringValue stringValue:@"value"], [FSTBooleanValue trueValue] ]];
  360. FSTArrayValue *actual = (FSTArrayValue *)FSTTestFieldValue(@[ @"value", @YES ]);
  361. XCTAssertEqualObjects(actual, expected);
  362. }
  363. - (void)testValueEquality {
  364. DatabaseId database_id = DatabaseId("project", DatabaseId::kDefault);
  365. NSArray *groups = @[
  366. @[ FSTTestFieldValue(@YES), [FSTBooleanValue booleanValue:YES] ],
  367. @[ FSTTestFieldValue(@NO), [FSTBooleanValue booleanValue:NO] ],
  368. @[ FSTTestFieldValue([NSNull null]), [FSTNullValue nullValue] ],
  369. @[ FSTTestFieldValue(@(0.0 / 0.0)), FSTTestFieldValue(@(NAN)), [FSTDoubleValue nanValue] ],
  370. // -0.0 and 0.0 compare: the same (but are not isEqual:)
  371. @[ FSTTestFieldValue(@(-0.0)) ], @[ FSTTestFieldValue(@0.0) ],
  372. @[ FSTTestFieldValue(@1), FSTTestFieldValue(@1LL), [FSTIntegerValue integerValue:1LL] ],
  373. // double and unit64_t values can compare: the same (but won't be isEqual:)
  374. @[ FSTTestFieldValue(@1.0), [FSTDoubleValue doubleValue:1.0] ],
  375. @[ FSTTestFieldValue(@1.1), [FSTDoubleValue doubleValue:1.1] ],
  376. @[
  377. FSTTestFieldValue(FSTTestData(0, 1, 2, -1)), [FSTBlobValue blobValue:FSTTestData(0, 1, 2, -1)]
  378. ],
  379. @[ FSTTestFieldValue(FSTTestData(0, 1, -1)) ],
  380. @[ FSTTestFieldValue(@"string"), [FSTStringValue stringValue:@"string"] ],
  381. @[ FSTTestFieldValue(@"strin") ],
  382. @[ FSTTestFieldValue(@"e\u0301b") ], // latin small letter e + combining acute accent
  383. @[ FSTTestFieldValue(@"\u00e9a") ], // latin small letter e with acute accent
  384. @[
  385. FSTTestFieldValue(date1),
  386. [FSTTimestampValue timestampValue:[FIRTimestamp timestampWithDate:date1]]
  387. ],
  388. @[ FSTTestFieldValue(date2) ],
  389. @[
  390. // NOTE: ServerTimestampValues can't be parsed via FSTTestFieldValue().
  391. [FSTServerTimestampValue
  392. serverTimestampValueWithLocalWriteTime:[FIRTimestamp timestampWithDate:date1]
  393. previousValue:nil],
  394. [FSTServerTimestampValue
  395. serverTimestampValueWithLocalWriteTime:[FIRTimestamp timestampWithDate:date1]
  396. previousValue:nil]
  397. ],
  398. @[ [FSTServerTimestampValue
  399. serverTimestampValueWithLocalWriteTime:[FIRTimestamp timestampWithDate:date2]
  400. previousValue:nil] ],
  401. @[
  402. FSTTestFieldValue(FSTTestGeoPoint(0, 1)),
  403. [FSTGeoPointValue geoPointValue:FSTTestGeoPoint(0, 1)]
  404. ],
  405. @[ FSTTestFieldValue(FSTTestGeoPoint(1, 0)) ],
  406. @[
  407. [FSTReferenceValue referenceValue:FSTTestDocKey(@"coll/doc1") databaseID:&database_id],
  408. FSTTestFieldValue(FSTTestRef("project", DatabaseId::kDefault, @"coll/doc1"))
  409. ],
  410. @[ FSTTestRef("project", "(default)", @"coll/doc2") ],
  411. @[ FSTTestFieldValue(@[ @"foo", @"bar" ]), FSTTestFieldValue(@[ @"foo", @"bar" ]) ],
  412. @[ FSTTestFieldValue(@[ @"foo", @"bar", @"baz" ]) ], @[ FSTTestFieldValue(@[ @"foo" ]) ],
  413. @[
  414. FSTTestFieldValue(
  415. @{ @"bar" : @1,
  416. @"foo" : @2 }),
  417. FSTTestFieldValue(
  418. @{ @"foo" : @2,
  419. @"bar" : @1 })
  420. ],
  421. @[ FSTTestFieldValue(
  422. @{ @"bar" : @2,
  423. @"foo" : @1 }) ],
  424. @[ FSTTestFieldValue(
  425. @{ @"bar" : @1,
  426. @"foo" : @1 }) ],
  427. @[ FSTTestFieldValue(
  428. @{ @"foo" : @1 }) ]
  429. ];
  430. FSTAssertEqualityGroups(groups);
  431. }
  432. - (void)testValueOrdering {
  433. NSArray *groups = @[
  434. // null first
  435. @[ [NSNull null] ],
  436. // booleans
  437. @[ @NO ], @[ @YES ],
  438. // numbers
  439. @[ @(0.0 / 0.0) ], @[ @(-INFINITY) ], @[ @(-DBL_MAX) ], @[ @(LLONG_MIN) ], @[ @(-1.1) ],
  440. @[ @(-1.0), @(-1LL) ], // longs and doubles compare the same
  441. @[ @(-DBL_MIN) ],
  442. @[ @(-0x1.0p-1074) ], // negative smallest subnormal
  443. @[ @(-0.0), @(0.0), @(0LL) ], // zeros all compare the same
  444. @[ @(0x1.0p-1074) ], // positive smallest subnormal
  445. @[ @(DBL_MIN) ], @[ @1.0, @1LL ], // longs and doubles compare the same
  446. @[ @1.1 ], @[ @(LLONG_MAX) ], @[ @(DBL_MAX) ], @[ @(INFINITY) ],
  447. // timestamps
  448. @[ date1 ], @[ date2 ],
  449. // server timestamps come after all concrete timestamps.
  450. // NOTE: server timestamps can't be parsed directly, so we have special sentinel strings (see
  451. // FSTWrapGroups()).
  452. @[ @"server-timestamp-1" ], @[ @"server-timestamp-2" ],
  453. // strings
  454. @[ @"" ], @[ @"\000\ud7ff\ue000\uffff" ], @[ @"(╯°□°)╯︵ ┻━┻" ], @[ @"a" ], @[ @"abc def" ],
  455. @[ @"e\u0301b" ], // latin small letter e + combining acute accent + latin small letter b
  456. @[ @"æ" ],
  457. @[ @"\u00e9a" ], // latin small letter e with acute accent + latin small letter a
  458. // blobs
  459. @[ FSTTestData(-1) ], @[ FSTTestData(0, -1) ], @[ FSTTestData(0, 1, 2, 3, 4, -1) ],
  460. @[ FSTTestData(0, 1, 2, 4, 3, -1) ], @[ FSTTestData(255, -1) ],
  461. // resource names
  462. @[ FSTTestRef("p1", "d1", @"c1/doc1") ], @[ FSTTestRef("p1", "d1", @"c1/doc2") ],
  463. @[ FSTTestRef("p1", "d1", @"c10/doc1") ], @[ FSTTestRef("p1", "d1", @"c2/doc1") ],
  464. @[ FSTTestRef("p1", "d2", @"c1/doc1") ], @[ FSTTestRef("p2", "d1", @"c1/doc1") ],
  465. // Geo points
  466. @[ FSTTestGeoPoint(-90, -180) ], @[ FSTTestGeoPoint(-90, 0) ], @[ FSTTestGeoPoint(-90, 180) ],
  467. @[ FSTTestGeoPoint(0, -180) ], @[ FSTTestGeoPoint(0, 0) ], @[ FSTTestGeoPoint(0, 180) ],
  468. @[ FSTTestGeoPoint(1, -180) ], @[ FSTTestGeoPoint(1, 0) ], @[ FSTTestGeoPoint(1, 180) ],
  469. @[ FSTTestGeoPoint(90, -180) ], @[ FSTTestGeoPoint(90, 0) ], @[ FSTTestGeoPoint(90, 180) ],
  470. // Arrays
  471. @[ @[] ], @[ @[ @"bar" ] ], @[ @[ @"foo" ] ], @[ @[ @"foo", @1 ] ], @[ @[ @"foo", @2 ] ],
  472. @[ @[ @"foo", @"0" ] ],
  473. // Objects
  474. @[
  475. @{ @"bar" : @0 }
  476. ],
  477. @[
  478. @{ @"bar" : @0,
  479. @"foo" : @1 }
  480. ],
  481. @[
  482. @{ @"foo" : @1 }
  483. ],
  484. @[
  485. @{ @"foo" : @2 }
  486. ],
  487. @[ @{@"foo" : @"0"} ]
  488. ];
  489. NSArray *wrapped = FSTWrapGroups(groups);
  490. FSTAssertComparisons(wrapped);
  491. }
  492. - (void)testValue {
  493. NSDate *date = [NSDate date];
  494. id input = @{ @"array" : @[ @1, date ], @"obj" : @{@"date" : date, @"string" : @"hi"} };
  495. FSTObjectValue *value = FSTTestObjectValue(input);
  496. id output = [value value];
  497. {
  498. XCTAssertTrue([output[@"array"][1] isKindOfClass:[FIRTimestamp class]]);
  499. FIRTimestamp *actual = output[@"array"][1];
  500. XCTAssertEqualObjects([FIRTimestamp timestampWithDate:date], actual);
  501. }
  502. {
  503. XCTAssertTrue([output[@"obj"][@"date"] isKindOfClass:[FIRTimestamp class]]);
  504. FIRTimestamp *actual = output[@"array"][1];
  505. XCTAssertEqualObjects([FIRTimestamp timestampWithDate:date], actual);
  506. }
  507. }
  508. @end