FSTFieldValueTests.mm 23 KB

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