FSTFieldValueTests.m 23 KB

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