binary_storage_test.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /**
  2. * @fileoverview Tests for storage.js.
  3. */
  4. goog.module('protobuf.runtime.BinaryStorageTest');
  5. goog.setTestOnly();
  6. const BinaryStorage = goog.require('protobuf.runtime.BinaryStorage');
  7. const {Field} = goog.require('protobuf.binary.field');
  8. /**
  9. * @type {number}
  10. */
  11. const DEFAULT_PIVOT = 24;
  12. const /** !Field */ field1 =
  13. Field.fromDecodedValue(/* decodedValue= */ 1, /* encoder= */ () => {});
  14. const /** !Field */ field2 =
  15. Field.fromDecodedValue(/* decodedValue= */ 2, /* encoder= */ () => {});
  16. const /** !Field */ field3 =
  17. Field.fromDecodedValue(/* decodedValue= */ 3, /* encoder= */ () => {});
  18. const /** !Field */ field4 =
  19. Field.fromDecodedValue(/* decodedValue= */ 4, /* encoder= */ () => {});
  20. /**
  21. * Returns the number of fields stored.
  22. *
  23. * @param {!BinaryStorage} storage
  24. * @return {number}
  25. */
  26. function getStorageSize(storage) {
  27. let size = 0;
  28. storage.forEach(() => void size++);
  29. return size;
  30. }
  31. describe('BinaryStorage', () => {
  32. it('sets and gets a field not greater than the pivot', () => {
  33. const storage = new BinaryStorage(DEFAULT_PIVOT);
  34. storage.set(1, field1);
  35. storage.set(DEFAULT_PIVOT, field2);
  36. expect(storage.getPivot()).toBe(DEFAULT_PIVOT);
  37. expect(storage.get(1)).toBe(field1);
  38. expect(storage.get(DEFAULT_PIVOT)).toBe(field2);
  39. });
  40. it('sets and gets a field greater than the pivot', () => {
  41. const storage = new BinaryStorage(DEFAULT_PIVOT);
  42. storage.set(DEFAULT_PIVOT + 1, field1);
  43. storage.set(100000, field2);
  44. expect(storage.get(DEFAULT_PIVOT + 1)).toBe(field1);
  45. expect(storage.get(100000)).toBe(field2);
  46. });
  47. it('sets and gets a field when pivot is zero', () => {
  48. const storage = new BinaryStorage(0);
  49. storage.set(0, field1);
  50. storage.set(100000, field2);
  51. expect(storage.getPivot()).toBe(0);
  52. expect(storage.get(0)).toBe(field1);
  53. expect(storage.get(100000)).toBe(field2);
  54. });
  55. it('sets and gets a field when pivot is undefined', () => {
  56. const storage = new BinaryStorage();
  57. storage.set(0, field1);
  58. storage.set(DEFAULT_PIVOT, field2);
  59. storage.set(DEFAULT_PIVOT + 1, field3);
  60. expect(storage.getPivot()).toBe(DEFAULT_PIVOT);
  61. expect(storage.get(0)).toBe(field1);
  62. expect(storage.get(DEFAULT_PIVOT)).toBe(field2);
  63. expect(storage.get(DEFAULT_PIVOT + 1)).toBe(field3);
  64. });
  65. it('returns undefined for nonexistent fields', () => {
  66. const storage = new BinaryStorage(DEFAULT_PIVOT);
  67. expect(storage.get(1)).toBeUndefined();
  68. expect(storage.get(DEFAULT_PIVOT)).toBeUndefined();
  69. expect(storage.get(DEFAULT_PIVOT + 1)).toBeUndefined();
  70. expect(storage.get(100000)).toBeUndefined();
  71. });
  72. it('returns undefined for nonexistent fields after map initialization',
  73. () => {
  74. const storage = new BinaryStorage(DEFAULT_PIVOT);
  75. storage.set(100001, field1);
  76. expect(storage.get(1)).toBeUndefined();
  77. expect(storage.get(DEFAULT_PIVOT)).toBeUndefined();
  78. expect(storage.get(DEFAULT_PIVOT + 1)).toBeUndefined();
  79. expect(storage.get(100000)).toBeUndefined();
  80. });
  81. it('deletes a field in delete() when values are only in array', () => {
  82. const storage = new BinaryStorage(DEFAULT_PIVOT);
  83. storage.set(1, field1);
  84. storage.delete(1);
  85. expect(storage.get(1)).toBeUndefined();
  86. });
  87. it('deletes a field in delete() when values are both in array and map',
  88. () => {
  89. const storage = new BinaryStorage(DEFAULT_PIVOT);
  90. storage.set(DEFAULT_PIVOT, field2);
  91. storage.set(DEFAULT_PIVOT + 1, field3);
  92. storage.delete(DEFAULT_PIVOT);
  93. storage.delete(DEFAULT_PIVOT + 1);
  94. expect(storage.get(DEFAULT_PIVOT)).toBeUndefined();
  95. expect(storage.get(DEFAULT_PIVOT + 1)).toBeUndefined();
  96. });
  97. it('deletes a field in delete() when values are only in map', () => {
  98. const storage = new BinaryStorage(DEFAULT_PIVOT);
  99. storage.set(100000, field4);
  100. storage.delete(100000);
  101. expect(storage.get(100000)).toBeUndefined();
  102. });
  103. it('loops over all the elements in forEach()', () => {
  104. const storage = new BinaryStorage(DEFAULT_PIVOT);
  105. storage.set(1, field1);
  106. storage.set(DEFAULT_PIVOT, field2);
  107. storage.set(DEFAULT_PIVOT + 1, field3);
  108. storage.set(100000, field4);
  109. const fields = new Map();
  110. storage.forEach(
  111. (field, fieldNumber) => void fields.set(fieldNumber, field));
  112. expect(fields.size).toEqual(4);
  113. expect(fields.get(1)).toBe(field1);
  114. expect(storage.get(DEFAULT_PIVOT)).toBe(field2);
  115. expect(storage.get(DEFAULT_PIVOT + 1)).toBe(field3);
  116. expect(fields.get(100000)).toBe(field4);
  117. });
  118. it('creates a shallow copy of the storage in shallowCopy()', () => {
  119. const storage = new BinaryStorage(DEFAULT_PIVOT);
  120. storage.set(1, field1);
  121. storage.set(100000, field2);
  122. const copy = storage.shallowCopy();
  123. expect(getStorageSize(copy)).toEqual(2);
  124. expect(copy.get(1)).not.toBe(field1);
  125. expect(copy.get(1).getDecodedValue()).toEqual(1);
  126. expect(copy.get(100000)).not.toBe(field1);
  127. expect(copy.get(100000).getDecodedValue()).toEqual(2);
  128. });
  129. });