FNextPushIdTest.m 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. /*
  2. * Copyright 2021 Google LLC
  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 <XCTest/XCTest.h>
  17. #import "FirebaseDatabase/Sources/Utilities/FNextPushId.h"
  18. #import "FirebaseDatabase/Sources/Utilities/FUtilities.h"
  19. @interface FNextPushIdTest : XCTestCase
  20. @end
  21. @implementation FNextPushIdTest
  22. static NSString *MIN_PUSH_CHAR = @"-";
  23. static NSString *MAX_PUSH_CHAR = @"z";
  24. static NSInteger MAX_KEY_LEN = 786;
  25. - (void)testSuccessorSpecialValues {
  26. NSString *maxIntegerKeySuccessor =
  27. [FNextPushId successor:[NSString stringWithFormat:@"%d", INTEGER_32_MAX]];
  28. XCTAssertEqualObjects(maxIntegerKeySuccessor, MIN_PUSH_CHAR,
  29. @"successor(INTEGER_32_MAX) == MIN_PUSH_CHAR");
  30. NSString *maxKey = [@"" stringByPaddingToLength:MAX_KEY_LEN
  31. withString:MAX_PUSH_CHAR
  32. startingAtIndex:0];
  33. NSString *maxKeySuccessor = [FNextPushId successor:maxKey];
  34. XCTAssertEqualObjects(maxKeySuccessor, [FUtilities maxName],
  35. @"successor(MAX_PUSH_CHAR repeated MAX_KEY_LEN times) == MAX_NAME");
  36. }
  37. - (void)testSuccessorBasic {
  38. NSString *actual = [FNextPushId successor:@"abc"];
  39. NSString *expected = [NSString stringWithFormat:@"abc%@", MIN_PUSH_CHAR];
  40. XCTAssertEqualObjects(expected, actual, @"successor(abc) == abc + MIN_PUSH_CHAR");
  41. actual = [FNextPushId successor:[@"abc" stringByPaddingToLength:MAX_KEY_LEN
  42. withString:MAX_PUSH_CHAR
  43. startingAtIndex:0]];
  44. expected = @"abd";
  45. XCTAssertEqualObjects(expected, actual,
  46. @"successor(abc + MAX_PUSH_CHAR repeated MAX_KEY_LEN - 3 times) == abd");
  47. actual = [FNextPushId successor:[NSString stringWithFormat:@"abc%@", MIN_PUSH_CHAR]];
  48. expected = [NSString stringWithFormat:@"abc%@%@", MIN_PUSH_CHAR, MIN_PUSH_CHAR];
  49. XCTAssertEqualObjects(expected, actual,
  50. @"successor(abc + MIN_PUSH_CHAR) == abc + MIN_PUSH_CHAR + MIN_PUSH_CHAR");
  51. }
  52. - (void)testPredecessorSpecialValues {
  53. NSString *actual = [FNextPushId predecessor:MIN_PUSH_CHAR];
  54. NSString *expected = [NSString stringWithFormat:@"%d", INTEGER_32_MAX];
  55. XCTAssertEqualObjects(expected, actual, @"predecessor(MIN_PUSH_CHAR) == INTEGER_32_MAX");
  56. actual = [FNextPushId predecessor:[NSString stringWithFormat:@"%ld", INTEGER_32_MIN]];
  57. expected = [FUtilities minName];
  58. XCTAssertEqualObjects(expected, actual, @"predecessor(INTEGER_32_MIN) == MIN_NAME");
  59. }
  60. - (void)testPredecessorBasic {
  61. NSString *actual = [FNextPushId predecessor:@"abc"];
  62. NSString *expected = [@"abb" stringByPaddingToLength:MAX_KEY_LEN
  63. withString:MAX_PUSH_CHAR
  64. startingAtIndex:0];
  65. XCTAssertEqualObjects(
  66. expected, actual,
  67. @"predecessor(abc) = abb + { MAX_PUSH_CHAR repeated MAX_KEY_LEN - 3 times }");
  68. actual = [FNextPushId predecessor:[NSString stringWithFormat:@"abc%@", MIN_PUSH_CHAR]];
  69. expected = @"abc";
  70. XCTAssertEqualObjects(expected, actual, @"predecessor(abc + MIN_PUSH_CHAR) == abc");
  71. }
  72. @end