Debian Codegraph @

bqplot/js/src/test/binary_serialization.ts

raw
1 import { array_or_json_serializer } from '../serialize';
2 import { expect } from 'chai';
3
4 describe('binary serialization >', () => {
5 beforeEach(async () => {});
6
7 it('deserialize/serialize number arrays', async () => {
8 const x = {
9 dtype: 'float32',
10 value: new DataView(new Float32Array([0, 0.5, 1]).buffer),
11 shape: [3],
12 type: null,
13 };
14 const deserialized_x = array_or_json_serializer.deserialize(x, null);
15 expect([...deserialized_x]).to.deep.equal([0, 0.5, 1.0]);
16
17 const serialized_x = array_or_json_serializer.serialize(
18 deserialized_x,
19 null
20 );
21 expect(serialized_x).to.deep.equal(x);
22 });
23
24 it('deserialize/serialize date arrays', async () => {
25 // 2005-02-25 and 2 days after:
26 const x = {
27 dtype: 'float64',
28 value: new DataView(
29 new Float64Array([1109289600000, 1109376000000, 1109462400000]).buffer
30 ),
31 shape: [3],
32 type: 'date',
33 };
34 const deserialized_x = array_or_json_serializer.deserialize(x, null);
35 expect(deserialized_x.type).to.equal('date');
36 expect([...deserialized_x]).to.deep.equal([
37 1109289600000, 1109376000000, 1109462400000,
38 ]);
39
40 const serialized_x = array_or_json_serializer.serialize(
41 deserialized_x,
42 null
43 );
44 expect(serialized_x).to.deep.equal(x);
45 });
46
47 it('deserialize/serialize string arrays', async () => {
48 // String arrays are not sent using binary buffers from the Python side
49 const strlist = ['H', 'E', 'L', 'L', 'O'];
50 const deserialized_strlist = array_or_json_serializer.deserialize(
51 strlist,
52 null
53 );
54 expect(deserialized_strlist).to.deep.equal(strlist);
55
56 const serialized_strlist = array_or_json_serializer.serialize(
57 deserialized_strlist,
58 null
59 );
60 expect(serialized_strlist).to.deep.equal(strlist);
61 });
62
63 it('deserialize/serialize nested arrays', async () => {
64 const x = [
65 {
66 dtype: 'float32',
67 value: new DataView(new Float32Array([0, 0.5, 1]).buffer),
68 shape: [3],
69 type: null,
70 },
71 {
72 dtype: 'float32',
73 value: new DataView(new Float32Array([0, 0.5]).buffer),
74 shape: [2],
75 type: null,
76 },
77 {
78 dtype: 'float32',
79 value: new DataView(new Float32Array([0, 0.5, 2, 3]).buffer),
80 shape: [4],
81 type: null,
82 },
83 ];
84 const deserialized_x = array_or_json_serializer.deserialize(x, null);
85 expect(deserialized_x.length).to.equal(3);
86 expect([...deserialized_x[0]]).to.deep.equal([0, 0.5, 1.0]);
87 expect([...deserialized_x[1]]).to.deep.equal([0, 0.5]);
88 expect([...deserialized_x[2]]).to.deep.equal([0, 0.5, 2, 3]);
89
90 const serialized_x = array_or_json_serializer.serialize(
91 deserialized_x,
92 null
93 );
94 expect(serialized_x).to.deep.equal(x);
95 });
96 });