FSTFieldValueTests.mm 23 KB

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