forked from hazelcast/hazelcast-nodejs-client
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSchemaTest.js
More file actions
81 lines (69 loc) · 3.33 KB
/
SchemaTest.js
File metadata and controls
81 lines (69 loc) · 3.33 KB
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
/*
* Copyright (c) 2008-2022, Hazelcast, Inc. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
'use strict';
const chai = require('chai');
const { FieldKind } = require('../../../../lib');
const { FieldDescriptor } = require('../../../../lib/serialization/generic_record/FieldDescriptor');
const { Schema } = require('../../../../lib/serialization/compact/Schema');
const { BitsUtil } = require('../../../../lib/util/BitsUtil');
chai.should();
describe('SchemaTest', function () {
it('should work with multiple booleans', function () {
const boolCount = 100;
const boolFields = new Array(100);
for (let i = 0; i < boolCount; i++) {
// the fields are sorted by name, so have to append 0 for numbers < 10
const name = i >= 10? i.toString() : '0' + i.toString();
boolFields[i] = new FieldDescriptor(name, FieldKind.BOOLEAN);
}
const fields = [
...boolFields, new FieldDescriptor('fixSized', FieldKind.INT32), new FieldDescriptor('varSized', FieldKind.STRING)
];
const schema = new Schema('something', fields);
schema.numberVarSizeFields.should.be.eq(1);
const expectedLength = Math.ceil(boolCount / 8) + 4;
schema.fixedSizeFieldsLength.should.be.eq(expectedLength);
schema.fieldDefinitionMap.get('fixSized').offset.should.be.eq(0);
schema.fieldDefinitionMap.get('varSized').index.should.be.eq(0);
let positionSoFar = 4;
let bitPositionSoFar = 0;
for (const fieldDescriptor of boolFields) {
if (bitPositionSoFar === BitsUtil.BITS_IN_A_BYTE) {
positionSoFar++;
bitPositionSoFar = 0;
}
const schemaField = schema.fieldDefinitionMap.get(fieldDescriptor.fieldName);
schemaField.offset.should.be.eq(positionSoFar);
schemaField.bitOffset.should.be.eq(bitPositionSoFar);
bitPositionSoFar += 1;
}
});
it('should return true when compare same FieldDescriptor', function() {
const field1 = new FieldDescriptor('SameField', FieldKind.INT32);
const field2 = new FieldDescriptor('SameField', FieldKind.INT32);
chai.expect(field1.equals(field2)).to.be.true;
});
it('should return false when compare different name FieldDescriptor', function() {
const field1 = new FieldDescriptor('SameField', FieldKind.INT32);
const field2 = new FieldDescriptor('SameField2', FieldKind.INT32);
chai.expect(field1.equals(field2)).to.be.false;
});
it('should return false when compare different kind FieldDescriptor', function() {
const field1 = new FieldDescriptor('SameField', FieldKind.INT32);
const field2 = new FieldDescriptor('SameField', FieldKind.INT64);
chai.expect(field1.equals(field2)).to.be.false;
});
});