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