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